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