package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TSuperviseTask;
import com.nantian.cloud.jljc.entity.TSuperviseTaskApply;
import com.nantian.cloud.jljc.service.TSuperviseTaskApplyService;
import com.nantian.cloud.jljc.service.TSuperviseTaskService;
import com.nantian.cloud.jljc.vo.TSuperviseTaskApplyVo;
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 lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/tSuperviseTaskApply")
@CrossOrigin
@AllArgsConstructor
@Slf4j
public class TSuperviseTaskApplyController {

    @Autowired
    private TSuperviseTaskApplyService tSuperviseTaskApplyService;

    @Autowired
    private TSuperviseTaskService tSuperviseTaskService;

    /**
     * 新增任务修改申请信息
     */
    @PostMapping("/addUpdateInfo")
    @SysLog("新增任务修改申请信息")
    public R addUpdateInfo(@RequestBody TSuperviseTaskApply tSuperviseTaskApply){
        String monitorId = tSuperviseTaskApply.getMonitorId();
        int flag = 0;
        List<TSuperviseTaskApply> list = tSuperviseTaskApplyService.list(new QueryWrapper<TSuperviseTaskApply>().eq("monitor_id", monitorId));
        for (int i = 0; i < list.size(); i++) {
            TSuperviseTaskApply superviseTaskApply = list.get(i);
            if (superviseTaskApply.getAuditResult().equals("0")){
                flag = 1;
            }
        }
        if (flag == 1){
            return R.failed("当前任务已提交修改，请耐心等待结果！");
        }else {
            ScUser user = SecurityUtils.getUser();
            tSuperviseTaskApply.setDelFlag("1");
            tSuperviseTaskApply.setModifyTime(new Date());
            tSuperviseTaskApply.setModifyUserName(user.getUserRealName());
            tSuperviseTaskApply.setDeptId(user.getDeptId());
            tSuperviseTaskApply.setModifyUserId(user.getId());
            tSuperviseTaskApply.setAuditResult("0");
            tSuperviseTaskApplyService.save(tSuperviseTaskApply);
            return R.ok();
        }
    }

    /**
     * 监察任务审核查询
     */
    @GetMapping("/getMonitorInfo")
    @SysLog("监察任务审核查询")
    public R getMonitorInfo(String monitorId){
        List<TSuperviseTaskApply> list = tSuperviseTaskApplyService.list(new QueryWrapper<TSuperviseTaskApply>().eq("monitor_id", monitorId).eq("del_flag", "1"));
        Collections.sort(list, new Comparator<TSuperviseTaskApply>() {
            @Override
            public int compare(TSuperviseTaskApply o1, TSuperviseTaskApply o2) {
                return o1.getModifyTime().compareTo(o2.getModifyTime());
            }
        });
        return R.ok(list.get(list.size() - 1));
    }

    /**
     * 任务审核
     * @return
     */
    @PostMapping("/taskAudit")
    @SysLog("监察任务审核")
    public R taskAudit(@RequestBody TSuperviseTaskApply tSuperviseTaskApply) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ScUser u = SecurityUtils.getUser();
        String monitorId = tSuperviseTaskApply.getMonitorId();
        TSuperviseTask tSuperviseTask = tSuperviseTaskService.getOne(new QueryWrapper<TSuperviseTask>()
                .eq("monitor_id", monitorId));
        //任务审核不通过
        if (tSuperviseTaskApply.getAuditResult().equals("2")){
            tSuperviseTaskApply.setAuditTime(new Date());
            tSuperviseTaskApply.setAuditUserId(u.getId());
            tSuperviseTaskApply.setAuditUserName(u.getUserRealName());
            tSuperviseTaskApplyService.updateById(tSuperviseTaskApply);
        }else {
            tSuperviseTask.setReportType(0);
            tSuperviseTaskService.updateById(tSuperviseTask);
            tSuperviseTaskApply.setAuditTime(new Date());
            tSuperviseTaskApply.setAuditUserId(u.getId());
            tSuperviseTaskApply.setDelFlag("4");
            tSuperviseTaskApply.setAuditUserName(u.getUserRealName());
            tSuperviseTaskApplyService.updateById(tSuperviseTaskApply);
        }
        return R.ok();
    }


    /**
     *分页获取数据
     */
    @GetMapping("/pageList")
    @SysLog("分页获取监察任务审核")
    public R getPageList(Page page, TSuperviseTaskApplyVo tSuperviseTaskApplyVo) throws ParseException {
        IPage<TSuperviseTaskApply> taskApplyIPage = tSuperviseTaskApplyService.getPage(page, tSuperviseTaskApplyVo);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (TSuperviseTaskApply record : taskApplyIPage.getRecords()) {
            String format = simpleDateFormat.format(record.getModifyTime());
            record.setModifyTimes(format);
            if (record.getAuditTime() != null){
                String s = simpleDateFormat.format(record.getAuditTime());
                record.setAuditTimes(s);
            }
        }
        return R.ok(taskApplyIPage);
    }


}
