package com.nantian.cloud.jljc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TSupervisionSuggest;
import com.nantian.cloud.jljc.service.TSupervisionSuggestService;
import com.nantian.cloud.jljc.vo.Query;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 监察处理建议/复查情况汇总表问题及答案
 * @since 2020-10-29
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionSuggest")
@Api(value = "tSupervisionSuggest", tags = "监察处理建议/复查情况汇总表")
public class TSupervisionSuggestController {

    private final TSupervisionSuggestService tSupervisionSuggestService;

    /**
     * 分页查询监察处理建议/复查情况汇总表
     */
    @GetMapping("/page")
    @SysLog("分页查询监察处理建议/复查情况汇总表")
    @ApiOperation(value = "分页查询监察处理建议/复查情况汇总表", notes = "分页查询监察处理建议/复查情况汇总表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Query", value = "查询条件(year年份,code监察编号,area地区,name监察单位名称,type类型(1复查情况,0处理建议))", required = false, dataType = "json", paramType = "path")
    })
    public R getSuggestPage(Page page, Query query) {
        setArea(query);
        return R.ok(tSupervisionSuggestService.getSuggestPage(page,query));
    }

    /**
     * 查询全部监察处理建议/复查情况汇总表
     */
    @GetMapping("/list")
    @SysLog("查询全部监察处理建议/复查情况汇总表")
    @ApiOperation(value = "查询全部监察处理建议/复查情况汇总表", notes = "查询全部监察处理建议/复查情况汇总表")
    @ApiImplicitParam(name = "Query", value = "查询条件(year年份,code监察编号,area地区,name监察单位名称,type类型(1复查情况,0处理建议))", required = false, dataType = "json", paramType = "path")
    public R getSuggestList(Query query) {
        setArea(query);
        return R.ok(tSupervisionSuggestService.getSuggestList(query));
    }

    /**
     * 根据id获取监察处理建议/复查情况汇总表
     */
    @GetMapping("/getById")
    @SysLog("根据id获取监察处理建议/复查情况汇总表")
    @ApiOperation(value = "根据id获取监察处理建议/复查情况汇总表", notes = "根据id获取监察处理建议/复查情况汇总表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Integer id) {
        return R.ok(tSupervisionSuggestService.getById(id));
    }

    /**
     * 根据监察编号、类型获取监察处理建议/复查情况汇总表
     */
    @GetMapping("/getByMonitorId")
    @SysLog("根据监察编号、类型获取监察处理建议/复查情况汇总表")
    @ApiOperation(value = "根据监察编号、类型获取监察处理建议/复查情况汇总表", notes = "根据监察编号、类型获取监察处理建议/复查情况汇总表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "monitorId", value = "监察编号", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "类型(1复查情况,0处理建议)", required = true, dataType = "string", paramType = "path")
    })
    public R getByMonitorId(String monitorId,String type) {
        TSupervisionSuggest t = new TSupervisionSuggest();
        t.setMonitorId(monitorId);
        t.setReviewType(Integer.parseInt(type));
        return R.ok(tSupervisionSuggestService.list(Wrappers.query(t)));
    }

    /**
     * 保存/修改监察处理建议/复查情况汇总表
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改监察处理建议/复查情况汇总表")
    @ApiOperation(value = "保存/修改监察处理建议/复查情况汇总表", notes = "保存/修改监察处理建议/复查情况汇总表")
    @ApiImplicitParam(name = "TSupervisionSuggest", value = "监察处理建议/复查情况汇总表对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TSupervisionSuggest tSupervisionSuggest) {

        hasOld(tSupervisionSuggest.getMonitorId(), tSupervisionSuggest.getReviewType(), tSupervisionSuggest);

        ScUser u = SecurityUtils.getUser();
        tSupervisionSuggest.setDelFlag("1");
        tSupervisionSuggest.setUserid(u.getId());
        tSupervisionSuggest.setDeptId(u.getDeptId());
        tSupervisionSuggest.setUpdateTime(new Date());
//        tSupervisionSuggestService.saveOrUpdate(tSupervisionSuggest);
        if(tSupervisionSuggest.getId()!=null){
            tSupervisionSuggestService.updateData(tSupervisionSuggest);
        }else{
            tSupervisionSuggestService.save(tSupervisionSuggest);
        }

        //是复查，并填写了违法行为，需新增处理建议
        if(tSupervisionSuggest.getReviewType()==1 && tSupervisionSuggest.getIllegalAct()!= null && !"".equals(tSupervisionSuggest.getIllegalAct())){
            hasOld(tSupervisionSuggest.getMonitorId(), 0, tSupervisionSuggest); //判断处理建议是否有历史数据
            tSupervisionSuggest.setReviewType(0); //处理建议
            if(tSupervisionSuggest.getId()!=null){
                tSupervisionSuggestService.updateData(tSupervisionSuggest);
            }else{
                tSupervisionSuggestService.save(tSupervisionSuggest);
            }
        }

        return R.ok();
    }

    //有历史数据，添加id
    private void hasOld(String monitorId, Integer type, TSupervisionSuggest tSupervisionSuggest) {
        TSupervisionSuggest t = new TSupervisionSuggest();
        t.setMonitorId(monitorId);
        t.setReviewType(type); //1是复查，0处理建议
        TSupervisionSuggest old = tSupervisionSuggestService.getOne(Wrappers.query(t));
        if(old!=null){ //有历史数据，添加id
            tSupervisionSuggest.setId(old.getId());
        }else{
            tSupervisionSuggest.setId(null);
        }
    }

    //设置区域编码
    private void setArea(Query query) {
        if (StringUtils.isNotEmpty(query.getArea())) {
            query.setArea(query.getArea().replaceAll("0000","".replaceAll("00","")));
        }
    }

}
