package com.engine.htxc.eseal.config;
import cn.hutool.core.date.DateUtil;
import static com.engine.htxc.eseal.type.EffectType.PAPER;
import com.engine.htxc.commonutil.user.UserHt;
import com.engine.htxc.eseal.counterparty.service.impl.CounterpartyServiceImpl;
import com.engine.htxc.eseal.counterparty.vo.Counterparty;
import com.engine.htxc.eseal.type.EffectType;
import com.engine.htxc.eseal.type.IColumnType;
import com.engine.integration.util.StringUtils;
import com.engine.common.util.ServiceUtil;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName EsealConfig.java
 * @createTime 2022年06月21日 15:09:00
 */
public class EsealConfig {
    public UserHt user;
    public UserHt operator;
    public String yzcode="";
    public String flowNum="htbh";
    public String typestatus="yyzt";
    public String detailyyid="yyid";
    public String linktalbe="uf_esealmodeinfo";
    public String linktalbeenum="CONTRACT";
    public String revokeFlag="false";
    public String codeType="USC";
    public String ownerType="INSIDE";
    public String signNum="1";
    public String keyword="华泰证券股份有限公司签章";
    public String signatoryType="ENTERPRISE";
    public String signMethod="TRUSTEESHIP";
    public String signedExamine="DISABLE";
    public String code="";
    public int enddateday=30;
    public String docType;
    public EffectType effectType;
    public String esealtype="";
    public String counterpartyids="";
    public List<String> acrossSealSkipPage=new ArrayList<>(Arrays.asList(new String[]{"DISABLE","ENABLE"}));
    public Map<String,Object> map=new HashMap<>();

    public EsealConfig() {
        map.put("flowName","htmc");
        map.put("flowNum","htbh");
    }

    public EsealConfig signUsermap(){
        if(operator==null){
            return this;
        }
        map.put("signUserId",operator.getWorkcode());
        map.put("signUserName",operator.getLastname());
        return  this;
    }

    public EsealConfig flowOwnermapmap(){
        if(user==null){
            return this;
        }
        try {
            DepartmentComInfo dc = new DepartmentComInfo();
            Map<String, Object> flowOwnermap = new HashMap<>(2);
            flowOwnermap.put("personNum", user.getWorkcode() );
            flowOwnermap.put("personName", user.getUsername());
            //部门编码
            String orgId = Util.null2String(dc.getDepartmentCode(user.getUserDepartment() + ""));
            flowOwnermap.put("orgId", orgId);
            //部门编码
            String orgName = Util.null2String(dc.getDepartmentName(user.getUserDepartment() + ""));
            flowOwnermap.put("orgName", orgName);
            flowOwnermap.put("flowSendTime", DateUtil.date().toString("yyyy-MM-dd hh:mm:ss"));
            map.put("flowOwner", flowOwnermap);
        }catch (Exception exo){

        }
        return this;
    }

    public EsealConfig docSigns(List<Map<String,Object>> list){
        if(list==null || list.size()<=0){
            return this;
        }
        List docsignlist=new ArrayList();
        list.forEach((Map<String,Object> map)->{
            Map<String,Object> docSigns=new HashMap<>(4);
            docSigns.put("docId",map.get("docId"));
            docSigns.put("fileNameDisplay",Util.null2String(map.get("fileNameDisplay")));
            docSigns.put("uniqueKey",Util.null2String(map.get("uniqueKey")));
            docSigns.put("effectType",Util.null2String(map.get("effectType")));
            if(!StringUtils.isBlank(Util.null2String(map.get("effectType")))){
               IColumnType iColumnType=(IColumnType)map.get("effectType");
               if(PAPER.equals(iColumnType)){
                   docSigns.put("acrossSealSkipPage",Util.null2String(map.get("acrossSealSkipPage")));
                   docSigns.put("printSetting",printSetting(
                           Util.null2String(map.get("printuser")),
                           Util.null2String(map.get("printNum"))));
               }
            }

            docSigns.put("docType",docType);
            docSigns.put("signMethod","ORDER");
            docSigns.put("validityStartDate",DateUtil.date().toString("yyyy-MM-dd hh:mm:ss"));
            docSigns.put("validityEndDate",DateUtil.date().toString("yyyy-MM-dd hh:mm:ss"));

            List<Map<String,Object>> signerslist=new ArrayList();
            signerslist.add(getsigners());
            if(esealtype.equals("online") &&!StringUtils.isBlank(counterpartyids)){
                //双方签署场景，取对手方场景
                signerslist= addsigners(signerslist);
            }
            docSigns.put("signers",signerslist);
            docsignlist.add(docSigns);
        });

        map.put("docSigns",docsignlist);

        return this;
    }

    public Map<String,Object> getsigners(){
        Map<String,Object> signers=new HashMap<>(9);
        this.signNum="1";
        signers.put("signatoryType",signatoryType);
        signers.put("ownerType",ownerType);
        signers.put("codeType",codeType);
        signers.put("code",code);
        signers.put("sealId",yzcode);
        signers.put("keyWord",keyword);
        signers.put("signMethod",signMethod);
        signers.put("signNum",signNum);
        signers.put("revokeFlag",revokeFlag);
        signers.put("signedExamine",signedExamine);
        return signers;
    }

    public List addsigners(List list){
        Map<String,Object> counterpartymap=new HashMap<>(1);
        counterpartymap.put("ids",counterpartyids);
        List<Counterparty> listcounterpart=  ServiceUtil.getService(CounterpartyServiceImpl.class).getcounterpartyvo(counterpartymap);
        if(listcounterpart==null || listcounterpart.size()<=0){
            return null;
        }
        listcounterpart.forEach(counterparty->{
            Map<String,Object> signers=new HashMap<>(3);
            signers.put("signatoryType",counterparty.getSignatoryType());
            signers.put("codeType",counterparty.getCodeType());
            signers.put("code",counterparty.getCode());
            if("PERSONAL".equals(counterparty.getSignatoryType())){
               signers.put("name",counterparty.getName());
               signers.put("mail",counterparty.getMail());
               signers.put("mobile",counterparty.getMobile());
            }
            //固定
            signers.put("ownerType","OUTSIDE");
            signers.put("sealId","");
            signers.put("signMethod",signMethod);
            this.signNum=Util.null2String(Util.getIntValues(signNum)+1);
            signers.put("signNum",signNum);
            signers.put("revokeFlag",revokeFlag);
            signers.put("signedExamine",signedExamine);
            list.add(signers);
        });
        return list;
    }


    public  Map<String,Object> printSetting(String userids,String fwdyfs){

        Map<String,String> retmap= getUserMap(userids);
        Map<String,Object> printSetting=new HashMap<>(3);
        if(retmap!=null && retmap.size()>0){
          printSetting.put("personName",Util.null2String(retmap.get("username")));
          printSetting.put("personNum",Util.null2String(retmap.get("userworkcode")));
        }
        printSetting.put("printNum",fwdyfs);

        return printSetting;
    }




    private Map<String,String> getUserMap(String userids){
        String value="";
        Map<String,String> map=new HashMap<>(2);
        if("".equals(userids)){
            return map;
        }
        List<String> userName= new ArrayList<>();
        List<String> userWorkcode= new ArrayList<>();
        String[] userarray=userids.split(",");
            Arrays.stream(userarray).forEach((userid) -> {
                try {
                    UserHt user = new UserHt(Integer.parseInt(userid));
                    if(user!=null){
                        userName.add(user.getUsername());
                        userWorkcode.add(user.getWorkcode());
                      }
                    }catch (Exception e){

                    }
            });
        map.put("username",userName.stream().collect(Collectors.joining(",")));
        map.put("userworkcode",userWorkcode.stream().collect(Collectors.joining(",")));
        return map;
    }



}
