package com.jeeplus.modules.sg.materialmodule.activiti.web;

import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.managementcenter.activiti.utils.MyActiviUtils;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.materialmodule.activiti.entity.MaterialModule;
import com.jeeplus.modules.sg.materialmodule.activiti.service.MaterialModuleService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "${adminPath}/materialmodule/activiti")
public class MaterialModuleController extends BaseController {
    @Autowired
    private MaterialModuleService materialModuleService;
    @Autowired
    private ActProcessService actProcessService;
    @Autowired
    private ActTaskService actTaskService;

    /**
     * 施工模块发起页面
     */
    @RequestMapping(value = "materialModuleForm")
    public String materialModuleForm(MaterialModule materialModule, Model model) {
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/materialModuleForm";
    }

    /**
     * 土建开工流程
     */
    @RequestMapping(value = "nonPowerStart")
    public String nonPowerStart(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/nonPowerStart";
    }

    /**
     * 土建施工中流程
     */
    @RequestMapping(value = "nonPowerMiddle")
    public String nonPowerMiddle(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/nonPowerMiddle";
    }

    /**
     * 审核土建施工中流程
     */
    @RequestMapping(value = "nonRePowerMiddle")
    public String nonRePowerMiddle(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/nonRePowerMiddle";
    }

    /**
     * 土建竣工流程
     */
    @RequestMapping(value = "nonPowerEnd")
    public String nonPowerEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/nonPowerEnd";
    }

    /**
     * 审核土建竣工流程
     */
    @RequestMapping(value = "nonRePowerEnd")
    public String nonRePowerEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/nonRePowerEnd";
    }

    /**
     * 经理审核土建
     */
    @RequestMapping(value = "reviewNonPower")
    public String reviewNonPower(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/reviewNonPower";
    }

    /**
     * 电气开工流程
     */
    @RequestMapping(value = "electricalStart")
    public String electricalStart(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/electricalStart";
    }

    /**
     * 电气施工流程
     */
    @RequestMapping(value = "electricalMiddle")
    public String electricalMiddle(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/electricalMiddle";
    }

    /**
     * 审核电气施工
     */
    @RequestMapping(value = "reElectricalMiddle")
    public String reElectricalMiddle(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/reElectricalMiddle";
    }

    /**
     * 电气竣工流程
     */
    @RequestMapping(value = "electricalEnd")
    public String electricalEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/electricalEnd";
    }

    /**
     * 审核电气竣工
     */
    @RequestMapping(value = "reElectricalEnd")
    public String reElectricalEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/reElectricalEnd";
    }

    /**
     * 审核电气流程
     */
    @RequestMapping(value = "reviewElectrical")
    public String reviewElectrical(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/reviewElectrical";
    }

    /**
     * 涉电开工流程
     */
    @RequestMapping(value = "PowerStart")
    public String PowerStart(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/PowerStart";
    }

    /**
     * 涉电竣工流程
     */
    @RequestMapping(value = "PowerEnd")
    public String PowerEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/PowerEnd";
    }

    /**
     * 审核涉电竣工
     */
    @RequestMapping(value = "rePowerEnd")
    public String rePowerEnd(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/rePowerEnd";
    }

    /**
     * 是否完成所有涉电工作
     */
    @RequestMapping(value = "PowerWhether")
    public String PowerWhether(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/PowerWhether";
    }

    /**
     * 施工单位项目经理判断项目是否包含涉电工作
     */
    @RequestMapping(value = "managerWhether")
    public String managerWhether(MaterialModule materialModule, Model model) {
        MaterialModule materialShow = materialModuleService.get(materialModule.getAct().getProcInsId());
        model.addAttribute("materialShow", materialShow);
        model.addAttribute("materialModule", materialModule);
        return "modules/sg/materialmodule/activiti/managerWhether";
    }


    /**
     * 施工流程发起
     */
    @ResponseBody
    @RequestMapping(value = "containSave")
    public AjaxJson save(MaterialModule materialModule, Model model,String procDefId,String flag) throws Exception {
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(materialModule);
        if (StringUtils.isNotBlank(errMsg)) {
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        String projectId = materialModule.getProjectId();
        if (projectId == null||"".equals(projectId)) {
            j.setSuccess(false);
            j.setMsg("请选择项目");
            return j;
        }

        /**
         * 流程审批
         */
        if (StringUtils.isBlank(materialModule.getId())) {
            String projectType = materialModule.getProjectType();
            String[] split = projectType.split("|");
            Map<String, Object> vars = Maps.newHashMap();
            Project userById = MyActiviUtils.findUserById(projectId);
            vars.put("manager", UserUtils.getByUserName(userById.getConstructionUnitManage()).getLoginName());
            //土建
            vars.put("contain", false);
            //电气
            vars.put("electrical", false);
            //涉电
            vars.put("notContain", false);
            boolean contain = false;
            boolean electrical = false;
            boolean notContain = false;
            materialModule.setNonPower(0);
            materialModule.setElectrical(0);
            materialModule.setPower(0);
            for (int i = 0; i < split.length; i++) {
                if ("1".equals(split[i])) {
                    materialModule.setNonPower(1);
                    contain = true;
                }
                if ("2".equals(split[i])) {
                    materialModule.setElectrical(1);
                    electrical = true;
                }
                if ("3".equals(split[i])) {
                    materialModule.setPower(1);
                    notContain = true;
                }
            }
            if (contain == true) {
                vars.put("contain", true);
            }
            if (electrical == true) {
                vars.put("electrical", true);
            }
            if (notContain == true && contain == false && electrical == false) {
                vars.put("notContain", true);
            }
            //修改项目的施工模块状态
            materialModule.setUserName(UserUtils.getUser().getName());
            materialModule.setFrequency(1);
            materialModuleService.save(materialModule);
            // 启动流程
            ProcessDefinition p = actProcessService.getProcessDefinition(procDefId);
            String title = materialModule.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
            actTaskService.startProcess(p.getKey(), "sg_construction_process", materialModule.getId(), title, vars);
            j.setMsg("发起施工流程成功!");
            j.getBody().put("targetUrl", "/act/task/process/");
        }
        return j;
    }
}


