package com.engine.htsc.jgzxzysx.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.htsc.payment.util.PayUtil;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName JgzxzdsywknumberCmd.java
 * @createTime 2022年05月19日 16:14:00
 */
public class JgzxzdsywknumberCmd extends AbstractCommonCommand<Map<String, Object>> {

    public JgzxzdsywknumberCmd(User user, Map<String, Object> params) {
        this.params = params;
        this.user = user;
    }
    private  DateTime newdate=DateTime.now();

    private  String tablename="uf_jgzxlcbh";

    private RecordSet rs=new RecordSet();
    @Override
    public BizLogContext getLogContext() {
        return null;
    }


    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        PropBean pb=new PropBean();
        Map<String,Object> returnmap=new HashMap<>();
        String datename=newdate.toString("yyyyMMdd");
        String deptid= Util.null2String(this.params.get("deptid"));
        String name=Util.null2String(pb.getPropName("jgzxzdsy.workflownumname"));
        //Util.null2String(user.getUserDepartment())

        String deptname = getdeptname(deptid);
        String unnumber= Util.null2String(this.params.get("unnumber"));

        String numberno=name+deptname+datename;
        String value=  getrangevalue(numberno);
        returnmap.put("data",value);
        return returnmap;
    }


    private  int rollnum=0;
    public String getrangevalue(String unnumber){
        int randnum=(int)((Math.random()*9+1)*100000);
        String value=unnumber+randnum;
        int maxnum=100;

//        while(!getsql(value) &&  rollnum<=maxnum){
//            rollnum++;
//            getrangevalue(unnumber);
//        };
        //递归有点吃线程资源
        for (int i=0;i<maxnum;i++){
            if(getsql(unnumber)) {
                break;
            }
        }
        return value;
    }

    public boolean getsql(String remark){
        int randnum=(int)((Math.random()*9+1)*100000);
       String sql="select count(*) as numcount from workflow_requestbase where requestmark=? ";
        rs.executeQuery(sql,remark);
        if(rs.getCounts()<=0){
            return true;
        }
        rs.next();
        int value=Util.getIntValue(Util.null2String(rs.getString("numcount")),-1);
        if(value<=0){
            return true;
        }
        return  false;
    }

    //
    public void insertmodel(Map<String,String> map){
        String modeuuid = UUID.randomUUID().toString();
        int formModeId = PayUtil.getFormModeIdByCubeName("uf_fklcbh");
        map.put("FORMMODEID",Util.null2String(formModeId));
        map.put("MODEDATACREATER",Util.null2String(this.user.getUID()));
        map.put("MODEDATACREATERTYPE",Util.null2String(this.user.getType()));
        map.put("MODEDATACREATEDATE", newdate.toString("yyyy-MM-dd"));
        map.put("MODEDATACREATETIME",newdate.toString("HH:mm:ss"));
        map.put("MODEUUID",modeuuid);
        JAVATools.Excetesql(map,tablename);
        //权限重构
        int dataId = PayUtil.getDataIdByUUID(modeuuid);
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.editModeDataShare(1, formModeId, dataId);
    }
    public String getdeptname(String id){
        if(StringUtils.isBlank(id)){
            return "";
        }
        String sql="select docnumabbreviation from hrmdepartmentdefined where 1=1 ";
        Map<String,String> wheremap=new HashMap<>();
        wheremap.put("deptid",id);
        Map<String,Object> retmap=JAVATools.GetTableToMap(sql,wheremap);
        if(retmap==null || retmap.size()<=0){
            return "";
        }

       return Util.null2String(retmap.get("docnumabbreviation"));
    }
        //        String isCreate = Util.null2String(request.getParameter("iscreate")); //是否为创建,1为创建
//        int workFlowId = Util.getIntValue(request.getParameter("workflowid"));
//        int formModeId = PayUtil.getFormModeIdByCubeName("uf_fklcbh");
//        StringBuilder mark = new StringBuilder(); //流程编码
//        String sql = "select WORKFLOWNAME,UNNUMBER from workflow_base where ID=?";
//        rs.executeQuery(sql, workFlowId);
//        String name = "";
//        if (rs.next()) {
//            name = rs.getString("WORKFLOWNAME");
//        }
//
//            String unNumber = PayUtil.getUnNumberByWorkFlowId(workFlowId);
//            String modeUuid = "";
//            LocalDateTime now = LocalDateTime.now();
//            String yM = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
//
//            synchronized (this) {
//                rs.executeQuery("select id,lsh,MODEUUID from uf_fklcbh where ny = '" + yM + "' and LCLX='" + unNumber + "'");
//                if (rs.next()) {
//                    //更新
//                    int num = Util.getIntValue(rs.getString("lsh")) + 1;
//                    int id = Util.getIntValue(rs.getString("id"));
//                    modeUuid = Util.null2String(rs.getString("MODEUUID"));
//                    String updateSql = "update uf_fklcbh set lsh=? where id=?";
//                    rs.executeUpdate(updateSql, num, id);
//                    mark = new StringBuilder(num + "");
//                    while (mark.length() < 6) {
//                        mark.insert(0, "0");
//                    }
//                    mark = new StringBuilder(name + now.format(DateTimeFormatter.ofPattern("yyyyMM")) + mark);
//                } else {
//                    String userId = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
//                    String userType = Util.null2String(request.getParameter("f_weaver_belongto_usertype"));
//                    String createDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//                    String createTime = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
//                    //插入
//                    String insertSql = "insert into uf_fklcbh" +
//                            "(FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME,MODEUUID,LSH,NY,LCLX) \n" +
//                            "values(?,?,?,?,?,?,?,?,?)";
//                    String uuid = UUID.randomUUID().toString();
//                    modeUuid = uuid;
//                    rs.executeUpdate(insertSql, formModeId, userId, userType, createDate, createTime, uuid, "1", yM, unNumber);
//                    mark = new StringBuilder(name + now.format(DateTimeFormatter.ofPattern("yyyyMM")) + "000001");
//                }
//                //权限重构
//                int dataId = PayUtil.getDataIdByUUID(modeUuid);
//                ModeRightInfo modeRightInfo = new ModeRightInfo();
//                modeRightInfo.editModeDataShare(1, formModeId, dataId);
//        return null;
//
}
