From fe82f1f9a9be911d1420fe3b018ea85dd5fff1a3 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期四, 21 十一月 2024 21:22:58 +0800
Subject: [PATCH] 代码整理
---
collect/src/main/java/com/qianwen/mdc/collect/cache/WorkstationCache.java | 145 ++++++++++++++++++++++++++----------------------
1 files changed, 78 insertions(+), 67 deletions(-)
diff --git a/collect/src/main/java/com/qianwen/mdc/collect/cache/WorkstationCache.java b/collect/src/main/java/com/qianwen/mdc/collect/cache/WorkstationCache.java
index 1083904..7f40c31 100644
--- a/collect/src/main/java/com/qianwen/mdc/collect/cache/WorkstationCache.java
+++ b/collect/src/main/java/com/qianwen/mdc/collect/cache/WorkstationCache.java
@@ -8,6 +8,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -17,24 +18,18 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.qianwen.core.redis.cache.BladeRedis;
import com.qianwen.mdc.collect.dto.WorkstationDTO;
+import com.qianwen.mdc.collect.entity.mgr.EmployeeOnOffWork;
import com.qianwen.mdc.collect.entity.mgr.GlobalWcsOfRps;
import com.qianwen.mdc.collect.entity.mgr.Workstation;
+import com.qianwen.mdc.collect.mapper.mgr.EmployeeOnOffWorkMapper;
import com.qianwen.mdc.collect.mapper.mgr.GlobalWcsOfRpsMapper;
import com.qianwen.mdc.collect.service.WorkstationService;
import com.qianwen.mdc.collect.utils.redis.RedisUtil;
-
import cn.hutool.core.util.ObjectUtil;
-//import com.qianwen.posting.convert.WorkstationConvert;
-
-//import com.qianwen.posting.entity.mysql.EmployeeOnOffWork;
-
-//import com.qianwen.posting.entity.mysql.Workstation;
-//import com.qianwen.posting.mapper.mysql.EmployeeOnOffWorkMapper;
-
-//import com.qianwen.posting.service.IWorkstationService;
@Component
public class WorkstationCache {
+ public static final String COLLECT_WORKSTATION = "collect:workstation";
private static final String WORKSTATION_ID = "workstation:id:";
private static final String CALENDAR_DATE = ":calendar:date:";
private static final String WORKSTATION_ALL = "workstation:all";
@@ -43,18 +38,17 @@
@Autowired
private RedisUtil redisUtil;
private static final Logger log = LoggerFactory.getLogger(WorkstationCache.class);
- //private static final BladeRedis bladeRedis = (BladeRedis) SpringUtil.getBean(BladeRedis.class);
- //private static final IWorkstationService workStationService = (IWorkstationService) SpringUtil.getBean(IWorkstationService.class);
+
@Autowired
private GlobalWcsOfRpsMapper globalWcsOfRpsMapper;
@Autowired
private WorkstationService workstationService;
- //private static final EmployeeOnOffWorkMapper employeeOnOffWorkMapper = (EmployeeOnOffWorkMapper) SpringUtil.getBean(EmployeeOnOffWorkMapper.class);
-
+ @Autowired
+ private EmployeeOnOffWorkMapper employeeOnOffWorkMapper;
-
- public Map<Long, WorkstationDTO> getWorkStations() {
- String redisKey = "posting:workstation".concat("::").concat(WORKSTATION_ALL);
+
+ public Map<String, WorkstationDTO> getWorkstations() {
+ String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ALL);
/*Map<String, WorkstationDTO> map = bladeRedis.hGetAll(redisKey);
@@ -63,38 +57,47 @@
}
return map;
*/
- Map<Long, WorkstationDTO> map = convertMap(redisUtil.hmget(redisKey));
+ Map<String, WorkstationDTO> map = convertMap(redisUtil.hmget(redisKey));
if (ObjectUtil.isEmpty(map)) {
- map = setWorkStations();
+ map = setWorkstations();
}
return map;
}
- private Map<Long, WorkstationDTO> setWorkStations() {
+ private Map<String, WorkstationDTO> setWorkstations() {
List<Workstation> list = workstationService.list();
- String redisKey = "posting:workstation".concat("::").concat(WORKSTATION_ALL);
-
+ String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ALL);
+ /*
list.forEach(ws -> {
- /*
- WorkStationDTO workStationDTO = WorkstationConvert.INSTANCE.convertDTO(workStation);
- bladeRedis.hSet(redisKey, workStation.getId(), workStationDTO);
- */
+
+ //WorkStationDTO workStationDTO = WorkstationConvert.INSTANCE.convertDTO(workStation);
+ //bladeRedis.hSet(redisKey, workStation.getId(), workStationDTO);
+
WorkstationDTO dto = new WorkstationDTO();
dto.setCalendarCode(ws.getCalendarCode());
dto.setCode(ws.getCode());
dto.setId(ws.getId());
dto.setName(ws.getName());
redisUtil.hset(redisKey, ws.getId(), dto);
- });
-
- //bladeRedis.expire(redisKey, 259200L);
- redisUtil.expire(redisKey, 259200L);
- //return bladeRedis.hGetAll(redisKey);
+ });*/
+ //Map<String, String> map = str.collect(Collectors.toMap(p -> p[0], p -> p[1]));
+ Map<String,WorkstationDTO> mp = list.stream().collect(Collectors.toMap(ws -> String.valueOf(ws.getId()), ws->{
+ WorkstationDTO dto = new WorkstationDTO();
+ dto.setCalendarCode(ws.getCalendarCode());
+ dto.setCode(ws.getCode());
+ dto.setId(ws.getId());
+ dto.setName(ws.getName());
+ return dto;
+ }));
+ redisUtil.hmset(redisKey, mp);
- return convertMap(redisUtil.hmget(redisKey));
+ redisUtil.expire(redisKey, 259200L);
+
+
+ return (Map<String, WorkstationDTO>)redisUtil.hmget(redisKey);
}
static <K,V> Map<K,V> convertMap(Map<?,?> map){
@@ -104,12 +107,7 @@
}
return result;
}
- /*
- public static Boolean clearWorkStationCache() {
- String redisKey = "posting:workstation".concat("::").concat(WORKSTATION_ALL);
- return bladeRedis.del(redisKey);
- }
- */
+
/**
* 鑾峰彇鎸囧畾鏃ユ湡鐨勬棩鍘嗕唬鐮�
* @param workstationId
@@ -117,7 +115,7 @@
* @return
*/
public String getWorkstationCalendarCodeForDate(Long workstationId, String date) {
- String redisKey = "posting:workstation".concat("::").concat(WORKSTATION_ID).concat(workstationId.toString().concat(CALENDAR_DATE)).concat(date);
+ String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ID).concat(workstationId.toString().concat(CALENDAR_DATE)).concat(date);
//String calendarCode = (String) bladeRedis.get(redisKey);
String calendarCode = (String) redisUtil.get(redisKey);
if (ObjectUtil.isEmpty(calendarCode)) {
@@ -132,15 +130,15 @@
return calendarCode;
}
- public GlobalWcsOfRps getWorkstationWcsSetting(Long workstationId, String code) {
- String redisKey = "posting:workstation".concat("::").concat(WORKSTATION_ID).concat(workstationId.toString()
+ public GlobalWcsOfRps getWorkstationWcsSetting(Long workstationId, String deviceStatusCode) {
+ String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ID).concat(workstationId.toString()
.concat(WCS_SETTING));
//GlobalWcsOfRps wcsSetting = (GlobalWcsOfRps)redisUtil.hGet(redisKey, code);
- GlobalWcsOfRps wcsSetting = (GlobalWcsOfRps)redisUtil.hget(redisKey, code);
+ GlobalWcsOfRps wcsSetting = (GlobalWcsOfRps)redisUtil.hget(redisKey, deviceStatusCode);
if (wcsSetting == null) {
wcsSetting = globalWcsOfRpsMapper.selectOne(Wrappers.<GlobalWcsOfRps>lambdaQuery()
- .eq(GlobalWcsOfRps::getCode, code)
+ .eq(GlobalWcsOfRps::getCode, deviceStatusCode)
.isNull(GlobalWcsOfRps::getPrecondition));
if(wcsSetting == null) {
wcsSetting = new GlobalWcsOfRps();
@@ -150,33 +148,46 @@
//wcsSetting = Func.isNotEmpty(wcsSetting) ? wcsSetting : GlobalWcsOfRps.builder().rps(0).isPlan(0).build();
//bladeRedis.hSet(redisKey, code, wcsSetting);
//bladeRedis.expire(redisKey, Duration.ofDays(1L));
- redisUtil.hset(redisKey, code, wcsSetting, Duration.ofDays(1L).getSeconds());
+ redisUtil.hset(redisKey, deviceStatusCode, wcsSetting, Duration.ofDays(1L).getSeconds());
}
return wcsSetting;
}
- /*
- public static Long getBelongToEmployeeForWorkstation(Long workstationId, Date timePoint) {
- Long employeeId = null;
- String redisKey = "posting:workstation".concat("::").concat("workstation:id:").concat(workstationId.toString()
- .concat(EMPLOYEE));
- Set<EmployeeOnOffWork> employeeOnOffWorks = bladeRedis.sMembers(redisKey);
- EmployeeOnOffWork matchRecord = null;
- if (Func.isNotEmpty(employeeOnOffWorks))
- matchRecord = employeeOnOffWorks.stream().filter(item -> (item.getOnlineTime().getTime() <= timePoint.getTime() && (item.getOfflineTime() == null || item.getOfflineTime().getTime() > timePoint.getTime()))).findFirst().orElse(null);
- if (Func.isNotEmpty(matchRecord)) {
- employeeId = matchRecord.getOnEmployeeId();
- } else {
- List<EmployeeOnOffWork> queryResult = employeeOnOffWorkMapper.selectList(Wrappers.<EmployeeOnOffWork>lambdaQuery()
- .eq(EmployeeOnOffWork::getWorkstationId, workstationId)
- .le(EmployeeOnOffWork::getOnlineTime, timePoint)
- .and(wrapper -> wrapper.gt(EmployeeOnOffWork::getOfflineTime, timePoint).or().isNull(EmployeeOnOffWork::getOfflineTime)));
- if (Func.isNotEmpty(queryResult)) {
- employeeId = ((EmployeeOnOffWork)queryResult.get(0)).getOnEmployeeId();
- bladeRedis.sAdd(redisKey, new Object[] { queryResult.get(0) });
- bladeRedis.expire(redisKey, 259200L);
- }
- }
- return employeeId;
- }*/
+ /**
+ * 鑾峰彇宸ヤ綅鍦ㄦ寚瀹氭椂闂寸殑涓婄彮鍛樺伐
+ * @param workstationId
+ * @param timePoint
+ * @return
+ */
+ public Long getBelongToEmployeeForWorkstation(Long workstationId, Date timePoint) {
+ Long employeeId = null;
+ String redisKey = COLLECT_WORKSTATION.concat("::").concat("workstation:id:")
+ .concat(workstationId.toString().concat(EMPLOYEE));
+ // Set<EmployeeOnOffWork> employeeOnOffWorks = bladeRedis.sMembers(redisKey);
+
+ Set<EmployeeOnOffWork> employeeOnOffWorks = (Set<EmployeeOnOffWork>) redisUtil.sGet(redisKey);
+ EmployeeOnOffWork matchEmployee = null;
+ if (ObjectUtil.isNotEmpty(employeeOnOffWorks)) {
+ matchEmployee = employeeOnOffWorks.stream().filter(
+ item -> (item.getOnlineTime().getTime() <= timePoint.getTime() && (item.getOfflineTime() == null
+ || item.getOfflineTime().getTime() > timePoint.getTime())))
+ .findFirst().orElse(null);
+ }
+ if (ObjectUtil.isNotEmpty(matchEmployee)) {
+ employeeId = matchEmployee.getOnEmployeeId();
+ } else {
+ List<EmployeeOnOffWork> queryResult = employeeOnOffWorkMapper.selectList(
+ Wrappers.<EmployeeOnOffWork>lambdaQuery().eq(EmployeeOnOffWork::getWorkstationId, workstationId)
+ .le(EmployeeOnOffWork::getOnlineTime, timePoint)
+ .and(wrapper -> wrapper.gt(EmployeeOnOffWork::getOfflineTime, timePoint).or()
+ .isNull(EmployeeOnOffWork::getOfflineTime)));
+ if (ObjectUtil.isNotEmpty(queryResult)) {
+ employeeId = ((EmployeeOnOffWork) queryResult.get(0)).getOnEmployeeId();
+ //bladeRedis.sAdd(redisKey, new Object[] { queryResult.get(0) });
+ //bladeRedis.expire(redisKey, 259200L);
+ redisUtil.sSetAndTime(redisKey, 259200L, new Object[] { queryResult.get(0) });
+ }
+ }
+ return employeeId;
+ }
}
--
Gitblit v1.9.3