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