skyouc
2 天以前 c229a84e0a8eb68c974e6d23fa1296e064025d76
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.vincent.rsf.server.manager.schedules;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.service.WcsService;
import com.vincent.rsf.server.api.utils.LocUtils;
import com.vincent.rsf.server.common.constant.Constants;
import com.vincent.rsf.server.manager.controller.params.LocToTaskParams;
import com.vincent.rsf.server.manager.enums.*;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.manager.service.impl.LocServiceImpl;
import com.vincent.rsf.server.manager.service.impl.TransferOrderServiceImpl;
import com.vincent.rsf.server.manager.utils.LocManageUtil;
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 com.vincent.rsf.server.system.utils.SystemAuthUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Author Ryan
 * 任务工作档
 */
@Component
public class TaskSchedules {
 
    public static Logger logger = LoggerFactory.getLogger(TaskSchedules.class);
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskItemService taskItemService;
    @Autowired
    private TaskLogService taskLogService;
    @Autowired
    private TaskItemLogService taskItemLogService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private LocServiceImpl locService;
    @Value("${wcs-slave.doubleLocs}")
    private List<Integer> rows;
    @Autowired
    private LocItemService locItemService;
    @Autowired
    private OutStockService outStockService;
    @Autowired
    private OutStockItemService outStockItemService;
    @Autowired
    private AsnOrderItemService asnOrderItemService;
    @Autowired
    private AsnOrderService asnOrderService;
    @Autowired
    private TransferOrderService transferOrderService;
    @Autowired
    private TransferService transferService;
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 完成入库,更新库存
     * @time 2025/4/2 12:37
     */
    @Scheduled(cron = "0/3 * * * * ?")
    public void completeInStock() throws Exception {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskStatus, TaskStsType.COMPLETE_IN.id).select(Task::getId));
        if (tasks.isEmpty()) {
            return;
        }
        List<Long> longSet = tasks.stream().map(Task::getId).collect(Collectors.toList());
        List<Task> vaildTasks = taskService.list(new LambdaQueryWrapper<Task>().in(Task::getId, longSet));
        taskService.complateInTask(vaildTasks);
    }
 
    /**
     * @author Ryan
     * @date 2025/5/20
     * @description: 完成出库任务,更新库存
     * @version 1.0
     */
    @Scheduled(cron = "0/5 * * * * ?  ")
    public void complateOutStock() throws Exception {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getTaskStatus, TaskStsType.COMPLETE_OUT.id)
                .select(Task::getId));
        if (tasks.isEmpty()) {
            return;
        }
        List<Long> longSet = tasks.stream().map(Task::getId).collect(Collectors.toList());
        List<Task> vaildTasks = taskService.list(new LambdaQueryWrapper<Task>().in(Task::getId, longSet));
        taskService.completeTask(vaildTasks);
 
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>()
                .eq(TaskItem::getWkType, OrderWorkType.ORDER_WORK_TYPE_STOCK_TERANSFER.type)
                .in(TaskItem::getTaskId, longSet));
        if (!taskItems.isEmpty()) {
            genInStock(taskItems);
        }
    }
 
    /**
     * @author Ryan
     * @date 2025/7/29
     * @description: 生成入库单
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void genInStock(List<TaskItem> taskItems) {
        Set<Long> orderIds = taskItems.stream().map(TaskItem::getSourceId).collect(Collectors.toSet());
        List<WkOrder> wkOrders = outStockService.list(new LambdaQueryWrapper<WkOrder>().in(WkOrder::getId, orderIds));
        wkOrders.forEach(wkOrder -> {
            List<WkOrderItem> orderItems = outStockItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, wkOrder.getId()));
            if (!orderItems.isEmpty()) {
                WkOrder order = new WkOrder();
                BeanUtils.copyProperties(wkOrder, order);
                order.setWkType(OrderWorkType.ORDER_WORK_TYPE_OTHER_TERANSFER.type)
                        .setId(null)
                        .setType(OrderType.ORDER_IN.type)
                        .setExceStatus(AsnExceStatus.ASN_EXCE_STATUS_RECEIPT_DONE.val);
                if (!asnOrderService.save(order)) {
                    throw new CoolException("入库单生成失败!!");
                }
 
                orderItems.forEach(orderItem -> {
                   orderItem.setOrderId(order.getId()).setOrderCode(order.getCode()).setId(null);
                   if (!asnOrderItemService.save(orderItem)) {
                       throw new CoolException("入库单明细保存失败!!");
                   }
                });
            }
        });
 
        Set<Long> taskIds = taskItems.stream().map(TaskItem::getTaskId).collect(Collectors.toSet());
        List<Task> tasks = taskService.listByIds(taskIds);
        if (!tasks.isEmpty()) {
            tasks.forEach(task -> {
                TaskItem one = taskItemService.getOne(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()), false);
                if (Objects.isNull(one)) {
                    throw new CoolException("数据错误:任务明细丢失!!");
                }
                TransferOrder order = transferOrderService.getOne(new LambdaQueryWrapper<TransferOrder>().eq(TransferOrder::getOrderId, one.getOrderId()));
                if (Objects.isNull(order)) {
                    throw new CoolException("数据错误!!");
                }
                Transfer transfer = transferService.getById(order.getTransferId());
                Task task1 = new Task();
                BeanUtils.copyProperties(task, task1);
                String targetLoc = LocManageUtil.getTargetLoc(transfer.getTarAreaId());
                if (Objects.isNull(targetLoc)) {
                    throw new CoolException("未能找到有效库位");
                }
                task.setTaskType(TaskType.TASK_TYPE_IN.type)
                        .setTargLoc(targetLoc)
                        .setTargSite(null)
                        .setId(null)
                        .setTaskStatus(TaskStsType.GENERATE_IN.id);
                if (!taskService.save(task1)) {
                    throw new CoolException("任务生成失败!!");
                }
                List<TaskItem> items = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
                if (items.isEmpty()) {
                    throw new CoolException("数据错误!!");
                }
                items.forEach(taskItem -> {
                    TaskItem item = new TaskItem();
                    BeanUtils.copyProperties(taskItem, item);
                    item.setTaskId(task1.getId()).setId(null);
                    if (!taskItemService.save(item)) {
                        throw new CoolException("入库任务明细保存失败!!");
                    }
                });
            });
        }
    }
 
    /**
     * 任务自动下发
     *
     * @throws Exception
     */
    @Scheduled(cron = "0/5 * * * * ?  ")
    @Transactional(rollbackFor = Exception.class)
    public void taskToWCS() throws Exception {
        Long loginUserId = SystemAuthUtils.getLoginUserId();
        List<Integer> list = Arrays.asList(TaskType.TASK_TYPE_IN.type, TaskType.TASK_TYPE_OUT.type);
        List<Integer> integers = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getTaskStatus, integers)
                .orderByDesc(Task::getSort));
        for (Task task : tasks) {
            Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getBarcode, task.getBarcode()));
            if (Objects.isNull(loc)) {
                continue;
            }
            //判断是否深库位
            if (!LocUtils.isShallowLoc(loc.getCode())) {
                //获取深库位对应的浅库位
                String shallowLoc = LocUtils.getShallowLoc(loc.getCode());
                if (StringUtils.isBlank(shallowLoc)) {
                    continue;
                }
                Loc shalloc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, shallowLoc));
                if (Objects.isNull(shalloc) || !shalloc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) {
                    //如果浅库位不在库跳出循环
                    continue;
                }
                LocToTaskParams params = new LocToTaskParams();
                params.setOrgLoc(shallowLoc).setType(TaskType.TASK_TYPE_LOC_MOVE.type + "");
                //生成移库任务
                Task moveTask = locItemService.genMoveTask(params, loginUserId);
                moveTask.setSort(!Objects.isNull(task.getSort()) ? task.getSort() + 1 : Constants.TASK_SORT_DEFAULT_VALUE + 1);
                if (!taskService.updateById(moveTask)) {
                    throw new Exception("任务优先级更新失败!!");
                }
            }
            //TODO 调用下发任务接口
        }
    }
 
    /**
     * 每五秒校验深库位是否为空,如果浅库位有货,将浅库位移至深库位
     *///TODO 生成移库任务前,需要检查是否有任务前往当前托盘
//    @Scheduled(cron = "0/35 * * * * ?  ")
//    @Transactional(rollbackFor = Exception.class)
//    public void shallocToDeep() throws Exception {
//        List<Loc> locs = locService.list(new LambdaQueryWrapper<Loc>()
//                .eq(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_O.type)
//                .in(Loc::getRow, rows));
//        if (locs.isEmpty()) {
//            return;
//        }
//        for (Loc loc : locs) {
//            String shallowLoc = LocUtils.getShallowLoc(loc.getCode());
//            Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, shallowLoc));
//            if (Objects.isNull(one) || !one.getUseStatus().equals(LocStsType.LOC_STS_TYPE_F.type)) {
//                continue;
//            }
//            taskService.moveToDeep(SystemAuthUtils.getLoginUserId(), shallowLoc);
//        }
//    }
 
 
    /**
     * @param
     * @return
     * @author Ryan
     * @description 已完成任务加入历史档
     * @time 2025/4/3 12:54
     */
    @Scheduled(cron = "0 0/01 * * * ?  ")
    @Transactional(rollbackFor = Exception.class)
    public void taskLogUpdate() {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<Task>();
        Config config = configService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getFlag, GlobalConfigCode.ORDER_INOF_REPORT_PLAT));//是否上报平台
        if (!Objects.isNull(config)) {
            if (Boolean.parseBoolean(config.getVal())) {
                queryWrapper.in(Task::getTaskStatus, TaskStsType.REPORT_IN.id);
            } else {
                queryWrapper.in(Task::getTaskStatus, TaskStsType.UPDATED_IN.id, TaskStsType.UPDATED_OUT.id);
            }
        } else {
            queryWrapper.in(Task::getTaskStatus, TaskStsType.UPDATED_IN.id, TaskStsType.UPDATED_OUT.id);
        }
        List<Task> tasks = taskService.list(queryWrapper);
        if (tasks.isEmpty()) {
            return;
        }
 
        tasks.forEach(task -> {
            TaskLog taskLog = new TaskLog();
            BeanUtils.copyProperties(task, taskLog);
            taskLog.setTaskId(task.getId())
                    .setId(null);
            if (!taskLogService.save(taskLog)) {
                throw new CoolException("任务历史档保存失败!!");
            }
            List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
            List<TaskItemLog> itemLogs = new ArrayList<>();
            for (TaskItem item : taskItems) {
                TaskItemLog itemLog = new TaskItemLog();
                BeanUtils.copyProperties(item, itemLog);
                itemLog.setId(null)
                        .setTaskId(task.getId())
                        .setLogId(taskLog.getId())
                        .setTaskItemId(item.getId());
                itemLogs.add(itemLog);
            }
 
            if (!taskService.removeById(task.getId())) {
                throw new CoolException("原始任务删除失败!!");
            }
 
            if (!taskItems.isEmpty()) {
                if (!taskItemLogService.saveBatch(itemLogs)) {
                    throw new CoolException("任务明细历史档保存失败!!");
                }
 
                if (!taskItemService.remove(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()))) {
                    throw new CoolException("原始任务明细删除失败!!");
                }
            }
        });
    }
}