package cn.com.ty.lift.ud.operation.controller;

import cn.com.ty.lift.ud.operation.controller.query.TaxSettingQuery;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ty.lift.ud.operation.mapper.entity.TaxSettingEntity;
import cn.com.ty.lift.ud.operation.service.ITaxSettingService;

import cn.com.ty.lift.ud.common.BaseController;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 税率设置 API
 * 
 * @author Xson
 * @date 2020-05-04
 */
@Api(tags = "税率设置 API -小陶")
@RestController
@RequestMapping("/api/taxSettings")
public class TaxSettingController extends BaseController<TaxSettingEntity, TaxSettingQuery> {

    private final Logger log = LoggerFactory.getLogger(TaxSettingController.class);
    
    private @Autowired ITaxSettingService taxSettingService;

    @Override
    protected IService<TaxSettingEntity> getService() {
        return taxSettingService;
    }

    @ApiOperation(value = "审批 1:通过 2:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody NotExamineDto neDto ) {
        return taxSettingService.examineById(neDto);
    }

    /**
     * 重写修改
     *
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody TaxSettingEntity request) {
        request.setExamineFlag(0);
        boolean updateFlag = getService().updateById(request);
        TaxSettingEntity e = getService().getById(request.getId());
        if (updateFlag) {
            return RestResponse.success(null, "修改成功");
        } else {
            return RestResponse.fail("修改失败");
        }
    }

    /**
     * 新增提交
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增提交")
    @Override
    public RestResponse create(@RequestBody TaxSettingEntity entity) {
        if(null == entity.getTaxName()){
            return RestResponse.fail("类目不能为空");
        }
        if(null == entity.getTaxRate()){
            return RestResponse.fail("税率不能为空");
        }
        return taxSettingService.create(entity);
    }

    /**
     * 新增保存
     *
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody TaxSettingEntity entity) {
        if(null == entity.getTaxName()){
            return RestResponse.fail("类目不能为空");
        }
        if(null == entity.getTaxRate()){
            return RestResponse.fail("税率不能为空");
        }
        entity.setExamineFlag(3);
        boolean saveFlag = getService().save(entity);
        if (saveFlag) {
            return RestResponse.success(entity.getId(), "保存成功");
        } else {
            return RestResponse.fail("保存失败");
        }
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @PostMapping("/submit/{id}")
    @ApiOperation(value = "提交操作")
    public RestResponse submit(@PathVariable("id") Long id) {
        if(null == id){
            return RestResponse.fail("id不能为空");
        }
        return taxSettingService.submit(id);
    }
}