jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.quantity.manager;
 
import com.ld.igds.camera.ApiCameraManager;
import com.ld.igds.camera.CameraUtil;
import com.ld.igds.camera.data.ApiCameraData;
import com.ld.igds.camera.data.ApiCameraResp;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.RespCodeEnum;
import com.ld.igds.data.PageResponse;
import com.ld.igds.file.CoreFileService;
import com.ld.igds.file.dto.FileData;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.QuantityRequest;
import com.ld.igds.io.response.BaseResponse;
import com.ld.igds.models.Depot;
import com.ld.igds.models.QuantityConf;
import com.ld.igds.quantity.dto.QuantityConfDto;
import com.ld.igds.quantity.dto.QuantityData;
import com.ld.igds.quantity.dto.QuantityParam;
import com.ld.igds.quantity.io.QuantityRemoteManager;
import com.ld.igds.quantity.io.RemoteQuantityService;
import com.ld.igds.quantity.service.CoreQuantityService;
import com.ld.igds.util.ContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
 
import java.io.File;
import java.util.List;
 
/**
 * 逻辑管理,用于实现控制层的逻辑
 */
@Component
public class QuantityManager {
 
    @Autowired
    private CoreCommonService coreCommonService;
    @Autowired
    private CoreQuantityService coreQuantityService;
 
    @Autowired
    private QuantityRemoteManager remoteManager;
    @Autowired
    private ApiCameraManager apiCameraManager;
 
    @Autowired
    private CoreFileService fileService;
 
    /**
     * 保存数据
     *
     * @param data
     */
    @SuppressWarnings("rawtypes")
    public PageResponse saveData(QuantityData data) {
        coreQuantityService.saveData(data);
        return new PageResponse<>(RespCodeEnum.CODE_0000, null);
    }
 
    /**
     * 根据仓库编号获取最新一条数据
     *
     * @param param
     * @return
     */
    public PageResponse<QuantityData> getLastData(QuantityParam param) {
 
        if (StringUtils.isEmpty(param.getDepotId())) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(), "仓库编号为空!");
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        Depot depot = coreCommonService.getCacheDepot(param.getCompanyId(), param.getDepotId());
        if (depot == null) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(), "没有获取到当前仓库信息");
        }
 
        QuantityData quantityData = coreQuantityService.getLastData(param);
        if (quantityData == null) {
            quantityData = new QuantityData();
        }
        quantityData.setDepotData(depot);
 
        return new PageResponse<>(RespCodeEnum.CODE_0000, quantityData);
    }
 
 
    /**
     * 单仓检测
     *
     * @param param
     * @return
     */
    public BaseResponse checkSingle(QuantityParam param) {
        if (param == null || StringUtils.isEmpty(param.getDepotId())) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        QuantityConf conf = coreQuantityService.getCacheQuantityConf(param.getCompanyId(), param.getDepotId());
        if (conf == null) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        Depot depot = coreCommonService.getCacheDepot(param.getCompanyId(), conf.getDepotId());
 
        if (depot.getBulkWeight() == null || depot.getBulkWeight() <= 0) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "当前仓库没有配置容重,取消检测!");
        }
 
        RemoteQuantityService remoteQuantityService = remoteManager.getRemoteQuantityService(conf.getProtocol());
 
        if (null == remoteQuantityService) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "系统没有" + conf.getProtocol() + "的协议实现,执行被拒绝!");
        }
 
        QuantityRequest request = new QuantityRequest();
        BeanUtils.copyProperties(conf, request);
        request.setDensity(depot.getBulkWeight());
        request.setDepotId(conf.getDepotId());
        request.setYtIp(conf.getYtIp());
        request.setIp(conf.getIp());
        request.setPort(conf.getPort());
        request.setCompanyId(conf.getCompanyId());
        request.setDeptId(conf.getDeptId());
        request.setDepotType(depot.getDepotType());
        request.setSn(conf.getSn());
        request.setLength(conf.getLength());
 
        request.setBatchId(ContextUtil.getDefaultBatchId());
 
        return remoteQuantityService.checkStart(request);
    }
 
    public BaseResponse remoteConf(QuantityConf conf) {
 
        Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(), conf.getDepotId());
 
        if (depot.getBulkWeight() == null || depot.getBulkWeight() <= 0) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "当前仓库没有配置容重,取消检测!");
        }
 
        RemoteQuantityService remoteQuantityService = remoteManager.getRemoteQuantityService(conf.getProtocol());
 
        if (null == remoteQuantityService) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "系统没有" + conf.getProtocol() + "的协议实现,执行被拒绝!");
        }
 
 
        QuantityRequest request = new QuantityRequest();
        BeanUtils.copyProperties(conf, request);
        request.setDensity(depot.getBulkWeight());
        request.setDepotId(conf.getDepotId());
        request.setIp(conf.getIp());
        request.setPort(conf.getPort());
        request.setCompanyId(conf.getCompanyId());
        request.setDeptId(conf.getDeptId());
        request.setDepotType(depot.getDepotType());
        request.setSn(conf.getSn());
        request.setLength(conf.getLength());
 
        return remoteQuantityService.remoteConf(request);
    }
 
 
    public BaseResponse closeWarn(QuantityParam param) {
        if (param == null || StringUtils.isEmpty(param.getDepotId())) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        QuantityConf conf = coreQuantityService.getCacheQuantityConf(param.getCompanyId(), param.getDepotId());
        if (conf == null) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        RemoteQuantityService remoteQuantityService = remoteManager.getRemoteQuantityService(conf.getProtocol());
 
        if (null == remoteQuantityService) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "系统没有" + conf.getProtocol() + "的协议实现,执行被拒绝!");
        }
 
        QuantityRequest request = new QuantityRequest();
        request.setCompanyId(conf.getCompanyId());
        request.setIp(conf.getIp());
        request.setPort(conf.getPort());
        request.setSn(conf.getSn());
        request.setDeptId(conf.getDeptId());
        request.setDepotId(conf.getDepotId());
 
        return remoteQuantityService.closeWarn(request);
    }
 
    public BaseResponse checkStop(QuantityParam param) {
        if (param == null || StringUtils.isEmpty(param.getDepotId())) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        QuantityConf conf = coreQuantityService.getCacheQuantityConf(param.getCompanyId(), param.getDepotId());
        if (conf == null) {
            return new BaseResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        RemoteQuantityService remoteQuantityService = remoteManager.getRemoteQuantityService(conf.getProtocol());
 
        if (null == remoteQuantityService) {
            return new BaseResponse(OrderRespEnum.ORDER_ERROR.getCode(),
                    "系统没有" + conf.getProtocol() + "的协议实现,执行被拒绝!");
        }
 
        QuantityRequest request = new QuantityRequest();
        request.setCompanyId(conf.getCompanyId());
        request.setIp(conf.getIp());
        request.setPort(conf.getPort());
        request.setSn(conf.getSn());
        request.setDeptId(conf.getDeptId());
        request.setDepotId(conf.getDepotId());
 
        return remoteQuantityService.checkStop(request);
    }
 
    @SuppressWarnings({"rawtypes", "unchecked"})
    public PageResponse<QuantityConfDto> quantityCamera(@RequestBody QuantityParam param) {
        if (param == null) {
            return new PageResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
        if (StringUtils.isEmpty(param.getDepotId())) {
            return new PageResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        QuantityConf conf = coreQuantityService.getCacheQuantityConf(param.getCompanyId(), param.getDepotId());
        if (conf == null) {
            return new PageResponse(RespCodeEnum.CODE_1007.getCode(), RespCodeEnum.CODE_1007.getMsg());
        }
 
        QuantityConfDto quantityConfDto = new QuantityConfDto();
        BeanUtils.copyProperties(conf, quantityConfDto);
 
        //判断播放方式,如果为vlc则直接返回
        if (CameraUtil.PLAY_TYPE_VLC.equals(conf.getPlayType())) {
            //更新列媒体地址
            String mediaAddr = CameraUtil.updateMediaAddr(quantityConfDto.getMediaAddr(), quantityConfDto.getLoginId(),
                    quantityConfDto.getPwd(), quantityConfDto.getIp(), quantityConfDto.getPortCtrl(), 1);
            quantityConfDto.setMediaAddr(mediaAddr);
 
            return new PageResponse<QuantityConfDto>(RespCodeEnum.CODE_0000, quantityConfDto);
        }
 
        quantityConfDto = updateMediaInfo(quantityConfDto);
        if (StringUtils.isNotEmpty(quantityConfDto.getMsg())) {
            return new PageResponse(RespCodeEnum.CODE_1007.getCode(), "获取播放地址失败!");
        }
 
        return new PageResponse<QuantityConfDto>(RespCodeEnum.CODE_0000, quantityConfDto);
    }
 
 
    public QuantityConfDto updateMediaInfo(QuantityConfDto conf) {
        if (StringUtils.isEmpty(conf.getCameraSn())
                && StringUtils.isEmpty(conf.getIp())) {
            conf.setMsg("没有获取到当前摄像机IP/SN,不支持云台控制");
            return conf;
        }
 
        //通过接口获取播放地址
        ApiCameraData apiCameraData = new ApiCameraData();
        apiCameraData.setCompanyId(conf.getCompanyId());
        apiCameraData.setSn(conf.getSn());
        apiCameraData.setPlayType(conf.getPlayType());
        apiCameraData.setMediaAddr(CameraUtil.updateMediaAddr(conf.getMediaAddr(), conf.getLoginId(), conf.getPwd(), conf.getIp(), conf.getPortCtrl(), conf.getChanNum()));
        ApiCameraResp result = apiCameraManager.getApiCameraService(conf.getPlayType()).getPlayAddr(apiCameraData);
 
        if (null != result && ApiCameraResp.CODE_SUCCESS.equals(result.getCode())) {
            conf.setPlayAddr(result.getPlayAddr());
        }
 
        return conf;
    }
 
    /**
     * 获取图片列表
     *
     * @param param
     * @return
     */
    public PageResponse<List<FileData>> listSnapImg(QuantityParam param) {
 
        if (null == param) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(), "参数异常!");
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        List<FileData> list = fileService.listFile(ContextUtil.getCompanyId(), param.getBatchId());
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "响应数据为空!");
        }
        String bathPath;
        File imgFile;
        for (FileData dto : list) {
            if (StringUtils.isNotEmpty(dto.getFileName())) {
                bathPath = fileService.getCommonFilePath(dto.getCreateTime());
                imgFile = new File(bathPath + dto.getFileName());
                if (imgFile.exists()) {
                    dto.setFilePath(bathPath + dto.getFileName());
                }
            }
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000, list);
    }
 
 
    /**
     * 根据仓库编码获取最近的5条检测记录信息
     *
     * @param param
     * @return
     */
    public PageResponse<List<QuantityData>> getRecordData(QuantityParam param) {
        if (StringUtils.isEmpty(param.getDepotId())) {
            return new PageResponse<>(RespCodeEnum.CODE_1007.getCode(), "仓库编号为空!");
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        //指定获取5条
        param.setLimit(5);
        List<QuantityData> list = coreQuantityService.getDataByLimit(param);
        if (null == list || list.isEmpty()) {
            return new PageResponse<>(RespCodeEnum.CODE_2000.getCode(), "获取数据为空!");
        }
        return new PageResponse<>(RespCodeEnum.CODE_0000, list);
    }
}