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