zhang
1 天以前 f73dad756121e3e12df88adeed0132342ddf115e
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.vincent.rsf.server.manager.schedules;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.constant.Constants;
import com.vincent.rsf.server.manager.constant.LocGroupConstant;
import com.vincent.rsf.server.manager.constant.StaGroupConstant;
import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.*;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.service.ConfigService;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
import static com.vincent.rsf.server.system.utils.SystemAuthUtils.getLoginUserId;
 
/**
 * 自动跑库程序
 */
@Slf4j
@Component
public class AutoRunSchedules {
 
    @Autowired
    private LocService locService;
 
    @Autowired
    private ConfigService configService;
 
    @Autowired
    private BasStationService stationService;
 
    @Autowired
    private TaskService taskService;
 
 
    @Autowired
    private TaskItemService taskItemService;
 
    @Autowired
    private MatnrService matnrService;
 
    @Autowired
    private LocItemService locItemService;
 
    public void genRun() {
        String autoRunArea = configService.getVal("AUTO_RUN_AREA", String.class);
        if (Cools.isEmpty(autoRunArea)) {
            return;
        }
        for (char c : autoRunArea.toCharArray()) {
            switch (c) {
                case '1':
                    this.autoRun(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST, StaGroupConstant.FAR_RIGHT_STA_ROW_LIST);
                    break;
                case '2':
                    this.autoRun(LocGroupConstant.RIGHT_LOC_ROW_LIST, StaGroupConstant.RIGHT_STA_ROW_LIST);
                    break;
                case '3':
                    this.autoRun(LocGroupConstant.MIDDLE_LOC_ROW_LIST, StaGroupConstant.MIDDLE_STA_ROW_LIST);
                    break;
                case '4':
                    this.autoRun(LocGroupConstant.LEFT_LOC_ROW_LIST, StaGroupConstant.LEFT_STA_ROW_LIST);
                    break;
                case '5':
                    this.autoRun(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST, StaGroupConstant.FAR_LEFT_STA_ROW_LIST);
                    break;
                default:
                    break;
            }
        }
 
    }
 
 
    private void autoRun(List<Integer> locGroupList, List<String> staGroupList) {
 
        List<String> staPreNos = getStaPrefixes(staGroupList);
        String staTaskMemo = "DEMO_STA_" + String.join("-", staPreNos);
 
        List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getMemo, staTaskMemo));
        if (!Cools.isEmpty(list) && list.size() > 30) {
            return;
        }
        // 入库 应该根据工作档回去
        this.runStaToLoc(locGroupList, staGroupList, staTaskMemo);
        // 出库,需要校验工作档是否存在,存在就说明站点处于忙碌状态
        this.runLocToSta(locGroupList, staGroupList, staTaskMemo);
 
        // 移库
        this.runLocToLoc(locGroupList, staTaskMemo);
    }
 
 
    // 入库
    private void runStaToLoc(List<Integer> locGroupList, List<String> staGroupList, String memo) {
        Integer startRow = Collections.min(locGroupList);
        Integer endRow = Collections.max(locGroupList);
 
 
        // STA IDLE
        LambdaQueryWrapper<BasStation> idleWrapper = new LambdaQueryWrapper<BasStation>().eq(BasStation::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).in(BasStation::getStationName, staGroupList);
        List<BasStation> idleList = stationService.list(idleWrapper);
        if (Cools.isEmpty(idleList)) {
            return;
        }
        Collections.shuffle(idleList);
 
        // LOC STOCK
        LambdaQueryWrapper<Loc> stockWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type);
        if (null != startRow) {
            stockWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            stockWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> stockList = locService.list(stockWrapper);
        if (Cools.isEmpty(stockList)) {
            return;
        }
        Collections.shuffle(stockList);
 
        //生成入库工作档
        generateTask(idleList.get(0).getStationName(), stockList.get(0).getCode(), idleList.get(0).getBarcode());
    }
 
 
    // 出库
    private void runLocToSta(List<Integer> locGroupList, List<String> staGroupList, String memo) {
        Integer startRow = Collections.min(locGroupList);
        Integer endRow = Collections.max(locGroupList);
 
 
        // STA IDLE
        LambdaQueryWrapper<BasStation> idleWrapper = new LambdaQueryWrapper<BasStation>().eq(BasStation::getUseStatus, LocStsType.LOC_STS_TYPE_O.type).in(BasStation::getStationName, staGroupList);
        List<BasStation> idleList = stationService.list(idleWrapper);
        if (Cools.isEmpty(idleList)) {
            return;
        }
        Collections.shuffle(idleList);
 
        // LOC STOCK
        LambdaQueryWrapper<Loc> stockWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type);
        if (null != startRow) {
            stockWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            stockWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> stockList = locService.list(stockWrapper);
        if (Cools.isEmpty(stockList)) {
            return;
        }
        Collections.shuffle(stockList);
 
 
        Loc loc = stockList.get(0);
        List<LocItem> list = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
        LocToTaskParams param = new LocToTaskParams();
        param.setType(Constants.TASK_TYPE_OUT_STOCK);
        param.setTarLoc(loc.getCode());
        param.setItems(list);
        param.setSiteNo(idleList.get(0).getStationName());
        param.setMemo(memo);
        //生成出库工作档
        try {
            locItemService.generateTask(TaskResouceType.TASK_RESOUCE_STOCK_TYPE.val, param, getLoginUserId());
        } catch (Exception e) {
            log.info("生成出库任务失败", e);
        }
 
    }
 
 
    // 移库
    private void runLocToLoc(List<Integer> locGroupList, String staTaskMemo) {
        Integer startRow = Collections.min(locGroupList);
        Integer endRow = Collections.max(locGroupList);
 
        String memo = "DEMO_LOC_" + startRow + "-" + endRow;
 
 
        // STOCK
        LambdaQueryWrapper<Loc> stockWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type);
        if (null != startRow) {
            stockWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            stockWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> stockList = locService.list(stockWrapper);
        if (Cools.isEmpty(stockList)) {
            return;
        }
        Collections.shuffle(stockList);
 
        // IDLE
        LambdaQueryWrapper<Loc> idleWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type);
        if (null != startRow) {
            idleWrapper.ge(Loc::getRow, startRow);
        }
        if (null != endRow) {
            idleWrapper.le(Loc::getRow, endRow);
        }
        List<Loc> idleList = locService.list(idleWrapper);
        if (Cools.isEmpty(idleList)) {
            return;
        }
        Collections.shuffle(idleList);
        //生成移库任务
        Loc loc = stockList.get(0);
        List<LocItem> list = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocId, loc.getId()));
        LocToTaskParams param = new LocToTaskParams();
        param.setType(Constants.TASK_TYPE_OUT_CHECK);
        param.setTarLoc(idleList.get(0).getCode());
        param.setItems(list);
        param.setOrgLoc(stockList.get(0).getCode());
        param.setMemo(memo);
        //生成移库工作档
        try {
            locItemService.genMoveTask(param, 6666L);
        } catch (Exception e) {
            log.info("生成出库任务失败", e);
        }
 
    }
 
    private void generateTask(String sourceStaNo, String locNo, String barcode) {
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
        if (StringUtils.isBlank(ruleCode)) {
            throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
        }
        Task task = new Task();
        task.setTaskCode(ruleCode)
                .setTaskStatus(TaskStsType.GENERATE_IN.id)
                .setTaskType(TaskType.TASK_TYPE_IN.type)
                .setResource(TaskResouceType.TASK_RESOUCE_PAKIN_TYPE.val)
                .setTargLoc(locNo)
                .setBarcode(barcode)
                .setOrgSite(sourceStaNo)
                .setCreateBy(6666L)
                .setUpdateBy(6666L);
 
        if (!taskService.save(task)) {
            throw new CoolException("任务保存失败!!");
        }
        LambdaQueryWrapper<Matnr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Matnr::getStatus, 1);
        wrapper.orderByAsc(Matnr::getUpdateTime);
        List<Matnr> list = matnrService.page(new Page<>(1, 100), wrapper).getRecords();
        Collections.shuffle(list);
        List<TaskItem> taskItems = new ArrayList<>();
        int i = 0;
        for (Matnr item : list) {
            if (i > 5) {
                break;
            }
            TaskItem taskItem = new TaskItem();
            BeanUtils.copyProperties(item, taskItem);
            taskItem.setTaskId(task.getId())
                    .setOrderType(OrderType.ORDER_IN.type)
                    .setCreateBy(6666L)
                    .setUpdateBy(6666L)
                    .setExtendFields(item.getExtendFields());
            taskItems.add(taskItem);
            item.setUpdateTime(new Date());
            matnrService.updateById(item);
            i++;
        }
        taskItemService.saveBatch(taskItems);
    }
 
 
    public static List<String> getStaPrefixes(List<String> staGroupList) {
        Set<String> rowSet = new HashSet<>();
        for (String s : staGroupList) {
            rowSet.add(s.split("-")[0]);
        }
        List<String> result = new ArrayList<>(rowSet);
        result.sort(Comparator.comparingInt(Integer::parseInt));
        return result;
    }
}