package com.engine.htxc.contract.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.util.ServiceUtil;
import com.engine.core.interceptor.CommandContext;
import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.dept.DeptTools;
import com.engine.htxc.workflow.select.service.WorkflowSelectService;
import com.engine.htxc.workflow.select.service.impl.WorkflowSelectServiceImpl;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>该类只做一件事情，合同分送节点的判断</p>
 *
 * @author K1810015
 * @version 1.0.0
 * @ClassName workflowNextJudgeCmd.java
 * @createTime 2022年08月26日 14:43:00
 */
public class WorkflowNextJudgeCmd extends AbstractCommonCommand<Map<String, Object>> {

    public WorkflowNextJudgeCmd(Map<String, Object> params, User user) {
            this.user = user;
            this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    DbTools dbtools=new DbTools();
    public  String jwfk="1";
    Map<String,Object> mainmap=new HashMap<>();
    Map<String,Object> flagmap=new HashMap<>();
    List<String> moneylist=new ArrayList<>();
    List<Map<String,Object>>  detail1list=new ArrayList<>();
    List<Map<String,Object>>  detail2list=new ArrayList<>();
    List<Map<String,Object>>  detail4list=new ArrayList<>();
    public  Double moneysum=0D;
    public PropBean pb=new PropBean();
    private WorkflowSelectService workflowSelectService=
            ServiceUtil.getService(WorkflowSelectServiceImpl.class);

        @Override
        public Map<String, Object> execute(CommandContext commandContext) {
           Map<String,Object> result=new HashMap<>(2);
            result.put("flag",false);
           String requestid= Util.null2String(params.get("requestid"));
           if(StringUtils.isBlank(requestid)){
               return result;
           }
            mainmap=dbtools.getRequestidToMap(requestid,new ArrayList<>());
            detail2list=dbtools.getRequestidToMapList(requestid,new ArrayList<>(),2);
            detail1list=dbtools.getRequestidToMapList(requestid,new ArrayList<>(),1);
            detail4list=dbtools.getRequestidToMapList(requestid,new ArrayList<>(),4);
            detail4list.forEach((item)->{
                if(!StringUtils.isBlank(Util.null2String(item.get("htzje")))){
                    moneylist.add(Util.null2String(item.get("htzje")));
                }
              if(!StringUtils.isBlank(Util.null2String(item.get("jwfk"))) && "0".equals(Util.null2String(item.get("jwfk")))){
                  jwfk="0";
              }
           });
            moneysum= moneylist.stream().collect(Collectors.summingDouble(num->Util.getDoubleValue(num)));
           flagmap.put("requestid",requestid);
           flagmap.put("deptid","");
           flagmap.put("isfreenode","1");
          // 送财税管理岗（有金额合同&境外付款：是）满足条件展示并必送
          if(!todofinanceflag()){
              result.put("msg","3");
              return result;
          }
          //  送预算管理岗（有合同金额&境外付款：否）满足条件展示并必送
          if(!todobudgetflag())
          {
              result.put("msg","4");
              return result;
          }
            //  送净资本分析员(合同金额>数字字典配置金额)满足条件展示并必送
          if(!todoNetAssetsflag()){
              result.put("msg","5");
              return result;
          }
//          // 送合同审核员审批（必送节点）
         if(!todoauditflag()){
                result.put("msg","6");
                return result;
          }
         //送金融产品代销岗审核（拟稿部门：资产托管部&印章：代销金融产品业务专业章）满足条件展示并必送
         if(!todoFinancialproductflag()){
             result.put("msg","7");
             return result;
         }
         // 送工会主席审核（拟稿部门：党群||会签部门包含党群且送会签）
          if(!todoUnionpresident()){
              result.put("msg","8");
              return result;
        }

        //送派驻纪检监察组审核（会签部门不为空）满足条件展示并必送
            if(!todoMonitoringTeamflag()){
                result.put("msg","9");
                return result;
            }
            //    送执行秘书
            if(!todoexecutiveSecretaryflag()){
                result.put("msg","10");
                return result;
            }
        result.put("flag",true);
          return result;

       }




       /**
        * <p>送财税管理岗审批必填</p>
        * @title todofinanceflag
        * @author K1810015
        * @updateTime 2022/8/26 15:04
        */
       public boolean todofinanceflag(){
         if(!"0".equals(jwfk) || moneysum<=0D){
            return  true;
         }
           flagmap.put("nodenumber","zbhtsp_csglg");
           return workflowSelectService.getnodeflag(flagmap);
       }

       /**
        * <p>送预管理岗</p>
        * @title todofinanceflag
        * @author K1810015
        * @updateTime 2022/8/26 15:11
        */
        public boolean todobudgetflag() {

            if(!"1".equals(jwfk) || moneysum<=0D){
                return  true;
            }
            flagmap.put("nodenumber","zbhtsp_ysglg");
            return workflowSelectService.getnodeflag(flagmap);
    }



    /**
     * <p>送净资产分析员</p></p>
     * @title todofinanceflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoNetAssetsflag() {
        Double vlaue=Util.getDoubleValue(new PropBean().getPropName("contract.datadicconfigamout"));
        if(moneysum<vlaue){
            return true;
        }
        flagmap.put("nodenumber","zbhtsp_jzbfxy");
        return workflowSelectService.getnodeflag(flagmap);
        // workflowSelectService.getnodeflag();

    }



    /**
     * <p>送合同审核员审批</p></p>
     * @title todofinanceflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoauditflag() {
        flagmap.put("nodenumber","zbhtsp_htshy");
        return workflowSelectService.getnodeflag(flagmap);
    }


    /**
     * <p>送金融产品代销岗审核</p></p>
     * @title todoFinancialproductflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoFinancialproductflag() {

        String deptid=Util.null2String(this.mainmap.get("szbm"));
        String yz=Util.null2String(this.mainmap.get("yz"));
        String jrcpyz=Util.null2String(pb.getPropName("eseal.code.jrcpyz"));
        String jrcpbcode=Util.null2String(pb.getPropName("dept.code.cgtgb"));
        if(StringUtils.isBlank(deptid) || StringUtils.isBlank(yz)){
            return true;
        }
        List<String> deptcodelist=DeptTools.getidtocode(deptid);
        if(!yz.equals(jrcpyz) || !deptcodelist.contains(jrcpbcode)){
            return true;
        }
        flagmap.put("nodenumber","zbhtsp_jrcpdxg");
        return workflowSelectService.getnodeflag(flagmap);
    }


    /**
     * <p>送工会主席审核</p></p>
     * @title todoUnionpresident
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean  todoUnionpresident() {
        String deptid=Util.null2String(this.mainmap.get("szbm"));
        String jrcpbcode=Util.null2String(pb.getPropName("dept.code.tq"));
        if(StringUtils.isBlank(deptid) || StringUtils.isBlank(jrcpbcode)){
            return true;
        }
        List<String> deptcodelist=DeptTools.getidtocode(deptid);
        List<String> hqbmlistcodelist=DeptTools.getidtocode(deptid);
        List<String> hqbmlist=new ArrayList<>();
        if(detail2list!=null && !detail2list.isEmpty()){
            detail2list.stream().forEach((item)->{
                if(!StringUtils.isBlank(Util.null2String(item.get("hqbm")))){
                    hqbmlist.add(Util.null2String(item.get("hqbm")));
                }
            });
            hqbmlistcodelist=DeptTools.getidtocode(hqbmlist.stream().distinct().collect(Collectors.joining(",")));
        }

         if(!deptcodelist.contains(jrcpbcode) && !hqbmlistcodelist.contains(jrcpbcode)){
            return true;
        }


        flagmap.put("nodenumber","zbhtsp_ghzx");
        return workflowSelectService.getnodeflag(flagmap);
    }


    /**
     * <p>纪检监察组</p>
     * @title todofinanceflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoMonitoringTeamflag() {
        if(detail2list!=null && detail2list.isEmpty()){
            return true;
        }
        flagmap.put("nodenumber","zbhtsp_pzjjjcz");
        return workflowSelectService.getnodeflag(flagmap);
    }


    /**
     * <p>执行秘书</p></p>
     * @title todofinanceflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoexecutiveSecretaryflag() {
        String ldsp=Util.null2String(this.mainmap.get("ldsp2"));
        if(StringUtils.isBlank(ldsp)){
            return true;
        }
        flagmap.put("nodenumber","zbhtsp_ldsp");
        flagmap.put("userid",ldsp);
        boolean flag= workflowSelectService.getnodeflag(flagmap);
        flagmap.remove("userid");
        if(flag){
            flagmap.put("nodenumber","zbhtsp_zxmsg");
            return workflowSelectService.getnodeflag(flagmap);
        }
        return flag;
    }


    /**
     * <p>送用印</p></p>
     * @title todofinanceflag
     * @author K1810015
     * @updateTime 2022/8/26 15:13
     */
    public boolean todoesealflag() {
        flagmap.put("nodenumber","zbhtsp_yy");
        return workflowSelectService.getnodeflag(flagmap);

    }

}
