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

import cn.com.ty.lift.ud.operation.controller.query.OtherSettingQuery;
import cn.com.ty.lift.ud.operation.service.IOtherSettingService;
import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.mapper.entity.OtherSettingEntity;
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-16
 */
@Api(tags = "其他设置 API - 小陶")
@RestController
@RequestMapping("/api/otherSettings")
public class OtherSettingController extends BaseController<OtherSettingEntity, OtherSettingQuery> {

    private final Logger log = LoggerFactory.getLogger(OtherSettingController.class);
    
    private @Autowired
    IOtherSettingService otherSettingService;

    @Override
    protected IService<OtherSettingEntity> getService() {
        return otherSettingService;
    }

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

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

    /**
     * 新增保存
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody OtherSettingEntity entity) {
        if(null == entity.getDefaultTax()){
            return RestResponse.fail("默认税率不能为空");
        }
        if(null == entity.getAppointExpertPrice()){
            return RestResponse.fail("平台置顶专家收费金额不能为空");
        }
        if(null == entity.getNearPeopleArea()){
            return RestResponse.fail("附近的人范围不能为空");
        }
        if(null == entity.getWithdrawFee()){
            return RestResponse.fail("单次提现上限金额不能为空");
        }
        if(null == entity.getWithdrawNum()){
            return RestResponse.fail("每月提现次数不能为空");
        }
        if(null == entity.getClockInArea()){
            return RestResponse.fail("打卡范围不能为空");
        }
        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 OtherSettingEntity entity) {
        if(null == entity.getDefaultTax()){
             return RestResponse.fail("默认税率不能为空");
        }
        if(null == entity.getAppointExpertPrice()){
            return RestResponse.fail("平台置顶专家收费金额不能为空");
        }
        if(null == entity.getNearPeopleArea()){
            return RestResponse.fail("附近的人范围不能为空");
        }
        if(null == entity.getWithdrawFee()){
            return RestResponse.fail("单次提现上限金额不能为空");
        }
        if(null == entity.getWithdrawNum()){
            return RestResponse.fail("每月提现次数不能为空");
        }
        if(null == entity.getClockInArea()){
            return RestResponse.fail("打卡范围不能为空");
        }
        return otherSettingService.create(entity);
    }

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

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

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

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

}