package com.zcloud.controller.eightwork;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.eightwork.*;
import com.zcloud.util.*;
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
 * 时间：2021-04-02
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/eightWork")
public class EightWorkController extends BaseController {

    @Autowired
    private CorpInfoService corpInfoService;
    @Autowired
    private EightWorkService eightWorkService;
    @Autowired
    private HighworkService highworkService;
    @Autowired
    private ConfinedspaceService confinedspaceService;
    @Autowired
    private HoistingService hoistingService;
    @Autowired
    private BreakGroundService breakGroundService;
    @Autowired
    private ElectricityService electricityService;
    @Autowired
    private CutRoadService cutRoadService;
    @Autowired
    private BlindBoardService blindBoardService;
    @Autowired
    private HotworkService hotworkService;

    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/eightWorkIndex")
    @ResponseBody
    public Object eightWorkIndex() {
        PageData pageData = this.getPageData();
        pageData.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        return ReturnMap.ok().put("data", eightWorkService.eightWorkIndex(pageData));
    }


    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/eightWorkBI")
    @ResponseBody
    public Object eightWorkBI() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        // 企业
        PageData count = corpInfoService.eightWorkBI(pd);
        map.put("count", count);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 持续改进企业报告
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/getDiagnosis")
    @ResponseBody
    public Object getDiagnosis() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        List<PageData> eightlist = eightWorkService.listAllForDiagnosis(pd);

        map.put("eightlist", eightlist);
        if (pd != null) {
            errInfo = "success";
        } else {
            errInfo = "faild";
        }
        map.put("result", errInfo);
        return map;
    }

    // pc 作业删除  作废
    @RequestMapping("/removeWorkById")
    @ResponseBody
    @LogAnno(menuType = "高危作业管理", menuServer = "作业删除", instructionsOperate = "作业删除", instructionsType = "删除")
    public Object removeWorkById() throws Exception {
        PageData pageData = this.getPageData();
        PageData work = new PageData();
        if (pageData.getString("WORK_TYPE").equals("HOTWORK")) {
            pageData.put("HOTWORK_ID", pageData.getString("WORK_ID"));
            hotworkService.delete(pageData);
            work = hotworkService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("HIGH")) {
            pageData.put("HIGHWORK_ID", pageData.getString("WORK_ID"));
            highworkService.delete(pageData);
            work = highworkService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("confined_space")) {
            pageData.put("CONFINEDSPACE_ID", pageData.getString("WORK_ID"));
            confinedspaceService.delete(pageData);
            work = confinedspaceService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("hoisting")) {
            pageData.put("HOISTING_ID", pageData.getString("WORK_ID"));
            hoistingService.delete(pageData);
            work = hoistingService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("groundbreaking")) {
            pageData.put("BREAKGROUND_ID", pageData.getString("WORK_ID"));
            breakGroundService.delete(pageData);
            work = breakGroundService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("electricity")) {
            pageData.put("ELECTRICITY_ID", pageData.getString("WORK_ID"));
            electricityService.delete(pageData);
            work = electricityService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("open_circuit")) {
            pageData.put("CUTROAD_ID", pageData.getString("WORK_ID"));
            cutRoadService.delete(pageData);
            work = cutRoadService.findById(pageData);
        }
        if (pageData.getString("WORK_TYPE").equals("blind")) {
            pageData.put("BLINDBOARD_ID", pageData.getString("WORK_ID"));
            blindBoardService.delete(pageData);
            work = blindBoardService.findById(pageData);
        }

        // 删除关联
        if(null!= work && null != work.getString("CHECK_NO")){
            String CHECK_NO = work.getString("CHECK_NO");
            StringBuffer specialWork = new StringBuffer();
            switch (CHECK_NO.substring(0,2)){
                case "DH":
                    specialWork.append("动火作业 ").append(CHECK_NO);
                    break;
                case "GC":
                    specialWork.append("高处作业 ").append(CHECK_NO);
                    break;
                case "SX":
                    specialWork.append("受限作业 ").append(CHECK_NO);
                    break;
                case "DZ":
                    specialWork.append("吊装作业 ").append(CHECK_NO);
                    break;
                case "DT":
                    specialWork.append("动土作业 ").append(CHECK_NO);
                    break;
                case "LS":
                    specialWork.append("临时用电 ").append(CHECK_NO);
                    break;
                case "DL":
                    specialWork.append("断路作业 ").append(CHECK_NO);
                    break;
                case "MB":
                    specialWork.append("盲板作业 ").append(CHECK_NO);
                    break;
                default :
                    break;
            }
            electricityService.updateSpecial(specialWork.toString(),"BUS_HOTWORK","HOTWORK_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_HIGHWORK","HIGHWORK_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_CONFINEDSPACE","CONFINEDSPACE_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_HOISTING","HOISTING_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_BREAKGROUND","BREAKGROUND_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_ELECTRICITY","ELECTRICITY_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_CUTROAD","CUTROAD_ID");
            electricityService.updateSpecial(specialWork.toString(),"BUS_BLINDBOARD","BLINDBOARD_ID");
        }
        return ReturnMap.ok();
    }

    //
    // pc 作业删除  作废
    @RequestMapping("/invalidWorkById")
    @ResponseBody
    @LogAnno(menuType = "高危作业管理", menuServer = "作业作废", instructionsOperate = "作业作废", instructionsType = "作废")
    public ReturnMap invalidWorkById() {
        PageData pageData = getPageData();
        pageData.put("OPERATOR", Jurisdiction.getUSER_ID());
        pageData.put("OPERATTIME", DateUtil.date2Str(new Date()));
        if ("HOTWORK".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_hotwork");
            pageData.put("PRIMARY_KEY", "HOTWORK_ID");
            pageData.put("HOTWORK_ID", pageData.getString("WORK_ID"));
        }
        if ("HIGH".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_highwork");
            pageData.put("PRIMARY_KEY", "HIGHWORK_ID");
            pageData.put("HIGHWORK_ID", pageData.getString("WORK_ID"));
        }
        if ("confined_space".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_confinedspace");
            pageData.put("PRIMARY_KEY", "CONFINEDSPACE_ID");
            pageData.put("CONFINEDSPACE_ID", pageData.getString("WORK_ID"));
        }
        if ("hoisting".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_hoisting");
            pageData.put("PRIMARY_KEY", "HOISTING_ID");
            pageData.put("HOISTING_ID", pageData.getString("WORK_ID"));
        }
        if ("groundbreaking".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_breakground");
            pageData.put("PRIMARY_KEY", "BREAKGROUND_ID");
            pageData.put("BREAKGROUND_ID", pageData.getString("WORK_ID"));
        }
        if ("electricity".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_electricity");
            pageData.put("PRIMARY_KEY", "ELECTRICITY_ID");
            pageData.put("ELECTRICITY_ID", pageData.getString("WORK_ID"));
        }
        if ("open_circuit".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_cutroad");
            pageData.put("PRIMARY_KEY", "CUTROAD_ID");
            pageData.put("CUTROAD_ID", pageData.getString("WORK_ID"));
        }
        if ("blind".equals(pageData.getString("WORK_TYPE"))) {
            pageData.put("table", "bus_blindboard");
            pageData.put("PRIMARY_KEY", "BLINDBOARD_ID");
        }
        eightWorkService.invalid(pageData);
        return ReturnMap.ok();
    }
}
