public class CalculatorTimeUtil {
@Resource
private SecurityUserService securityUserService;
@Resource
private RedisUtils redisUtils;
/**
* 平均分配rediskey
*/
private final String redisKey = "executorList";
/**
* 超时时长(小时)
*/
public static final int IND_144 = 144;
public static final int IND_168 = 168;
/**
* 根据起止时间,来计算时间差(小时)
* @param workStartTime String 建单时间
* @return float 小时
*/
public float getHours(String workStartTime){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前系统时间
String endTime = DateUtil.now();
try{
//建单时间戳
long startTime = sf.parse(workStartTime).getTime();
//当前系统时间戳
long deadTime = sf.parse(endTime).getTime();
return new BigDecimal(deadTime-startTime).divide(new BigDecimal(60*60*1000),2, RoundingMode.HALF_UP).floatValue();
}catch (Exception e){
e.printStackTrace();
throw new PeachException("时间转换异常");
}
}
/**
* 小时转超时类型
* @param hours float
* @return String
*/
public String getType(float hours){
String msg = "未超时";
if (MagicNumber.IND_48 <= hours && hours <= MagicNumber.IND_72) {
msg = "响应即将超时";
} else if (MagicNumber.IND_72 < hours && hours < IND_144) {
msg = "响应已超时";
} else if (IND_144 <= hours && hours <= IND_168) {
msg = "最终即将超时";
} else if (IND_168 < hours) {
msg = "最终已超时";
}
return msg;
}
/**
* 自动分配规则
* @return Long
*/
public synchronized Long autoAssign() {
log.info("开始执行平均分配");
Long executorId;
List<SecurityUserVO> executorList;
//从redis中获取
Object executors = redisUtils.get(redisKey);
if (null == executors) {
log.info("redis中无key = {},数据",redisKey);
//获取当前人员
List<SecurityUser> list = securityUserService.getUserList();
executorList = Lists.transform(list,(entity) -> {
SecurityUserVO securityUserVO = new SecurityUserVO();
BeanUtils.copyProperties(entity,securityUserVO);
securityUserVO.setCount(0);
return securityUserVO;
});
} else {
log.info("redis key = {} , 缓存数据 = {}" , redisKey , JSONObject.toJSONString(executors));
String executor = JSONUtil.toJsonStr(executors);
JSONArray jsonArray = JSONUtil.parseArray(JSONUtil.toJsonStr(executor));
executorList = jsonArray.toList(SecurityUserVO.class);
}
if (CollectionUtil.isNotEmpty(executorList)) {
//获取当前最小值
executorId = executorList.stream().min(Comparator.comparing(SecurityUserVO::getCount)).get().getId();
log.info("平均分配被分配员工编号为:" + executorId);
Long finalExecutorId = executorId;
executorList.stream().filter(s -> s.getId() != null && finalExecutorId.equals(s.getId()))
.forEach(securityUserVO -> securityUserVO.setCount(securityUserVO.getCount() + 1));
log.info("可分配员工list集合 = {}" + JSONObject.toJSONString(executorList));
redisUtils.set(redisKey,executorList,600);
return finalExecutorId;
}
return null;
}
}
12-10
287
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交