package com.iailab.module.data.point.service.impl; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.constant.Constant; import com.iailab.module.data.common.enums.CommonConstant; import com.iailab.module.data.common.enums.IsEnableEnum; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.data.point.common.IncreaseCodeEnum; import com.iailab.module.data.point.dao.DaPointDao; import com.iailab.module.data.point.service.DaMeasurePointService; import com.iailab.module.data.point.common.PointTypeEnum; import com.iailab.module.data.point.dto.DaMeasurePointDTO; import com.iailab.module.data.point.dto.DaPointDTO; import com.iailab.module.data.point.entity.DaPointEntity; import com.iailab.module.data.point.service.DaMathPointService; import com.iailab.module.data.point.service.DaPointService; import com.iailab.module.data.point.service.DaSequenceNumService; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2024年05月12日 */ @Service public class DaPointServiceImpl extends BaseServiceImpl implements DaPointService { @Resource private DaMeasurePointService daMeasurePointService; @Resource private DaMathPointService daMathPointService; @Resource private DaSequenceNumService daSequenceNumService; @Override public PageData page(Map params) { IPage page = baseDao.getPageList( getPage(params, Constant.CREATE_TIME, false), params ); return getPageData(page, DaPointDTO.class); } @Override public DaPointDTO get(String id) { DaPointEntity entity = baseDao.selectById(id); DaPointDTO result = ConvertUtils.sourceToTarget(entity, DaPointDTO.class); if (PointTypeEnum.MEASURE_POINT.getCode().equals(result.getPointType())) { DaMeasurePointDTO measurePoint = daMeasurePointService.getByPoint(id); result.setMeasurePoint(measurePoint); List sourceOption = new ArrayList<>(); sourceOption.add(measurePoint.getSourceType()); sourceOption.add(measurePoint.getSourceId()); sourceOption.add(measurePoint.getTagNo()); result.setSourceOption(sourceOption); } else if (PointTypeEnum.CALCULATE_POINT.getCode().equals(result.getPointType())) { result.setMathPoint(daMathPointService.getByPoint(id)); } return result; } @Override public List list(Map params) { String page = (String) params.get("page"); String limit = (String) params.get("limit"); String pointType = (String)params.get("pointType"); List pointNos = new ArrayList<>(); if (params.get("pointNos") != null) { pointNos = JSONArray.parseArray(JSONArray.toJSONString(params.get("pointNos")), String.class); } String pointNoLike = (String)params.get("pointNoLike"); QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.eq(StringUtils.isNotBlank(pointType), "point_type", pointType) .in(!CollectionUtils.isEmpty(pointNos),"point_no", pointNos) .like(StringUtils.isNotBlank(pointNoLike), "point_no", pointNoLike) .last("limit 1, 10"); List list = baseDao.selectList(queryWrapper); return ConvertUtils.sourceToTarget(list, DaPointDTO.class); } @Override @Transactional(rollbackFor = Exception.class) public void add(DaPointDTO dataPoint) { DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class); daPointEntity.setId(UUID.randomUUID().toString()); if (PointTypeEnum.MEASURE_POINT.getName().equals(dataPoint.getPointType())) { DaMeasurePointDTO measurePoint = new DaMeasurePointDTO(); measurePoint.setSourceType(dataPoint.getSourceOption().get(0)); measurePoint.setSourceId(dataPoint.getSourceOption().get(1)); measurePoint.setTagNo(dataPoint.getSourceOption().get(2)); daMeasurePointService.add(measurePoint, daPointEntity.getId()); daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_M.name())); } else if (PointTypeEnum.CALCULATE_POINT.getName().equals(dataPoint.getPointType())) { daMathPointService.add(dataPoint.getMathPoint(), daPointEntity.getId()); daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_C.name())); } else if (PointTypeEnum.CONSTANT.getName().equals(dataPoint.getPointType())) { daPointEntity.setPointNo(daSequenceNumService.getAndIncreaseByCode(IncreaseCodeEnum.POINT_F.name())); } daPointEntity.setIsEnable(CommonConstant.IS_ENABLE); daPointEntity.setCreateTime(new Date()); baseDao.insert(daPointEntity); } @Override @Transactional(rollbackFor = Exception.class) public void update(DaPointDTO dataPoint) { DaPointEntity daPointEntity = ConvertUtils.sourceToTarget(dataPoint, DaPointEntity.class); daPointEntity.setUpdateTime(new Date()); baseDao.updateById(daPointEntity); if (PointTypeEnum.MEASURE_POINT.getName().equals(dataPoint.getPointType())) { DaMeasurePointDTO measurePoint = dataPoint.getMeasurePoint(); measurePoint.setSourceType(dataPoint.getSourceOption().get(0)); measurePoint.setSourceId(dataPoint.getSourceOption().get(1)); measurePoint.setTagNo(dataPoint.getSourceOption().get(2)); daMeasurePointService.update(measurePoint); } else if (PointTypeEnum.CALCULATE_POINT.equals(dataPoint.getPointType())) { daMathPointService.update(dataPoint.getMathPoint()); } } @Override @Transactional(rollbackFor = Exception.class) public void delete(String[] ids) { baseDao.deleteBatchIds(Arrays.asList(ids)); daMeasurePointService.deleteByPoint(ids); daMathPointService.deleteByPoint(ids); } @Override public List getConstantPoint(String freq) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.CONSTANT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("minfreqid", freq); return baseDao.getConstantPoint(params); } @Override public List getConstantPoint(List pointNos) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.CONSTANT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("pointNos", pointNos); return baseDao.getConstantPoint(params); } @Override public List getMeasurePoint(String freq) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("minfreqid", freq); return baseDao.getMeasurePoint(params); } @Override public List getMeasurePoint(List pointNos) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("pointNos", pointNos); return baseDao.getMeasurePoint(params); } @Override public DaPointDTO getMeasurePointByNo(String pointNo) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.MEASURE_POINT.getCode()); params.put("pointNo", pointNo); List list = baseDao.getMeasurePoint(params); if (CollectionUtils.isEmpty(list)) { return null; } return list.get(0); } @Override public List getMathPoint(String freq) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("minfreqid", freq); return baseDao.getMathPoint(params); } @Override public List getMathPoint(List pointNos) { Map params = new HashMap<>(); params.put("pointType", PointTypeEnum.CALCULATE_POINT.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("pointNos", pointNos); return baseDao.getMathPoint(params); } @Override public DaPointDTO getByNo(String pointNo) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("point_no", pointNo); DaPointEntity entity = baseDao.selectOne(wrapper); return ConvertUtils.sourceToTarget(entity, DaPointDTO.class); } @Override public List getByNos(List pointNos) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("point_no", pointNos); List list = baseDao.selectList(wrapper); return ConvertUtils.sourceToTarget(list, DaPointDTO.class); } @Override public void updateDefaultValue(DaPointDTO dto) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("point_no", dto.getPointNo()); DaPointEntity entity = new DaPointEntity(); entity.setDefaultValue(dto.getDefaultValue()); baseDao.update(entity, wrapper); } @Override public void enableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { DaPointEntity entity = new DaPointEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.ENABLE.value()); baseDao.updateById(entity); }); } @Override public void disableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { DaPointEntity entity = new DaPointEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.DISABLE.value()); baseDao.updateById(entity); }); } }