#
vincentlu
1 天以前 dcee04e577389842f31d6c8b114e046c837bb05e
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
package com.zy.acs.manager.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.acs.framework.common.BaseRes;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.domain.TaskDto;
import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam;
import com.zy.acs.manager.manager.entity.Bus;
import com.zy.acs.manager.manager.entity.Loc;
import com.zy.acs.manager.manager.entity.Task;
import com.zy.acs.manager.manager.enums.BusStsType;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.mapper.BusMapper;
import com.zy.acs.manager.manager.service.BusService;
import com.zy.acs.manager.manager.service.LocService;
import com.zy.acs.manager.manager.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
@Slf4j
@Service("busService")
public class BusServiceImpl extends ServiceImpl<BusMapper, Bus> implements BusService {
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private LocService locService;
 
    @Override
    public Bus selectByUuid(String uuid) {
        return this.getOne(new LambdaQueryWrapper<Bus>().eq(Bus::getUuid, uuid));
    }
 
    private void test(OpenBusSubmitParam param) {
        if (Cools.isEmpty(param)) {
            return;
        }
        List<TaskDto> taskList = param.getTaskList();
        if (Cools.isEmpty(taskList)) {
            return;
        }
        for (TaskDto dto : taskList) {
            if (dto.getPriority() == 9527) {
                if (Cools.isEmpty(dto.getDestLoc())) {
                    List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                            .ge(Loc::getRow, 31).eq(Loc::getLocSts, LocStsType.IDLE.val()));
                    if (Cools.isEmpty(locList)) {
                        break;
                    }
                    log.error("there is no such idle loc");
                    Collections.shuffle(locList);
                    Loc loc = locList.get(0);
                    dto.setDestLoc(loc.getLocNo());
                }
            }
        }
    }
 
    @Override
    public String checkoutValid(OpenBusSubmitParam param) {
        if (null == param) {
            return BaseRes.PARAM;
        }
        if (Cools.isEmpty(param.getBatch())) {
            return "Batch cannot be empty!";
        }
        this.test(param);
        Set<String> oriStaNoSet = new HashSet<>();
        Set<String> oriLocNoSet = new HashSet<>();
        Set<String> destStaNoSet = new HashSet<>();
        Set<String> destLocNoSet = new HashSet<>();
        for (TaskDto dto : param.getTaskList()) {
            if (!Cools.isEmpty(dto.getOriSta()) && !Cools.isEmpty(dto.getOriLoc())) {
                return "OriSta and OriLoc cannot exist at the same time!";
            }
            if (Cools.isEmpty(dto.getOriSta()) && Cools.isEmpty(dto.getOriLoc())) {
                return "OriSta and OriLoc must have one!";
            }
            if (!Cools.isEmpty(dto.getOriSta())) {
                if (Cools.isEmpty(dto.getDestSta()) && Cools.isEmpty(dto.getDestLoc())) {
                    return "Destination cannot be empty!";
                }
            }
            if (!Cools.isEmpty(dto.getOriLoc())) {
                if (Cools.isEmpty(dto.getDestSta()) && Cools.isEmpty(dto.getDestLoc())) {
                    return "Destination cannot be empty!";
                }
                if (oriLocNoSet.contains(dto.getOriLoc())) {
                    return "OriLoc cannot be repeated";
                } else {
                    oriLocNoSet.add(dto.getOriLoc());
                }
            }
 
            if (!Cools.isEmpty(dto.getDestSta()) && !Cools.isEmpty(dto.getDestLoc())) {
                return "DestSta and DestLoc cannot exist at the same time!";
            }
            if (Cools.isEmpty(dto.getDestSta()) && Cools.isEmpty(dto.getDestLoc())) {
                return "DestSta and DestLoc must have one!";
            }
            if (!Cools.isEmpty(dto.getDestSta())) {
                if (Cools.isEmpty(dto.getOriSta()) && Cools.isEmpty(dto.getOriLoc())) {
                    return "Origin cannot be empty!";
                }
            }
            if (!Cools.isEmpty(dto.getDestLoc())) {
                if (Cools.isEmpty(dto.getOriSta()) && Cools.isEmpty(dto.getOriLoc())) {
                    return "Origin cannot be empty!";
                }
                if (destLocNoSet.contains(dto.getDestLoc())) {
                    return "DestLoc cannot be repeated";
                } else {
                    destLocNoSet.add(dto.getDestLoc());
                }
            }
            if (Cools.isEmpty(dto.getSeqNum())) {
                dto.setSeqNum(taskService.generateSeqNum());
            }
 
        }
        return null;
    }
 
    @Override
    public List<Bus> selectBySts(BusStsType busStsType) {
        return this.selectBySts(busStsType, null);
    }
 
    @Override
    public List<Bus> selectInSts(BusStsType... busStsTypes) {
        List<Long> params = new ArrayList<>();
        for (BusStsType busStsType : busStsTypes) {
            params.add(busStsType.val());
        }
        return this.list(new LambdaQueryWrapper<Bus>().in(Bus::getBusSts, params));
    }
 
    @Override
    public List<Bus> selectBySts(BusStsType busStsType, String memo) {
        LambdaQueryWrapper<Bus> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bus::getBusSts, busStsType.val());
        if (!Cools.isEmpty(memo)) {
            wrapper.eq(Bus::getMemo, memo);
        }
        return this.list(wrapper);
    }
 
    @Override
    public void checkoutComplete(Long busId) {
        if (null == busId) {
            return;
        }
        int taskTotalCount = taskService.count(new LambdaQueryWrapper<Task>().eq(Task::getBusId, busId));
        List<Task> actualDoneTasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getBusId, busId)
                .in(Task::getTaskSts
                        , TaskStsType.COMPLETE.val()
                        , TaskStsType.CANCEL.val()
                )
        );
        if (taskTotalCount == actualDoneTasks.size()) {
            Bus bus = this.getById(busId);
            long cancelTasksCount = actualDoneTasks.stream().filter(task -> TaskStsType.CANCEL.val() == task.getTaskSts()).count();
            if (cancelTasksCount == taskTotalCount) {
                bus.setBusSts(BusStsType.CANCEL.val());
            } else {
                bus.setBusSts(BusStsType.FINISH.val());
            }
            bus.setUpdateTime(new Date());
            if (!this.updateById(bus)) {
                throw new CoolException("Bus[{" + busId + "}] failed to update");
            }
        }
    }
 
}