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