package com.zcloud.controller.specheck;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.specheck.SpecheckService;
import com.zcloud.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/speCheck")
public class SpeCheckController extends BaseController {

    @Resource
    private SpecheckService specheckService;

    /**
     * 查询危化专项检查任务信息
     *
     * @param 企业编码
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getCheckTask")
    @ResponseBody
    public Object getCheckTask(Page page) {
        PageData pageData = this.getPageData();
        pageData.put("corpinfoId", Jurisdiction.getCORPINFO_ID());
//        String corpinfoId = Jurisdiction.getCORPINFO_ID();
//        if (Tools.isEmpty(corpinfoId)) {
//            return ReturnMap.error("企业编码 参数错误");SPECHECK_TASK_ID
//        }
        page.setPd(pageData);
//        List<PageData> data = specheckService.getCheckTask(page);
        List<PageData> data = specheckService.getCheckTaskdatalistPage(page);
        return ReturnMap.ok().put("data", data).put("page", page);

    }

    /**
     * 有分页
     *
     * @param 检查任务id SPECHECK_TASK_ID
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getCheckSourceList")
    @ResponseBody
    public Object getCheckSourceList(Page page) {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        if (Tools.isEmpty(pageData.getString("specheckTaskId"))) {
            return ReturnMap.error("specheckTaskId 参数错误");
        }
        page.setPd(pageData);
        List<PageData> data = specheckService.getCheckSource(page);
        return ReturnMap.ok().put("data", data).put("page", page);

    }

    /**
     * 查询检查任务中检查项信息
     * 有分页
     *
     * @param 检查任务id taskId
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getCheckItem")
    @ResponseBody
    public Object getCheckItem(Page page) {
        PageData pageData = this.getPageData();
        if (Tools.isEmpty(pageData.getString("taskId"))) {
            return ReturnMap.error("检查任务id 参数错误");
        }
        page.setPd(pageData);
        List<PageData> checkItem = specheckService.getCheckItem(page);
        return ReturnMap.ok().put("varList", checkItem).put("page", page);
    }


    /**
     * 查询检查项评分细则信息
     * 有分页
     *
     * @param 检查任务ID taskId 检查项ID checkItemId
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getCheckScore")
    @ResponseBody
    public Object getCheckScore(Page page) {
        PageData pageData = this.getPageData();
        if (Tools.isEmpty(pageData.getString("taskId"))) {
            return ReturnMap.error("检查任务id 参数错误");
        }
        if (Tools.isEmpty(pageData.getString("checkItemId"))) {
            return ReturnMap.error("检查项id 参数错误");
        }
        page.setPd(pageData);
        List<PageData> checkScore = specheckService.getCheckScore(page);
        return ReturnMap.ok().put("vatList", checkScore).put("page", page);
    }

    /**
     * 上报检查情况记录
     *
     * @param 检查任务id taskId
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/receiveCheckRecordList")
    @ResponseBody
    public Object receiveCheckRecordList() {
        PageData pageData = this.getPageData();
        specheckService.saveReceiveCheckRecordList(pageData);
        return ReturnMap.ok();
    }

    /**
     * 新增隐患上报
     *
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/saveSpeCheckHidden")
    @ResponseBody
    public Object saveSpeCheckHidden() {
        PageData pd = this.getPageData();
        pd.put("HIDDEN_ID", UuidUtil.get32UUID()); // 主键
        pd.put("CREATOR", Jurisdiction.getUSER_ID()); // 发现人
        pd.put("HIDDENFINDDEPT", Jurisdiction.getDEPARTMENT_ID());
        pd.put("CREATTIME", DateUtil.date2Str(new Date())); // 发现时间
        pd.put("ISDELETE", "0"); // 是否删除 1 删除 0未删除
        pd.put("ISCONFIRM", "0"); //
        pd.put("STATE", "0");
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); // 企业id
        if (pd.get("RECTIFICATIONTYPE") != null && Tools.notEmpty(pd.get("RECTIFICATIONTYPE").toString())) {
            // todo 后台上报时候根据整改类型 去 激活 隐患 目前 暂存
            if ("1".equals(pd.get("RECTIFICATIONTYPE").toString())) {
                pd.put("RECTIFICATIONTIME", DateUtil.getDay()); // 整改时间
                pd.put("RECTIFICATIONOR", Jurisdiction.getUSER_ID()); // 整改人
                pd.put("RECTIFICATIONDEPT", Jurisdiction.getDEPARTMENT_ID());
            }
        }
        specheckService.addHidden(pd);
        return ReturnMap.ok().put("pd", pd);
    }

    /**
     * 修改隐患上报
     *
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/editSpeCheckHidden")
    @ResponseBody
    public Object editSpeCheckHidden() {
        PageData pd = this.getPageData();
        specheckService.editHidden(pd);
        return ReturnMap.ok().put("pd", pd);
    }

    /**
     * 删除隐患
     *
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/removeSpeCheckHidden")
    @ResponseBody
    public Object removeSpeCheckHidden() {
        PageData pd = this.getPageData();
        specheckService.removeHidden(pd);
        return ReturnMap.ok();
    }

    /**
     * 修改上报记录
     *
     * @param 检查任务id taskId
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/editReceiveCheckRecordList")
    @ResponseBody
    public Object editReceiveCheckRecordList() {
        PageData pageData = this.getPageData();
        specheckService.editReceiveCheckRecordList(pageData);
        return ReturnMap.ok();
    }


    /**
     * 查询上报结果
     *
     * @param 检查任务id taskId
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/reportedResult")
    @ResponseBody
    public Object reportedResult() {
        PageData pageData = this.getPageData();
        specheckService.reportedResult(pageData);
        return ReturnMap.ok();
    }


    /**
     * 根据任务id查询 记录
     *
     * @param
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getRecordingList")
    @ResponseBody
    public Object getRecordingList(Page page) {
        PageData pageData = this.getPageData();
        page.setPd(pageData);
        List<PageData> resData = specheckService.getRecordingList(page);
        return ReturnMap.ok().put("page", page).put("varList", resData);
    }

    /**
     * 根据 检查记录查询详情
     *
     * @param
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getRecordInfo")
    @ResponseBody
    public Object getRecordInfo() {
        PageData pageData = this.getPageData();
        PageData resData = specheckService.getRecordInfo(pageData);
        return ReturnMap.ok().put("pd", resData);
    }

    /**
     * 根据任务id与检查项id查询 隐患记录
     *
     * @param
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/getHiddenInfo")
    @ResponseBody
    public Object getHiddenList() {
        PageData pageData = this.getPageData();
        PageData resData = specheckService.getHiddenInfo(pageData);
        return ReturnMap.ok().put("pd", resData);
    }


    /**
     * 上报当前记录。
     * specheckRecordId
     *
     * @param
     * @return 返回自定义信息的error
     */
    @RequestMapping(value = "/reportCurrentRecord")
    @ResponseBody
    public Object reportCurrentRecord() {
        PageData pageData = this.getPageData();
        ReturnMap res = specheckService.reportCurrentRecord(pageData);
//        PageData resData = specheckService.getHiddenInfo(pageData);
        return res;
    }


    // 首页提醒 有几个待检查的任务
    @RequestMapping(value = "/getTasksToChecked")
    @ResponseBody
    public Object getTasksToChecked() {
        PageData pageData = this.getPageData();
        return specheckService.getTasksToChecked(pageData);
//        PageData resData = specheckService.getHiddenInfo(pageData);
    }

    // 初始化任务，现在定时任务每周一获取一次，可手动触发 立即 更新一次
    @RequestMapping(value = "/syncTaskNow")
    @ResponseBody
    public Object SyncTaskNow() {
        specheckService.parsingTasksByHour();
        return ReturnMap.ok();
    }

}
