package com.engine.htxc.contract.number.service.impl;

import com.engine.htsc.seal.web.RedisTools;
import com.engine.htxc.contract.number.cmd.ContractNumberCmd;
import com.engine.htxc.contract.number.config.NumberConfig;
import com.engine.htxc.contract.number.service.ContractNumberService;
import com.engine.core.impl.Service;
import com.engine.integration.util.StringUtils;
import redis.clients.jedis.Jedis;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.wechat.util.Utils;

import java.util.Map;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractNumberServiceImpl.java
 * @createTime 2022年06月08日 14:20:00
 */
public class ContractNumberServiceImpl extends Service implements ContractNumberService {

    private PropBean pb=new PropBean();
    private BaseBean bs=new BaseBean();
    @Override
    public Boolean createNumber(Map<String, Object> map, User user) {

        if(map==null || map.size()<=0 ){
            return false;
        }
        NumberConfig numberConfig=new NumberConfig();
        //先去取外部的变量因素
        String firstno= Utils.null2String(pb.getPropName("contract.firstno"));
        String positionno= Utils.null2String(pb.getPropName("contract.positionno"));
        String prefix=Utils.null2String(pb.getPropName("contract.prefix"));
          if(StringUtils.isBlank(prefix)){
            return  false;
        }
        numberConfig.prefix=prefix;
        if(!StringUtils.isBlank(positionno)){
            numberConfig.positionNo=positionno;
        }
        if(!StringUtils.isBlank(firstno)){
            int isum=Util.getIntValue(firstno,-1);
            if(isum>0){
                numberConfig.firstNo=isum;
            }
        }

        map.put("numberconfig",numberConfig);
        //重置配置 config文件

        String deptid=Util.null2String(user.getUserDepartment());

    //    Jedis jedis = RedisTools.GetJedis();
   //     String rediskey = "dept"+deptid+"_redis_key";
        boolean flag=false;
        Map<String,Object> retmap= new ContractNumberCmd(map,user).execute(null);
        String value="";
        if(retmap!=null && retmap.size()>0){
            value=Util.null2String(retmap.get("data"));
        }
        if("true".equals(value)){
            flag= true;
        }
//    try{
//            int counts=0;
//            int expireTime=Util.getIntValue(Util.null2String(pb.getPropName("RedisExpiretime")));
//            boolean redisfalg=RedisTools.TryGetDistributedLock(jedis,rediskey,deptid,expireTime);
//            bs.writeLog("redis开始状态:"+redisfalg+",deptid:"+deptid);
//            while (redisfalg==false && counts<10) {
//                Thread.sleep(500);
//                counts++;
//                redisfalg=RedisTools.TryGetDistributedLock(jedis,rediskey,deptid,expireTime);
//            }
//            bs.writeLog("redis结束状态:"+redisfalg+",deptid:"+deptid);
//        }catch (Exception e){
//            bs.writeLog(rediskey+"保存失败"+e.getMessage());
//        }finally {
//            bs.writeLog(rediskey+"解锁开始");
//            RedisTools.releaseDistributedLock(jedis,rediskey,deptid);
//            bs.writeLog(rediskey+"解锁结束");
//            bs.writeLog(rediskey+"释放锁开始");
//            jedis.close();
//            bs.writeLog(rediskey+"释放锁结束");
//        }
       return flag;
    }


}
