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

import cn.hutool.core.date.DateTime;
import com.engine.htxc.commonutil.base.BaseUtil;
import com.engine.htxc.commonutil.db.DbTools;
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.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 ConstractEndServiceImpl.java
 * @createTime 2022年07月15日 10:20:00
 */
public class ConstractEndServiceImpl extends ContractWorkflowData implements CustomizeWorkflow {

    private PropBean pb=new PropBean();


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


    @Override
    public int create() {

        Map<String,Object> mainmap=this.getmaintable(null);
        String  workflowid=Util.null2String(pb.getPropName("constract.end.workflowid"));
        if(StringUtils.isBlank(requestid) || StringUtils.isBlank(requestname)){
            return -101;
        }
        String creatorid=Util.null2String(mainmap.get("ngr"));
        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);
        Map<String,Object> requestnamemap= ContracrtBaseUtil.getufhtqcmap(wheremap);
        String prefix=Util.null2String(pb.getPropName("constract.end.workflowname"));
        String requestnamevalue=prefix+requestnamemap.get("htmc");
        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;
        return Util.getIntValue(value,-100);
    }
    public Map<String,String> initdata(){
        String field= "ngr,szbm,szjg,jjcd,qsrqfzzd,htyjsmj,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<>(15);
        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")));
        String qsrqfzzd=getMaxDate(5);
        retmap.put("qsrqfzzd",Util.null2String(qsrqfzzd));
        retmap.put("gljmmc",Util.null2String(maintable.get("gljmmc")));
        String htyjsmj=Util.null2String(maintable.get("htyjsmj"));
        if(!StringUtils.isBlank(htyjsmj)){
            retmap.put("htyjsmj", newFileIds(htyjsmj).stream().collect(Collectors.joining(",")));
        }

        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;
    }

    public String getMaxDate(int dtnum){
        String datestr=DateTime.now().toString("yyyy-MM-dd");
        if(StringUtils.isBlank(this.mainid)||dtnum<=-1){
            return datestr;
        }
        DbTools dbtools=new DbTools();
        String sql="select to_char(max(to_date(htqsrq,'yyyy-MM-dd')),'yyyy-MM-dd') as maxdate from "+this.tablename+"_dt"+dtnum+" where mainid=?";
        Map<String,Object> retmap= dbtools.getsqltomap(sql,this.mainid);
        if(retmap==null || retmap.size()<=0){
            return datestr;
        }

        return Util.null2String(retmap.get("maxdate"));
    }
}
