潘志宝
2024-11-29 0ed8ac1f3171e0d43d07f0c1bac32fc3712bd15b
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.point.service.impl;
H 2
139c6a 3 import cn.hutool.core.collection.CollUtil;
a6de49 4 import com.alibaba.fastjson.JSONArray;
cfbd83 5 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
a6de49 6 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
250190 7 import com.baomidou.mybatisplus.core.metadata.IPage;
6bf63b 8 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
L 9 import com.iailab.framework.common.pojo.PageResult;
139c6a 10 import com.iailab.framework.common.util.object.BeanUtils;
6bf63b 11 import com.iailab.framework.common.util.object.ConvertUtils;
01d6f8 12 import com.iailab.module.data.channel.common.service.ChannelSourceService;
a6de49 13 import com.iailab.module.data.common.enums.CommonConstant;
H 14 import com.iailab.module.data.common.enums.IsEnableEnum;
c96e44 15 import com.iailab.module.data.common.enums.IncreaseCodeEnum;
a6de49 16 import com.iailab.module.data.point.common.PointTypeEnum;
6bf63b 17 import com.iailab.module.data.point.dao.DaPointDao;
0ed8ac 18 import com.iailab.module.data.point.dto.DaCumulatePointDTO;
19 import com.iailab.module.data.point.dto.DaMathPointDTO;
a6de49 20 import com.iailab.module.data.point.dto.DaMeasurePointDTO;
H 21 import com.iailab.module.data.point.dto.DaPointDTO;
139c6a 22 import com.iailab.module.data.point.entity.DaMeasurePointEntity;
a6de49 23 import com.iailab.module.data.point.entity.DaPointEntity;
0ed8ac 24 import com.iailab.module.data.point.service.*;
f21253 25 import com.iailab.module.data.point.vo.DaPointPageReqVO;
J 26 import com.iailab.module.data.point.vo.PointImportExcelVO;
27 import com.iailab.module.data.point.vo.PointImportRespVO;
48c57b 28 import org.apache.commons.lang3.ObjectUtils;
0ed8ac 29 import org.springframework.beans.factory.annotation.Autowired;
a6de49 30 import org.springframework.stereotype.Service;
H 31 import org.springframework.util.CollectionUtils;
32
6bf63b 33 import javax.annotation.Resource;
a6de49 34 import java.util.*;
9df837 35 import java.util.concurrent.ConcurrentHashMap;
139c6a 36
D 37 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
f21253 38 import static com.iailab.module.data.enums.ErrorCodeConstants.POINT_EXISTS;
J 39 import static com.iailab.module.data.enums.ErrorCodeConstants.POINT_IMPORT_LIST_IS_EMPTY;
a6de49 40
H 41 /**
6bf63b 42  * @author lirm
a6de49 43  * @Description
6bf63b 44  * @createTime 2024年09月2日
a6de49 45  */
H 46 @Service
6bf63b 47 public class DaPointServiceImpl extends ServiceImpl<DaPointDao, DaPointEntity> implements DaPointService {
a6de49 48
0ed8ac 49     @Autowired
a6de49 50     private DaMeasurePointService daMeasurePointService;
H 51
0ed8ac 52     @Autowired
a6de49 53     private DaMathPointService daMathPointService;
H 54
0ed8ac 55     @Autowired
56     private DaCumulatePointService daCumulatePointService;
57
58     @Autowired
a6de49 59     private DaSequenceNumService daSequenceNumService;
6bf63b 60     
L 61     @Resource
62     private DaPointDao daPointDao;
63
139c6a 64     @Resource
01d6f8 65     private ChannelSourceService channelSourceService;
9df837 66
a4891a 67     private static Map<String, DaPointDTO> pointIdMap = new ConcurrentHashMap<>();
68
9df837 69     private static Map<String, DaPointDTO> pointNoMap = new ConcurrentHashMap<>();
a6de49 70
H 71     @Override
250190 72     public PageResult<DaPointDTO> queryPage(DaPointPageReqVO reqVO) {
139c6a 73         IPage<DaPointDTO> page = daPointDao.selectPageList(reqVO);
a4891a 74         return new PageResult<>(page.getRecords(), page.getTotal());
75     }
76
77     private void clearCache() {
78         pointIdMap.clear();
79         pointNoMap.clear();
a6de49 80     }
H 81
82     @Override
6bf63b 83     public DaPointDTO info(String id) {
L 84         DaPointEntity entity = daPointDao.selectById(id);
a6de49 85         DaPointDTO result = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
0ed8ac 86         result.setMeasurePoint(new DaMeasurePointDTO());
87         result.setMathPoint(new DaMathPointDTO());
88         result.setCumulatePoint(new DaCumulatePointDTO());
89         switch (PointTypeEnum.getEumByCode(result.getPointType())) {
90             case MEASURE_POINT:
91                 DaMeasurePointDTO measurePoint = daMeasurePointService.getByPoint(id);
92                 result.setMeasurePoint(measurePoint);
93                 List<String> sourceOption = new ArrayList<>();
94                 sourceOption.add(measurePoint.getSourceType());
95                 sourceOption.add(measurePoint.getSourceId());
96                 sourceOption.add(measurePoint.getTagNo());
97                 result.setSourceOption(sourceOption);
98                 break;
99             case CALCULATE_POINT:
100                 result.setMathPoint(daMathPointService.getByPoint(id));
101                 break;
102             case CUMULATE:
103                 result.setCumulatePoint(daCumulatePointService.getByPoint(id));
104                 break;
105             default:
106                 break;
a6de49 107         }
H 108         return result;
109     }
110
111     @Override
b8b8cb 112     public DaPointDTO getSimpleInfoById(String id) {
a4891a 113         if (pointIdMap.containsKey(id)) {
114             return pointIdMap.get(id);
115         }
116         DaPointDTO dto = ConvertUtils.sourceToTarget(daPointDao.selectById(id), DaPointDTO.class);
5f7008 117         if (dto == null) {
118             return null;
119         }
a4891a 120         pointIdMap.put(id, dto);
5f7008 121         return pointIdMap.get(id);
b8b8cb 122     }
123
124     @Override
125     public DaPointDTO getSimpleInfoByNo(String no) {
126         QueryWrapper<DaPointEntity> queryWrapper = new QueryWrapper();
127         queryWrapper.eq("pointNo", no);
128         return ConvertUtils.sourceToTarget(daPointDao.selectOne(queryWrapper), DaPointDTO.class);
129     }
130
131     @Override
a6de49 132     public List<DaPointDTO> list(Map<String, Object> params) {
48c57b 133         Object pointType = params.get("pointType");
a6de49 134         List<String> pointNos = new ArrayList<>();
H 135         if (params.get("pointNos") != null) {
136             pointNos = JSONArray.parseArray(JSONArray.toJSONString(params.get("pointNos")), String.class);
137         }
14cb32 138         List<String> pointTypes = new ArrayList<>();
139         if (params.get("pointTypes") != null) {
140             pointTypes = Arrays.asList(params.get("pointTypes").toString().split(","));
141         }
142
48c57b 143         Object pointNoLike = params.get("pointNoLike");
a6de49 144         QueryWrapper<DaPointEntity> queryWrapper = new QueryWrapper();
48c57b 145         queryWrapper.eq(!ObjectUtils.isEmpty(pointType), "point_type", pointType);
L 146         queryWrapper.in(pointNos.size() != 0,"point_no", pointNos);
147         queryWrapper.like(!ObjectUtils.isEmpty(pointNoLike), "point_no", pointNoLike);
14cb32 148         queryWrapper.in(pointTypes.size() != 0,"point_type", pointTypes);
6bf63b 149         List<DaPointEntity> list = daPointDao.selectList(queryWrapper);
a6de49 150         return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
H 151     }
152
153     @Override
cfbd83 154     @DSTransactional(rollbackFor = Exception.class)
a6de49 155     public void add(DaPointDTO dataPoint) {
H 156         DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class);
157         daPointEntity.setId(UUID.randomUUID().toString());
01d6f8 158         switch (PointTypeEnum.getEumByCode(dataPoint.getPointType())) {
159             case MEASURE_POINT:
160                 DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
161                 measurePoint.setSourceType(dataPoint.getSourceOption().get(0));
162                 measurePoint.setSourceId(dataPoint.getSourceOption().get(1));
163                 measurePoint.setTagNo(dataPoint.getSourceOption().get(2));
164                 daMeasurePointService.add(measurePoint, daPointEntity.getId());
165                 daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
166                 break;
167             case CALCULATE_POINT:
168                 daMathPointService.add(dataPoint.getMathPoint(), daPointEntity.getId());
169                 daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
170                 break;
171             case CONSTANT:
172                 daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
173                 break;
0ed8ac 174             case CUMULATE:
175                 daCumulatePointService.add(dataPoint.getCumulatePoint(), daPointEntity.getId());
176                 daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_L.name()));
177                 break;
01d6f8 178             default:
179                 break;
a6de49 180         }
H 181         daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
182         daPointEntity.setCreateTime(new Date());
6bf63b 183         daPointDao.insert(daPointEntity);
9df837 184
185         // 清空缓存
a4891a 186         clearCache();
a6de49 187     }
H 188
189     @Override
cfbd83 190     @DSTransactional(rollbackFor = Exception.class)
a6de49 191     public void update(DaPointDTO dataPoint) {
H 192         DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class);
193         daPointEntity.setUpdateTime(new Date());
6bf63b 194         daPointDao.updateById(daPointEntity);
01d6f8 195         switch (PointTypeEnum.getEumByCode(dataPoint.getPointType())) {
196             case MEASURE_POINT:
197                 DaMeasurePointDTO measurePoint = dataPoint.getMeasurePoint();
198                 measurePoint.setSourceType(dataPoint.getSourceOption().get(0));
199                 measurePoint.setSourceId(dataPoint.getSourceOption().get(1));
200                 measurePoint.setTagNo(dataPoint.getSourceOption().get(2));
201                 daMeasurePointService.update(measurePoint);
202                 break;
203             case CALCULATE_POINT:
204                 daMathPointService.update(dataPoint.getMathPoint());
205                 break;
0ed8ac 206             case CUMULATE:
207                 daCumulatePointService.update(dataPoint.getCumulatePoint());
208                 break;
01d6f8 209             default:
210                 break;
a6de49 211         }
9df837 212         // 清空缓存
a4891a 213         clearCache();
a6de49 214     }
H 215
216     @Override
cfbd83 217     @DSTransactional(rollbackFor = Exception.class)
2070c0 218     public void delete(String[] id) {
L 219         daPointDao.deleteBatchIds(Arrays.asList(id));
220         daMeasurePointService.deleteByPoint(id);
221         daMathPointService.deleteByPoint(id);
9df837 222         // 清空缓存
a4891a 223         clearCache();
a6de49 224     }
H 225
226     @Override
14cb32 227     public List<DaPointDTO> getConstantPoint(DaPointPageReqVO reqVO) {
228         Map<String, Object> params = new HashMap<>();
229         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
230         params.put("pointNo", reqVO.getPointNo());
231         params.put("pointName", reqVO.getPointName());
232         return daPointDao.getConstantPoint(params);
233     }
234
235     @Override
a6de49 236     public List<DaPointDTO> getConstantPoint(String freq) {
H 237         Map<String, Object> params = new HashMap<>();
238         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
239         params.put("isEnable", CommonConstant.IS_ENABLE);
240         params.put("minfreqid", freq);
6bf63b 241         return daPointDao.getConstantPoint(params);
a6de49 242     }
H 243
244     @Override
245     public List<DaPointDTO> getConstantPoint(List<String> pointNos) {
246         Map<String, Object> params = new HashMap<>();
247         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
248         params.put("isEnable", CommonConstant.IS_ENABLE);
249         params.put("pointNos", pointNos);
6bf63b 250         return daPointDao.getConstantPoint(params);
14cb32 251     }
252
253     @Override
254     public List<DaPointDTO> getMeasurePoint(DaPointPageReqVO reqVO) {
255         Map<String, Object> params = new HashMap<>();
256         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
257         params.put("pointNo", reqVO.getPointNo());
258         params.put("pointName", reqVO.getPointName());
259         params.put("sourceName", reqVO.getSourceName());
260         return daPointDao.getMeasurePoint(params);
a6de49 261     }
H 262
263     @Override
264     public List<DaPointDTO> getMeasurePoint(String freq) {
265         Map<String, Object> params = new HashMap<>();
266         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
267         params.put("isEnable", CommonConstant.IS_ENABLE);
268         params.put("minfreqid", freq);
6bf63b 269         return daPointDao.getMeasurePoint(params);
a6de49 270     }
H 271
272     @Override
273     public List<DaPointDTO> getMeasurePoint(List<String> pointNos) {
274         Map<String, Object> params = new HashMap<>();
275         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
276         params.put("isEnable", CommonConstant.IS_ENABLE);
277         params.put("pointNos", pointNos);
6bf63b 278         return daPointDao.getMeasurePoint(params);
a6de49 279     }
H 280
281     @Override
282     public DaPointDTO getMeasurePointByNo(String pointNo) {
283         Map<String, Object> params = new HashMap<>();
284         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
285         params.put("pointNo", pointNo);
6bf63b 286         List<DaPointDTO> list = daPointDao.getMeasurePoint(params);
a6de49 287         if (CollectionUtils.isEmpty(list)) {
H 288             return null;
289         }
290         return list.get(0);
14cb32 291     }
292
293     @Override
294     public List<DaPointDTO> getMathPoint(DaPointPageReqVO reqVO) {
295         Map<String, Object> params = new HashMap<>();
296         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
297         params.put("pointNo", reqVO.getPointNo());
298         params.put("pointName", reqVO.getPointName());
299         return daPointDao.getMathPoint(params);
a6de49 300     }
H 301
302     @Override
303     public List<DaPointDTO> getMathPoint(String freq) {
304         Map<String, Object> params = new HashMap<>();
305         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
306         params.put("isEnable", CommonConstant.IS_ENABLE);
307         params.put("minfreqid", freq);
6bf63b 308         return daPointDao.getMathPoint(params);
a6de49 309     }
H 310
311     @Override
312     public List<DaPointDTO> getMathPoint(List<String> pointNos) {
313         Map<String, Object> params = new HashMap<>();
314         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
315         params.put("isEnable", CommonConstant.IS_ENABLE);
316         params.put("pointNos", pointNos);
6bf63b 317         return daPointDao.getMathPoint(params);
a6de49 318     }
H 319
320     @Override
321     public DaPointDTO getByNo(String pointNo) {
9df837 322         if (pointNoMap.containsKey(pointNo)) {
323             return pointNoMap.get(pointNo);
324         }
a6de49 325         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
H 326         wrapper.eq("point_no", pointNo);
6bf63b 327         DaPointEntity entity = daPointDao.selectOne(wrapper);
9df837 328         DaPointDTO dto = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
329         pointNoMap.put(pointNo, dto);
330         return dto;
a6de49 331     }
H 332
333     @Override
334     public List<DaPointDTO> getByNos(List<String> pointNos) {
335         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
336         wrapper.in("point_no", pointNos);
6bf63b 337         List<DaPointEntity> list = daPointDao.selectList(wrapper);
a6de49 338         return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
H 339     }
340
341     @Override
342     public void updateDefaultValue(DaPointDTO dto) {
343         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
344         wrapper.eq("point_no", dto.getPointNo());
345         DaPointEntity entity = new DaPointEntity();
346         entity.setDefaultValue(dto.getDefaultValue());
6bf63b 347         daPointDao.update(entity, wrapper);
a6de49 348     }
H 349
350     @Override
139c6a 351     @DSTransactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
D 352     public PointImportRespVO importPointList(List<PointImportExcelVO> importPoints, boolean isUpdateSupport) {
353         // 1.1 参数校验
354         if (CollUtil.isEmpty(importPoints)) {
355             throw exception(POINT_IMPORT_LIST_IS_EMPTY);
356         }
01d6f8 357
358         Map<String, Map<String, String>> sourcesIdMap = channelSourceService.getSourcesId();
139c6a 359         // 2. 遍历,逐个创建 or 更新
D 360         PointImportRespVO respVO = PointImportRespVO.builder().createPointnames(new ArrayList<>())
361                 .updatePointnames(new ArrayList<>()).failurePointnames(new LinkedHashMap<>()).build();
362         importPoints.forEach(importPoint -> {
363
364             // 判断如果不存在,再进行插入
365             DaPointEntity existPoint = baseMapper.selectByPointName(importPoint.getPointName());
366             if (existPoint == null) {
367                 DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(importPoint, DaPointEntity.class);
368                 daPointEntity.setId(UUID.randomUUID().toString());
369                 daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
370                 daPointEntity.setCreateTime(new Date());
01d6f8 371                 switch (PointTypeEnum.getEumByCode(daPointEntity.getPointType())) {
372                     case MEASURE_POINT:
373                         DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
374                         measurePoint.setSourceType(importPoint.getSourceType());
375                         measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
376                         measurePoint.setTagNo(importPoint.getTagNo());
377                         measurePoint.setValueType(importPoint.getValueType());
378                         measurePoint.setDimension(importPoint.getDimension());
379                         daMeasurePointService.add(measurePoint, daPointEntity.getId());
380                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
381                         break;
382                     case CALCULATE_POINT:
383                         daMathPointService.add(importPoint.getExpression(), daPointEntity.getId());
384                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
385                         break;
386                     case CONSTANT:
387                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
388                         break;
389                     default:
390                         break;
391                 }
139c6a 392
D 393                 daPointDao.insert(daPointEntity);
394                 respVO.getCreatePointnames().add(importPoint.getPointName());
395                 return;
396             }
397
398             // 如果存在,判断是否允许更新
399             if (!isUpdateSupport) {
400                 respVO.getFailurePointnames().put(importPoint.getPointName(), POINT_EXISTS.getMsg());
401                 return;
402             }
403
404             DaPointEntity updatePoint = BeanUtils.toBean(importPoint, DaPointEntity.class);
405             updatePoint.setId(existPoint.getId());
406             baseMapper.updateById(updatePoint);
407             DaMeasurePointEntity measurePoint = new DaMeasurePointEntity();
408             measurePoint.setSourceType(importPoint.getSourceType());
01d6f8 409             measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
139c6a 410             measurePoint.setTagNo(importPoint.getTagNo());
D 411             daMeasurePointService.update(measurePoint, new QueryWrapper<DaMeasurePointEntity>().eq("point_id",updatePoint.getId()));
01d6f8 412
413
414
139c6a 415             respVO.getUpdatePointnames().add(importPoint.getPointName());
D 416         });
417         return respVO;
418     }
419
420     @Override
14cb32 421     public List<DaPointDTO> getList(DaPointPageReqVO exportReqVO) {
422         return daPointDao.getList(exportReqVO);
139c6a 423     }
D 424
425     @Override
ee9f60 426     @DSTransactional(rollbackFor = Exception.class)
a6de49 427     public void enableByIds(String[] ids) {
H 428         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
429             return;
430         }
431         Arrays.asList(ids).forEach(item -> {
432             DaPointEntity entity = new DaPointEntity();
433             entity.setId(item);
f21253 434             entity.setIsEnable(IsEnableEnum.ENABLE.getCode());
ee9f60 435             entity.setUpdateTime(new Date());
6bf63b 436             daPointDao.updateById(entity);
a6de49 437         });
H 438     }
439
440     @Override
ee9f60 441     @DSTransactional(rollbackFor = Exception.class)
a6de49 442     public void disableByIds(String[] ids) {
H 443         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
444             return;
445         }
446         Arrays.asList(ids).forEach(item -> {
447             DaPointEntity entity = new DaPointEntity();
448             entity.setId(item);
f21253 449             entity.setIsEnable(IsEnableEnum.DISABLE.getCode());
ee9f60 450             entity.setUpdateTime(new Date());
6bf63b 451             daPointDao.updateById(entity);
a6de49 452         });
H 453     }
454 }