package com.jeeplus.modules.sg.managementcenter.activiti.utils;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.managementcenter.activiti.service.ConstructionService;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.managementcenter.project.service.ProjectService;
import com.jeeplus.modules.sg.materialmodule.activiti.entity.NonPower;
import com.jeeplus.modules.sg.materialmodule.activiti.service.NonPowerService;
import com.jeeplus.modules.sg.picking.activiti.entity.ShowList;
import com.jeeplus.modules.sg.picking.activiti.service.ShowListService;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.Serializable;
import java.util.Map;

/**
 * 施工交底流程结束，自动发起物资领料及土建验收流程
 */
public class EndTaskListen implements Serializable, ExecutionListener {
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 8513750196548027535L;

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        try {
            ShowListService showListService = SpringContextHolder.getBean(ShowListService.class);
            NonPowerService nonPowerService = SpringContextHolder.getBean(NonPowerService.class);
            ProjectService projectService = SpringContextHolder.getBean(ProjectService.class);
            ActProcessService actProcessService = SpringContextHolder.getBean(ActProcessService.class);
            ActTaskService actTaskService = SpringContextHolder.getBean(ActTaskService.class);
            ConstructionService constructionService = SpringContextHolder.getBean(ConstructionService.class);
            ShowList showList = new ShowList();
            //获取流程定义号
            String processInstanceId = delegateExecution.getProcessInstanceId();
            //根据流程定义号获取项目信息
            Project projectProc = projectService.findByProcInsId(processInstanceId);
            String projectId = projectProc.getProjectId();
            String projectName = projectProc.getProjectName();
            String reserveProjectName = projectProc.getReserveProjectName();
            //变更交底状态
            constructionService.updateEndStatus(reserveProjectName);
            //变更政处处理状态
            constructionService.updatePolicyStatus(reserveProjectName, "2");

            //判断是否有项目定义号和项目名称（没有不予发送领料流程）
            if ("".equals(projectId) || "".equals(projectName) || null == projectId || null == projectName) {

            } else {
                if ("0".equals(projectProc.getAcquisitionStatus())) {
                    showList.setTheNumber("1");
                    showList.setProjectName(projectName);
                    showList.setReserveProjectName(reserveProjectName);
                    showList.setProjectId(projectId);
                    showList.setsVersion(MyActiviUtils.findDic().getValue());
                    showListService.updateAcqStatus(reserveProjectName);
                    //保存数据
                    showListService.save(showList);
                    //修改项目领料状态
                    projectService.wzStatusByName("1", reserveProjectName);
                    // 启动物资领料流程
                    Map<String, Object> vars = Maps.newHashMap();
                    vars.put("manager", UserUtils.getByUserName(projectProc.getAssignment().getOwnerManager()).getLoginName());
                    String procDefId = projectService.findDefId("wzll");
                    ProcessDefinition p = actProcessService.getProcessDefinition(procDefId);
                    String title = showList.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
                    actTaskService.startProcess(p.getKey(), "md_acquisition_process", showList.getId(), title, vars);
                }
                //判断是否需要发起土建领料流程(是否有土建工程量)
                boolean isEngineering = nonPowerService.findIsEngineering(reserveProjectName);
                if (isEngineering) {
                    if ("0".equals(projectProc.getAdvanceStatus())) {
                        NonPower nonPower = new NonPower();
                        nonPower.setProjectId(projectId);
                        nonPower.setProjectName(projectName);
                        nonPower.setReserveProjectName(reserveProjectName);
                        //保存土建基本数据
                        nonPowerService.insert(nonPower);
                        //修改项目基础信息土建流程状态
                        projectService.updateAdvanceStart(reserveProjectName, "1");
                        //启动土建流程
                        Map<String, Object> vars2 = Maps.newHashMap();
                        vars2.put("nopass", false);
                        vars2.put("pass", true);
                        vars2.put("design", UserUtils.getByUserName(projectProc.getAssignment().getConstructionManage()).getLoginName());
                        String procDefId2 = projectService.findDefId("tjys");
                        ProcessDefinition p2 = actProcessService.getProcessDefinition(procDefId2);
                        String title2 = nonPower.getCurrentUser().getName() + "在" + DateUtils.getDateTime() + "发起" + p2.getName();
                        actTaskService.startProcess(p2.getKey(), "xm_nonpower_process", nonPower.getId(), title2, vars2);
                    }
                } else {
                    if ("0".equals(projectProc.getAdvanceStatus())) {
                        //修改项目基础信息土建流程状态
                        projectService.updateAdvanceStart(reserveProjectName, "1");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
