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

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.operation.mapper.entity.ExpertLevelSettingEntity;
import cn.com.ty.lift.ud.operation.service.IExpertLevelSettingService;
import cn.com.ty.lift.ud.common.NotExamineDto;
import cn.com.ty.lift.ud.operation.controller.query.ExpertLevelSettingQuery;
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/expertLevelSettings")
public class ExpertLevelSettingController extends BaseController<ExpertLevelSettingEntity,ExpertLevelSettingQuery> {

    private final Logger log = LoggerFactory.getLogger(ExpertLevelSettingController.class);
    
    private @Autowired
    IExpertLevelSettingService expertLevelSettingService;

    @Override
    protected IService<ExpertLevelSettingEntity> getService() {
        return expertLevelSettingService;
    }

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

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

    /**
     * 新增保存
     * @param entity
     * @return
     */
    @PostMapping("/insert-save")
    @ApiOperation(value = "新增保存")
    public RestResponse createSave(@RequestBody ExpertLevelSettingEntity entity) {
        if(null == entity.getLeveiTwo()){
            return RestResponse.fail("升级2星不能为空");
        }
        if(null == entity.getLevelThree()){
            return RestResponse.fail("升级3星不能为空");
        }
        if(null == entity.getLevelFour()){
            return RestResponse.fail("升级4星不能为空");
        }
        if(null == entity.getLevelFive()){
            return RestResponse.fail("升级5星不能为空");
        }
        if(null == entity.getScoreOne()){
            return RestResponse.fail("评论1星不能为空");
        }
        if(null == entity.getScoreTwo()){
            return RestResponse.fail("评论2星不能为空");
        }
        if(null == entity.getScoreThree()){
            return RestResponse.fail("评论3星不能为空");
        }
        if(null == entity.getScoreFour()){
            return RestResponse.fail("评论4星不能为空");
        }
        if(null == entity.getScoreFive()){
            return RestResponse.fail("评论5星不能为空");
        }
        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 ExpertLevelSettingEntity entity) {
        if(null == entity.getLeveiTwo()){
            return RestResponse.fail("升级2星不能为空");
        }
        if(null == entity.getLevelThree()){
            return RestResponse.fail("升级3星不能为空");
        }
        if(null == entity.getLevelFour()){
            return RestResponse.fail("升级4星不能为空");
        }
        if(null == entity.getLevelFive()){
            return RestResponse.fail("升级5星不能为空");
        }
        if(null == entity.getScoreOne()){
            return RestResponse.fail("评论1星不能为空");
        }
        if(null == entity.getScoreTwo()){
            return RestResponse.fail("评论2星不能为空");
        }
        if(null == entity.getScoreThree()){
            return RestResponse.fail("评论3星不能为空");
        }
        if(null == entity.getScoreFour()){
            return RestResponse.fail("评论4星不能为空");
        }
        if(null == entity.getScoreFive()){
            return RestResponse.fail("评论5星不能为空");
        }
        return expertLevelSettingService.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 expertLevelSettingService.submit(id);
    }


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

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

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