1
zhang
2025-07-03 13ea8b334572c2423abb8d156fd8428f8d074172
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package com.zy.acs.manager.core.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.enums.AgvBackpackType;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.common.utils.Utils;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.domain.TaskDto;
import com.zy.acs.manager.common.domain.param.HandlerPublishParam;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.domain.AgvBackpackDto;
import com.zy.acs.manager.core.domain.Lane;
import com.zy.acs.manager.core.domain.TaskPosDto;
import com.zy.acs.manager.core.service.astart.MapDataDispatcher;
import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.manager.utils.ActionSorter;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 2023/6/14
 */
@Slf4j
@Service("mainService")
public class MainLxtService extends BaseMainService {
 
    @Autowired
    private BusService busService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ActionService actionService;
    @Autowired
    private StaService staService;
    @Autowired
    private LocService locService;
    @Autowired
    private AgvService agvService;
    @Autowired
    private AgvDetailService agvDetailService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private ValidService validService;
    @Autowired
    private AllocateService allocateService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private MapService mapService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private CodeGapService codeGapService;
    @Autowired
    private AgvCmdService agvCmdService;
    @Autowired
    private FuncStaService funcStaService;
    @Autowired
    private MapDataDispatcher mapDataDispatcher;
    @Autowired
    private TravelService travelService;
    @Autowired
    private SegmentService segmentService;
//    @Autowired
//    private TrafficService trafficService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private LaneService laneService;
    @Autowired
    private ActionSorter actionSorter;
 
    @SuppressWarnings("all")
    @Transactional
    public Bus generateBusAndTask(OpenBusSubmitParam busSubmitParam, String memo) {
        String errorMsg = busService.checkoutValid(busSubmitParam);
        if (!Cools.isEmpty(errorMsg)) {
            throw new BusinessException(errorMsg);
        }
        String batch = busSubmitParam.getBatch();
        List<TaskDto> taskDtoList = busSubmitParam.getTaskList();
        if (Cools.isEmpty(taskDtoList)) {
            throw new BusinessException("taskList can't be empty!");
        }
 
        // 优先级排序
        taskDtoList.sort((o1, o2) -> o2.getPriority() - o1.getPriority());
 
        // 校验
        List<Task> taskList = validService.validTaskDtoList(taskDtoList);
 
        // 保存总线
        Date now = new Date();
        Bus bus = new Bus();
        bus.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
        bus.setBusNo(batch);
        bus.setStartTime(now);
        bus.setBusSts(BusStsType.RECEIVE.val());
        bus.setMemo(memo);
 
        if (!busService.save(bus)) {
            throw new BusinessException("Internal Server Error!");
        }
 
        // 保存任务
        for (Task task : taskList) {
            task.setBusId(bus.getId());
            task.setTaskSts(TaskStsType.INIT.val());
            if (!taskService.save(task)) {
                throw new BusinessException("seqNum:" + task.getSeqNum() + " failed to save!");
            }
 
            // 修改库位状态
            Loc oriLoc = null;
            Loc destLoc = null;
            Sta oriSta = null;
            Sta destSta = null;
            switch (TaskTypeType.get(task.getTaskTypeEl())) {
                case LOC_TO_LOC:
                    oriLoc = locService.getById(task.getOriLoc());
                    if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                        throw new BusinessException("oriLoc:" + task.getOriLoc$() + " is not in STOCK status");
                    }
                    oriLoc.setLocSts(LocStsType.PAKOUT.val());
                    oriLoc.setUpdateTime(now);
                    if (!locService.updateById(oriLoc)) {
                        throw new BusinessException("oriLoc:" + task.getOriLoc$() + " failed to update");
                    }
 
                    destLoc = locService.getById(task.getDestLoc());
                    if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                        throw new BusinessException("destLoc:" + task.getDestLoc$() + " is not in IDLE status");
                    }
                    destLoc.setLocSts(LocStsType.PAKIN.val());
                    destLoc.setUpdateTime(now);
                    if (!locService.updateById(destLoc)) {
                        throw new BusinessException("destLoc:" + task.getDestLoc$() + " failed to update");
                    }
                    break;
                case LOC_TO_STA:
                    oriLoc = locService.getById(task.getOriLoc());
                    if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                        throw new BusinessException("oriLoc:" + task.getOriLoc$() + " is not in STOCK status");
                    }
                    oriLoc.setLocSts(LocStsType.PAKOUT.val());
                    oriLoc.setUpdateTime(now);
                    if (!locService.updateById(oriLoc)) {
                        throw new BusinessException("oriLoc:" + task.getOriLoc$() + " failed to update");
                    }
 
                    destSta = staService.getById(task.getDestSta());
                    if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " is not in IDLE status");
                    }
                    destSta.setStaSts(StaStsType.READY_RELEASE.val());
                    destSta.setUpdateTime(now);
                    if (!staService.updateById(destSta)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to update");
                    }
                    break;
                case STA_TO_LOC:
                    oriSta = staService.getById(task.getOriSta());
                    if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " is not in STOCK status");
                    }
                    oriSta.setStaSts(StaStsType.READY_TAKE.val());
                    oriSta.setUpdateTime(now);
                    if (!staService.updateById(oriSta)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to update");
                    }
 
                    destLoc = locService.getById(task.getDestLoc());
                    if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                        throw new BusinessException("destLoc:" + task.getDestLoc$() + " is not in IDLE status");
                    }
                    destLoc.setLocSts(LocStsType.PAKIN.val());
                    destLoc.setUpdateTime(now);
                    if (!locService.updateById(destLoc)) {
                        throw new BusinessException("destLoc:" + task.getDestLoc$() + " failed to update");
                    }
                    break;
                case STA_TO_STA:
                    oriSta = staService.getById(task.getOriSta());
                    if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " is not in STOCK status");
                    }
                    oriSta.setStaSts(StaStsType.READY_TAKE.val());
                    oriSta.setUpdateTime(now);
                    if (!staService.updateById(oriSta)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to update");
                    }
 
                    destSta = staService.getById(task.getDestSta());
                    if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " is not in IDLE status");
                    }
                    destSta.setStaSts(StaStsType.READY_RELEASE.val());
                    destSta.setUpdateTime(now);
                    if (!staService.updateById(destSta)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to update");
                    }
                    break;
                default:
                    break;
            }
 
        }
        return bus;
    }
 
    /**
     * 任务分配给车辆 ( 车辆此时是空闲且静止的 )
     */
    @Transactional
    public synchronized void allocateTask(Bus bus) {
        try {
            Date now = new Date();
            List<Task> taskList = taskService.list(new LambdaQueryWrapper<Task>()
                    .eq(Task::getBusId, bus.getId())
                    .eq(Task::getTaskSts, TaskStsType.INIT.val())
                    .orderByDesc(Task::getPriority)
            );
 
            if (Cools.isEmpty(taskList)) {
                bus.setBusSts(BusStsType.PROGRESS.val());
                bus.setUpdateTime(now);
                if (!busService.updateById(bus)) {
                    log.error("Bus [{}] failed to Update !!!", bus.getUuid());
                }
                return;
            }
 
            List<Long> taskIds = taskList.stream().map(Task::getId).distinct().collect(Collectors.toList());
            for (Task task : taskList) {
                Agv agv = allocateService.execute(task);
                if (null == agv) {
//                    log.warn("Task[{}] has an issue, because it failed to check out agv which is idle...", task.getSeqNum());
                    continue;
                }
                task.setAgvId(agv.getId());
                task.setTaskSts(TaskStsType.WAITING.val());
                task.setIoTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
                }
            }
        } catch (Exception e) {
            log.error("mainService.infuseAgvForTask", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
 
    /**
     * 解析取放货集合任务,进行最优的排列组合顺序 ( 车辆此时是空闲且静止的 )
     * todo: {@link com.zy.acs.manager.core.HandlerController#controlAgv(String, HandlerPublishParam)}
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buildMajorTask(Long agvId, List<Task> taskList) {
        if (Cools.isEmpty(agvId, taskList)) {
            return;
        }
        try {
            // valid -----------------------------------------------
            Agv agv = agvService.getById(agvId);
            if (!agvService.judgeEnable(agv.getId(), false)) {
                return;
            }
            if (!Cools.isEmpty(taskService.selectInSts(agvId, TaskStsType.ASSIGN, TaskStsType.PROGRESS))) {
                throw new CoolException("AGV[" + agv.getUuid() + "]分配任务失败,已存在执行任务...");
            }
            if (!Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.WAITING.toString()))
                    || !Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.RUNNING.toString()))) {
                throw new CoolException("AGV[" + agv.getUuid() + "] failed to assign,because already has the segment in running...");
            }
 
            // execute ----------------------------------------------------
            Date now = new Date();
            // sort and sub
            taskList.sort(new Comparator<Task>() {
                @Override
                public int compare(Task o1, Task o2) {
                    return o2.getPriority() - o1.getPriority();
                }
            });
            Integer backpack = agvService.getBackpack(agv);
            if (taskList.size() > backpack) {
                taskList = taskList.subList(0, backpack);
            }
 
            AgvDetail agvDetail = agvDetailService.selectByAgvId(agvId);
            List<AgvBackpackDto> backpackDtoList = new ArrayList<>();
 
            /**
             * 同巷道归类
             * same lane for every single agvId
             *
             * key: y(val) + TaskPosDto.PosType.ORI_LOC / ORI_STA / DEST_LOC / DEST_STA
             * val: new TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType)
             */
            Map<String, List<TaskPosDto>> groups = new HashMap<>();
            final String sameGroupXy = configService.getVal("sameGroupXy", String.class);
 
            int backpackLev = 0;
            for (Task task : taskList) {
 
                backpackLev++;
                Code startCode = null;
                Code endCode = null;
                Loc oriLoc = null;
                Loc destLoc = null;
                Sta oriSta = null;
                Sta destSta = null;
                switch (Objects.requireNonNull(TaskTypeType.get(task.getTaskTypeEl()))) {
                    case LOC_TO_LOC:
                        oriLoc = locService.getById(task.getOriLoc());
                        destLoc = locService.getById(task.getDestLoc());
 
                        startCode = codeService.getCacheById(oriLoc.getCode());
                        endCode = codeService.getCacheById(destLoc.getCode());
 
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy);
                        break;
                    case LOC_TO_STA:
                        oriLoc = locService.getById(task.getOriLoc());
                        destSta = staService.getById(task.getDestSta());
 
                        startCode = codeService.getCacheById(oriLoc.getCode());
                        endCode = codeService.getCacheById(destSta.getCode());
 
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy);
                        break;
                    case STA_TO_LOC:
                        oriSta = staService.getById(task.getOriSta());
                        destLoc = locService.getById(task.getDestLoc());
 
                        startCode = codeService.getCacheById(oriSta.getCode());
                        endCode = codeService.getCacheById(destLoc.getCode());
 
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy);
                        break;
                    case STA_TO_STA:
                        oriSta = staService.getById(task.getOriSta());
                        destSta = staService.getById(task.getDestSta());
 
                        startCode = codeService.getCacheById(oriSta.getCode());
                        endCode = codeService.getCacheById(destSta.getCode());
 
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy);
                        break;
                    default:
                        throw new BusinessException(task.getSeqNum() + "任务类型错误");
 
                }
 
                if (backpackLev > backpack) {
                    throw new BusinessException("解析Task失败,AGV背篓已满......");
                }
 
                backpackDtoList.add(new AgvBackpackDto(backpackLev, task.getId()));
            }
 
            /**
             * 1.Map<String, List<TaskPosDto>> groups
             *
             * key: 1000 + ORIGIN
             * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...]
             *
             * key: 3000 + ORIGIN
             * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...]
             *
             * key: 2000 + ORIGIN
             * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...]
             *
             * key: 1000 + DESTINATION
             * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...]
             *
             * key: 2000 + DESTINATION
             * val: [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...]
             *
             * ......
             *
             * 2.ArrayList<List<TaskPosDto>> list
             * [
             *  [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...],
             *  [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...],
             *  [TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType), TaskPosDto(taskId, new Double[]{code.getX(), code.getY()}, posType),...],
             *  ......
             * ]
             */
 
            /**
             * 对所有巷道进行有序排序,先是针对List为一个单位,对他们进行FirstWeight排序,相当于表1的key的数值进行有序排序
             *  List<TaskPosDto>: task list on the same lane
             *  ArrayList<List<TaskPosDto>>: all the task list by one agv
             *
             *  tip: ORI 和 DEST 永远不会存在同一个 List
             */
            ArrayList<List<TaskPosDto>> list = new ArrayList<>(groups.values());
            list.sort((o1, o2) -> {
                double o1CompVal = (o1.get(0).getFirstWeight(sameGroupXy) * 100) + o1.get(0).getPosType().compOffset;
                double o2CompVal = (o2.get(0).getFirstWeight(sameGroupXy) * 100) + o2.get(0).getPosType().compOffset;
                return (int) (o1CompVal - o2CompVal);
            });
            // 针对Dto,按照SecondWeight进行排序
            for (List<TaskPosDto> taskPosDtoList : list) {
                taskPosDtoList.sort((o1, o2) -> (int) (o1.getSecondWeight(sameGroupXy) * 100 - o2.getSecondWeight(sameGroupXy) * 100));
            }
 
            // re-order by agv current position
            Code currCode = codeService.getCacheById(agvDetail.getRecentCode());
            Double[] currPosition = new Double[]{currCode.getX(), currCode.getY()};
 
            List<List<TaskPosDto>> pickGroups = new ArrayList<>();
            List<List<TaskPosDto>> dropGroups = new ArrayList<>();
 
            for (List<TaskPosDto> group : list) {
                // Assume 一个任务组中所有TaskPosDto的PosType.brief相同
                TaskPosDto.PosType posType = group.get(0).getPosType();
                if (posType == TaskPosDto.PosType.ORI_LOC || posType == TaskPosDto.PosType.ORI_STA) {
                    pickGroups.add(group);
                } else if (posType == TaskPosDto.PosType.DEST_LOC || posType == TaskPosDto.PosType.DEST_STA) {
                    dropGroups.add(group);
                } else {
                    // import tip: the list must only contain ORIGIN and DESTINATION
                    log.error("the list must only contain ORIGIN and DESTINATION");
                }
            }
 
            currPosition = allocateService.pac(currPosition, pickGroups);
            currPosition = allocateService.pac(currPosition, dropGroups);
 
            List<List<TaskPosDto>> reorderedList = new ArrayList<>();
            reorderedList.addAll(pickGroups);
            reorderedList.addAll(dropGroups);
 
            list.clear();
            list.addAll(reorderedList);
 
 
            // generate travel
            Travel travel = new Travel();
            travel.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setTravelId(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setAgvId(agvId);
            travel.setTaskContent(JSON.toJSONString(list));
            travel.setTaskIds(JSON.toJSONString(taskList.stream().map(Task::getId).collect(Collectors.toList())));
            travel.setState(TravelStateType.RUNNING.toString());
            if (!travelService.save(travel)) {
                throw new BusinessException("任务组保存失败");
            }
 
            // generate segment
            int segSerial = 0;
            List<Segment> segmentList = new ArrayList<>();
            for (List<TaskPosDto> dtoList : list) {
                for (TaskPosDto taskPosDto : dtoList) {
                    segSerial++;
                    AgvBackpackType backpackType = AgvBackpackDto.find(backpackDtoList, taskPosDto.getTaskId());
                    assert null != backpackType;
 
                    Segment segment = new Segment();
                    segment.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
                    segment.setTravelId(travel.getId());
                    segment.setAgvId(agvId);
                    segment.setTaskId(taskPosDto.getTaskId());
                    segment.setSerial(segSerial);
                    segment.setEndNode(taskPosDto.getCodeId());
                    segment.setPosType(taskPosDto.getPosType().toString());
                    segment.setBackpack(backpackType.lev);
                    segment.setState(SegmentStateType.INIT.toString());
                    segmentList.add(segment);
                }
            }
            for (int i = 0; i < segmentList.size(); i++) {
                Segment segment = segmentList.get(i);
                if (i == 0) {
                    segment.setState(SegmentStateType.WAITING.toString());
                }
                if (!segmentService.save(segment)) {
                    throw new BusinessException("任务组保存失败");
                }
            }
 
            // task
            for (Task task : taskList) {
                task.setTaskSts(TaskStsType.ASSIGN.val());
                task.setStartTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException(task.getUuid() + "任务更新失败");
                }
            }
 
        } catch (Exception e) {
 
            log.error("mainService.buildMajorTask[task]", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
 
 
    /**
     * 充电 回待机位任务
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    // although there is a Transactional here that the lock is isolated, but we can't join the caller's Transactional
    public boolean buildMinorTask(Long agvId, TaskTypeType taskType, String destination, Jam jam) {
        if (Cools.isEmpty(agvId, taskType)) {
            return false;
        }
        try {
            String agvNo = agvService.getAgvNo(agvId);
            if (!agvService.judgeEnable(agvId)) {
                return false;
            }
            if (!Cools.isEmpty(taskService.selectInSts(agvId, TaskStsType.ASSIGN, TaskStsType.PROGRESS))) {
                throw new CoolException("AGV[" + agvNo + "] failed to assign,because already has the task in running...");
            }
            if (!Cools.isEmpty(segmentService.getByAgvAndState(agvId, SegmentStateType.WAITING.toString()))
                    || !Cools.isEmpty(segmentService.getByAgvAndState(agvId, SegmentStateType.RUNNING.toString()))) {
                throw new CoolException("AGV[" + agvNo + "] failed to assign,because already has the segment in running...");
            }
 
            Date now = new Date();
            AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId);
            Code endCode = null;
            switch (taskType) {
                case TO_CHARGE:
                case TO_STANDBY:
                    List<FuncSta> idleFunStaList = funcStaService.findInIdleStatus(FuncStaType.query(taskType), agvId);
                    if (!Cools.isEmpty(idleFunStaList)) {
                        FuncSta funcSta = funcStaService.checkoutClosestFunSta(agvDetail.getRecentCode(), idleFunStaList);
                        endCode = codeService.getCacheById(funcSta.getCode());
                    }
                    if (null == endCode) {
                        log.warn("AGV[{}] failed to search destination,there hadn't any idle funSta,TaskTypeType:{}", agvNo, taskType.toString());
                    }
                    break;
                case MOVE:
                    endCode = codeService.getCacheByData(destination);
                    break;
                default:
                    return false;
            }
 
            if (null == endCode) {
                return false;
            }
            if (!allocateService.validCapacityOfLane(agvNo, endCode)) {
                throw new BusinessException("the lane with code:" + endCode.getData() + " is full of AGV[" + agvNo + "]!!!");
            }
 
            Task task = new Task();
            task.setAgvId(agvId);
            task.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            List<Task> lastTasks = taskService.list(new LambdaQueryWrapper<Task>().orderByDesc(Task::getId));
            task.setSeqNum(Utils.generateSeqNum(Cools.isEmpty(lastTasks) ? null : lastTasks.get(0).getSeqNum()));
            task.setOriCode(agvDetail.getCode());
            task.setDestCode(endCode.getId());
            // lane
            Lane destLane = laneService.search(endCode.getData());
            if (null != destLane) {
                task.setDestLaneHash(destLane.getHashCode());
            }
            task.setPriority(taskType.equals(TaskTypeType.TO_CHARGE) ? 2 : 1);
            task.setTaskSts(TaskStsType.ASSIGN.val());
            task.setTaskType(taskType.val());
            task.setIoTime(now);
            task.setStartTime(now);
            if (!taskService.save(task)) {
                throw new BusinessException(task.getSeqNum() + " failed to save");
            }
 
            // generate travel
            Travel travel = new Travel();
            travel.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setTravelId(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setAgvId(agvId);
//            travel.setTaskContent(JSON.toJSONString(list));
            travel.setTaskIds(GsonUtils.toJson(Utils.singletonList(task.getId())));
            travel.setState(TravelStateType.RUNNING.toString());
            if (!travelService.save(travel)) {
                throw new BusinessException("travel failed to save");
            }
 
            // generate segment
            int segSerial = 0;
            segSerial++;
            List<Segment> segmentList = new ArrayList<>();
 
            String posType = "";
            switch (taskType) {
                case TO_CHARGE:
                    posType = TaskPosDto.PosType.TO_CHARGE.toString();
                    break;
                case TO_STANDBY:
                    posType = TaskPosDto.PosType.TO_STANDBY.toString();
                    break;
                case MOVE:
                    posType = TaskPosDto.PosType.MOVE.toString();
                    break;
                default:
                    break;
            }
 
            Segment segment = new Segment();
            segment.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            segment.setTravelId(travel.getId());
            segment.setAgvId(agvId);
            segment.setTaskId(task.getId());
            segment.setSerial(segSerial);
            segment.setEndNode(endCode.getId());
            segment.setPosType(posType);
            segment.setState(SegmentStateType.INIT.toString());
            segmentList.add(segment);
 
            for (int i = 0; i < segmentList.size(); i++) {
                Segment next = segmentList.get(i);
                if (i == 0) {
                    next.setState(SegmentStateType.WAITING.toString());
                }
                if (!segmentService.save(next)) {
                    throw new BusinessException("segment failed to save");
                } else {
                    if (null != jam && i == 0) {
                        jam.setAvoSeg(next.getId());
                        jam.setAvoCode(endCode.getId());
                    }
                }
            }
 
            switch (taskType) {
                case TO_CHARGE:
                case TO_STANDBY:
                    // funcSta
                    FuncSta destFuncSta = funcStaService.getByCodeAndType(task.getDestCode(), FuncStaType.query(taskType).toString());
                    destFuncSta.setState(FuncStaStateType.OCCUPIED.toString());
                    destFuncSta.setUpdateTime(now);
                    if (!funcStaService.updateById(destFuncSta)) {
                        log.error("FuncSta [{}] failed to update !!!", destFuncSta.getName());
                    }
                    break;
                case MOVE:
                    break;
                default:
                    break;
            }
 
            return true;
        } catch (Exception e) {
            log.error("mainService.buildMinorTask[task]", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 
            return false;
        }
    }
 
 
}