package com.engine.htxc.workflow.create.business.contract.approval.service.impl;

import cn.hutool.core.date.DateTime;
import com.engine.htxc.commonutil.base.BaseUtil;
import com.engine.htxc.commonutil.user.UserHt;
import com.engine.htxc.commonutil.workflow.WorkflowCreate;

import static com.engine.htxc.workflow.create.business.contract.init.ContractType.ContractwaitSign;

import com.engine.htxc.workflow.create.business.contract.init.ContractWorkflowData;
import com.engine.htxc.workflow.create.business.contract.util.ContracrtBaseUtil;
import com.engine.htxc.workflow.create.business.contract.util.ContractUpdateStatus;
import com.engine.htxc.workflow.create.cinterface.CustomizeWorkflow;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * <p>触发签订流程</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName approvaltosign.java
 * @createTime 2022年07月20日 17:00:00
 */
public class Endtosign extends ContractWorkflowData implements CustomizeWorkflow {


    private PropBean pb=new PropBean();
    private String requestnamevalue="";
    Map<String,Object> requestnamemap=new HashMap<>();


    public Endtosign(Map<String,Object> map) {
        super(map);
    }


    @Override
    public int create() {
        Map<String,Object> mainmap=this.getmaintable(null);
        String  workflowid=Util.null2String(pb.getPropName("constract.sign.workflowid"));
        if(StringUtils.isBlank(requestid) || StringUtils.isBlank(requestname)){
            return -101;
        }
        String creatorid=Util.null2String(map.get("creatorid"));
        if(StringUtils.isBlank(creatorid)){
            return -102;
        }
        String xzhtlb=Util.null2String(mainmap.get("xzhtlb"));
        String htbb=Util.null2String(mainmap.get("htbb"));
        Map<String,Object> wheremap=new HashMap<>();
        wheremap.put("htbh",xzhtlb);
        wheremap.put("htbb",htbb);
        requestnamemap= ContracrtBaseUtil.getufhtqcmap(wheremap);
        String prefix=Util.null2String(pb.getPropName("constract.sign.workflowname"));
        if(requestnamemap==null || requestnamemap.size()<=0){
            return -106;
        }
        String requestnamevalue=prefix+requestnamemap.get("htmc");
        Map<String,String> retmap=initdata();
        if(retmap==null || retmap.size()<=0){
            return -103;
        }
        WorkflowCreate workflowCreate=WorkflowCreate.createWorkflow(p->{
            p.setCreatorId(creatorid);
            p.setWorkflowId(workflowid);
            p.setRequestName(requestnamevalue);
            p.setIsNextFlow("");
            p.setRequestMainMap(retmap);
        }).workflowCreateByRequestMap();
        String value=workflowCreate.requestid;
        if(Util.getIntValue(value,-100)>0){
            String htdsf = Util.null2String(requestnamemap.get("htdsf"));
            if (!StringUtils.isBlank(htdsf)) {
                List<Map<String, String>> dsflist = new ArrayList<>();
                Arrays.stream(htdsf.split(",")).distinct().forEach((item) -> {
                    Map<String, String> dsfmap = new HashMap<>(1);
                    dsfmap.put("dsfmc", Util.null2String(item));
                    dsflist.add(dsfmap);
                });
                workflowCreate.workflowCreatelist("5", dsflist);
            }
            ContractUpdateStatus contractUpdateStatus=ContractUpdateStatus.createContractUpdateStatus(requestid,new User(Util.getIntValue(creatorid,-1)));
            Map<String,String> modeset=new HashMap<>();
            modeset.put("zt",Util.null2String(ContractwaitSign.getValue()));
            modeset.put("glqsxx",contractUpdateStatus.initgllc("glqsxx",value));
            contractUpdateStatus.modeupdate(modeset);

        }
        return Util.getIntValue(value,-100);
    }

    public Map<String,String> initdata(){
        String field= "ngr,szbm,szjg,jjcd,lxfs,ngrq,jjyy,xzhtlb,htbb,htlx,fhtlx,htzt,gljmmc";
        Map<String,Object>maintable=getmaintable(Arrays.stream(field.split(",")).collect(Collectors.toList()));
        Map<String,String> retmap=new HashMap<>();
        int creatorid=Util.getIntValue(Util.null2String(map.get("creatorid")),-1);
        UserHt userht=new UserHt(creatorid);
        retmap.put("ngr",Util.null2String(creatorid));
        retmap.put("szbm",Util.null2String(userht.getUserDepartment()));
        retmap.put("szjg",Util.null2String(BaseUtil.getusertoszjg(Util.null2String(creatorid))));
        retmap.put("jjcd",Util.null2String(maintable.get("jjcd")));
        retmap.put("lxfs",Util.null2String(userht.getMobile()));
        retmap.put("ngrq", DateTime.now().toString("yyyy-MM-dd hh:mm"));
        retmap.put("jjyy",Util.null2String(maintable.get("jjyy")));
        retmap.put("xzhtlb",Util.null2String(maintable.get("xzhtlb")));
        retmap.put("htbb",Util.null2String(maintable.get("htbb")));
        retmap.put("htlx",Util.null2String(maintable.get("htlx")));
        retmap.put("fhtlx",Util.null2String(maintable.get("fhtlx")));
        retmap.put("htzt",Util.null2String(maintable.get("htzt")));
        retmap.put("gljmmc",Util.null2String(maintable.get("gljmmc")));
        Map<String,String> result= retmap.entrySet().stream().filter(mapfilter->
                !StringUtils.isBlank(Util.null2String(mapfilter.getValue()))
        ).collect(Collectors.toMap(p->p.getKey(),p-> p.getValue()));
        return result;
    }
}
