yangys
2024-11-13 04d53749b21921c9bceebe120d170c2ee6e533af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.qianwen.mdc.collect.cache;
 
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
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;
 
@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";
    private static final String WCS_SETTING = ":wcsSetting";
    private static final String EMPLOYEE = ":employee";
    @Autowired
    private RedisUtil redisUtil;
    private static final Logger log = LoggerFactory.getLogger(WorkstationCache.class);
    
    @Autowired
    private GlobalWcsOfRpsMapper globalWcsOfRpsMapper;
    @Autowired
    private WorkstationService workstationService;
    @Autowired
    private EmployeeOnOffWorkMapper employeeOnOffWorkMapper;
    
    
    public Map<String, WorkstationDTO> getWorkstations() {
        String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ALL);
        /*Map<String, WorkstationDTO> map = bladeRedis.hGetAll(redisKey);
       
        
        if (Func.isEmpty(map)) {
            map = setWorkStations();
        }
        return map;
        */
        Map<String, WorkstationDTO> map = convertMap(redisUtil.hmget(redisKey));
       
        if (ObjectUtil.isEmpty(map)) {
            map = setWorkstations();
        }
 
        return map;
        
    }
    
    private Map<String, WorkstationDTO> setWorkstations() {
        List<Workstation> list = workstationService.list();
        String redisKey = COLLECT_WORKSTATION.concat("::").concat(WORKSTATION_ALL);
        /*
        list.forEach(ws -> {
            
            //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);
        });*/
        //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);
        
        redisUtil.expire(redisKey, 259200L);
        
        
        return (Map<String, WorkstationDTO>)redisUtil.hmget(redisKey);
    }
    
    static <K,V> Map<K,V> convertMap(Map<?,?> map){
        Map<K,V> result = new HashMap<>();
        for(Map.Entry<?,?> entry : map.entrySet()) {
            result.put((K)entry.getKey(), (V)entry.getValue());
        }
        return result;
    }
  
    /**
     * 获取指定日期的日历代码
     * @param workstationId
     * @param date
     * @return
     */
    public String getWorkstationCalendarCodeForDate(Long workstationId, String 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)) {
            log.warn("获取工位{}指定日期{}日历缓存不存在,从数据库内获取并生成缓存", workstationId, date);
            Optional.ofNullable(workstationService.getById(workstationId)).ifPresent(workStation -> {
                //bladeRedis.setEx(redisKey, workStation.getCalendarCode(), 259200L);
                redisUtil.set(redisKey, workStation.getCalendarCode(), 259200L);
            });
        }
        
        //return (String) bladeRedis.get(redisKey);
        return calendarCode;
    }
    
    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, deviceStatusCode);
        if (wcsSetting == null) {
          wcsSetting = globalWcsOfRpsMapper.selectOne(Wrappers.<GlobalWcsOfRps>lambdaQuery()
              .eq(GlobalWcsOfRps::getCode, deviceStatusCode)
              .isNull(GlobalWcsOfRps::getPrecondition));
          if(wcsSetting == null) {
              wcsSetting = new GlobalWcsOfRps();
              wcsSetting.setRps(0);
              wcsSetting.setIsPlan(GlobalWcsOfRps.IN_PLAN);
          }
          //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, deviceStatusCode, wcsSetting, Duration.ofDays(1L).getSeconds());
        } 
        return wcsSetting;
      }
 
    /**
     * 获取工位在指定时间的上班员工
     * @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;
    }
}