package com.engine.htxc.contract.number.cmd;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.htxc.contract.number.config.NumberConfig;
import com.engine.htxc.contract.number.entity.Uf_ContractNoAggr;
import com.engine.htxc.contract.number.entity.Uf_ContractNoAggrUtil;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.DBTools;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.HashMap;
import java.util.Map;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractNumberCmd.java
 * @createTime 2022年06月08日 14:24:00
 */
public class ContractNumberCmd extends AbstractCommonCommand<Map<String, Object>> {

    public ContractNumberCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object>  execute(CommandContext commandContext) {
        String billid= Util.null2String(this.params.get("billid"));
        Map<String,Object> maintable=gethtqcmap(billid);
        Map<String,Object> retmap=new HashMap();
        if(maintable==null || maintable.size()<=0){
            return null;
        }
        boolean iscreate=false;
        String htlx=Util.null2String(maintable.get("htlx"));
        String deptid=Util.null2String(this.user.getUserDepartment());
        Uf_ContractNoAggr uf_contractNoAggr = Uf_ContractNoAggrUtil.getmodeluf_contractnoaggr(billid);
        if(uf_contractNoAggr==null || StringUtils.isBlank(uf_contractNoAggr.getId())){
            iscreate=true;
            uf_contractNoAggr=new Uf_ContractNoAggr();
        }
        if(!iscreate){
          String modehtlx=Util.null2String(uf_contractNoAggr.getHtlx());
          if(modehtlx.equals(htlx)){
              retmap.put("data",true);
              return retmap;
          }
        }
        NumberConfig numberconfig= (NumberConfig)this.params.get("numberconfig");
        String year= DateUtil.format(DateTime.now(),"yyyy");
        String month=DateUtil.format(DateTime.now(),"MM");
        String day=DateUtil.format(DateTime.now(),"dd");
        Map<String,Object> deptmap= getdeptname(deptid);
        String deptmark=Util.null2String(deptmap.get("docnumabbreviation"));
        Map<String,Object> maxnomap=new HashMap<>();
        maxnomap.put("year",year);
        maxnomap.put("deptname",deptmark);
        maxnomap.put("prefix",numberconfig.prefix);
        if(!iscreate && !StringUtils.isBlank(uf_contractNoAggr.getId())){
            maxnomap.put("id",uf_contractNoAggr.getId());
        }
        Integer maxno=getmaxno(maxnomap);
        if(maxno<=0){
            maxno=numberconfig.firstNo;
        }else{
            maxno+=1;
        }
        maxno=numbernofilter(maxno,numberconfig.positionNo);
        if(maxno<0){
            return null;
        }
       try {
           uf_contractNoAggr.setDay(day);
           uf_contractNoAggr.setMonth(month);
           uf_contractNoAggr.setYear(year);
           uf_contractNoAggr.setZzbh(maxno);
           uf_contractNoAggr.setZzjgjc(deptmark);
           uf_contractNoAggr.setQz(numberconfig.prefix);
           uf_contractNoAggr.setMbid(billid);
           uf_contractNoAggr.setHtlx(htlx);
           if(StringUtils.isBlank(uf_contractNoAggr.getId())){
               uf_contractNoAggr.create();
           }else{
               uf_contractNoAggr.updata();
           }

           StringBuilder mark = new StringBuilder(maxno + "");
            while (mark.length() < numberconfig.length) {
                mark.insert(0, "0");
            }
            String htlxvalue="";
            if(!StringUtils.isBlank(htlx)){
                htlxvalue=htlx.split("-").length>1?htlx.split("-")[htlx.split("-").length-1]:"";
            }
            boolean flag=updatemodel(billid,numberconfig.prefix+deptmark+year+month+day+htlxvalue+mark);
            retmap.put("data",flag);
            return retmap;
       }catch (Exception ex){

       }
        return null;
    }


    public boolean updatemodel(String billid,String htbh){
        if(StringUtils.isBlank(billid) || StringUtils.isBlank(htbh)){
            return false;
        }

        Map<String,String> setmap=new HashMap<>();
        setmap.put("htbh",htbh);
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("id",billid);
        return JAVATools.Updatesql(setmap,"uf_htqc",wheremap);
    }


    public Map<String,Object> gethtqcmap(String billid){
        if(StringUtils.isBlank(billid)){
            return null;
        }
        String sql="select htlx from uf_htqc where id=?";
        return DBTools.getsqltomap(sql,billid);
    }

    /**
     * @title numbernofilter
     * @author K1810015
     * @updateTime 2022/6/10 9:58
     * @Remark 做过滤
     * @throws
     */
    public Integer numbernofilter(int numberno,String positionNo){
        if((","+positionNo+",").indexOf(","+numberno+",")<0){
            return numberno;
        }
        boolean flag=true;
        int sum=0;
        while(flag&&sum<10000){
            numberno+=1;
            if((","+positionNo+",").indexOf(","+numberno+",")<0){
                flag =false;
            }
        }
        if(flag){
            return -1;
        }
        return numberno;
    }

    /**
     * @title getmaxno
     * @author K1810015
     * @updateTime 2022/6/9 15:29
     * @Remark 获取建模表里最大的数据
     * @throws
     */
    public Integer getmaxno(Map<String,Object> map){
       String year=Util.null2String(map.get("year"));
       String deptname=Util.null2String(map.get("deptname"));
       String prefix=Util.null2String(map.get("prefix"));
       String id=Util.null2String(map.get("id"));
        Map<String,Object> retmap=null;
       if(StringUtils.isBlank(id)){
           String sql="select  max(zzbh) as zzbh  from uf_ContractNoAggr where qz=? and year=? and zzjgjc=? ";
           retmap=DBTools.getsqltomap(sql,prefix,year,deptname);
       }else{
           String sql="select  zzbh-1 as zzbh  from uf_ContractNoAggr where id=?";
           retmap=DBTools.getsqltomap(sql,id);

       }
       if(retmap==null || retmap.size()<=0){
           return -1;
       }
       int  maxno =Util.getIntValue(Util.null2String(retmap.get("zzbh")),-1);
       return maxno;
    }

    /**
     * @title getdeptname
     * @author K1810015
     * @updateTime 2022/6/9 15:08
     * @Remark  获取部门扩展表的所有数据
     * @throws
     */
    public Map<String,Object> getdeptname(String deptid){
        RecordSet rs=new RecordSet();
        if(StringUtils.isBlank(deptid)){
          return  null;
        }
        String sql="select * from hrmdepartmentdefined where 1=1 and  deptid=?" ;
        return DBTools.getsqltomap(sql,deptid);
    }


}
