提交 | 用户 | 时间
|
3205a2
|
1 |
package com.iailab.module.data.point.service.impl; |
D |
2 |
|
|
3 |
import com.iailab.framework.common.service.impl.BaseServiceImpl; |
|
4 |
import com.iailab.module.data.api.point.DataPointApiImpl; |
|
5 |
import com.iailab.module.data.api.point.dto.ApiPointValueDTO; |
|
6 |
import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; |
|
7 |
import com.iailab.module.data.common.enums.ArcCalculateTypeEnum; |
|
8 |
import com.iailab.module.data.point.dao.ArcPointDataDao; |
|
9 |
import com.iailab.module.data.point.entity.ArcPointDataEntity; |
|
10 |
import com.iailab.module.data.point.entity.ArcPointSettingEntity; |
|
11 |
import com.iailab.module.data.point.service.ArcPointDataService; |
|
12 |
import com.iailab.module.data.common.enums.ArcTypeEnum; |
|
13 |
import com.iailab.module.data.point.service.ArcPointSettingService; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.stereotype.Service; |
|
16 |
|
|
17 |
import java.math.BigDecimal; |
|
18 |
import java.text.SimpleDateFormat; |
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.Calendar; |
|
21 |
import java.util.Date; |
|
22 |
import java.util.List; |
|
23 |
|
|
24 |
|
|
25 |
@Service |
|
26 |
public class ArcPointDataServiceImpl extends BaseServiceImpl<ArcPointDataDao, ArcPointDataEntity> implements ArcPointDataService { |
|
27 |
|
|
28 |
@Autowired |
|
29 |
private ArcPointSettingService arcPointSettingService; |
|
30 |
|
|
31 |
@Autowired |
|
32 |
private DataPointApiImpl dataPointApi; |
|
33 |
|
|
34 |
//根据归档类型进行归档 |
|
35 |
@Override |
|
36 |
public void archiving(String type) { |
|
37 |
switch (ArcTypeEnum.getEumByCode(type)) { |
|
38 |
case HOUR: |
|
39 |
//查询对应类型的归档设置列表 |
|
40 |
List<ArcPointSettingEntity> arcHourList = arcPointSettingService.getListByType(ArcTypeEnum.HOUR.getCode()); |
|
41 |
//遍历列表 |
|
42 |
arcHourList.forEach(item -> { |
|
43 |
log.debug("开始归档,point:"+item.getPoint()); |
|
44 |
//获取开始时间 |
|
45 |
Calendar calendar = Calendar.getInstance(); |
|
46 |
calendar.add(Calendar.HOUR_OF_DAY, -1); |
|
47 |
Date startTime = calendar.getTime(); |
|
48 |
//通过point编号查询数据 |
|
49 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
50 |
queryDto.setPointNo(item.getPoint()); |
|
51 |
queryDto.setStart(startTime); |
|
52 |
queryDto.setEnd(new Date()); |
|
53 |
List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
|
54 |
//判断根据计算类型计算 |
|
55 |
BigDecimal value = calculate(item.getCalculate(), valueList); |
|
56 |
//归档 |
|
57 |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); |
|
58 |
String arcTime = dateFormat.format(startTime); |
|
59 |
ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
|
60 |
arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
|
61 |
arcPointDataEntity.setArcTime(arcTime); |
|
62 |
arcPointDataEntity.setArcId(item.getId()); |
|
63 |
arcPointDataEntity.setValue(String.valueOf(value)); |
|
64 |
insert(arcPointDataEntity); |
|
65 |
log.debug("归档完成,point:"+item.getPoint()); |
|
66 |
}); |
|
67 |
break; |
|
68 |
case SHIFT: |
|
69 |
//查询对应类型的归档设置列表 |
|
70 |
List<ArcPointSettingEntity> arcShiftList = arcPointSettingService.getListByType(ArcTypeEnum.SHIFT.getCode()); |
|
71 |
//遍历列表 |
|
72 |
arcShiftList.forEach(item -> { |
|
73 |
log.debug("开始归档,point:"+item.getPoint()); |
|
74 |
//获取开始时间 |
|
75 |
Calendar calendar = Calendar.getInstance(); |
|
76 |
calendar.add(Calendar.HOUR_OF_DAY, -12); |
|
77 |
Date startTime = calendar.getTime(); |
|
78 |
//通过point编号查询数据 |
|
79 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
80 |
queryDto.setPointNo(item.getPoint()); |
|
81 |
queryDto.setStart(startTime); |
|
82 |
queryDto.setEnd(new Date()); |
|
83 |
List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
|
84 |
//判断根据计算类型计算 |
|
85 |
BigDecimal value = calculate(item.getCalculate(), valueList); |
|
86 |
//归档 |
|
87 |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); |
|
88 |
String arcTime = dateFormat.format(startTime); |
|
89 |
ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
|
90 |
arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
|
91 |
arcPointDataEntity.setArcTime(arcTime); |
|
92 |
arcPointDataEntity.setArcId(item.getId()); |
|
93 |
arcPointDataEntity.setValue(String.valueOf(value)); |
|
94 |
insert(arcPointDataEntity); |
|
95 |
log.debug("归档完成,point:"+item.getPoint()); |
|
96 |
}); |
|
97 |
break; |
|
98 |
case DAY: |
|
99 |
//查询对应类型的归档设置列表 |
|
100 |
List<ArcPointSettingEntity> arcDayList = arcPointSettingService.getListByType(ArcTypeEnum.DAY.getCode()); |
|
101 |
//遍历列表 |
|
102 |
arcDayList.forEach(item -> { |
|
103 |
log.debug("开始归档,point:"+item.getPoint()); |
|
104 |
//获取开始时间 |
|
105 |
Calendar calendar = Calendar.getInstance(); |
|
106 |
calendar.add(Calendar.DAY_OF_MONTH, -1); |
|
107 |
Date startTime = calendar.getTime(); |
|
108 |
//通过point编号查询数据 |
|
109 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
110 |
queryDto.setPointNo(item.getPoint()); |
|
111 |
queryDto.setStart(startTime); |
|
112 |
queryDto.setEnd(new Date()); |
|
113 |
List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
|
114 |
//判断根据计算类型计算 |
|
115 |
BigDecimal value = calculate(item.getCalculate(), valueList); |
|
116 |
//归档 |
|
117 |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|
118 |
String arcTime = dateFormat.format(startTime); |
|
119 |
ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
|
120 |
arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
|
121 |
arcPointDataEntity.setArcTime(arcTime); |
|
122 |
arcPointDataEntity.setArcId(item.getId()); |
|
123 |
arcPointDataEntity.setValue(String.valueOf(value)); |
|
124 |
insert(arcPointDataEntity); |
|
125 |
log.debug("归档完成,point:"+item.getPoint()); |
|
126 |
}); |
|
127 |
break; |
|
128 |
case MONTH: |
|
129 |
//查询对应类型的归档设置列表 |
|
130 |
List<ArcPointSettingEntity> arcMonthList = arcPointSettingService.getListByType(ArcTypeEnum.MONTH.getCode()); |
|
131 |
//遍历列表 |
|
132 |
arcMonthList.forEach(item -> { |
|
133 |
log.debug("开始归档,point:"+item.getPoint()); |
|
134 |
//获取开始时间 |
|
135 |
Calendar calendar = Calendar.getInstance(); |
|
136 |
calendar.add(Calendar.MONTH, -1); |
|
137 |
Date startTime = calendar.getTime(); |
|
138 |
//通过point编号查询数据 |
|
139 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
140 |
queryDto.setPointNo(item.getPoint()); |
|
141 |
queryDto.setStart(startTime); |
|
142 |
queryDto.setEnd(new Date()); |
|
143 |
List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
|
144 |
//判断根据计算类型计算 |
|
145 |
BigDecimal value = calculate(item.getCalculate(), valueList); |
|
146 |
//归档 |
|
147 |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); |
|
148 |
String arcTime = dateFormat.format(startTime); |
|
149 |
ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
|
150 |
arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
|
151 |
arcPointDataEntity.setArcTime(arcTime); |
|
152 |
arcPointDataEntity.setArcId(item.getId()); |
|
153 |
arcPointDataEntity.setValue(String.valueOf(value)); |
|
154 |
insert(arcPointDataEntity); |
|
155 |
log.debug("归档完成,point:"+item.getPoint()); |
|
156 |
}); |
|
157 |
break; |
|
158 |
case YEAR: |
|
159 |
//查询对应类型的归档设置列表 |
|
160 |
List<ArcPointSettingEntity> arcYearList = arcPointSettingService.getListByType(ArcTypeEnum.YEAR.getCode()); |
|
161 |
//遍历列表 |
|
162 |
arcYearList.forEach(item -> { |
|
163 |
log.debug("开始归档,point:"+item.getPoint()); |
|
164 |
//获取开始时间 |
|
165 |
Calendar calendar = Calendar.getInstance(); |
|
166 |
calendar.add(Calendar.YEAR, -1); |
|
167 |
Date startTime = calendar.getTime(); |
|
168 |
//通过point编号查询数据 |
|
169 |
ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); |
|
170 |
queryDto.setPointNo(item.getPoint()); |
|
171 |
queryDto.setStart(startTime); |
|
172 |
queryDto.setEnd(new Date()); |
|
173 |
List<ApiPointValueDTO> valueList = dataPointApi.queryPointHistoryValue(queryDto); |
|
174 |
//判断根据计算类型计算 |
|
175 |
BigDecimal value = calculate(item.getCalculate(), valueList); |
|
176 |
//归档 |
|
177 |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy"); |
|
178 |
String arcTime = dateFormat.format(startTime); |
|
179 |
ArcPointDataEntity arcPointDataEntity = new ArcPointDataEntity(); |
|
180 |
arcPointDataEntity.setId(String.valueOf(new Date().getTime())); |
|
181 |
arcPointDataEntity.setArcTime(arcTime); |
|
182 |
arcPointDataEntity.setArcId(item.getId()); |
|
183 |
arcPointDataEntity.setValue(String.valueOf(value)); |
|
184 |
insert(arcPointDataEntity); |
|
185 |
log.debug("归档完成,point:"+item.getPoint()); |
|
186 |
}); |
|
187 |
break; |
|
188 |
|
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
private BigDecimal calculate(String calculate, List<ApiPointValueDTO> valueList) { |
|
193 |
log.debug("开始计算"); |
|
194 |
if (valueList == null || valueList.isEmpty()) { |
|
195 |
throw new IllegalArgumentException("valueList 为空"); |
|
196 |
} |
|
197 |
|
|
198 |
BigDecimal value = BigDecimal.ZERO; |
|
199 |
ArcCalculateTypeEnum calculateType = ArcCalculateTypeEnum.getEumByCode(calculate); |
|
200 |
|
|
201 |
switch (calculateType) { |
|
202 |
case NONE: |
|
203 |
value = BigDecimal.valueOf(valueList.get(valueList.size() - 1).getV()); |
|
204 |
break; |
|
205 |
case SUM: |
|
206 |
for (ApiPointValueDTO dto : valueList) { |
|
207 |
value = value.add(BigDecimal.valueOf(dto.getV())); |
|
208 |
} |
|
209 |
break; |
|
210 |
case DIFF: |
|
211 |
if (valueList.size() < 2) { |
|
212 |
throw new IllegalArgumentException("valueList size小于2"); |
|
213 |
} |
|
214 |
BigDecimal prev = BigDecimal.valueOf(valueList.get(0).getV()); |
|
215 |
for (int i = 1; i < valueList.size(); i++) { |
|
216 |
BigDecimal curr = BigDecimal.valueOf(valueList.get(i).getV()); |
|
217 |
value = value.add(curr.subtract(prev)); |
|
218 |
prev = curr; |
|
219 |
} |
|
220 |
break; |
|
221 |
case AVG: |
|
222 |
for (ApiPointValueDTO dto : valueList) { |
|
223 |
value = value.add(BigDecimal.valueOf(dto.getV())); |
|
224 |
} |
|
225 |
value = value.divide(BigDecimal.valueOf(valueList.size()), 2, BigDecimal.ROUND_HALF_UP); |
|
226 |
break; |
|
227 |
default: |
|
228 |
throw new IllegalArgumentException("没有对应计算方法"); |
|
229 |
} |
|
230 |
return value; |
|
231 |
} |
|
232 |
} |