package com.engine.htxc.workflow.create.business.contract.util;

import static com.engine.htxc.workflow.create.business.contract.init.ContractSignType.CEwDeptEnd;
import static com.engine.htxc.workflow.create.business.contract.init.ContractSignType.CEwaitDeptEnd;
import static com.engine.htxc.workflow.create.business.contract.init.ContractType.*;

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

import com.engine.htxc.commonutil.db.DbTools;
import static com.engine.htxc.commonutil.type.DateEnum.ShortSuffix;
import com.engine.integration.util.StringUtils;

import cn.hutool.core.date.DateTime;
import weaver.docs.docs.DocManager;
import weaver.general.Util;
import weaver.hrm.User;

/**
 *<p></p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractUpdateStatus.java
 * @createTime 2022-08-15 19:43:00
 */
public class ContractUpdateStatus {

    public String requestid="";

    public String xzhtlb="";
    public String htbb="";

    public User user;

    public String tablename="";
    public Map<String,Object> formtablemap=new HashMap<>();




    private String modetable="";
    private String modeid="";
    public Map<String,Object> modetablemap=new HashMap<>();


    private DbTools dbtools=new DbTools();

    public ContractUpdateStatus(String requestid, String xzhtlb, String htbb, Map<String, Object> modetablemap,
        User user) {
        this.requestid = requestid;
        this.xzhtlb = xzhtlb;
        this.htbb = htbb;
        this.user = user;
        this.modetablemap = modetablemap;
    }
    public ContractUpdateStatus(String requestid,  User user) {
        this.requestid = requestid;
        this.user = user;
    }



    public void init() {
        tablename = dbtools.getTableName(requestid);
        formtablemap = dbtools.getRequestidToMap(requestid, null);
        boolean flag= (StringUtils.isBlank(xzhtlb) || StringUtils.isBlank(htbb)) && modetablemap.size() <= 0 ;
        if (flag) {
             xzhtlb = Util.null2String(formtablemap.get("xzhtlb"));
             htbb = Util.null2String(formtablemap.get("htbb"));
            Map<String, Object> wheremap = new HashMap<>(2);
            wheremap.put("htbh", xzhtlb);
            wheremap.put("htbb", htbb);
            if (!StringUtils.isBlank(xzhtlb) && !StringUtils.isBlank(htbb)) {
                modetablemap = ContracrtBaseUtil.getufhtqcmap(wheremap);
                if (modetablemap != null && modetablemap.size() >= 0) {
                    this.modeid = Util.null2String(modetablemap.get("id"));
                }
            }

        } else {
            if (StringUtils.isBlank(modeid) && modetablemap.size() > 0) {
                this.modeid = Util.null2String(modetablemap.get("id"));
            }
        }
        modetable="uf_htqc";
    }

    /**
     * <P></P>
     * @title ContractUpdateStatus
     * @author K1810015
     * @createtime 2022/8/16 10:24
     * @throws
     */
    public static ContractUpdateStatus createContractUpdateStatus(String requestid, String xzhtlb, String htbb,
        Map<String, Object> map, User user) {
        ContractUpdateStatus contractUpdateStatus = new ContractUpdateStatus(requestid, xzhtlb, htbb, map, user);
        return contractUpdateStatus;
    }

    public static ContractUpdateStatus createContractUpdateStatus(String requestid, User user) {
        ContractUpdateStatus contractUpdateStatus = new ContractUpdateStatus(requestid, user);
        return contractUpdateStatus;
    }

    private List<String> newFileIds(String htyjsmj){
        List<String> list=new ArrayList<>();
        if(!StringUtils.isBlank(htyjsmj)){
            Arrays.stream(htyjsmj.split(",")).forEach((item) -> {
                DocManager docManager = new DocManager();
                docManager.setId(Util.getIntValue(item, -1));
                try {
                    docManager.copyDocForNoRightAndNoChangeStatus();
                    int newdocid = docManager.getId();
                    if(newdocid>0){
                        list.add(Util.null2String(newdocid));
                    }
                }catch (Exception e){
                }
            });
        }
        return list;
    }

//*****************************以下为业务**************************

    /**
     * <p>废止流程办结之后操作</p>
     * @title Discardend
     * @author K1810015
     * @updateTime 2022/8/16 14:23
     */
    public void approvalend(String newrequestid){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return ;
        }
        Map<String, String> wheremap = new HashMap<>(1);
        wheremap.put("id", modeid);
        Map<String, String> setmap = new HashMap<>(2);
        setmap.put("zt", Util.null2String(ContractwaitSign.getValue()));
        //合同审批流程发起关联到建模对应的字段
        setmap.put("glqsxx", initgllc("glqsxx",newrequestid));
        dbtools.updateSql(modetable, setmap, wheremap);
        Map<String, String> tablewheremap = new HashMap<>(1);
        tablewheremap.put("requestid", requestid);
        Map<String, String> tablesetmap = new HashMap<>(1);
        tablesetmap.put("lyjszt", "1");
        dbtools.updateSql(tablename, tablesetmap, tablewheremap);
    }


    /**
     * @title signend
     * @author K1810015
     * @updateTime 2022/8/16 14:22
     */
    public void signend(String newrequestid){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return ;
        }
        String czxz = Util.null2String(formtablemap.get("czxz"));
        String htyjsmj = Util.null2String(formtablemap.get("htyjsmj"));

        Map<String, String> wheremap = new HashMap<>(1);
        wheremap.put("id", modeid);
        Map<String, String> setmap = new HashMap<>(1);
        List<String> htqsjlist=newFileIds(htyjsmj);
        setmap.put("htqsj",Util.null2String(htqsjlist.stream().collect(Collectors.joining(","))));
        if("0".equals(czxz)){
            setmap.put("zt", Util.null2String(ContractDoneSign.getValue()));
            setmap.put("yyjgdzt", Util.null2String(CEwaitDeptEnd.getValue()));
            setmap.put("glgdxx",initgllc("glgdxx",newrequestid));
        }else  if("1".equals(czxz)){
            setmap.put("zt", Util.null2String(ContractwaitSign.getValue()));
        }else   if("2".equals(czxz)){
            setmap.put("zt", Util.null2String(ContractDiscarding.getValue()));
            setmap.put("glfzxx",initgllc("glfzxx",newrequestid));
          }

        dbtools.updateSql(modetable, setmap, wheremap);
        List<Map<String, String>> insertlist = new ArrayList<>();
        List<Map<String, Object>> modelist = dbtools.getRequestidToMapList(requestid, null, 5);
        modelist.forEach((item) -> {
            String dsfmc = Util.null2String(item.get("dsfmc"));
            String htqsrq = Util.null2String(item.get("htqsrq"));
            if (!StringUtils.isBlank(dsfmc)) {
                Map<String, String> insertmap = new HashMap<>(3);
                insertmap.put("dsfmc", dsfmc);
                insertmap.put("htqsrq", htqsrq);
                insertmap.put("mainid", modeid);
                insertlist.add(insertmap);
            }
        });
        if (insertlist.size() > 0) {
            String modetabledt6 = modetable + "_dt6";
            Map<String, Object> deltemap = new HashMap<>(1);
            deltemap.put("mainid", modeid);
            dbtools.delete(modetabledt6, deltemap);
            dbtools.insertAllSql(modetabledt6, insertlist);
        }
    }

    /**
     * <p>废止流程办结之后操作</p>
     * @title Discardend
     * @author K1810015
     * @updateTime 2022/8/16 14:23
     */
    public void discardend(){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return ;
        }
        String fzyy=Util.null2String(formtablemap.get("fzyy"));
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("id",modeid);
        Map<String,String> setmap=new HashMap<>(4);
        setmap.put("zt",Util.null2String(ContractDoneSignEnd.getValue()));
        setmap.put("fzyy",Util.null2String(fzyy));
        setmap.put("fzrq", DateTime.now().toString(ShortSuffix.getValue()));
        dbtools.updateSql(modetable,setmap,wheremap);
    }


    /**
     * <p>归档流程办结</p>
     * @title Conventionend
     * @author K1810015
     * @updateTime 2022/8/16 15:38
     */
    public void  donewkend(String newrequestid){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return ;
        }
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("id",modeid);
        Map<String,String>  setmap=new HashMap<>(7);
        List<String> htqsjlist=newFileIds(Util.null2String(formtablemap.get("htyjsmj")));
        setmap.put("htgdj",Util.null2String(htqsjlist.stream().collect(Collectors.joining(","))));
        setmap.put("zt",Util.null2String(Contractconvention.getValue()));
        setmap.put("yyjgdzt",Util.null2String(CEwDeptEnd.getValue()));
        setmap.put("gdbz",Util.null2String(formtablemap.get("gdbz")));
        setmap.put("jsgdrq",DateTime.now().toString(ShortSuffix.getValue()));
        setmap.put("gllyxx",initgllc("gllyxx",newrequestid));
        dbtools.updateSql(modetable,setmap,wheremap);
    }

    public ContractUpdateStatus modeupdatezt(int value){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return this;
        }
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("id",modeid);
        Map<String,String> setmap=new HashMap<>(1);
        setmap.put("zt",Util.null2String(value));
        dbtools.updateSql(modetable,setmap,wheremap);
        return this;
    }

    public ContractUpdateStatus modeupdate(Map<String,String> setmap){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0 || setmap==null || setmap.size()<=0){
            return this;
        }
        Map<String,String> wheremap=new HashMap<>(1);
        wheremap.put("id",modeid);
        dbtools.updateSql(modetable,setmap,wheremap);
        return this;
    }

    public  ContractUpdateStatus tableUpdateType(Map<String, String> tablesetmap){
        init();
        if(tablesetmap==null || tablesetmap.isEmpty()){
            return this;
        }
        init();
        Map<String, String> tablewheremap = new HashMap<>(1);
        tablewheremap.put("requestid", requestid);
        dbtools.updateSql(tablename, tablesetmap, tablewheremap);
        return this;

    }
    /**
     * <p>履约流程办结</p>
     * @title Conventionend
     * @author K1810015
     * @updateTime 2022/8/16 15:38
     */
    public void  convertionend(){
        init();
        if(StringUtils.isBlank(requestid) || modetablemap.size()<=0 || formtablemap.size()<=0){
            return ;
        }
        Map<String,String> wheremap=new HashMap<>(1);

        wheremap.put("id",modeid);
        Map<String,String> setmap=new HashMap<>(10);
        setmap.put("htgdj",Util.null2String(formtablemap.get("sfymqlyrq")));
        setmap.put("lyks",Util.null2String(formtablemap.get("lyks")));
        setmap.put("lyjs",Util.null2String(formtablemap.get("lyjs")));
        setmap.put("lyjh",Util.null2String(formtablemap.get("lyjh")));
        setmap.put("mqyfje",Util.null2String(formtablemap.get("mqyfje")));
        setmap.put("htyfje",Util.null2String(formtablemap.get("htyfje")));
        setmap.put("fkqs",Util.null2String(formtablemap.get("fkqs")));
        setmap.put("jsgdrq",DateTime.now().toString(ShortSuffix.getValue()));
        dbtools.updateSql(modetable,setmap,wheremap);
        List<Map<String,String>> insertlist=new ArrayList<>();
        List<Map<String,Object>> modelist=dbtools.getRequestidToMapList(requestid,null,5);
        modelist.forEach((item)->{
                Map<String,String> insertmap=new HashMap<>(5);
                insertmap.put("fkbl",Util.null2String(item.get("fkbl")));
                insertmap.put("fkje",Util.null2String(item.get("fkje")));
                insertmap.put("fpje",Util.null2String(item.get("fpje")));
                insertmap.put("fksj",Util.null2String(item.get("fksj")));
                insertmap.put("mainid",modeid);
                insertlist.add(insertmap);
        });
        if(insertlist.size()>0){
            dbtools.insertAllSql(modetable+"_dt7",insertlist);
        }
    }

    public String initgllc(String oldkey,String newrequestid){
        if(modetablemap==null || modetablemap.size()<=0){
            init();
        }
        String gllcvalue= Util.null2String(modetablemap.get(oldkey));
        if(StringUtils.isBlank(newrequestid)){
            return gllcvalue;
        }

        if(StringUtils.isBlank(gllcvalue)){
            gllcvalue=newrequestid;
        }else{
            gllcvalue+=","+newrequestid;
        }
        return gllcvalue;
    }



}
