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

import cn.com.ty.lift.ud.operation.mapper.entity.CommissionSettingEntity;
import cn.com.ty.lift.ud.operation.service.ICommissionSettingService;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.controller.query.CommissionSettingQuery;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 抽成设置 API - 单页面
 * 
 * @author Xson
 * @date 2020-05-15
 */
@Api(tags = "抽成设置 API - 小陶")
@RestController
@RequestMapping("/api/commissionSettings")
public class CommissionSettingController extends BaseController<CommissionSettingEntity,CommissionSettingQuery> {

    private final Logger log = LoggerFactory.getLogger(CommissionSettingController.class);
    
    private @Autowired
    ICommissionSettingService commissionSettingService;

    @Override
    protected IService<CommissionSettingEntity> getService() {
        return commissionSettingService;
    }

    /**
     * 查询历史版版本（最新版本的上一个版本）
     */
    @GetMapping("/query-history")
    @ApiOperation(value = "查询历史版版本")
    public RestResponse getHistoryVersion() {
        return commissionSettingService.getHistoryVersion();
    }

    /**
     * 查询最新的版本
     */
    @GetMapping("/query-new")
    @ApiOperation(value = "查询最新的版本")
    public RestResponse getLatestVersion() {
        return commissionSettingService.getLatestVersion();
    }

    /**
     * 新增保存
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody CommissionSettingEntity entity) {
        entity.setExamineFlag(3);
        boolean saveFlag = false;
        //如果存在待提交的，就直接修改不新增
        if(entity.getId() == null){
            //新增
             saveFlag = getService().save(entity);
        }else{
            //修改
            saveFlag = getService().updateById(entity);
        }
        if (saveFlag) {
            return RestResponse.success(entity.getId(), "保存成功");
        }else {
            return RestResponse.fail("保存失败");
        }
    }

    /**
     * 新增提交
     * @param entity
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增提交")
    @Override
    public RestResponse create(@RequestBody CommissionSettingEntity entity) {
        return commissionSettingService.create(entity);
    }

    /**
     * 提交操作
     *
     * @param id
     * @return
     */
    @PostMapping("/submit/{id}")
    @ApiOperation(value = "提交操作")
    public RestResponse submit(@PathVariable("id") Long id) {
        return commissionSettingService.submit(id);
    }

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

    /**
     * 查询所有待审核的
     */
    @GetMapping("/query-stay")
    @ApiOperation(value = "查询所有待审核的")
    public RestResponse getAllStay() {
        return commissionSettingService.getAllStay();
    }

    /**
     * 查询所有待提交的
     */
    @GetMapping("/query-commit")
    @ApiOperation(value = "查询所有待提交的")
    public RestResponse getAllCommit() {
        return commissionSettingService.getAllCommit();
    }

}