package com.zcloud.controller.keyProjects;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.keyProjects.KeyprojectCheckInfoService;
import com.zcloud.service.keyProjects.KeyprojectCheckService;
import com.zcloud.service.keyProjects.KeyprojectHiddenService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 说明：重点工程检查
 * 作者：luoxiaobao
 * 时间：2022-09-19
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/keyprojectcheck")
public class KeyprojectCheckController extends BaseController {

    @Autowired
    private KeyprojectCheckService keyprojectcheckService;
    @Autowired
    private KeyprojectCheckInfoService keyprojectcheckInfoService;
    @Autowired
    private KeyprojectHiddenService keyprojectHiddenService;
    @Autowired
    private ImgFilesService imgFilesService;
    /**新增
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Object add() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("KEYPROJECTCHECK_ID", this.get32UUID());	//主键
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
        pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
        pd.put("ISDELETE", 0);	//是否删除
        pd.put("SOURCE", 1);	//企业ID
        keyprojectcheckService.save(pd);

        String currentTime = DateUtil.date2Str(new Date());
        if (Tools.notEmpty(pd.getString("SITUATIONJSON"))) {
            List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.getString("SITUATIONJSON"));
            String time = currentTime;
            for (JSONObject json : list) {
                PageData situationPd = new PageData();
                situationPd.put("KEYPROJECTCHECKINFO_ID", UuidUtil.get32UUID());    //主键
                situationPd.put("KEYPROJECTCHECK_ID", pd.getString("KEYPROJECTCHECK_ID"));    //检查ID
                situationPd.put("SITUATION", json.get("SITUATION"));    //检查情况
                situationPd.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
                situationPd.put("CREATOR", pd.getString("CREATOR"));    //添加人
                situationPd.put("CREATTIME", time);    //添加时间
                situationPd.put("OPERATOR", pd.getString("OPERATOR"));    //修改人
                situationPd.put("OPERATTIME", time);    //修改时间
                keyprojectcheckInfoService.save(situationPd);
                time = DateUtil.calculateDateSecond(time, 1);
            }
        }

        if (Tools.notEmpty(pd.getString("HIDDENJSON"))) {
            List<PageData> hiddenList = new ArrayList<PageData>();
            List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.getString("HIDDENJSON"));
            String time = currentTime;
            for (JSONObject json : list) {
                PageData hidden = new PageData();
                hidden.put("HIDDEN_ID", UuidUtil.get32UUID());    //主键
                hidden.put("FOREIGN_ID", pd.getString("KEYPROJECTCHECK_ID"));    //外键ID
                hidden.put("HIDDENDESCR", json.getString("HIDDENDESCR"));    //隐患描述
                hidden.put("HIDDENPART", json.getString("HIDDENPART"));    //隐患部位
                hidden.put("HIDDENLEVEL", json.getString("HIDDENLEVEL"));    //隐患级别
                hidden.put("HIDDENTYPE", json.getString("HIDDENTYPE"));    //隐患类型1
                hidden.put("HIDDENTYPE2", json.getString("HIDDENTYPE2"));    //隐患类型2
                hidden.put("LONGITUDE", json.getString("LONGITUDE"));    //隐患位置经度
                hidden.put("LATITUDE", json.getString("LATITUDE"));    //隐患位置纬度
                hidden.put("DISCOVERYTIME", json.getString("DISCOVERYTIME"));    //隐患发现时间
                hidden.put("HIDDENFINDDEPT", json.getString("HIDDENFINDDEPT"));    //隐患发现部门（隐患责任人部门）
                hidden.put("RECTIFICATIONTYPE", json.getString("RECTIFICATIONTYPE"));    //隐患处置
                hidden.put("RECTIFICATIONDEADLINE", json.getString("RECTIFICATIONDEADLINE"));    //整改期限
                hidden.put("RECTIFYDESCR", json.getString("RECTIFYDESCR"));    //整改描述
                hidden.put("RECTIFICATIONDEPT", json.getString("RECTIFICATIONDEPT"));    //整改部门
                hidden.put("RECTIFICATIONOR", json.getString("RECTIFICATIONOR"));    //整改人
                hidden.put("CREATOR", json.getString("CREATOR"));    //发现人（隐患责任人）
                hidden.put("SOURCE", json.getString("SOURCE"));    //隐患来源
                hidden.put("STATE", "100");    //隐患状态 100-安全环保检查暂存的隐患（流程完转0）
                hidden.put("CREATTIME", time);    //发现时间
                hidden.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));    //企业ID
                hidden.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
                hidden.put("POSITIONDESC",json.getString("POSITIONDESC"));// 隐患位置描述

                keyprojectHiddenService.save(hidden);
                hiddenList.add(hidden);
                time = DateUtil.calculateDateSecond(time, 1);
            }
            pd.put("hiddenList", hiddenList);
        }
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**删除
     * @param out
     * @throws Exception
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object delete() throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        keyprojectcheckService.delete(pd);
        map.put("result", errInfo);				//返回结果
        return map;
    }

    /**修改
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Object edit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        keyprojectcheckService.edit(pd);
        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @param page
     * @throws Exception
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public Object list(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData>	varList = keyprojectcheckService.list(page);	//列出KeyprojectCheck列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**去修改页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = keyprojectcheckService.findById(pd);	//根据ID读取

        List<PageData> situationList = keyprojectcheckInfoService.listAll(pd);
        pd.put("FOREIGN_ID", pd.getString("KEYPROJECTCHECK_ID"));
        pd.put("hiddenList", keyprojectHiddenService.listAllInspection(pd));
        pd.put("situationList", situationList);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**去隐患页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/listHidden")
    @ResponseBody
    public Object listHidden(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData>	varList = keyprojectHiddenService.list(page);	//列出KeyprojectCheck列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**去隐患页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/findHidden")
    @ResponseBody
    public Object findHidden() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = keyprojectHiddenService.findById(pd);	//根据ID读取

        PageData pd2 = new PageData();
        pd2.put("FOREIGN_KEY",pd.getString("HIDDEN_ID"));
        pd2.put("TYPE",3);	//隐患图片
        pd.put("hiddenImgs", imgFilesService.listAll(pd2));
        if(imgFilesService.listAll(pd2).size()==0){
            pd2.put("TYPE",114);	//AI隐患图片
            pd.put("hiddenImgs", imgFilesService.listAll(pd2));
        }
        pd2.put("TYPE",102);	//隐患视频
        pd.put("hiddenVideos", imgFilesService.listAll(pd2));
        pd2.put("TYPE",4);
        pd.put("zgImgs", imgFilesService.listAll(pd2));//整改图片
        pd2.put("TYPE",5);
        pd.put("ysImgs", imgFilesService.listAll(pd2));//整改图片
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**批量删除
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/deleteAll")
    @ResponseBody
    public Object deleteAll() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String DATA_IDS = pd.getString("DATA_IDS");
        if(Tools.notEmpty(DATA_IDS)){
            String ArrayDATA_IDS[] = DATA_IDS.split(",");
            keyprojectcheckService.deleteAll(ArrayDATA_IDS);
            errInfo = "success";
        }else{
            errInfo = "fail";
        }
        map.put("result", errInfo);				//返回结果
        return map;
    }

}

