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