package com.qianwen.smartman.common.cache.cps; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.SetOperations; import org.springframework.util.SystemPropertyUtils; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.qianwen.core.redis.cache.BladeRedis; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.SpringUtil; import com.qianwen.smartman.common.constant.ExtCacheConstant; import com.qianwen.smartman.common.constant.ProductionTimeConstant; import com.qianwen.smartman.common.enums.GlobalWcsTypeEnum; import com.qianwen.smartman.common.enums.WcsDataTypeEnums; import com.qianwen.smartman.common.utils.CommonUtil; import com.qianwen.smartman.modules.cps.convert.WorkstationConvert; import com.qianwen.smartman.modules.cps.dto.WorkstationDTO; import com.qianwen.smartman.modules.cps.dto.WorkstationParamTypeDTO; import com.qianwen.smartman.modules.cps.dto.WorkstationWcsDTO; import com.qianwen.smartman.modules.cps.dto.WorkstationWcsDmpDTO; import com.qianwen.smartman.modules.cps.entity.GlobalWcs; import com.qianwen.smartman.modules.cps.entity.Workstation; import com.qianwen.smartman.modules.cps.message.dto.TelemetryDataResponseDTO; import com.qianwen.smartman.modules.cps.service.IGlobalWcsService; import com.qianwen.smartman.modules.cps.service.IMachineService; import com.qianwen.smartman.modules.cps.service.IWorkstationService; import com.qianwen.smartman.modules.cps.service.IWorkstationWcsService; import com.qianwen.smartman.modules.cps.vo.WorkstationInGroupVO; import com.qianwen.smartman.modules.mdc.dto.WorkstationDmpDTO; import com.qianwen.smartman.modules.mdc.entity.WorkstationCollectData; import com.qianwen.smartman.modules.mdc.mapper.SuperProcessParameterMapper; import com.qianwen.smartman.modules.mdc.service.IProcessParameterService; public class WorkstationCache { public static final String CRC = "crc:"; private static final String WORKSTATION = "workstation:"; private static final String WORKSTATION_ID = "workstation:id:"; public static final String WORKSTATION_REAL_TIME = "workstation:id:{}:real-time"; public static final String WORKSTATION_DMP = "workstation:id:{}:dmp-collect-item"; public static final String WORKSTATION_WCS = "workstation:id:{}:wcs-collect-item"; public static final String WORKSTATION_DEFAULT_STATUS_DMP = "workstation:id:{}:status-collect-item"; private static final String WORKSTATION_DMP_VAR = "workstation:id:{}:dmp-var"; private static final String SHIFT_WORKSTATION = "shift:workstation:"; private static final String MACHINE_ID = "machine:id:"; private static final String PARAM_NAME = "paramName"; private static final String AlARM_NAME = "alarm"; private static final String CALENDAR_DATE = ":calendar:date:"; private static final String WCSSETTING = ":wcsSetting"; private static final String WORKSTATION_ALL = "workstation:all"; private static final Logger log = LoggerFactory.getLogger(WorkstationCache.class); private static final BladeRedis BLADE_REDIS = (BladeRedis) SpringUtil.getBean(BladeRedis.class); private static final IWorkstationService WORKSTATION_SERVICE = (IWorkstationService) SpringUtil.getBean(IWorkstationService.class); private static final IMachineService MACHINE_SERVICE = (IMachineService) SpringUtil.getBean(IMachineService.class); private static final IGlobalWcsService WCS_SERVICE = (IGlobalWcsService) SpringUtil.getBean(IGlobalWcsService.class); private static final IWorkstationWcsService WORKSTATION_WCS_SERVICE = (IWorkstationWcsService) SpringUtil.getBean(IWorkstationWcsService.class); private static final SuperProcessParameterMapper superProcessParameterMapper = (SuperProcessParameterMapper) SpringUtil.getBean(SuperProcessParameterMapper.class); private static final IProcessParameterService processParameterService = SpringUtil.getBean(IProcessParameterService.class); public static List getWorkstationWcsList(String workstationId) { String redisKey = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_WCS, "", workstationId); SetOperations setOps = BLADE_REDIS.getSetOps(); Set members = setOps.members(redisKey); if (Func.isEmpty(members)) { List wcsList = WORKSTATION_SERVICE.queryWcsList(workstationId); if (Func.isEmpty(wcsList)) { return Lists.newArrayList(); } wcsList.forEach(w -> { setOps.add(redisKey, new Object[]{w}); }); BLADE_REDIS.expire(redisKey, 600L); return wcsList; } return members.stream().map(c -> { return (WorkstationWcsDmpDTO) Func.convert(c, WorkstationWcsDmpDTO.class); }).filter(c2 -> { return Func.notNull(c2) && Func.isNotBlank(c2.getName()); }).sorted(Comparator.comparing((v0) -> { return v0.getDescription(); })).collect(Collectors.toList()); } public static void clearWorkstationWcs(String workstationId) { String redisKey = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_WCS, "", workstationId); BLADE_REDIS.del(redisKey); } /** * 从缓存获取DMP状态,如果不存在则查询并写入缓存 * @param workstationId * @return */ public static WorkstationWcsDmpDTO getDmpStatus(String workstationId) { String redisKey = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_DEFAULT_STATUS_DMP, "", workstationId); Object value = BLADE_REDIS.get(redisKey); if (Func.isNull(value)) { WorkstationWcsDmpDTO workstationWcs = WORKSTATION_SERVICE.queryDateTypeState(WcsDataTypeEnums.WcsDataType.STATE.getCode(), workstationId); if (Func.isNull(workstationWcs)) { return null; } BLADE_REDIS.set(redisKey, workstationWcs); BLADE_REDIS.expire(redisKey, 600L); return workstationWcs; } return (WorkstationWcsDmpDTO) Func.convert(value, WorkstationWcsDmpDTO.class); } public static void clearDmpStatus(List workstationIds) { List keys = (List) workstationIds.stream().map(id -> { return CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_DEFAULT_STATUS_DMP, "", id); }).collect(Collectors.toList()); BLADE_REDIS.del(keys); } public static List getDefaultWcs() { return WCS_SERVICE.getGlobalWcsList(GlobalWcsTypeEnum.DEFAULT); } public static String getDefaultWcsColor(String code) { return (String) getDefaultWcs().stream().filter(wcs -> { return code.equals(wcs.getCode()); }).findFirst().map(wcs2 -> { return wcs2.getColor(); }).orElse(null); } public static Map getWorkstationRealTime(String workstationId) { String cacheName = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_REAL_TIME, "", workstationId); Map resultMapping = BLADE_REDIS.hGetAll(cacheName); if (Func.isEmpty(resultMapping)) { // //List workstationCollectData = superProcessParameterMapper.queryLastParameter(workstationId); Long wid = Long.parseLong(workstationId); List workstationCollectData = processParameterService.queryLastParameter(wid); if (Func.isNotEmpty(workstationCollectData)) { resultMapping = workstationCollectData.stream().collect(Collectors.toMap((v0) -> { return v0.getN(); }, item -> { TelemetryDataResponseDTO result = new TelemetryDataResponseDTO(); result.setT(item.getTs()); result.setV(item.getV()); return result; })); } } return resultMapping; } public static Map getWorkstationAllCollect(String workstationId) { String cacheName = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_REAL_TIME, "", workstationId); return BLADE_REDIS.hGetAll(cacheName); } public static TelemetryDataResponseDTO getWorkstationRealTime(Long workstationId, String paramName) { String cacheName = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_REAL_TIME, "", workstationId); return (TelemetryDataResponseDTO) BLADE_REDIS.hGet(cacheName, paramName); } public static void setWorkstationRealTime(Long workstationId, String paramName, TelemetryDataResponseDTO telemetryDataResponseDTO) { String cacheName = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_REAL_TIME, "", workstationId); BLADE_REDIS.hSet(cacheName, paramName, telemetryDataResponseDTO); BLADE_REDIS.expire(cacheName, ExtCacheConstant.LONG_CACHE_EXPIRE_TIME.longValue()); } public static void clearWorkstationRealTime(Long workstationId) { String cacheName = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, WORKSTATION_REAL_TIME, "", workstationId); BLADE_REDIS.del(cacheName); } public static List getWorkstationParamTypeCache(String machineId, String paramName) { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(MACHINE_ID).concat(machineId).concat(SystemPropertyUtils.VALUE_SEPARATOR).concat(PARAM_NAME); List list = (List) BLADE_REDIS.hGet(redisKey, paramName); if (list == null) { log.error("查询设备{} 参数:{}", machineId, paramName); list = setWorkstationParamTypeCache(machineId, paramName); } return list; } public static Boolean clearWorkstationParamTypeCache(String machineId) { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(MACHINE_ID).concat(machineId).concat(SystemPropertyUtils.VALUE_SEPARATOR).concat(PARAM_NAME); String postingKey = ExtCacheConstant.POSTING_MACHINE.concat("::").concat(MACHINE_ID).concat(machineId).concat(SystemPropertyUtils.VALUE_SEPARATOR).concat(PARAM_NAME); String postingAlarmKey = ExtCacheConstant.POSTING_MACHINE.concat("::").concat(MACHINE_ID).concat(machineId).concat(SystemPropertyUtils.VALUE_SEPARATOR).concat(AlARM_NAME); BLADE_REDIS.del(redisKey); BLADE_REDIS.del(postingKey); BLADE_REDIS.del(postingAlarmKey); return true; } private static List setWorkstationParamTypeCache(String machineId, String paramName) { List workstationWcsDTOList = MACHINE_SERVICE.getWorkstationWcsByMachineId(Long.valueOf(machineId), paramName); String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(MACHINE_ID).concat(machineId).concat(SystemPropertyUtils.VALUE_SEPARATOR).concat(PARAM_NAME); if (Func.isEmpty(workstationWcsDTOList)) { BLADE_REDIS.hSet(redisKey, paramName, new ArrayList()); } else { List collect = workstationWcsDTOList.stream().map(workstationWcsDTO -> { WorkstationParamTypeDTO workstationParamTypeDTO = WorkstationParamTypeDTO.builder().type(workstationWcsDTO.getDataType()).workstationId(workstationWcsDTO.getWorkstationId()).id(workstationWcsDTO.getId()).build(); return workstationParamTypeDTO; }).collect(Collectors.toList()); BLADE_REDIS.hSet(redisKey, paramName, collect); } BLADE_REDIS.expire(redisKey, 259200L); return (List) BLADE_REDIS.hGet(redisKey, paramName); } public static Map getWorkstations() { String userKey = AuthUtil.isAdministrator() ? "" : SystemPropertyUtils.VALUE_SEPARATOR + AuthUtil.getUserId(); String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(WORKSTATION_ALL).concat(userKey); Map map = BLADE_REDIS.hGetAll(redisKey); if (Func.isEmpty(map)) { map = setWorkstations(); } return map; } private static Map setWorkstations() { List list = WORKSTATION_SERVICE.list(); String userKey = AuthUtil.isAdministrator() ? "" : SystemPropertyUtils.VALUE_SEPARATOR + AuthUtil.getUserId(); String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(WORKSTATION_ALL).concat(userKey); if (Func.isNotEmpty(list)) { list.forEach(workstation -> { WorkstationDTO workstationDTO = WorkstationConvert.INSTANCE.convertDTO(workstation); BLADE_REDIS.hSet(redisKey, workstation.getId(), workstationDTO); }); BLADE_REDIS.expire(redisKey, 86400L); } return BLADE_REDIS.hGetAll(redisKey); } public static Boolean clearWorkstationCache() { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(WORKSTATION_ALL); String postingKey = ExtCacheConstant.POSTING_MACHINE.concat("::").concat(WORKSTATION_ALL); String shiftWorkStationKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(SHIFT_WORKSTATION); Set keys = BLADE_REDIS.keys(redisKey.concat("**")); if (Func.isNotEmpty(keys)) { BLADE_REDIS.del(keys); } BLADE_REDIS.del(postingKey); BLADE_REDIS.del(shiftWorkStationKey); return true; } public static String getWorkstationCalendarCodeForDate(Long workstationId, String date) { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(WORKSTATION_ID).concat(workstationId.toString().concat(CALENDAR_DATE)).concat(date); String calendarCode = (String) BLADE_REDIS.get(redisKey); if (Func.isEmpty(calendarCode)) { Optional.ofNullable(WORKSTATION_SERVICE.getById(workstationId)).ifPresent(workstation -> { BLADE_REDIS.setEx(redisKey, workstation.getCalendarCode(), ProductionTimeConstant.TIME_SLICE_EXPIRE_TIME); }); } return (String) BLADE_REDIS.get(redisKey); } public static List getShiftWorkStation(Long id) { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(SHIFT_WORKSTATION).concat(String.valueOf(id)); List workstationInGroupVOList = (List) BLADE_REDIS.get(redisKey); return workstationInGroupVOList; } public static void setShiftWorkStation(List list, Long id) { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(SHIFT_WORKSTATION).concat(String.valueOf(id)); BLADE_REDIS.setEx(redisKey, list, ExtCacheConstant.FIVE_MINUTES_EXPIRE_TIME); } public static Map getWorkstationDmpVar(String workstationId) { String redisKey = CommonUtil.getReallyCacheName(ExtCacheConstant.CPS_CACHE, CRC, WORKSTATION_DMP_VAR, workstationId); Map map = BLADE_REDIS.hGetAll(redisKey); if (Func.isEmpty(map)) { List dtos = WORKSTATION_SERVICE.getDmpVar(workstationId); if (Func.isEmpty(dtos)) { return Maps.newHashMap(); } Map dmpMap = dtos.stream().collect(Collectors.toMap(WorkstationDmpDTO::getDmpName, v -> v)); BLADE_REDIS.getRedisTemplate().opsForHash().putAll(redisKey, dmpMap); BLADE_REDIS.expire(redisKey, ExtCacheConstant.FIVE_MINUTES_EXPIRE_TIME.longValue()); return dmpMap; } return map; } }