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.common.utils.StringUtils;
import com.jeeplus.modules.act.service.ActProcessService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.Construction;
import com.jeeplus.modules.sg.managementcenter.activiti.service.ConstructionService;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialInfo;
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.MaterialModule;
import com.jeeplus.modules.sg.materialmodule.activiti.entity.NonPower;
import com.jeeplus.modules.sg.materialmodule.activiti.service.MaterialModuleService;
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.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.List;
import java.util.Map;

/**
 * 物资及土建具体流程
 */
public class nonPowerListen implements Serializable, ExecutionListener {
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 8513750196548027535L;

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        try {
            MaterialModuleService materialModuleService = SpringContextHolder.getBean(MaterialModuleService.class);
            ProjectService projectService = SpringContextHolder.getBean(ProjectService.class);
            ActProcessService actProcessService = SpringContextHolder.getBean(ActProcessService.class);
            ActTaskService actTaskService = SpringContextHolder.getBean(ActTaskService.class);
            MaterialModule materialModule = new MaterialModule();
            String procInsId = delegateExecution.getProcessInstanceId();
            //修改领料状态
            projectService.updateWzStatus("2", procInsId);
            //判断是否领料完成，是否进场完成，根据条件发送施工交底流程
            Project wzProcInsId = projectService.findWzProcInsId(procInsId);
            String constructionStatus = wzProcInsId.getConstructionStatus();
            String canEnter = wzProcInsId.getCanEnter();
            String acquisitionStatus = wzProcInsId.getAcquisitionStatus();
            if ("0".equals(constructionStatus)) {
                if ("2".equals(canEnter) && "2".equals(acquisitionStatus)) {
                    //发送电气流程
                    boolean electric = false;
                    //判单钢管杠，水泥杠，存在电气，判断是否包含
                    List<MaterialInfo> allInfo = projectService.findWzInfo(procInsId);
                    if (allInfo.size() > 0) {
                        for (MaterialInfo mi : allInfo
                        ) {
                            String materialName = mi.getMaterialName();
                            if (null != materialName && !"".equals(materialName)) {
                                //判断项目是否包含描述为钢管杆或水泥杠的物料，存在步入电气流程
                                if (materialName.contains("钢管杆") || materialName.contains("水泥杆")) {
                                    electric = true;
                                    break;
                                }
                            }
                        }
                    }
                    //查询出项目的基础信息，停电线路（如果纯在），人员流转信息
                    Construction blockAss = projectService.findWzAss(procInsId);
                    Map<String, Object> vars = Maps.newHashMap();
                    //没用电气及涉电 运行条件
                    vars.put("no", false);
                    //没有电气，有涉电 运行条件
                    vars.put("notContain", false);
                    //包含电气/涉电-优先电气 运行条件
                    vars.put("electrical", false);
                    if (electric) {
                        //发送开始电气流程
                        vars.put("electrical", true);
                    } else {
                        //没用电气进行涉电
                        //获取项目的领料单，查询停电线路
                        String blackoutLine = blockAss.getQuantity().getBlackoutLine();
                        if (null != blackoutLine && !"".equals(blackoutLine) && "不涉电".equals(blackoutLine)) {
                            //填写停电线路,不为空，-步入发起涉电流程
                            vars.put("notContain", true);
                        } else {
                            vars.put("no", true);
                        }
                    }
                    if (StringUtils.isBlank(materialModule.getId())) {
                        vars.put("manager", UserUtils.getByUserName(blockAss.getAssignment().getConstructionManage()).getLoginName());
                        materialModule.setProjectId(blockAss.getProjectId());
                        materialModule.setProjectName(blockAss.getProjectName());
                        materialModule.setReserveProjectName(blockAss.getReserveProjectName());
                        //保存项目储备项目名称，项目定义号，项目名称
                        materialModuleService.save(materialModule);
                        String procDefId = projectService.findDefId("sgys");
                        //修改项目状态
                        materialModuleService.updateConstructionStatus("1", blockAss.getReserveProjectName());
                        // 启动流程
                        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);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
