潘志宝
2024-11-29 e0b86ecb9040b56bad93356263805baff76442b4
提交 | 用户 | 时间
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
56dba6 321     public List<DaPointDTO> getCumulatePoint(String freq) {
322         Map<String, Object> params = new HashMap<>();
323         params.put("pointType", PointTypeEnum.CUMULATE.getCode());
324         params.put("isEnable", CommonConstant.IS_ENABLE);
325         params.put("minfreqid", freq);
326         return daPointDao.getCumulatePoint(params);
327     }
328
329     @Override
a6de49 330     public DaPointDTO getByNo(String pointNo) {
9df837 331         if (pointNoMap.containsKey(pointNo)) {
332             return pointNoMap.get(pointNo);
333         }
a6de49 334         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
H 335         wrapper.eq("point_no", pointNo);
6bf63b 336         DaPointEntity entity = daPointDao.selectOne(wrapper);
9df837 337         DaPointDTO dto = ConvertUtils.sourceToTarget(entity, DaPointDTO.class);
338         pointNoMap.put(pointNo, dto);
339         return dto;
a6de49 340     }
H 341
342     @Override
343     public List<DaPointDTO> getByNos(List<String> pointNos) {
344         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
345         wrapper.in("point_no", pointNos);
6bf63b 346         List<DaPointEntity> list = daPointDao.selectList(wrapper);
a6de49 347         return ConvertUtils.sourceToTarget(list, DaPointDTO.class);
H 348     }
349
350     @Override
351     public void updateDefaultValue(DaPointDTO dto) {
352         QueryWrapper<DaPointEntity> wrapper = new QueryWrapper<>();
353         wrapper.eq("point_no", dto.getPointNo());
354         DaPointEntity entity = new DaPointEntity();
355         entity.setDefaultValue(dto.getDefaultValue());
6bf63b 356         daPointDao.update(entity, wrapper);
a6de49 357     }
H 358
359     @Override
139c6a 360     @DSTransactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
D 361     public PointImportRespVO importPointList(List<PointImportExcelVO> importPoints, boolean isUpdateSupport) {
362         // 1.1 参数校验
363         if (CollUtil.isEmpty(importPoints)) {
364             throw exception(POINT_IMPORT_LIST_IS_EMPTY);
365         }
01d6f8 366
367         Map<String, Map<String, String>> sourcesIdMap = channelSourceService.getSourcesId();
139c6a 368         // 2. 遍历,逐个创建 or 更新
D 369         PointImportRespVO respVO = PointImportRespVO.builder().createPointnames(new ArrayList<>())
370                 .updatePointnames(new ArrayList<>()).failurePointnames(new LinkedHashMap<>()).build();
371         importPoints.forEach(importPoint -> {
372
373             // 判断如果不存在,再进行插入
374             DaPointEntity existPoint = baseMapper.selectByPointName(importPoint.getPointName());
375             if (existPoint == null) {
376                 DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(importPoint, DaPointEntity.class);
377                 daPointEntity.setId(UUID.randomUUID().toString());
378                 daPointEntity.setIsEnable(CommonConstant.IS_ENABLE);
379                 daPointEntity.setCreateTime(new Date());
01d6f8 380                 switch (PointTypeEnum.getEumByCode(daPointEntity.getPointType())) {
381                     case MEASURE_POINT:
382                         DaMeasurePointDTO measurePoint = new DaMeasurePointDTO();
383                         measurePoint.setSourceType(importPoint.getSourceType());
384                         measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
385                         measurePoint.setTagNo(importPoint.getTagNo());
386                         measurePoint.setValueType(importPoint.getValueType());
387                         measurePoint.setDimension(importPoint.getDimension());
388                         daMeasurePointService.add(measurePoint, daPointEntity.getId());
389                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name()));
390                         break;
391                     case CALCULATE_POINT:
392                         daMathPointService.add(importPoint.getExpression(), daPointEntity.getId());
393                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name()));
394                         break;
395                     case CONSTANT:
396                         daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name()));
397                         break;
398                     default:
399                         break;
400                 }
139c6a 401
D 402                 daPointDao.insert(daPointEntity);
403                 respVO.getCreatePointnames().add(importPoint.getPointName());
404                 return;
405             }
406
407             // 如果存在,判断是否允许更新
408             if (!isUpdateSupport) {
409                 respVO.getFailurePointnames().put(importPoint.getPointName(), POINT_EXISTS.getMsg());
410                 return;
411             }
412
413             DaPointEntity updatePoint = BeanUtils.toBean(importPoint, DaPointEntity.class);
414             updatePoint.setId(existPoint.getId());
415             baseMapper.updateById(updatePoint);
416             DaMeasurePointEntity measurePoint = new DaMeasurePointEntity();
417             measurePoint.setSourceType(importPoint.getSourceType());
01d6f8 418             measurePoint.setSourceId(sourcesIdMap.get(importPoint.getSourceType()).get(importPoint.getSourceName()));
139c6a 419             measurePoint.setTagNo(importPoint.getTagNo());
D 420             daMeasurePointService.update(measurePoint, new QueryWrapper<DaMeasurePointEntity>().eq("point_id",updatePoint.getId()));
01d6f8 421
422
423
139c6a 424             respVO.getUpdatePointnames().add(importPoint.getPointName());
D 425         });
426         return respVO;
427     }
428
429     @Override
14cb32 430     public List<DaPointDTO> getList(DaPointPageReqVO exportReqVO) {
431         return daPointDao.getList(exportReqVO);
139c6a 432     }
D 433
434     @Override
ee9f60 435     @DSTransactional(rollbackFor = Exception.class)
a6de49 436     public void enableByIds(String[] ids) {
H 437         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
438             return;
439         }
440         Arrays.asList(ids).forEach(item -> {
441             DaPointEntity entity = new DaPointEntity();
442             entity.setId(item);
f21253 443             entity.setIsEnable(IsEnableEnum.ENABLE.getCode());
ee9f60 444             entity.setUpdateTime(new Date());
6bf63b 445             daPointDao.updateById(entity);
a6de49 446         });
H 447     }
448
449     @Override
ee9f60 450     @DSTransactional(rollbackFor = Exception.class)
a6de49 451     public void disableByIds(String[] ids) {
H 452         if (CollectionUtils.isEmpty(Arrays.asList(ids))) {
453             return;
454         }
455         Arrays.asList(ids).forEach(item -> {
456             DaPointEntity entity = new DaPointEntity();
457             entity.setId(item);
f21253 458             entity.setIsEnable(IsEnableEnum.DISABLE.getCode());
ee9f60 459             entity.setUpdateTime(new Date());
6bf63b 460             daPointDao.updateById(entity);
a6de49 461         });
H 462     }
463 }