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

import cn.hutool.core.date.DateTime;
import com.engine.htxc.commonutil.user.UserHt;
import com.engine.htxc.commonutil.workflow.WorkflowCreate;
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.interfaces.htsc.comInfo.PropBean;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>创建流程--合同履约</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName service.java
 * @createTime 2022年07月22日 10:53:00
 */
public class ConventionserviceImpl extends ContractWorkflowData implements CustomizeWorkflow {

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

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


    @Override
    public int create() {
        Map<String,Object> mainmap=this.getmaintable(null);
        String  workflowid= Util.null2String(pb.getPropName("constract.convertion.workflowid"));
        if(StringUtils.isBlank(requestid) || StringUtils.isBlank(requestname)){
            return -101;
        }
        String creatorid=Util.null2String(mainmap.get("lyclr"));
        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<>(2);
        wheremap.put("htbh",xzhtlb);
        wheremap.put("htbb",htbb);
         requestnamemap= ContracrtBaseUtil.getufhtqcmap(wheremap);
        if(requestnamemap==null || requestnamemap.size()<=0){
            return -106;
        }
        String prefix=Util.null2String(pb.getPropName("constract.convertion.workflowname"));
        String requestnamevalue=prefix+requestnamemap.get("htmc");
        String contractmodeid=Util.null2String(requestnamemap.get("id"));
        Map<String,String> retmap=initdata();
        if(retmap==null || retmap.size()<=0){
            return -103;
        }
        String value= WorkflowCreate.createWorkflow(p->{
            p.setCreatorId(creatorid);
            p.setWorkflowId(workflowid);
            p.setRequestName(requestnamevalue);
            p.setIsNextFlow("");
            p.setRequestMainMap(retmap);
        }).workflowCreateByRequestMap().requestid;
        if(Util.getIntValue(value,-100)>0){
             ContractUpdateStatus.createContractUpdateStatus(requestid,user).donewkend(value);
        }
        return Util.getIntValue(value,-100);
    }
    public Map<String,String> initdata(){
        String field= "ngr,szbm,szjg,jjcd,lxfs,qsrqfzzd,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<>(15);
        int creatorid= Util.getIntValue(Util.null2String(maintable.get("ngr")),-1);
        UserHt userht=new UserHt(creatorid);
        retmap.put("ngr",Util.null2String(creatorid));
        retmap.put("szbm",Util.null2String(maintable.get("szbm")));
        retmap.put("szjg",Util.null2String(maintable.get("szjg")));
        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("qsrqfzzd",Util.null2String(maintable.get("qsrqfzzd")));
        retmap.put("htzt",Util.null2String(maintable.get("htzt")));
        retmap.put("gljmmc",Util.null2String(maintable.get("gljmmc")));
        retmap.put("sfymqlyrq","0");
        String htjexz=Util.null2String(requestnamemap.get("htjexz"));
        if(!StringUtils.isBlank(htjexz)){
            //无需转化
            retmap.put("jelx",htjexz);
        }
        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;
    }

}
