#
luxiaotao1123
2024-04-11 7109f668b71c21810cb8257e4dd34584b3a1844e
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
package com.zy.asrs.wcs.core.map.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.common.domain.enums.DictType;
import com.zy.asrs.wcs.core.entity.Loc;
import com.zy.asrs.wcs.core.map.controller.param.MapDataParam;
import com.zy.asrs.wcs.core.map.controller.param.MapQueryParam;
import com.zy.asrs.wcs.core.map.controller.result.LiftVo;
import com.zy.asrs.wcs.core.map.controller.result.LocVo;
import com.zy.asrs.wcs.core.map.controller.result.ShuttleVo;
import com.zy.asrs.wcs.core.map.entity.MapItem;
import com.zy.asrs.wcs.core.model.enums.LocStsType;
import com.zy.asrs.wcs.core.service.LocService;
import com.zy.asrs.wcs.core.service.LocStsService;
import com.zy.asrs.wcs.core.utils.Utils;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.entity.DeviceType;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.LiftProtocol;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceService;
import com.zy.asrs.wcs.rcs.service.DeviceTypeService;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import com.zy.asrs.wcs.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 3/15/2024
 */
@Service
public class MapService {
 
    @Autowired
    private UserService userService;
    @Autowired
    private DictService dictService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private LocService locService;
    @Autowired
    private LocStsService locStsService;
 
    public String getMapFloorList(Long userId) {
        String floorKey = "floor-list";
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, floorKey));
        if (null == dict) {
            dict = new Dict();
            dict.setName(floorKey);
            dict.setFlag(floorKey);
            dict.setType(DictType.JSON.flag);
            dict.setValue(floorListStr);
            dict.setCreateBy(userId);
            dict.setUpdateBy(userId);
            if (!dictService.save(dict)) {
                throw new CoolException("服务器内部错误");
            }
        }
        return Optional.ofNullable(dict).map(Dict::getValue).orElse(null);
    }
 
    public String getMapData(MapQueryParam param, Long userId) {
        String mapKey = getMapKey(param.getFloor());
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, mapKey));
        return Optional.ofNullable(dict).map(Dict::getValue).orElse(null);
    }
 
    @Transactional
    public void saveMapData(MapDataParam param, Long userId) {
        Date now = new Date();
        // dict
        String mapKey = getMapKey(param.getFloor());
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, mapKey));
        if (Cools.isEmpty(dict)) {
           dict = new Dict();
           dict.setName(mapKey);
           dict.setFlag(mapKey);
           dict.setType(DictType.JSON.flag);
           dict.setValue(JSON.toJSONString(param.getItemList()));
           dict.setCreateBy(userId);
           dict.setUpdateBy(userId);
           if (!dictService.save(dict)) {
               throw new CoolException("服务器内部错误");
           }
        } else {
            dict.setValue(JSON.toJSONString(param.getItemList()));
            dict.setUpdateTime(new Date());
            dict.setUpdateBy(userId);
            if (!dictService.updateById(dict)) {
                throw new CoolException("服务器内部错误");
            }
        }
        // loc
        List<String> newLocList = new ArrayList<>();
        for (MapItem item : param.getItemList()) {
            int row = Integer.parseInt(String.valueOf(item.getProperty().get("row")));
            int bay = Integer.parseInt(String.valueOf(item.getProperty().get("bay")));
            String locNo = Utils.getLocNo(row, bay, param.getFloor());
            newLocList.add(locNo);
        }
 
        List<String> existLocList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLev, param.getFloor()))
                .stream().map(Loc::getLocNo).collect(Collectors.toList());
 
 
        List<String> locListToSave = newLocList.stream().filter(locNo -> !existLocList.contains(locNo)).collect(Collectors.toList());
 
        List<String> locListToRemove = existLocList.stream().filter(locNo -> !newLocList.contains(locNo)).collect(Collectors.toList());
 
        List<String> locListInBoth = newLocList.stream().filter(existLocList::contains).collect(Collectors.toList());
 
        for (String locNo : locListToSave) {
            Loc loc = new Loc();
            loc.setLocNo(locNo);
            loc.setName(locNo);
            loc.setRow(Utils.getRow(locNo));
            loc.setBay(Utils.getBay(locNo));
            loc.setLev(Utils.getLev(locNo));
            loc.setLocSts(LocStsType.O.val());
            loc.setCreateBy(userId);
            loc.setCreateTime(now);
            loc.setUpdateBy(userId);
            loc.setUpdateTime(now);
            if (!locService.save(loc)) {
                throw new CoolException("服务器内部错误");
            }
        }
 
        for (String locNo : locListToRemove) {
            if (!locService.remove(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, locNo))) {
                throw new CoolException("服务器内部错误");
            }
        }
 
        for (String locNo : locListInBoth) {
 
        }
    }
 
 
    public List<LocVo> getShelfLocNoList(String locNo) {
        List<LocVo> locVos = new ArrayList<>();
        int row = Utils.getRow(locNo);
        int bay = Utils.getBay(locNo);
        List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getRow, row).eq(Loc::getBay, bay).orderByAsc(Loc::getLev));
        for (Loc loc : locList) {
            locVos.add(new LocVo(loc.getLocNo(), loc.getLocStsFlag()));
        }
        return locVos;
    }
 
    public ShuttleVo getShuttleInfo(String shuttleNo) {
        ShuttleVo vo = new ShuttleVo();
        vo.setShuttleNo(shuttleNo);
        DeviceType deviceType = deviceTypeService.getOne(new LambdaQueryWrapper<DeviceType>().eq(DeviceType::getFlag, SlaveType.Shuttle.toString()));
        Device device = deviceService.getOne(new LambdaQueryWrapper<Device>().eq(Device::getDeviceType, deviceType.getId()).eq(Device::getDeviceNo, shuttleNo));
        if (null == device) {
            return vo;
        }
        ShuttleThread thread = (ShuttleThread)SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
        if (null == thread) {
            return vo;
        }
        ShuttleProtocol protocol = thread.getStatus();
        if (null == protocol) {
            return vo;
        }
        vo.setTaskNo(String.valueOf(protocol.getTaskNo()));
        vo.setStatus(protocol.getProtocolStatusType().desc);
        vo.setOriginLocNo(protocol.getSourceLocNo());
        vo.setDestLocNo(protocol.getLocNo());
        vo.setCurrentCode(protocol.getCurrentCode());
        vo.setBatteryPower(protocol.getBatteryPower());
        vo.setErrorCode(protocol.getErrorCode());
        vo.setBatteryTemp(protocol.getBatteryTemp());
        vo.setHasLift(protocol.getHasLift());
        vo.setHasPallet(protocol.getHasPallet());
        vo.setHasCharge(protocol.getHasCharge());
        vo.setBatteryVoltage(String.valueOf(protocol.getBatteryVoltage()));
        vo.setPakMk(protocol.getPakMk());
        return vo;
    }
 
    public LiftVo getLiftInfo(String liftNo) {
        LiftVo vo = new LiftVo();
        vo.setLiftNo(liftNo);
        LiftProtocol protocol = new LiftProtocol();
        // todo checkout protocol
//        vo.setTaskNo(String.valueOf(protocol.getTaskNo()));
//        vo.setStatus(protocol.getProtocolStatusType().desc);
//        vo.setOriginLocNo(protocol.getSourceLocNo());
//        vo.setDestLocNo(protocol.getLocNo());
//        vo.setCurrentCode(protocol.getCurrentCode());
//        vo.setBatteryPower(protocol.getBatteryPower());
//        vo.setErrorCode(protocol.getErrorCode());
//        vo.setBatteryTemp(protocol.getBatteryTemp());
//        vo.setHasLift(protocol.getHasLift());
//        vo.setHasPallet(protocol.getHasPallet());
//        vo.setHasCharge(protocol.getHasCharge());
//        vo.setBatteryVoltage(String.valueOf(protocol.getBatteryVoltage()));
//        vo.setPakMk(protocol.getPakMk());
        return vo;
    }
 
    private String getMapKey(Integer floor) {
        if (null != floor) {
            return "map-" + floor;
        }
        return "map";
    }
 
    private final String floorListStr = "[\n" +
            "    {\n" +
            "        label: '1F',\n" +
            "        value: 1\n" +
            "    },\n" +
            "]";
 
}