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.service.NonPowerService;
import com.jeeplus.modules.sg.picking.activiti.service.ShowListService;
import com.jeeplus.modules.sg.settActivi.entity.SettActivi;
import com.jeeplus.modules.sg.settActivi.service.SettActiviService;
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;
import java.util.Set;

/**
 * 土建流程结束，自动判断是否完成施工验收流程，发送
 */
public class nonModuleListen implements Serializable, ExecutionListener {
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = 8513750196548027535L;

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        try {
            ProjectService projectService = SpringContextHolder.getBean(ProjectService.class);
            ActProcessService actProcessService = SpringContextHolder.getBean(ActProcessService.class);
            ActTaskService actTaskService = SpringContextHolder.getBean(ActTaskService.class);
            SettActiviService settActiviService = SpringContextHolder.getBean(SettActiviService.class);
            SettActivi settActivi = new SettActivi();
            String procInsId = delegateExecution.getProcessInstanceId();
            //土建流程完成，修改项目状态
            projectService.updateTjStatus("2", procInsId);
            //判断是否完成土建，是否完成施工验收，完成发送
            Project sgProcInsId = projectService.findTjProcInsId(procInsId);
            String advanceStatus = sgProcInsId.getAdvanceStatus();
            String constructionStatus = sgProcInsId.getConstructionStatus();
            if ("2".equals(advanceStatus) && "2".equals(constructionStatus)) {
                //发送结算流程
                settActivi.setProjectId(sgProcInsId.getProjectId());
                settActivi.setProjectName(sgProcInsId.getProjectName());
                settActivi.setReserveProjectName(sgProcInsId.getReserveProjectName());
                settActiviService.save(settActivi);
                //修改项目结算状态
                projectService.updateJsStatus("1", sgProcInsId.getReserveProjectName());
                // 启动施工验收
                Map<String, Object> vars = Maps.newHashMap();
                vars.put("manager", UserUtils.getByUserName(sgProcInsId.getAssignment().getOwnerManager()).getLoginName());
                String procDefId = projectService.findDefId("sett");
                ProcessDefinition p = actProcessService.getProcessDefinition(procDefId);
                String title = UserUtils.getUser().getLoginName() + "在" + DateUtils.getDateTime() + "发起" + p.getName();
                actTaskService.startProcess(p.getKey(), "xm_settlement_process", settActivi.getId(), title, vars);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
