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