package com.jeeplus.modules.sg.scheduleView.util;

import com.jeeplus.modules.sg.managementcenter.activiti.entity.Disclose;
import com.jeeplus.modules.sg.managementcenter.activiti.utils.MyActiviUtils;
import com.jeeplus.modules.sg.scheduleView.entity.DisView;
import com.jeeplus.modules.sg.scheduleView.entity.ReceiveView;
import com.jeeplus.modules.sg.scheduleView.entity.ScheduleView;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.task.Task;

import java.text.SimpleDateFormat;
import java.util.List;
/*
处理流程进度信息工具类
 */
public class ScheduleViewUtil {

    /**
     * 处理进度信息中的施工交底信息
     * @param list
     */
    public static void dealDiscloseList(List<ScheduleView> list){
        if(list!=null&&list.size()>0){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String fin = "";
            boolean flag;
            boolean designFlag;
            boolean closeFlag;
            boolean finalClose;
            boolean policyStart1;
            boolean policyStart2;
            boolean policyStart3;
            boolean policyEnd1;
            boolean policyEnd2;
            boolean policyEnd3;
            boolean policy1;
            boolean policy2;
            boolean policy3;
            for (ScheduleView s : list) {
                String status = s.getStatus();

                flag = false;
                designFlag = false;
                closeFlag = false;
                finalClose = false;
                policyStart1 = false;
                policyStart2 = false;
                policyStart3 = false;
                policyEnd1 = false;
                policyEnd2 = false;
                policyEnd3 = false;
                String procInsId = s.getProcInsId();
                if (procInsId != null && !"".equals(procInsId)) {
                    List<HistoricActivityInstance> historicActivityInstances = MyActiviUtils.hisTask(procInsId);
                    for (HistoricActivityInstance a : historicActivityInstances) {
                        String activityName = a.getActivityName();
                        String format = formatter.format(a.getEndTime());
                        if (s.getDisView() == null) {
                            s.setDisView(new DisView());
                        }
                        if ("2".equals(status)) {
                            s.getDisView().setPolicyType("政策处理完成");
                            s.getDisView().setProjectClosure("项目关闭完成");
                            s.getDisView().setDesignChange("设计变更完成");
                            continue;
                        }
                        if ("开始".equals(activityName)) {
                            DisView disView = s.getDisView();
                            if (disView != null) {
                                s.getDisView().setFullTimeStart(format);
                            } else {
                                DisView disView1 = new DisView();
                                disView1.setFullTimeStart(format);
                                s.setDisView(disView1);
                            }
                        }
                        //交底:项目经理交底内容
                        if ("交底:项目经理交底内容".equals(activityName)) {
                            s.getDisView().setProjectManagerImport(format);
                        }
                        //交底:导入材料表和拆旧物资表
                        if ("交底:导入材料表和拆旧物资表".equals(activityName)) {
                            s.getDisView().setUnitManagerJudge(format);
                        }
                        //交底:填写部分工程量信息
                        if ("交底:填写部分工程量信息".equals(activityName)) {
                            s.getDisView().setProjectManagerJudge(format);
                        }
                        if ("交底:填写部分工程量信息".equals(activityName)) {
                            if (fin.equals("")) {
                                fin = format;
                            }
                            if (fin.compareTo(format) < 1) {
                                fin = format;
                            }
                        }


                        //设计变更
                        if ("交底:配网项目经理判定设计变更".equals(activityName)
                                || "交底:经研所判定是否设计变更".equals(activityName)
                                || "交底:运行单位经理审核方案".equals(activityName)
                                || "交底:设计单位人员重新设计".equals(activityName)
                                || "交底:运行经理审核设计变更后".equals(activityName)
                                || "交底:配网经理审核设计变更后".equals(activityName)
                                ) {
                            s.getDisView().setDesignChange("设计变更流程中");
                        }
                        //项目关闭
                        if ("交底:项目经理判定是否关闭".equals(activityName)
                                || "交底:配网工程专职判定是否关闭".equals(activityName)
                                || "交底:配网运行人员判定是否关闭".equals(activityName)
                                || "交底:配网工程专职判定变更或关闭".equals(activityName)
                                || "交底:配网系统专职确认系统关闭".equals(activityName)

                                ) {
                            s.getDisView().setProjectClosure("项目关闭流程中");
                        }


                        //如果因为各种原因完成了设计变更流程或者项目关闭流程
                        if ("交底:导入材料表和拆旧物资表".equals(activityName)) {
                            flag = true;
                        }

                        if ("交底:配网项目经理判定设计变更".equals(activityName)) {
                            designFlag = true;
                        }

                        if ("交底:项目经理判定是否关闭".equals(activityName)) {
                            closeFlag = true;
                        }

                        if ("交底:配网项目经理判定变更或关闭".equals(activityName) || "交底:配网系统专职确认系统关闭".equals(activityName)) {
                            finalClose = true;
                        }

                        //政策处理
                        if ("交底:农村政处".equals(activityName)
                                || "交底:政策处理专职判断".equals(activityName)
                                || "交底:政策处理专职判断".equals(activityName)
                                || "交底:农村上传文件".equals(activityName)
                                || "交底:行政政处".equals(activityName)
                                || "行政:政策处理专职判断".equals(activityName)
                                || "交底:行政上传文件".equals(activityName)
                                || "交底:民事政处".equals(activityName)
                                || "民事:政策处理专职判断".equals(activityName)
                                || "交底:民事上传文件".equals(activityName)
                                || "是否需要农村政处".equals(activityName)
                                || "是否需要行政政处".equals(activityName)
                                || "是否需要民事政处".equals(activityName)
                                ) {
                            s.getDisView().setPolicyType("政策处理流程中");
                        }
                        //判断是否发起农村政处
                        if ("交底:农村政处".equals(activityName)) {
                            policyStart1 = true;
                        }
                        //判断是否发起行政政处
                        if ("交底:行政政处".equals(activityName)) {
                            policyStart2 = true;
                        }
                        //判断是否发起民事政处
                        if ("交底:民事政处".equals(activityName)) {
                            policyStart3 = true;
                        }
                        //判断农村政处是否完成
                        if ("交底:民事是否支付".equals(activityName)
                                || "交底:民事是完成否借款".equals(activityName)) {
                            policyEnd1 = true;
                        }
                        //判断行政政处是否完成
                        if ("交底:行政是否支付".equals(activityName)
                                || "交底:行政是完成否借款".equals(activityName)) {
                            policyEnd2 = true;
                        }
                        //判断民事政处是否完成
                        if ("交底:农村是否支付".equals(activityName)
                                || "交底:农村是完成否借款".equals(activityName)) {
                            policyEnd3 = true;
                        }
                    }
                    //判断项目是否完成
                    if ("2".equals(s.getStatus())) {
                        s.getDisView().setFinTime(fin);
                    }
                    //判断是否已经导入拆旧表
                    if (flag) {
                        //判断是否发起过设计变更
                        if (designFlag) {
                            s.getDisView().setDesignChange("设计变更完成");
                        }
                    }
                    //判断是否发起过项目关闭
                    if (closeFlag) {
                        if (finalClose) {
                            s.getDisView().setProjectClosure("项目关闭完成");
                        }
                    }
                    if (policyStart1) {
                        if (policyEnd1) {
                            policy1 = true;
                        } else {
                            policy1 = false;
                        }
                    } else {
                        policy1 = true;

                    }
                    if (policyStart2) {
                        if (policyEnd2) {
                            policy2 = true;
                        } else {
                            policy2 = false;
                        }
                    } else {
                        policy2 = true;
                    }
                    if (policyStart3) {
                        if (policyEnd3) {
                            policy3 = true;
                        } else {
                            policy3 = false;
                        }
                    } else {
                        policy3 = true;
                    }
                    if (policyStart1 == false && policyStart2 == false && policyStart3 == false) {

                    } else {
                        if (policy1 == true && policy2 == true && policy3 == true) {
                            s.getDisView().setPolicyType("政策处理完成");
                        }
                    }
                }
            }
        }
    }

    /**
     * 处理进度信息中的领料信息
     * @param list
     */
    public static void dealRec(List<ScheduleView> list){
        if(list!=null && list.size()>0){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (ScheduleView s : list) {
                String procInsId = s.getProcInsId();
                if (procInsId != null && !"".equals(procInsId)) {
                    List<HistoricActivityInstance> historicActivityInstances = MyActiviUtils.hisTask(procInsId);
//                    List<Task> tasks = MyActiviUtils.runNowTask(procInsId);
                    for (HistoricActivityInstance a : historicActivityInstances) {
                        String activityName = a.getActivityName();
                        String format = formatter.format(a.getEndTime());
                        if (s.getReceiveView() == null) {
                            s.setReceiveView(new ReceiveView());
                        }
                        if ("项目经理开领料单".equals(activityName)) {
                            s.getReceiveView().setOpenList(format);
                        }
                        if ("领料:经理填写项目模块".equals(activityName)) {
                            s.getReceiveView().setMangagerFile(format);
                        }
                        if ("领料:施工单位领导审核".equals(activityName)) {
                            s.getReceiveView().setMaLeaderReviewed(format);
                        }
                        if ("领料:施工单位物资专职审核".equals(activityName)) {
                            s.getReceiveView().setConUnitReviewed(format);
                        }
                        if ("领料:配网项目经理审核领料单".equals(activityName)) {
                            s.getReceiveView().setConMangagerReviewed(format);
                        }
                        if ("领料:配网物资专职审核领料单".equals(activityName)) {
                            s.getReceiveView().setFullReviewed(format);
                            s.getReceiveView().setRecFinTime(format);
                        }
/*                    if ("领料:项目中心领导审核领料单".equals(activityName)) {
                        s.getReceiveView().setLeaderReviewed(format);
                        s.getReceiveView().setRecFinTime(format);
                    }*/
                    }
                }
            }
        }
    }
}
