dengzedong
3 天以前 07890ec0dcb75572740f25a29c80f6d7bddb39e0
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
package com.iailab.module.data.point.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.module.data.api.point.dto.ApiPointDTO;
import com.iailab.module.data.channel.common.service.ChannelSourceService;
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.module.data.common.enums.IsEnableEnum;
import com.iailab.module.data.common.enums.IncreaseCodeEnum;
import com.iailab.module.data.point.common.PointTypeEnum;
import com.iailab.module.data.point.dao.DaPointDao;
import com.iailab.module.data.point.dto.DaCumulatePointDTO;
import com.iailab.module.data.point.dto.DaMathPointDTO;
import com.iailab.module.data.point.dto.DaMeasurePointDTO;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.point.entity.DaMeasurePointEntity;
import com.iailab.module.data.point.entity.DaPointEntity;
import com.iailab.module.data.point.service.*;
import com.iailab.module.data.point.vo.DaPointPageReqVO;
import com.iailab.module.data.point.vo.PointImportExcelVO;
import com.iailab.module.data.point.vo.PointImportRespVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.iailab.module.data.enums.ErrorCodeConstants.POINT_EXISTS;
import static com.iailab.module.data.enums.ErrorCodeConstants.POINT_IMPORT_LIST_IS_EMPTY;
 
/**
 * @author lirm
 * @Description
 * @createTime 2024年09月2日
 */
@Service
public class DaPointServiceImpl extends ServiceImpl<DaPointDao, DaPointEntity> implements DaPointService {
 
    @Autowired
    private DaMeasurePointService daMeasurePointService;
 
    @Autowired
    private DaMathPointService daMathPointService;
 
    @Autowired
    private DaCumulatePointService daCumulatePointService;
 
    @Autowired
    private DaSequenceNumService daSequenceNumService;
    
    @Resource
    private DaPointDao daPointDao;
 
    @Resource
    private ChannelSourceService channelSourceService;
 
    @Resource
    private DaPointCollectStatusService daPointCollectStatusService;
 
    private static Map<String, ApiPointDTO> pointIdMap = new ConcurrentHashMap<>();
 
    private static Map<String, DaPointDTO> pointNoMap = new ConcurrentHashMap<>();
 
    @Override
    public PageResult<DaPointDTO> queryPage(DaPointPageReqVO reqVO) {
        IPage<DaPointDTO> page = daPointDao.selectPageList(reqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }
 
    private void clearCache() {
        pointIdMap.clear();
        pointNoMap.clear();
    }
 
    @Override
    public DaPointDTO info(String id) {
        DaPointEntity entity = daPointDao.selectById(id);
        DaPointDTO result = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
        result.setMeasurePoint(new DaMeasurePointDTO());
        result.setMathPoint(new DaMathPointDTO());
        result.setCumulatePoint(new DaCumulatePointDTO());
        switch (PointTypeEnum.getEumByCode(result.getPointType())) {
            case MEASURE_POINT:
                DaMeasurePointDTO measurePoint = daMeasurePointService.getByPoint(id);
                result.setMeasurePoint(measurePoint);
                List<String> sourceOption = new ArrayList<>();
                sourceOption.add(measurePoint.getSourceType());
                sourceOption.add(measurePoint.getSourceId());
                sourceOption.add(measurePoint.getTagNo());
                result.setSourceOption(sourceOption);
                break;
            case CALCULATE_POINT:
                result.setMathPoint(daMathPointService.getByPoint(id));
                break;
            case CUMULATE:
                result.setCumulatePoint(daCumulatePointService.getByPoint(id));
                break;
            default:
                break;
        }
        return result;
    }
 
    @Override
    public ApiPointDTO getSimpleInfoById(String id) {
        if (pointIdMap.containsKey(id)) {
            return pointIdMap.get(id);
        }
        ApiPointDTO dto = ConvertUtils.sourceToTarget(daPointDao.selectById(id), ApiPointDTO.class);
        if (dto == null) {
            return null;
        }
        pointIdMap.put(id, dto);
        return dto;
    }
 
    @Override
    public DaPointDTO getSimpleInfoByNo(String no) {
        QueryWrapper<DaPointEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("point_no", no);
        return ConvertUtils.sourceToTarget(daPointDao.selectOne(queryWrapper), DaPointDTO.class);
    }
 
    @Override
    public List<DaPointDTO> list(Map<String, Object> params) {
        Object pointType = params.get("pointType");
        List<String> pointNos = new ArrayList<>();
        if (params.get("pointNos") != null) {
            pointNos = JSONArray.parseArray(JSONArray.toJSONString(params.get("pointNos")), String.class);
        }
        List<String> pointTypes = new ArrayList<>();
        if (params.get("pointTypes") != null) {
            pointTypes = Arrays.asList(params.get("pointTypes").toString().split(","));
        }
 
        Object pointNoLike = params.get("pointNoLike");
        QueryWrapper<DaPointEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pointType), "point_type", pointType);
        queryWrapper.in(pointNos.size() != 0,"point_no", pointNos);
        queryWrapper.like(!ObjectUtils.isEmpty(pointNoLike), "point_no", pointNoLike);
        queryWrapper.in(pointTypes.size() != 0,"point_type", pointTypes);
        List<DaPointEntity> list = daPointDao.selectList(queryWrapper);
        return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void add(DaPointDTO dataPoint) {
        DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class);
        daPointEntity.setId(UUID.randomUUID().toString());
        switch (PointTypeEnum.getEumByCode(dataPoint.getPointType())) {
            case MEASURE_POINT:
                DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
                measurePoint.setSourceType(dataPoint.getSourceOption().get(0));
                measurePoint.setSourceId(dataPoint.getSourceOption().get(1));
                measurePoint.setTagNo(dataPoint.getSourceOption().get(2));
                daMeasurePointService.add(measurePoint, daPointEntity.getId());
                daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
                break;
            case CALCULATE_POINT:
                daMathPointService.add(dataPoint.getMathPoint(), daPointEntity.getId());
                daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
                break;
            case CONSTANT:
                daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
                break;
            case CUMULATE:
                daCumulatePointService.add(dataPoint.getCumulatePoint(), daPointEntity.getId());
                daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_L.name()));
                break;
            default:
                break;
        }
        daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
        daPointEntity.setCreateTime(new Date());
        daPointDao.insert(daPointEntity);
 
        daPointCollectStatusService.add(daPointEntity.getPointNo());
        // 清空缓存
        clearCache();
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void update(DaPointDTO dataPoint) {
        DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class);
        daPointEntity.setUpdateTime(new Date());
        daPointDao.updateById(daPointEntity);
        switch (PointTypeEnum.getEumByCode(dataPoint.getPointType())) {
            case MEASURE_POINT:
                DaMeasurePointDTO measurePoint = dataPoint.getMeasurePoint();
                measurePoint.setSourceType(dataPoint.getSourceOption().get(0));
                measurePoint.setSourceId(dataPoint.getSourceOption().get(1));
                measurePoint.setTagNo(dataPoint.getSourceOption().get(2));
                daMeasurePointService.update(measurePoint);
                break;
            case CALCULATE_POINT:
                daMathPointService.update(dataPoint.getMathPoint());
                break;
            case CUMULATE:
                daCumulatePointService.update(dataPoint.getCumulatePoint());
                break;
            default:
                break;
        }
        // 清空缓存
        clearCache();
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        daPointDao.deleteBatchIds(Arrays.asList(ids));
        daMeasurePointService.deleteByPoint(ids);
        daMathPointService.deleteByPoint(ids);
        daCumulatePointService.deleteByPoint(ids);
        // 清空缓存
        clearCache();
    }
 
    @Override
    public List<DaPointDTO> getConstantPoint(DaPointPageReqVO reqVO) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CONSTANT.getCode());
        params.put("pointNo", reqVO.getPointNo());
        params.put("pointName", reqVO.getPointName());
        return daPointDao.getConstantPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getConstantPoint(String freq) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CONSTANT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("minfreqid", freq);
        return daPointDao.getConstantPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getConstantPoint(List<String> pointNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CONSTANT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("pointNos", pointNos);
        return daPointDao.getConstantPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getMeasurePoint(DaPointPageReqVO reqVO) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
        params.put("pointNo", reqVO.getPointNo());
        params.put("pointName", reqVO.getPointName());
        params.put("sourceName", reqVO.getSourceName());
        return daPointDao.getMeasurePoint(params);
    }
 
    @Override
    public List<DaPointDTO> getMeasurePoint(String freq) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("minfreqid", freq);
        return daPointDao.getMeasurePoint(params);
    }
 
    @Override
    public List<DaPointDTO> getMeasurePoint(List<String> pointNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("pointNos", pointNos);
        return daPointDao.getMeasurePoint(params);
    }
 
    @Override
    public DaPointDTO getMeasurePointByNo(String pointNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
        params.put("pointNo", pointNo);
        List<DaPointDTO> list = daPointDao.getMeasurePoint(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
 
    @Override
    public List<DaPointDTO> getMathPoint(DaPointPageReqVO reqVO) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
        params.put("pointNo", reqVO.getPointNo());
        params.put("pointName", reqVO.getPointName());
        return daPointDao.getMathPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getMathPoint(String freq) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("minfreqid", freq);
        return daPointDao.getMathPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getMathPoint(List<String> pointNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("pointNos", pointNos);
        return daPointDao.getMathPoint(params);
    }
 
    @Override
    public List<DaPointDTO> getCumulatePoint(String freq) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("pointType", PointTypeEnum.CUMULATE.getCode());
        params.put("isEnable", CommonConstant.IS_ENABLE);
        params.put("minfreqid", freq);
        return daPointDao.getCumulatePoint(params);
    }
 
    @Override
    public List<DaPointDTO> getCumulatePoint(DaPointPageReqVO reqVO) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("pointType", PointTypeEnum.CUMULATE.getCode());
        params.put("pointNo", reqVO.getPointNo());
        params.put("pointName", reqVO.getPointName());
        return daPointDao.getCumulatePoint(params);
    }
 
    @Override
    public DaPointDTO getByNo(String pointNo) {
        if (pointNoMap.containsKey(pointNo)) {
            return pointNoMap.get(pointNo);
        }
        QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("point_no", pointNo);
        DaPointEntity entity = daPointDao.selectOne(wrapper);
        DaPointDTO dto = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
        pointNoMap.put(pointNo, dto);
        return dto;
    }
 
    @Override
    public List<DaPointDTO> getByNos(List<String> pointNos) {
        QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
        wrapper.in("point_no", pointNos);
        List<DaPointEntity> list = daPointDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
    }
 
    @Override
    public void updateDefaultValue(DaPointDTO dto) {
        QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("point_no", dto.getPointNo());
        DaPointEntity entity = new DaPointEntity();
        entity.setDefaultValue(dto.getDefaultValue());
        daPointDao.update(entity, wrapper);
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
    public PointImportRespVO importPointList(List<PointImportExcelVO> importPoints, boolean isUpdateSupport) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(importPoints)) {
            throw exception(POINT_IMPORT_LIST_IS_EMPTY);
        }
 
        Map<String, Map<String, String>> sourcesIdMap = channelSourceService.getSourcesId();
        // 2. 遍历,逐个创建 or 更新
        PointImportRespVO respVO = PointImportRespVO.builder().createPointnames(new ArrayList<>())
                .updatePointnames(new ArrayList<>()).failurePointnames(new LinkedHashMap<>()).build();
        importPoints.forEach(importPoint -> {
 
            // 判断如果不存在,再进行插入
            DaPointEntity existPoint = baseMapper.selectByPointName(importPoint.getPointName());
            if (existPoint == null) {
                DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(importPoint, DaPointEntity.class);
                daPointEntity.setId(UUID.randomUUID().toString());
                daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
                daPointEntity.setCreateTime(new Date());
                switch (PointTypeEnum.getEumByCode(daPointEntity.getPointType())) {
                    case MEASURE_POINT:
                        DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
                        measurePoint.setSourceType(importPoint.getSourceType());
                        measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
                        measurePoint.setTagNo(importPoint.getTagNo());
                        measurePoint.setValueType(importPoint.getValueType());
                        measurePoint.setDimension(importPoint.getDimension());
                        daMeasurePointService.add(measurePoint, daPointEntity.getId());
                        daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
                        break;
                    case CALCULATE_POINT:
                        daMathPointService.add(importPoint.getExpression(), daPointEntity.getId());
                        daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
                        break;
                    case CONSTANT:
                        daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
                        break;
                    case CUMULATE:
                        DaCumulatePointDTO cumulatePoint = new DaCumulatePointDTO();
                        cumulatePoint.setMomentPoint(importPoint.getMomentPoint());
                        cumulatePoint.setLength(importPoint.getLength());
                        cumulatePoint.setDivisor(importPoint.getDivisor());
                        daCumulatePointService.add(cumulatePoint, daPointEntity.getId());
                        daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_L.name()));
                    default:
                        break;
                }
 
                daPointDao.insert(daPointEntity);
                daPointCollectStatusService.add(daPointEntity.getPointNo());
                respVO.getCreatePointnames().add(importPoint.getPointName());
                return;
            }
 
            // 如果存在,判断是否允许更新
            if (!isUpdateSupport) {
                respVO.getFailurePointnames().put(importPoint.getPointName(), POINT_EXISTS.getMsg());
                return;
            }
 
            DaPointEntity updatePoint = BeanUtils.toBean(importPoint, DaPointEntity.class);
            updatePoint.setId(existPoint.getId());
            baseMapper.updateById(updatePoint);
            DaMeasurePointEntity measurePoint = new DaMeasurePointEntity();
            measurePoint.setSourceType(importPoint.getSourceType());
            measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
            measurePoint.setTagNo(importPoint.getTagNo());
            daMeasurePointService.update(measurePoint, new QueryWrapper<DaMeasurePointEntity>().eq("point_id",updatePoint.getId()));
            respVO.getUpdatePointnames().add(importPoint.getPointName());
        });
        return respVO;
    }
 
    @Override
    public List<DaPointDTO> getList(DaPointPageReqVO exportReqVO) {
        return daPointDao.getList(exportReqVO);
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void enableByIds(String[] ids) {
        if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
            return;
        }
        Arrays.asList(ids).forEach(item -> {
            DaPointEntity entity = new DaPointEntity();
            entity.setId(item);
            entity.setIsEnable(IsEnableEnum.ENABLE.getCode());
            entity.setUpdateTime(new Date());
            daPointDao.updateById(entity);
        });
    }
 
    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void disableByIds(String[] ids) {
        if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
            return;
        }
        Arrays.asList(ids).forEach(item -> {
            DaPointEntity entity = new DaPointEntity();
            entity.setId(item);
            entity.setIsEnable(IsEnableEnum.DISABLE.getCode());
            entity.setUpdateTime(new Date());
            daPointDao.updateById(entity);
        });
    }
}