| | |
| | | package com.iailab.module.data.arc.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.iailab.framework.common.pojo.PageResult; |
| | | import com.iailab.framework.common.service.impl.BaseServiceImpl; |
| | | import com.iailab.framework.common.util.object.ConvertUtils; |
| | | import com.iailab.module.data.api.arc.dto.ApiArcDataDTO; |
| | | import com.iailab.module.data.api.arc.dto.ApiArcValueQueryDTO; |
| | | import com.iailab.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import com.iailab.framework.mybatis.core.query.QueryWrapperX; |
| | | import com.iailab.module.data.api.point.DataPointApiImpl; |
| | |
| | | import com.iailab.module.data.common.enums.ArcTypeEnum; |
| | | import com.iailab.module.data.arc.service.ArcSettingService; |
| | | import com.iailab.module.data.arc.vo.ArcDataPageReqVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class ArcDataServiceImpl extends BaseServiceImpl<ArcDataDao, ArcDataEntity> implements ArcDataService { |
| | | |
| | |
| | | |
| | | //根据归档类型进行归档 |
| | | @Override |
| | | public void archiving(String type) { |
| | | Map<String, Object> params = new HashMap<String, Object>(); |
| | | params.put("type", type); |
| | | switch (ArcTypeEnum.getEumByCode(type)) { |
| | | case HOUR: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcHourList = arcPointSettingService.list(params); |
| | | //遍历列表 |
| | | arcHourList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //获取开始时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.HOUR_OF_DAY, -1); |
| | | Date startTime = calendar.getTime(); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(new Date()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId()) |
| | | ); |
| | | insert(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | public void archiving(String type, Calendar calendar) { |
| | | ArcTypeEnum arcType = ArcTypeEnum.getEumByCode(type); |
| | | |
| | | switch (arcType) { |
| | | case MIN15: |
| | | break; |
| | | case HOUR: |
| | | case SHIFT: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcShiftList = arcPointSettingService.list(params); |
| | | //遍历列表 |
| | | arcShiftList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //获取开始时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.HOUR_OF_DAY, -12); |
| | | Date startTime = calendar.getTime(); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(new Date()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId()) |
| | | ); |
| | | insert(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | calendar.set(Calendar.MINUTE,0); |
| | | break; |
| | | case DAY: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcDayList = arcPointSettingService.list(params); |
| | | //遍历列表 |
| | | arcDayList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //获取开始时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -1); |
| | | Date startTime = calendar.getTime(); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(new Date()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataEntity.setCode(item.getCode()); |
| | | arcDataEntity.setCreateTime(new Date()); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId()) |
| | | ); |
| | | insert(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case MONTH: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcMonthList = arcPointSettingService.list(params); |
| | | //遍历列表 |
| | | arcMonthList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //获取开始时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.MONTH, -1); |
| | | Date startTime = calendar.getTime(); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(new Date()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataEntity.setCode(item.getCode()); |
| | | arcDataEntity.setCreateTime(new Date()); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId()) |
| | | ); |
| | | insert(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case YEAR: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcYearList = arcPointSettingService.list(params); |
| | | //遍历列表 |
| | | arcYearList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //获取开始时间 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.YEAR, -1); |
| | | Date startTime = calendar.getTime(); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(new Date()); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy"); |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataEntity.setCode(item.getCode()); |
| | | arcDataEntity.setCreateTime(new Date()); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcId()) |
| | | ); |
| | | insert(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | calendar.set(Calendar.MINUTE,0); |
| | | calendar.set(Calendar.HOUR_OF_DAY,0); |
| | | break; |
| | | |
| | | } |
| | | |
| | | Map<String, Object> params = new HashMap<String, Object>(); |
| | | params.put("type", type); |
| | | // 时间格式 |
| | | SimpleDateFormat dateFormat = arcType.getDateFormat(); |
| | | int calendarType = arcType.getCalendarType(); |
| | | int calendarNum = arcType.getCalendarNum(); |
| | | |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcSettingEntity> arcSettingList = arcPointSettingService.list(params); |
| | | Date endTime = calendar.getTime(); |
| | | calendar.add(calendarType, calendarNum); |
| | | Date startTime = calendar.getTime(); |
| | | //遍历列表 |
| | | List<ArcDataEntity> result = new ArrayList<>(arcSettingList.size()); |
| | | arcSettingList.forEach(item -> { |
| | | log.debug("开始归档,point:"+item.getPoint()); |
| | | //通过point编号查询数据 |
| | | ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
| | | queryDto.setPointNo(item.getPoint()); |
| | | queryDto.setStart(startTime); |
| | | queryDto.setEnd(endTime); |
| | | List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
| | | //判断根据计算类型计算 |
| | | BigDecimal value = calculate(item.getCalculate(), valueList); |
| | | //归档 |
| | | String arcTime = dateFormat.format(startTime); |
| | | ArcDataEntity arcDataEntity = new ArcDataEntity(); |
| | | arcDataEntity.setId(UUID.randomUUID().toString()); |
| | | arcDataEntity.setArcTime(arcTime); |
| | | arcDataEntity.setArcId(item.getId()); |
| | | arcDataEntity.setArcValue(value); |
| | | arcDataEntity.setCode(item.getCode()); |
| | | arcDataEntity.setCreateTime(new Date()); |
| | | arcDataDao.delete(new LambdaQueryWrapperX<ArcDataEntity>() |
| | | .eq(ArcDataEntity::getArcId, arcDataEntity.getArcId()) |
| | | .eq(ArcDataEntity::getArcTime, arcDataEntity.getArcTime()) |
| | | ); |
| | | result.add(arcDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint() + ",value:" + value); |
| | | }); |
| | | baseDao.insert(result); |
| | | } |
| | | |
| | | private BigDecimal calculate(String calculate, List<ApiPointValueDTO> valueList) { |
| | | valueList = valueList.stream().filter(e -> !Double.valueOf(e.getV()).equals(-2.0) ).collect(Collectors.toList()); |
| | | log.debug("开始计算"); |
| | | if (valueList == null || valueList.isEmpty()) { |
| | | throw new IllegalArgumentException("valueList 为空"); |
| | | if ( valueList.isEmpty()) { |
| | | log.error("valueList is empty,return zero!"); |
| | | return BigDecimal.ZERO; |
| | | } |
| | | |
| | | BigDecimal value = BigDecimal.ZERO; |
| | |
| | | value = BigDecimal.valueOf(valueList.get(valueList.size() - 1).getV()); |
| | | break; |
| | | case SUM: |
| | | for (ApiPointValueDTO dto : valueList) { |
| | | value = value.add(BigDecimal.valueOf(dto.getV())); |
| | | } |
| | | value = BigDecimal.valueOf(valueList.stream().mapToDouble(ApiPointValueDTO::getV).sum()); |
| | | break; |
| | | case DIFF: |
| | | if (valueList.size() < 2) { |
| | | throw new IllegalArgumentException("valueList size小于2"); |
| | | log.error("valueFilterList size小于2"); |
| | | return BigDecimal.ZERO; |
| | | } |
| | | BigDecimal prev = BigDecimal.valueOf(valueList.get(0).getV()); |
| | | for (int i = 1; i < valueList.size(); i++) { |
| | | BigDecimal curr = BigDecimal.valueOf(valueList.get(i).getV()); |
| | | if(curr.compareTo(prev) > 0) { |
| | | if(curr.compareTo(prev) > 0 ) { |
| | | value = value.add(curr.subtract(prev)); |
| | | } |
| | | prev = curr; |
| | | } |
| | | break; |
| | | case AVG: |
| | | for (ApiPointValueDTO dto : valueList) { |
| | | value = value.add(BigDecimal.valueOf(dto.getV())); |
| | | } |
| | | value = value.divide(BigDecimal.valueOf(valueList.size()), 2, BigDecimal.ROUND_HALF_UP); |
| | | value = BigDecimal.valueOf(valueList.stream().mapToDouble(ApiPointValueDTO::getV).average().orElse(0.0)); |
| | | break; |
| | | case ZERO: |
| | | value = BigDecimal.valueOf(valueList.stream().filter(e -> Double.valueOf(e.getV()).equals(0.0)).count()); |
| | | break; |
| | | default: |
| | | throw new IllegalArgumentException("没有对应计算方法"); |
| | | return BigDecimal.ZERO; |
| | | } |
| | | return value; |
| | | } |
| | |
| | | public PageResult<ArcDataEntity> queryPage(ArcDataPageReqVO reqVO) { |
| | | return arcDataDao.selectPage(reqVO); |
| | | } |
| | | |
| | | @Override |
| | | public List<ApiArcDataDTO> queryHistoryValue(ApiArcValueQueryDTO queryDTO) { |
| | | ArcSettingEntity settingEntity = arcPointSettingService.selectByCode(queryDTO.getCode()); |
| | | if (settingEntity == null) { |
| | | log.error("arcPointSettingCode:" + queryDTO.getCode() + "不存在"); |
| | | return new ArrayList<>(); |
| | | } |
| | | ArcTypeEnum arcType = ArcTypeEnum.getEumByCode(settingEntity.getType()); |
| | | // 时间格式 |
| | | SimpleDateFormat dateFormat = arcType.getDateFormat(); |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | if (queryDTO.getEnd() == null) { |
| | | queryDTO.setEnd(calendar.getTime()); |
| | | } |
| | | if (queryDTO.getStart() == null) { |
| | | calendar.add(arcType.getCalendarType(),arcType.getCalendarNum()); |
| | | queryDTO.setStart(calendar.getTime()); |
| | | } |
| | | |
| | | QueryWrapper<ArcDataEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.between("arc_time",dateFormat.format(queryDTO.getStart()),dateFormat.format(queryDTO.getEnd())) |
| | | .eq("code",queryDTO.getCode()); |
| | | |
| | | return ConvertUtils.sourceToTarget(arcDataDao.selectList(queryWrapper),ApiArcDataDTO.class); |
| | | } |
| | | |
| | | @Override |
| | | public ApiArcDataDTO queryArcLastValue(String code) { |
| | | ArcSettingEntity settingEntity = arcPointSettingService.selectByCode(code); |
| | | if (settingEntity == null) { |
| | | log.error("arcPointSettingCode:" + code + "不存在"); |
| | | return new ApiArcDataDTO(); |
| | | } |
| | | |
| | | ArcTypeEnum arcType = ArcTypeEnum.getEumByCode(settingEntity.getType()); |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | switch (arcType) { |
| | | case MIN15: |
| | | break; |
| | | case HOUR: |
| | | case SHIFT: |
| | | calendar.set(Calendar.MINUTE,0); |
| | | break; |
| | | case DAY: |
| | | case MONTH: |
| | | case YEAR: |
| | | calendar.set(Calendar.MINUTE,0); |
| | | calendar.set(Calendar.HOUR_OF_DAY,0); |
| | | break; |
| | | } |
| | | // 最后归档时间 |
| | | calendar.add(arcType.getCalendarType(), arcType.getCalendarNum()); |
| | | String arcTime = arcType.getDateFormat().format(calendar.getTime()); |
| | | QueryWrapper<ArcDataEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("arc_time",arcTime) |
| | | .eq("code",code); |
| | | ArcDataEntity arcDataEntity = baseDao.selectOne(queryWrapper); |
| | | |
| | | return ConvertUtils.sourceToTarget(arcDataEntity,ApiArcDataDTO.class); |
| | | } |
| | | } |