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

import java.util.*;
import java.util.stream.Collectors;
import com.engine.htxc.commonutil.base.BaseUtil;
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 cn.hutool.core.date.DateTime;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

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

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

    @Override
    public int create() {
        if (StringUtils.isBlank(requestid) || StringUtils.isBlank(requestname)) {
            return -101;
        }
        String creatorid = Util.null2String(map.get("creatorid"));
        if (StringUtils.isBlank(creatorid)) {
            return -102;
        }
        int value = createworkflow();
        return value;
    }

    public int createworkflow() {
        String workflowid = Util.null2String(pb.getPropName("constract.sign.workflowid"));
        String field = "ngr,szbm,szjg,jjcd,lxfs,ngrq,jjyy,xzhtlb,fhtlx";
        Map<String, Object> maintable = getmaintable(Arrays.stream(field.split(",")).collect(Collectors.toList()));
        List<Map<String, String>> dt4maplist = getdetailtable(4, null);
        boolean flag= (
                maintable == null || maintable.isEmpty()
                || dt4maplist == null || dt4maplist.isEmpty());
        if (flag) {
            return -1;
        }
        Map<String, String> retmap = new HashMap<>(3);
        retmap.put("jjcd", Util.null2String(maintable.get("jjcd")));
        retmap.put("ngrq", DateTime.now().toString("yyyy-MM-dd hh:mm"));
        retmap.put("jjyy", Util.null2String(maintable.get("jjyy")));
        dt4maplist.forEach(dt4map -> {
            Map<String, Object> draftedmap = new HashMap<>(2);
            draftedmap.put("htbh", Util.null2String(dt4map.get("htbh")));
            draftedmap.put("htbb", Util.null2String(dt4map.get("htbb")));
            String prefix = Util.null2String(pb.getPropName("constract.sign.workflowname"));
            ufhtqcmap = ContracrtBaseUtil.getufhtqcmap(draftedmap);
            if (ufhtqcmap != null || ufhtqcmap.size() > 0) {
                String htmc = Util.null2String(ufhtqcmap.get("htmc"));
                String qcr = Util.null2String(ufhtqcmap.get("qcr"));
                if (!StringUtils.isBlank(htmc) && !StringUtils.isBlank(qcr)) {
                    requestnamevalue = prefix + htmc;
                    List<String> draftedlist = Arrays.stream(qcr.split(",")).collect(Collectors.toList());
                    draftedlist.forEach(createidvalue -> {
                        int userid = Util.getIntValue(createidvalue, -1);
                        UserHt userht = new UserHt(userid);
                        if (userid > 0) {
                            retmap.put("xzhtlb", Util.null2String(dt4map.get("htbh")));
                            retmap.put("fhtlx", Util.null2String(dt4map.get("htflx")));
                            retmap.put("htbb", Util.null2String(dt4map.get("htbb")));
                            retmap.put("htlx", Util.null2String(dt4map.get("htlx")));
                            retmap.put("htzt", "3");
                            retmap.put("ngr", Util.null2String(userid));
                            retmap.put("szbm", Util.null2String(userht.getUserDepartment()));
                            retmap.put("szjg", Util.null2String(BaseUtil.getusertoszjg(Util.null2String(userid))));

                            Map<String, Object> contracttypemap = new HashMap<>(1);
                            contracttypemap.put("contracttype", Util.null2String(dt4map.get("htlx")));
                            linkmodemap = ContracrtBaseUtil.getmodeltype(contracttypemap);
                            if (linkmodemap != null && linkmodemap.size() > 0) {
                                retmap.put("gljmmc", Util.null2String(linkmodemap.get("jmbd")));
                            }

                            Map<String,
                                String> result = retmap.entrySet().stream()
                                    .filter(mapfilter -> !StringUtils.isBlank(Util.null2String(mapfilter.getValue())))
                                    .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
                            WorkflowCreate workflowCreate = WorkflowCreate.createWorkflow(p -> {
                                p.setCreatorId(createidvalue);
                                p.setWorkflowId(workflowid);
                                p.setRequestName(requestnamevalue);
                                p.setIsNextFlow("");
                                p.setRequestMainMap(result);
                            }).workflowCreateByRequestMap();
                            if (!StringUtils.isBlank(workflowCreate.requestid)
                                && Util.getIntValue(workflowCreate.requestid, -100) > 0) {
                                String htdsf = Util.null2String(ufhtqcmap.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
                                    .createContractUpdateStatus(requestid, Util.null2String(dt4map.get("htflx")),
                                        Util.null2String(dt4map.get("htbh")), ufhtqcmap, user)
                                    .approvalend(workflowCreate.requestid);

                            } else {
                                // 做好记录
                            }

                        }
                    });

                }
            }
        });
        return 1;
    }
}
