#
vincentlu
1 天以前 dcc2fc4faf3150480c83c1c818d6a945eb6ed007
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
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.common.utils.CodeUtils;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.domain.CodeExcel;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.CodeGap;
import com.zy.acs.manager.manager.mapper.CodeMapper;
import com.zy.acs.manager.manager.service.CodeGapService;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.manager.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.util.*;
 
@Slf4j
@Service("codeService")
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Code> implements CodeService {
 
    @Autowired
    private CodeGapService codeGapService;
    @Autowired
    private RouteService routeService;
 
    private static final double STRAIGHT_LINE_SIN_THRESHOLD = 1e-3;
 
    public static final Map<Long, Code> CODE_ID_CACHE = new HashMap<>();
    public static final Map<String, Code> CODE_DATA_CACHE = new HashMap<>();
 
    @PostConstruct
    public void init() {
        for (Code code : this.list()) {
            CODE_ID_CACHE.put(code.getId(), code);
            CODE_DATA_CACHE.put(code.getData(), code);
        }
        log.info("The code cache was initialized...");
    }
 
    @Override
    public Code selectByData(String data) {
        return this.getOne(new LambdaQueryWrapper<Code>().eq(Code::getData, data));
    }
 
    @Override
    public Code getCacheById(Long id) {
        Code code = CODE_ID_CACHE.get(id);
        if (code == null) {
            code = this.getById(id);
            if (code != null) {
                CODE_ID_CACHE.put(id, code);
            }
        }
        return code;
    }
 
    @Override
    public Code getCacheByData(String data) {
        Code code = CODE_DATA_CACHE.get(data);
        if (code == null) {
            code = this.selectByData(data);
            if (null != code) {
                CODE_DATA_CACHE.put(data, code);
            }
        }
        return code;
    }
 
    @Override
    public void refreshCornerByCodeIds(List<Long> codeIds) {
        if (Cools.isEmpty(codeIds)) {
            return;
        }
        Date now = new Date();
        Set<Long> targets = new HashSet<>(codeIds);
        for (Long codeId : targets) {
            Code code = this.getCacheById(codeId);
            if (null == code) {
                continue;
            }
            boolean corner = shouldMarkAsCorner(code, routeService.getAdjacencyNode(codeId));
            Integer cornerVal = corner ? 1 : 0;
            if (!Objects.equals(code.getCorner(), cornerVal)) {
                code.setCorner(cornerVal);
                code.setUpdateTime(now);
                if (!this.updateById(code)) {
                    throw new BusinessException(code.getData() + "拐角标记更新失败");
                }
                CODE_ID_CACHE.put(code.getId(), code);
                CODE_DATA_CACHE.put(code.getData(), code);
            }
        }
    }
 
    private boolean shouldMarkAsCorner(Code code, List<Long> adjacencyIds) {
        if (Cools.isEmpty(adjacencyIds)) {
            return false;
        }
        if (adjacencyIds.size() > 2) {
            return true;
        }
        if (adjacencyIds.size() < 2) {
            return false;
        }
        Code first = this.getCacheById(adjacencyIds.get(0));
        Code second = this.getCacheById(adjacencyIds.get(1));
        if (null == first || null == second) {
            return false;
        }
        return !isStraightLine(code, first, second);
    }
 
    private boolean isStraightLine(Code center, Code first, Code second) {
        if (center.getX() == null || center.getY() == null
                || first.getX() == null || first.getY() == null
                || second.getX() == null || second.getY() == null) {
            return true;
        }
        double v1x = first.getX() - center.getX();
        double v1y = first.getY() - center.getY();
        double v2x = second.getX() - center.getX();
        double v2y = second.getY() - center.getY();
        double len1 = Math.hypot(v1x, v1y);
        double len2 = Math.hypot(v2x, v2y);
        if (len1 == 0 || len2 == 0) {
            return true;
        }
        double sinValue = Math.abs(v1x * v2y - v1y * v2x) / (len1 * len2);
        return sinValue < STRAIGHT_LINE_SIN_THRESHOLD;
    }
 
    @Override
    public void importExecute(List<CodeExcel> excelList) {
        for (CodeExcel excel : excelList) {
            Code code = this.selectByData(excel.getCode());
            if (null == code) {
                code = new Code();
                code.setUuid("code" + excel.getCode());
                code.setData(excel.getCode());
                code.setX(excel.getX());
                code.setY(excel.getY());
                code.setScale(CodeUtils.DEFAULT_SCALE);
                code.setMemo(excel.getMemo());
                if (!save(code)) {
                    throw new BusinessException(excel.getCode() + "保存失败");
                }
            }
        }
        for (CodeExcel excel : excelList) {
            Code code = selectByData(excel.getCode());
            if (!Cools.isEmpty(excel.getNearCode1())) {
                Code nearCode = selectByData(excel.getNearCode1());
                if (null == nearCode) {
                    throw new BusinessException(excel.getNearCode1() + "不存在");
                }
                CodeGap codeGap = codeGapService.findByCodeOfBoth(code.getId(), nearCode.getId());
                if (null == codeGap) {
                    codeGap = new CodeGap();
                    codeGap.setCode0(code.getId());
                    codeGap.setCode1(nearCode.getId());
                    codeGap.setDistance(excel.getNearCodeDistance1());
                    if (!codeGapService.save(codeGap)) {
                        throw new BusinessException(code.getData() + " - " + nearCode.getData() + "间距保存失败");
                    } else {
                        routeService.createRouteByCode(code, nearCode);
                    }
                }
            }
            if (!Cools.isEmpty(excel.getNearCode2())) {
                Code nearCode = selectByData(excel.getNearCode2());
                if (null == nearCode) {
                    throw new BusinessException(excel.getNearCode2() + "不存在");
                }
                CodeGap codeGap = codeGapService.findByCodeOfBoth(code.getId(), nearCode.getId());
                if (null == codeGap) {
                    codeGap = new CodeGap();
                    codeGap.setCode0(code.getId());
                    codeGap.setCode1(nearCode.getId());
                    codeGap.setDistance(excel.getNearCodeDistance2());
                    if (!codeGapService.save(codeGap)) {
                        throw new BusinessException(code.getData() + " - " + nearCode.getData() + "间距保存失败");
                    } else {
                        routeService.createRouteByCode(code, nearCode);
                    }
                }
            }
            if (!Cools.isEmpty(excel.getNearCode3())) {
                Code nearCode = selectByData(excel.getNearCode3());
                if (null == nearCode) {
                    throw new BusinessException(excel.getNearCode3() + "不存在");
                }
                CodeGap codeGap = codeGapService.findByCodeOfBoth(code.getId(), nearCode.getId());
                if (null == codeGap) {
                    codeGap = new CodeGap();
                    codeGap.setCode0(code.getId());
                    codeGap.setCode1(nearCode.getId());
                    codeGap.setDistance(excel.getNearCodeDistance3());
                    if (!codeGapService.save(codeGap)) {
                        throw new BusinessException(code.getData() + " - " + nearCode.getData() + "间距保存失败");
                    } else {
                        routeService.createRouteByCode(code, nearCode);
                    }
                }
            }
            if (!Cools.isEmpty(excel.getNearCode4())) {
                Code nearCode = selectByData(excel.getNearCode4());
                if (null == nearCode) {
                    throw new BusinessException(excel.getNearCode4() + "不存在");
                }
                CodeGap codeGap = codeGapService.findByCodeOfBoth(code.getId(), nearCode.getId());
                if (null == codeGap) {
                    codeGap = new CodeGap();
                    codeGap.setCode0(code.getId());
                    codeGap.setCode1(nearCode.getId());
                    codeGap.setDistance(excel.getNearCodeDistance4());
                    if (!codeGapService.save(codeGap)) {
                        throw new BusinessException(code.getData() + " - " + nearCode.getData() + "间距保存失败");
                    } else {
                        routeService.createRouteByCode(code, nearCode);
                    }
                }
            }
        }
    }
 
    @Override
    @Transactional
    public void adaptation(Double width, Double height) {
        width = width - CodeUtils.ADAPTATION_OFFSET_X;  // 菜单栏
 
        Map<String, Object> map = this.baseMapper.selectMinAndMaxByPoint();
        Double minX = Double.parseDouble(String.valueOf(map.get("min_x")));
        Double minY = Double.parseDouble(String.valueOf(map.get("min_y")));
        Double maxX = Double.parseDouble(String.valueOf(map.get("max_x")));
        Double maxY = Double.parseDouble(String.valueOf(map.get("max_y")));
 
        // 缩放后需要的偏移量
        double scaleOffsetX = width * (1 - CodeUtils.ADAPTATION_SCALE) / 2;
        double scaleOffsetY = height * (1 - CodeUtils.ADAPTATION_SCALE) / 2;
 
        List<Code> codeList = this.list();
        for (Code code : codeList) {
            double adaptedX = ((code.getX() - minX) / (maxX - minX)) * width * CodeUtils.ADAPTATION_SCALE + scaleOffsetX;
            double adaptedY = ((code.getY() - minY) / (maxY - minY)) * height * CodeUtils.ADAPTATION_SCALE + scaleOffsetY;
 
            adaptedX = adaptedX + CodeUtils.ADAPTATION_OFFSET_X;  // 菜单栏
 
            code.setX(adaptedX);
            code.setY(adaptedY);
 
            if (!updateById(code)) {
                throw new BusinessException(code.getData() + "条码调整坐标失败");
            }
        }
    }
 
    @Override
    public int selectDistinctCountFromX() {
        return this.baseMapper.selectDistinctCountFromX();
    }
 
    @Override
    public int selectDistinctCountFromY() {
        return this.baseMapper.selectDistinctCountFromY();
    }
 
    @Override
    public List<Code> getAllLocCode() {
        List<Long> ids = this.baseMapper.selectAllLocCode();
        List<Code> codeList = new ArrayList<>();
        for (Long id : ids) {
            Code byId = this.getById(id);
            if (byId != null) {
                codeList.add(byId);
            }
 
        }
        return codeList;
    }
 
}