houzhongjian
2024-12-04 a82313d17b2b5d1c02e880122efc1b701c401dcf
提交 | 用户 | 时间
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)
eb1c5f 218     public void delete(String[] ids) {
219         daPointDao.deleteBatchIds(Arrays.asList(ids));
220         daMeasurePointService.deleteByPoint(ids);
221         daMathPointService.deleteByPoint(ids);
222         daCumulatePointService.deleteByPoint(ids);
9df837 223         // 清空缓存
a4891a 224         clearCache();
a6de49 225     }
H 226
227     @Override
14cb32 228     public List<DaPointDTO> getConstantPoint(DaPointPageReqVO reqVO) {
229         Map<String, Object> params = new HashMap<>();
230         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
231         params.put("pointNo", reqVO.getPointNo());
232         params.put("pointName", reqVO.getPointName());
233         return daPointDao.getConstantPoint(params);
234     }
235
236     @Override
a6de49 237     public List<DaPointDTO> getConstantPoint(String freq) {
H 238         Map<String, Object> params = new HashMap<>();
239         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
240         params.put("isEnable", CommonConstant.IS_ENABLE);
241         params.put("minfreqid", freq);
6bf63b 242         return daPointDao.getConstantPoint(params);
a6de49 243     }
H 244
245     @Override
246     public List<DaPointDTO> getConstantPoint(List<String> pointNos) {
247         Map<String, Object> params = new HashMap<>();
248         params.put("pointType", PointTypeEnum.CONSTANT.getCode());
249         params.put("isEnable", CommonConstant.IS_ENABLE);
250         params.put("pointNos", pointNos);
6bf63b 251         return daPointDao.getConstantPoint(params);
14cb32 252     }
253
254     @Override
255     public List<DaPointDTO> getMeasurePoint(DaPointPageReqVO reqVO) {
256         Map<String, Object> params = new HashMap<>();
257         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
258         params.put("pointNo", reqVO.getPointNo());
259         params.put("pointName", reqVO.getPointName());
260         params.put("sourceName", reqVO.getSourceName());
261         return daPointDao.getMeasurePoint(params);
a6de49 262     }
H 263
264     @Override
265     public List<DaPointDTO> getMeasurePoint(String freq) {
266         Map<String, Object> params = new HashMap<>();
267         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
268         params.put("isEnable", CommonConstant.IS_ENABLE);
269         params.put("minfreqid", freq);
6bf63b 270         return daPointDao.getMeasurePoint(params);
a6de49 271     }
H 272
273     @Override
274     public List<DaPointDTO> getMeasurePoint(List<String> pointNos) {
275         Map<String, Object> params = new HashMap<>();
276         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
277         params.put("isEnable", CommonConstant.IS_ENABLE);
278         params.put("pointNos", pointNos);
6bf63b 279         return daPointDao.getMeasurePoint(params);
a6de49 280     }
H 281
282     @Override
283     public DaPointDTO getMeasurePointByNo(String pointNo) {
284         Map<String, Object> params = new HashMap<>();
285         params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode());
286         params.put("pointNo", pointNo);
6bf63b 287         List<DaPointDTO> list = daPointDao.getMeasurePoint(params);
a6de49 288         if (CollectionUtils.isEmpty(list)) {
H 289             return null;
290         }
291         return list.get(0);
14cb32 292     }
293
294     @Override
295     public List<DaPointDTO> getMathPoint(DaPointPageReqVO reqVO) {
296         Map<String, Object> params = new HashMap<>();
297         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
298         params.put("pointNo", reqVO.getPointNo());
299         params.put("pointName", reqVO.getPointName());
300         return daPointDao.getMathPoint(params);
a6de49 301     }
H 302
303     @Override
304     public List<DaPointDTO> getMathPoint(String freq) {
305         Map<String, Object> params = new HashMap<>();
306         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
307         params.put("isEnable", CommonConstant.IS_ENABLE);
308         params.put("minfreqid", freq);
6bf63b 309         return daPointDao.getMathPoint(params);
a6de49 310     }
H 311
312     @Override
313     public List<DaPointDTO> getMathPoint(List<String> pointNos) {
314         Map<String, Object> params = new HashMap<>();
315         params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode());
316         params.put("isEnable", CommonConstant.IS_ENABLE);
317         params.put("pointNos", pointNos);
6bf63b 318         return daPointDao.getMathPoint(params);
a6de49 319     }
H 320
321     @Override
56dba6 322     public List<DaPointDTO> getCumulatePoint(String freq) {
eb1c5f 323         Map<String, Object> params = new HashMap<>(3);
56dba6 324         params.put("pointType", PointTypeEnum.CUMULATE.getCode());
325         params.put("isEnable", CommonConstant.IS_ENABLE);
326         params.put("minfreqid", freq);
eb1c5f 327         return daPointDao.getCumulatePoint(params);
328     }
329
330     @Override
331     public List<DaPointDTO> getCumulatePoint(DaPointPageReqVO reqVO) {
332         Map<String, Object> params = new HashMap<>(3);
333         params.put("pointType", PointTypeEnum.CUMULATE.getCode());
334         params.put("pointNo", reqVO.getPointNo());
335         params.put("pointName", reqVO.getPointName());
56dba6 336         return daPointDao.getCumulatePoint(params);
337     }
338
339     @Override
a6de49 340     public DaPointDTO getByNo(String pointNo) {
9df837 341         if (pointNoMap.containsKey(pointNo)) {
342             return pointNoMap.get(pointNo);
343         }
a6de49 344         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
H 345         wrapper.eq("point_no", pointNo);
6bf63b 346         DaPointEntity entity = daPointDao.selectOne(wrapper);
9df837 347         DaPointDTO dto = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
348         pointNoMap.put(pointNo, dto);
349         return dto;
a6de49 350     }
H 351
352     @Override
353     public List<DaPointDTO> getByNos(List<String> pointNos) {
354         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
355         wrapper.in("point_no", pointNos);
6bf63b 356         List<DaPointEntity> list = daPointDao.selectList(wrapper);
a6de49 357         return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
H 358     }
359
360     @Override
361     public void updateDefaultValue(DaPointDTO dto) {
362         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
363         wrapper.eq("point_no", dto.getPointNo());
364         DaPointEntity entity = new DaPointEntity();
365         entity.setDefaultValue(dto.getDefaultValue());
6bf63b 366         daPointDao.update(entity, wrapper);
a6de49 367     }
H 368
369     @Override
139c6a 370     @DSTransactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
D 371     public PointImportRespVO importPointList(List<PointImportExcelVO> importPoints, boolean isUpdateSupport) {
372         // 1.1 参数校验
373         if (CollUtil.isEmpty(importPoints)) {
374             throw exception(POINT_IMPORT_LIST_IS_EMPTY);
375         }
01d6f8 376
377         Map<String, Map<String, String>> sourcesIdMap = channelSourceService.getSourcesId();
139c6a 378         // 2. 遍历,逐个创建 or 更新
D 379         PointImportRespVO respVO = PointImportRespVO.builder().createPointnames(new ArrayList<>())
380                 .updatePointnames(new ArrayList<>()).failurePointnames(new LinkedHashMap<>()).build();
381         importPoints.forEach(importPoint -> {
382
383             // 判断如果不存在,再进行插入
384             DaPointEntity existPoint = baseMapper.selectByPointName(importPoint.getPointName());
385             if (existPoint == null) {
386                 DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(importPoint, DaPointEntity.class);
387                 daPointEntity.setId(UUID.randomUUID().toString());
388                 daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
389                 daPointEntity.setCreateTime(new Date());
01d6f8 390                 switch (PointTypeEnum.getEumByCode(daPointEntity.getPointType())) {
391                     case MEASURE_POINT:
392                         DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
393                         measurePoint.setSourceType(importPoint.getSourceType());
394                         measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
395                         measurePoint.setTagNo(importPoint.getTagNo());
396                         measurePoint.setValueType(importPoint.getValueType());
397                         measurePoint.setDimension(importPoint.getDimension());
398                         daMeasurePointService.add(measurePoint, daPointEntity.getId());
399                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
400                         break;
401                     case CALCULATE_POINT:
402                         daMathPointService.add(importPoint.getExpression(), daPointEntity.getId());
403                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
404                         break;
405                     case CONSTANT:
406                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
407                         break;
eb1c5f 408                     case CUMULATE:
409                         DaCumulatePointDTO cumulatePoint = new DaCumulatePointDTO();
410                         cumulatePoint.setMomentPoint(importPoint.getMomentPoint());
411                         cumulatePoint.setLength(importPoint.getLength());
412                         cumulatePoint.setDivisor(importPoint.getDivisor());
413                         daCumulatePointService.add(cumulatePoint, daPointEntity.getId());
414                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_L.name()));
01d6f8 415                     default:
416                         break;
417                 }
139c6a 418
D 419                 daPointDao.insert(daPointEntity);
420                 respVO.getCreatePointnames().add(importPoint.getPointName());
421                 return;
422             }
423
424             // 如果存在,判断是否允许更新
425             if (!isUpdateSupport) {
426                 respVO.getFailurePointnames().put(importPoint.getPointName(), POINT_EXISTS.getMsg());
427                 return;
428             }
429
430             DaPointEntity updatePoint = BeanUtils.toBean(importPoint, DaPointEntity.class);
431             updatePoint.setId(existPoint.getId());
432             baseMapper.updateById(updatePoint);
433             DaMeasurePointEntity measurePoint = new DaMeasurePointEntity();
434             measurePoint.setSourceType(importPoint.getSourceType());
01d6f8 435             measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
139c6a 436             measurePoint.setTagNo(importPoint.getTagNo());
D 437             daMeasurePointService.update(measurePoint, new QueryWrapper<DaMeasurePointEntity>().eq("point_id",updatePoint.getId()));
438             respVO.getUpdatePointnames().add(importPoint.getPointName());
439         });
440         return respVO;
441     }
442
443     @Override
14cb32 444     public List<DaPointDTO> getList(DaPointPageReqVO exportReqVO) {
445         return daPointDao.getList(exportReqVO);
139c6a 446     }
D 447
448     @Override
ee9f60 449     @DSTransactional(rollbackFor = Exception.class)
a6de49 450     public void enableByIds(String[] ids) {
H 451         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
452             return;
453         }
454         Arrays.asList(ids).forEach(item -> {
455             DaPointEntity entity = new DaPointEntity();
456             entity.setId(item);
f21253 457             entity.setIsEnable(IsEnableEnum.ENABLE.getCode());
ee9f60 458             entity.setUpdateTime(new Date());
6bf63b 459             daPointDao.updateById(entity);
a6de49 460         });
H 461     }
462
463     @Override
ee9f60 464     @DSTransactional(rollbackFor = Exception.class)
a6de49 465     public void disableByIds(String[] ids) {
H 466         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
467             return;
468         }
469         Arrays.asList(ids).forEach(item -> {
470             DaPointEntity entity = new DaPointEntity();
471             entity.setId(item);
f21253 472             entity.setIsEnable(IsEnableEnum.DISABLE.getCode());
ee9f60 473             entity.setUpdateTime(new Date());
6bf63b 474             daPointDao.updateById(entity);
a6de49 475         });
H 476     }
477 }