chen.lin
昨天 a488088a18a9b8808bc57124681cee8c4ada7299
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.vincent.rsf.server.manager.schedules;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
import com.vincent.rsf.server.manager.entity.BasContainer;
import com.vincent.rsf.server.manager.entity.Loc;
import com.vincent.rsf.server.manager.entity.WarehouseAreas;
import com.vincent.rsf.server.manager.enums.LocStsType;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.manager.service.impl.BasContainerServiceImpl;
import com.vincent.rsf.server.manager.service.impl.WarehouseAreasServiceImpl;
import com.vincent.rsf.server.system.constant.GlobalConfigCode;
import com.vincent.rsf.server.system.entity.Config;
import com.vincent.rsf.server.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author Munch D. Luffy
 * @date 2026/01/15
 * 缓存区域任务自动触发
 */
@Slf4j
@Component
public class TaskCacheLocSchedules {
 
    public static Logger logger = LoggerFactory.getLogger(TaskCacheLocSchedules.class);
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private WarehouseAreasServiceImpl warehouseAreasService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocItemService locItemService;
    @Autowired
    private BasContainerServiceImpl basContainerService;
 
    /**
     * @author Munch D. Luffy
     * @date 2026/01/15
     * @description: 缓存区域自动生成移库任务
     * @version 1.0
     */
    @Scheduled(cron = "0/15 * * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void startCacheInLocStock() throws Exception {
        Config config = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.AUTO_RUN_CHECK_IN_YZ));
        if (!Boolean.parseBoolean(config.getVal())) {
            return;
        }
 
        String autoRunAreaStartYz = configService.getVal("AUTO_RUN_IN_START_YZ", String.class);
        if (Cools.isEmpty(autoRunAreaStartYz)) {
            return;
        }
 
        String autoRunAreaEndYz = configService.getVal("AUTO_RUN_IN_END_YZ", String.class);
        if (Cools.isEmpty(autoRunAreaEndYz)) {
            return;
        }
        String curLoc = null;
        String deepLoc = null;
        try{
            String[] split = autoRunAreaStartYz.split(";");
            for (String c : split) {
                WarehouseAreas byId = warehouseAreasService.getById(Integer.parseInt(c));
                if (!Cools.isEmpty(byId)) {
                    Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getAreaId, byId.getId()).eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type)
                            .last("LIMIT 1"));
                    if (!Objects.isNull(loc)) {
                        curLoc = loc.getCode();
                        break;
                    }
                }
            }
        } catch (Exception e){
            log.error("配置参数AUTO_RUN_AREA_START_YZ解析失败,请检查!!!");
        }
        try{
            String[] split = autoRunAreaEndYz.split(";");
            for (String c : split) {
                WarehouseAreas byId = warehouseAreasService.getById(Integer.parseInt(c));
                if (!Cools.isEmpty(byId)) {
                    Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getAreaId, byId.getId()).eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                            .last("LIMIT 1"));
                    if (!Objects.isNull(loc)) {
                        deepLoc = loc.getCode();
                        break;
                    }
                }
            }
        } catch (Exception e){
            log.error("配置参数AUTO_RUN_AREA_START_YZ解析失败,请检查!!!");
        }
 
        if (curLoc == null ||  deepLoc == null) {
            return;
        }
        LocToTaskParams params = new LocToTaskParams();
        params.setOrgLoc(curLoc).setTarLoc(deepLoc);
        //生成移库位任务
        locItemService.genMoveTask(params, 9999L);
    }
 
    /**
     * @author Munch D. Luffy
     * @date 2026/01/15
     * @description: 缓存区域自动生成空货架出库任务
     * @version 1.0
     */
    @Scheduled(cron = "0/5 * * * * ?  ")
    @Transactional(rollbackFor = Exception.class)
    public void startCacheOutLocStock() throws Exception {
        Config config = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.AUTO_RUN_CHECK_OUT_YZ));
        if (!Boolean.parseBoolean(config.getVal())) {
            return;
        }
 
        String autoRunAreaStartYz = configService.getVal("AUTO_RUN_OUT_START_YZ", String.class);
        if (Cools.isEmpty(autoRunAreaStartYz)) {
            return;
        }
 
        String autoRunAreaEndYz = configService.getVal("AUTO_RUN_OUT_END_YZ", String.class);
        if (Cools.isEmpty(autoRunAreaEndYz)) {
            return;
        }
 
        String autoRunContainerEndYz = configService.getVal("AUTO_RUN_OUT_CONTAINER_YZ", String.class);
        if (Cools.isEmpty(autoRunContainerEndYz)) {
            return;
        }
 
        String autoRunContainerEndYzCount = configService.getVal("AUTO_RUN_CHECK_OUT_YZ_COUNT", String.class);
        if (Cools.isEmpty(autoRunContainerEndYzCount)) {
            return;
        }
        int autoOutCount = Integer.parseInt(autoRunContainerEndYzCount);
        if (autoOutCount <= 0) {
            return;
        }
        List<Long> souLocArea = new ArrayList<>();
        List<Long> endLocArea = new ArrayList<>();
        String curLoc = null;
        String deepLoc = null;
        try{
            for (String c :  autoRunAreaStartYz.split(";")) {
                WarehouseAreas byId = warehouseAreasService.getById(Integer.parseInt(c));
                if (!Cools.isEmpty(byId)) {
                    souLocArea.add(byId.getId());
                }
            }
            if (souLocArea.isEmpty()) {
                return;
            }
        } catch (Exception e){
            log.error("配置参数AUTO_RUN_AREA_START_YZ解析失败,请检查!!!");
            return;
        }
        try{
            for (String c : autoRunAreaEndYz.split(";")) {
                WarehouseAreas byId = warehouseAreasService.getById(Integer.parseInt(c));
                if (!Cools.isEmpty(byId)) {
                    endLocArea.add(byId.getId());
                }
            }
            if (endLocArea.isEmpty()) {
                return;
            }
        } catch (Exception e){
            log.error("配置参数AUTO_RUN_OUT_END_YZ解析失败,请检查!!!");
            return;
        }
 
        ConcurrentHashMap<String,Integer> CONTAINER_IDS = new ConcurrentHashMap<>();//需要补充的容器查询条件
//        ArrayList<String> CONTAINER_IDS = new ArrayList<>();//需要补充的容器查询条件
        try{
            String[] containerList = autoRunContainerEndYz.split(";");
            for (String c : containerList) {//容器集合
                long parseLong = Long.parseLong(c);
                BasContainer basContainer = basContainerService.getOne(new LambdaQueryWrapper<BasContainer>()
                        .eq(BasContainer::getContainerType, parseLong).last("LIMIT 1"));
                if (Cools.isEmpty(basContainer)){
                    throw new CoolException("未查询到相关容器规则,容器编码CONTAINER_ID:"+c);
                }
                //查询终点是否需要补充此容器
                String barcodeType = "barcode REGEXP '"+basContainer.getCodeType()+"'";
                int count = locService.count(new LambdaQueryWrapper<Loc>()
                        .apply(barcodeType)
                        .in(Loc::getUseStatus, new ArrayList<>(Arrays.asList(
                                LocStsType.LOC_STS_TYPE_D.type,
                                LocStsType.LOC_STS_TYPE_S.type
                        )))
                        .in(Loc::getAreaId, endLocArea)
                        .eq(Loc::getDeleted, 0)
                        .eq(Loc::getStatus, 1)
                        .orderByDesc(Loc::getId));
                if (!Cools.isEmpty(count) && count<autoOutCount){
                    CONTAINER_IDS.put(barcodeType, autoOutCount-count);
                }
            }
        } catch (Exception e){
            log.error("配置参数AUTO_RUN_AREA_START_YZ解析失败,请检查!!!");
            return;
        }
        List<LocToTaskParams> locMove = new  ArrayList<>();
        List<String> locList = new  ArrayList<>();
        for (ConcurrentHashMap.Entry<String,Integer> entry : CONTAINER_IDS.entrySet()) {
            String barcodeType = entry.getKey();// 获取String键
            Integer entryValue = entry.getValue();// 获取数量
            List<Loc> souLocList = locService.list(new LambdaQueryWrapper<Loc>()
                    .apply(barcodeType)
                    .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_D.type)
                    .in(Loc::getAreaId, souLocArea)
                    .eq(Loc::getDeleted, 0)
                    .eq(Loc::getStatus, 1)
                    .orderByDesc(Loc::getId));
            for (Loc souLoc : souLocList) {
                if (entryValue<=0){
                    break;
                }
                entryValue--;
 
                Loc endLoc = locService.getOne(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
                        .in(Loc::getAreaId, endLocArea)
                        .notIn(!locList.isEmpty(), Loc::getCode, locList)
                        .eq(Loc::getDeleted, 0)
                        .eq(Loc::getStatus, 1)
                        .orderByDesc(Loc::getId).last("LIMIT 1"));
                if (Cools.isEmpty(souLoc)){
                    continue;
                }
                locMove.add(new LocToTaskParams().setOrgLoc(souLoc.getCode()).setTarLoc(endLoc.getCode()));
                locList.add(endLoc.getCode());
            }
        }
 
        if (locMove.isEmpty()) {
            return;
        }
        for (LocToTaskParams locToTaskParams : locMove) {
            //生成移库位任务
            try{
                locItemService.genMoveTask(locToTaskParams, 9999L);
            }catch (Exception e){
                log.error("生成移库位任务失败,请检查!!!"+locToTaskParams.toString());
            }
        }
    }
}