liwen
6 months ago
13 changed files with 332 additions and 38 deletions
@ -0,0 +1,36 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.constants; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/29 08:31 |
||||
*/ |
||||
@AllArgsConstructor |
||||
public enum DataMethodEnum { |
||||
USUAL("详情",6,"详情"), |
||||
EARLIEST("最早值",0,"最早值"), |
||||
MAX("最大值",1,"最大值"), |
||||
MIN("最小值",2,"最小值"), |
||||
AVERAGE("平均值",3,"平均值"), |
||||
SUM("累计值/和值",4,"总和值"), |
||||
DIFFERENCE("变化值/差值",5,"变化值"), |
||||
LATEST("最新值",6,"最新值"), |
||||
; |
||||
@Getter |
||||
private String name; |
||||
@Getter |
||||
private Integer accessRule; |
||||
@Getter |
||||
private String method; |
||||
|
||||
public static DataMethodEnum getEnumByMethod(String method) { |
||||
return Arrays.stream(DataMethodEnum.class.getEnumConstants()) |
||||
.filter(e -> e.getMethod().equals(method)) |
||||
.findFirst().orElse(null); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.constants; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/28 11:37 |
||||
*/ |
||||
@AllArgsConstructor |
||||
public enum DateEnum { |
||||
YEAR("year","年","按年",5,6), |
||||
MONTH("month","月","按月",4,5), |
||||
DAY("day","日","按日",3,3), |
||||
HOUR("hour","时","按时",2,2), |
||||
MINUTE("minute","分","按分",1,1), |
||||
SECOND("second","秒","按秒",0,0), |
||||
; |
||||
@Getter |
||||
private String code; |
||||
@Getter |
||||
private String name; |
||||
@Getter |
||||
private String dateType; |
||||
@Getter |
||||
private int order; |
||||
@Getter |
||||
private Integer saveTimeType; |
||||
|
||||
public static DateEnum getDateEnumByType(String dateType) { |
||||
return Arrays.stream(DateEnum.class.getEnumConstants()).filter(d -> d.getDateType().equals(dateType)).findFirst().orElse(null); |
||||
} |
||||
|
||||
public static DateEnum getDateEnumByOrder(int order) { |
||||
return Arrays.stream(DateEnum.class.getEnumConstants()).filter(d -> d.getOrder() == order).findFirst().orElse(null); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.controller; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; |
||||
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springframework.validation.annotation.Validated; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/29 09:22 |
||||
*/ |
||||
@RequestMapping("/analyse/data") |
||||
@AllArgsConstructor |
||||
@Api(value = "数据查询管理",tags = "数据查询管理") |
||||
@RestController |
||||
public class AnalyseDataController { |
||||
|
||||
private final IAnalyseDataService analyseDataService; |
||||
|
||||
@GetMapping("/getHistoryData") |
||||
@ApiOperation("获取历史数据") |
||||
@ApiOperationSupport(order = 1) |
||||
public R getHistoryData(@Validated HistoryDataSearchVO searchVO) { |
||||
return R.data(analyseDataService.getHistoryData(searchVO)); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.service; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/28 11:29 |
||||
*/ |
||||
public interface IAnalyseDataService { |
||||
|
||||
ExtraVO getHistoryData(HistoryDataSearchVO searchVO); |
||||
|
||||
} |
@ -0,0 +1,136 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.service.impl; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.google.common.collect.Lists; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.DataMethodEnum; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.DateEnum; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; |
||||
import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants; |
||||
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO; |
||||
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO; |
||||
import com.hnac.hzinfo.sdk.core.response.Result; |
||||
import com.hnac.hzinfo.sdk.v5.device.DeviceDataClient; |
||||
import com.hnac.hzinfo.sdk.v5.device.dto.ReductionAttrDataDTO; |
||||
import com.hnac.hzinfo.sdk.v5.device.dto.ReductionDataDTO; |
||||
import com.hnac.hzinfo.sdk.v5.device.vo.ReductionDataVO; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springblade.core.tool.utils.DateUtil; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import java.time.Duration; |
||||
import java.time.LocalDateTime; |
||||
import java.time.Period; |
||||
import java.time.temporal.ChronoUnit; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/28 11:29 |
||||
*/ |
||||
@Service |
||||
@AllArgsConstructor |
||||
public class AnalyseDataServiceImpl implements IAnalyseDataService { |
||||
|
||||
private final DeviceDataClient deviceDataClient; |
||||
public static final int DATA_COUNT_MAX = 8000; |
||||
|
||||
@Override |
||||
public ExtraVO getHistoryData(HistoryDataSearchVO searchVO) { |
||||
if(DataMethodEnum.USUAL.getMethod().equals(searchVO.getMethod())) { |
||||
searchVO.setDataType(DateEnum.SECOND.getDateType()); |
||||
} |
||||
// 聚合数据方式处理
|
||||
DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType()); |
||||
searchVO.setDataType(dateEnum.getDateType()); |
||||
// 获取最适配时间段
|
||||
DateEnum adapterDate = this.getDateEnumByDuration(searchVO.getStartTime(), searchVO.getEndTime(), dateEnum); |
||||
searchVO.setDataType(adapterDate.getDateType()); |
||||
String label = "根据数据量匹配到的取数规则为:" + adapterDate.getDateType() + "查询,查询结果如下:"; |
||||
ExtraVO extraVO = new ExtraVO(); |
||||
extraVO.setType(FuncRouteEnum.HISTORY_DATA.getType().getType()); |
||||
extraVO.setFuncCode(FuncRouteEnum.HISTORY_DATA.getFuncCode()); |
||||
extraVO.setLabel(label); |
||||
Map<String,Object> params = new HashMap<>(1); |
||||
ReductionDataVO dataVO = this.getPolymerizationData(searchVO); |
||||
List<Map> datalist = dataVO.getDataList().stream().map(m -> { |
||||
m.put("val", m.get(searchVO.getSignage())); |
||||
m.remove(searchVO.getSignage()); |
||||
return m; |
||||
}).collect(Collectors.toList()); |
||||
params.put("data",datalist); |
||||
params.put("deviceName",searchVO.getDeviceName()); |
||||
params.put("attrName",dataVO.getFieldMap().get(searchVO.getSignage())); |
||||
params.put("stationName",searchVO.getStationName()); |
||||
extraVO.setParams(params); |
||||
return extraVO; |
||||
} |
||||
|
||||
private ReductionDataVO getPolymerizationData(HistoryDataSearchVO searchVO) { |
||||
DataMethodEnum enumByMethod = DataMethodEnum.getEnumByMethod(searchVO.getMethod()); |
||||
Assert.isTrue(Func.isNotEmpty(enumByMethod),() -> { |
||||
throw new ServiceException("数据查询聚合方式传参有误,查询失败!"); |
||||
}); |
||||
// 聚合数据方式处理
|
||||
DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType()); |
||||
ReductionDataDTO dataDTO = new ReductionDataDTO(); |
||||
ReductionAttrDataDTO reductionAttrData = new ReductionAttrDataDTO(); |
||||
reductionAttrData.setSignage(searchVO.getSignage()); |
||||
reductionAttrData.setAccessRules(enumByMethod.getAccessRule()); |
||||
reductionAttrData.setKeepFigures(2); |
||||
dataDTO.setBeginTime(LocalDateTime.parse(searchVO.getStartTime(),DateUtil.DATETIME_FORMATTER)); |
||||
dataDTO.setEndTime(LocalDateTime.parse(searchVO.getEndTime(),DateUtil.DATETIME_FORMATTER)); |
||||
dataDTO.setDeviceCode(searchVO.getDeviceCode()); |
||||
dataDTO.setNeedPage(false); |
||||
dataDTO.setSaveTimeType(dateEnum.getSaveTimeType()); |
||||
dataDTO.setTimeInterval(1); |
||||
dataDTO.setDtos(Lists.newArrayList(reductionAttrData)); |
||||
Result<ReductionDataVO> dataVO = deviceDataClient.pageDeviceCodeAndSignages(dataDTO); |
||||
Assert.isTrue(dataVO.isSuccess(),() -> { |
||||
throw new ServiceException("聚合历史数据查询失败!"); |
||||
}); |
||||
return dataVO.getData(); |
||||
} |
||||
|
||||
private DateEnum getDateEnumByDuration(String startTime,String endTime,DateEnum dateEnum) { |
||||
LocalDateTime start = LocalDateTime.parse(startTime, DateUtil.DATETIME_FORMATTER); |
||||
LocalDateTime end = LocalDateTime.parse(endTime, DateUtil.DATETIME_FORMATTER); |
||||
Duration duration = Duration.between(start, end); |
||||
int count; |
||||
switch(dateEnum) { |
||||
case YEAR: |
||||
count = Long.valueOf(ChronoUnit.YEARS.between(start.toLocalDate(), end.toLocalDate())).intValue(); |
||||
break; |
||||
case MONTH: |
||||
count = Long.valueOf(ChronoUnit.MONTHS.between(start.toLocalDate(), end.toLocalDate())).intValue(); |
||||
break; |
||||
case DAY: |
||||
count = Long.valueOf(ChronoUnit.DAYS.between(start.toLocalDate(), end.toLocalDate())).intValue(); |
||||
break; |
||||
case HOUR: |
||||
count = Long.valueOf(duration.toHours()).intValue(); |
||||
break; |
||||
case MINUTE: |
||||
count = Long.valueOf(duration.toMinutes()).intValue(); |
||||
break; |
||||
case SECOND: |
||||
count = Long.valueOf(duration.getSeconds()).intValue(); |
||||
break; |
||||
default: |
||||
throw new ServiceException("未找到相关时间类型,查询失败!"); |
||||
} |
||||
if(count <= DATA_COUNT_MAX) { |
||||
return dateEnum; |
||||
} |
||||
else { |
||||
return getDateEnumByDuration(startTime, endTime, DateEnum.getDateEnumByOrder(dateEnum.getOrder() + 1)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.hnac.hzims.bigmodel.interactive.vo; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/05/28 11:48 |
||||
*/ |
||||
@Data |
||||
public class HistoryDataSearchVO { |
||||
|
||||
@NotBlank |
||||
private String startTime; |
||||
|
||||
@NotBlank |
||||
private String endTime; |
||||
|
||||
@NotBlank |
||||
private String deviceCode; |
||||
|
||||
@NotBlank |
||||
private String signage; |
||||
|
||||
@NotBlank |
||||
private String method; |
||||
|
||||
private String dataType; |
||||
|
||||
private String stationName; |
||||
|
||||
private String deviceName; |
||||
|
||||
} |
Loading…
Reference in new issue