对比新文件 |
| | |
| | | package com.iailab.module.data.point.service.impl; |
| | | |
| | | import com.iailab.framework.common.service.impl.BaseServiceImpl; |
| | | import com.iailab.module.data.api.point.DataPointApiImpl; |
| | | import com.iailab.module.data.api.point.dto.ApiPointValueDTO; |
| | | import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; |
| | | import com.iailab.module.data.common.enums.ArcCalculateTypeEnum; |
| | | import com.iailab.module.data.point.dao.ArcPointDataDao; |
| | | import com.iailab.module.data.point.entity.ArcPointDataEntity; |
| | | import com.iailab.module.data.point.entity.ArcPointSettingEntity; |
| | | import com.iailab.module.data.point.service.ArcPointDataService; |
| | | import com.iailab.module.data.common.enums.ArcTypeEnum; |
| | | import com.iailab.module.data.point.service.ArcPointSettingService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | |
| | | @Service |
| | | public class ArcPointDataServiceImpl extends BaseServiceImpl<ArcPointDataDao, ArcPointDataEntity> implements ArcPointDataService { |
| | | |
| | | @Autowired |
| | | private ArcPointSettingService arcPointSettingService; |
| | | |
| | | @Autowired |
| | | private DataPointApiImpl dataPointApi; |
| | | |
| | | //根据归档类型进行归档 |
| | | @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<ArcPointSettingEntity> 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); |
| | | ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
| | | arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcPointDataEntity.setArcTime(arcTime); |
| | | arcPointDataEntity.setArcId(item.getId()); |
| | | arcPointDataEntity.setValue(String.valueOf(value)); |
| | | insert(arcPointDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case SHIFT: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcPointSettingEntity> 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); |
| | | ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
| | | arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcPointDataEntity.setArcTime(arcTime); |
| | | arcPointDataEntity.setArcId(item.getId()); |
| | | arcPointDataEntity.setValue(String.valueOf(value)); |
| | | insert(arcPointDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case DAY: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcPointSettingEntity> 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); |
| | | ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
| | | arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcPointDataEntity.setArcTime(arcTime); |
| | | arcPointDataEntity.setArcId(item.getId()); |
| | | arcPointDataEntity.setValue(String.valueOf(value)); |
| | | insert(arcPointDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case MONTH: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcPointSettingEntity> 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); |
| | | ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
| | | arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcPointDataEntity.setArcTime(arcTime); |
| | | arcPointDataEntity.setArcId(item.getId()); |
| | | arcPointDataEntity.setValue(String.valueOf(value)); |
| | | insert(arcPointDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | case YEAR: |
| | | //查询对应类型的归档设置列表 |
| | | List<ArcPointSettingEntity> 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); |
| | | ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
| | | arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
| | | arcPointDataEntity.setArcTime(arcTime); |
| | | arcPointDataEntity.setArcId(item.getId()); |
| | | arcPointDataEntity.setValue(String.valueOf(value)); |
| | | insert(arcPointDataEntity); |
| | | log.debug("归档完成,point:"+item.getPoint()); |
| | | }); |
| | | break; |
| | | |
| | | } |
| | | } |
| | | |
| | | private BigDecimal calculate(String calculate, List<ApiPointValueDTO> valueList) { |
| | | log.debug("开始计算"); |
| | | if (valueList == null || valueList.isEmpty()) { |
| | | throw new IllegalArgumentException("valueList 为空"); |
| | | } |
| | | |
| | | BigDecimal value = BigDecimal.ZERO; |
| | | ArcCalculateTypeEnum calculateType = ArcCalculateTypeEnum.getEumByCode(calculate); |
| | | |
| | | switch (calculateType) { |
| | | case NONE: |
| | | value = BigDecimal.valueOf(valueList.get(valueList.size() - 1).getV()); |
| | | break; |
| | | case SUM: |
| | | for (ApiPointValueDTO dto : valueList) { |
| | | value = value.add(BigDecimal.valueOf(dto.getV())); |
| | | } |
| | | break; |
| | | case DIFF: |
| | | if (valueList.size() < 2) { |
| | | throw new IllegalArgumentException("valueList size小于2"); |
| | | } |
| | | BigDecimal prev = BigDecimal.valueOf(valueList.get(0).getV()); |
| | | for (int i = 1; i < valueList.size(); i++) { |
| | | BigDecimal curr = BigDecimal.valueOf(valueList.get(i).getV()); |
| | | 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); |
| | | break; |
| | | default: |
| | | throw new IllegalArgumentException("没有对应计算方法"); |
| | | } |
| | | return value; |
| | | } |
| | | } |