iailab-module-data/iailab-module-data-api/src/main/java/com/iailab/module/data/api/plan/PlanItemApi.java
@@ -1,5 +1,6 @@ package com.iailab.module.data.api.plan; import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO; import com.iailab.module.data.common.ApiDataQueryDTO; import com.iailab.module.data.common.ApiDataValueDTO; import com.iailab.module.data.enums.ApiConstants; @@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.LinkedHashMap; import java.util.List; /** @@ -25,4 +27,8 @@ @PostMapping(PREFIX + "/query-plan/history-value") @Operation(summary = "查询计划历史值") List<ApiDataValueDTO> queryPlanItemHistoryValue(@RequestBody ApiDataQueryDTO dto); @PostMapping(PREFIX + "/query-plans/record-value") @Operation(summary = "查询计划记录") LinkedHashMap<String, List<ApiPlanDataDTO>> queryPlanItemRecordValue(@RequestBody ApiDataQueryDTO dto); } iailab-module-data/iailab-module-data-api/src/main/java/com/iailab/module/data/api/plan/dto/ApiPlanDataDTO.java
对比新文件 @@ -0,0 +1,22 @@ package com.iailab.module.data.api.plan.dto; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.Data; import java.io.Serializable; /** * @author PanZhibao * @Description * @createTime 2024年11月04日 */ @Data @Tag(name = "计划值") public class ApiPlanDataDTO implements Serializable { private static final long serialVersionUID = 1L; private String startTime; private String endTime; } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/admin/ApiDataController.java
@@ -1,6 +1,5 @@ package com.iailab.module.data.api.controller.admin; import com.iailab.framework.apilog.core.annotation.ApiAccessLog; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.framework.common.util.object.ConvertUtils; @@ -12,6 +11,7 @@ import com.iailab.module.data.api.ind.dto.ApiIndItemQueryDTO; import com.iailab.module.data.api.ind.dto.ApiIndItemValueDTO; import com.iailab.module.data.api.plan.PlanItemApi; import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointValueDTO; import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; @@ -28,7 +28,7 @@ import com.iailab.module.data.api.dto.DeviceValueDTO; import com.iailab.module.data.common.utils.ApiSecurityUtils; import com.iailab.module.data.plan.item.service.PlanItemService; import com.iailab.module.data.point.vo.PointValueExportVO; import com.iailab.module.data.point.vo.DaPointExcelVO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; @@ -37,7 +37,6 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; @@ -46,12 +45,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT; import static com.iailab.framework.common.pojo.CommonResult.success; /** @@ -216,8 +212,23 @@ apiSecurityUtils.validate(request); result = planItemApi.queryPlanItemHistoryValue(dto); return new CommonResult<List<ApiDataValueDTO>>().setData(result); } catch (Exception ex) { } catch (Exception ex) { return new CommonResult<List<ApiDataValueDTO>>().setMsg(ex.getMessage()); } } @PermitAll @PostMapping("/query-plan/record-value") @Operation(summary = "查询单个计划历史值") public CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>> queryPlanItemRecordValue(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiDataQueryDTO dto) { LinkedHashMap<String, List<ApiPlanDataDTO>> result = new LinkedHashMap<>(); try { apiSecurityUtils.validate(request); result = planItemApi.queryPlanItemRecordValue(dto); return new CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>>().setData(result); } catch (Exception ex) { return new CommonResult<LinkedHashMap<String, List<ApiPlanDataDTO>>>().setMsg(ex.getMessage()); } } @@ -226,13 +237,16 @@ request, @RequestBody ApiDataQueryDTO dto) { BarLineDTO CommonResult = new BarLineDTO(); try { apiSecurityUtils.validate(request); List<String> legend = new ArrayList<>(); List<SeriesItem> series = new ArrayList<>(); String endDateStr = dto.getEnd() == null ? DateUtils.format(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) : DateUtils.format(dto.getEnd(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); Date endDate = DateUtils.parse(endDateStr, "yyyy-MM-dd HH:mm"); Date startDate = dto.getStart() == null ? DateUtils.addDateHours(endDate, -2) : dto.getStart(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); Date endDate = dto.getEnd() == null ? DateUtils.addDateHours(calendar.getTime(), 2) : dto.getEnd(); Date startDate = dto.getStart() == null ? calendar.getTime() : dto.getStart(); List<String> categories = DateUtils.getTimeScale(startDate, endDate, dto.getGranularity() == null ? 60 : dto.getGranularity()); if (CollectionUtils.isEmpty(dto.getItemNos())) { return new CommonResult<BarLineDTO>().setData(CommonResult); @@ -268,24 +282,28 @@ return new CommonResult<BarLineDTO>().setData(CommonResult); } @GetMapping("/export-plan/history-value") @PostMapping("/export-plan/history-value") @Operation(summary = "导出计划数据") public void exportPlanHistoryValue(@RequestParam("itemNo") String itemNo, @RequestParam("start") String start, @RequestParam("end") String end, HttpServletResponse response) throws IOException { public void exportPlanHistoryValue(HttpServletResponse response, HttpServletRequest request, @RequestBody ApiDataQueryDTO dto) throws IOException { //apiSecurityUtils.validate(request); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); Date startDate = StringUtils.isBlank(start) ? DateUtils.addDateHours(calendar.getTime(), 0) : DateUtils.parse(start, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); Date endDate = StringUtils.isBlank(end) ? DateUtils.addDateHours(calendar.getTime(), 3) : DateUtils.parse(end, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); Date endDate = dto.getEnd() == null ? DateUtils.addDateHours(calendar.getTime(), 2) : dto.getEnd(); Date startDate = dto.getStart() == null ? calendar.getTime() : dto.getStart(); ApiDataQueryDTO queryDto = new ApiDataQueryDTO(); queryDto.setItemNo(itemNo); queryDto.setItemNo(dto.getItemNo()); queryDto.setStart(startDate); queryDto.setEnd(endDate); List<ApiDataValueDTO> list = planItemApi.queryPlanItemHistoryValue(queryDto); List<PlanItemValueExportVO> exportList = ConvertUtils.sourceToTarget(list, PlanItemValueExportVO.class); ExcelUtils.write(response, "计划数据.xls", "数据", PlanItemValueExportVO.class, exportList); List<PlanItemValueExportVO> exportList = list.stream().map(item -> { PlanItemValueExportVO exportVO = new PlanItemValueExportVO(); exportVO.setDataTime(DateUtils.format(item.getDataTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); exportVO.setDataValue(new BigDecimal(item.getDataValue()).setScale(0, BigDecimal.ROUND_HALF_UP).toString()); return exportVO; }).collect(Collectors.toList()); ExcelUtils.write(response, "计划数据.xls", "计划数据", PlanItemValueExportVO.class, exportList); } @PermitAll iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/plan/PlanItemApiImpl.java
@@ -1,14 +1,18 @@ package com.iailab.module.data.api.plan; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.data.api.plan.dto.ApiPlanDataDTO; import com.iailab.module.data.common.ApiDataQueryDTO; import com.iailab.module.data.common.ApiDataValueDTO; import com.iailab.module.data.plan.item.collection.PlanItemCollector; import com.iailab.module.data.plan.item.vo.PlanItemDataVO; import com.iailab.module.data.plan.item.vo.PlanItemValueVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; import java.util.LinkedHashMap; import java.util.List; /** @@ -18,7 +22,7 @@ */ @RestController // 提供 RESTful API 接口,给 Feign 调用 @Validated public class PlanItemApiImpl implements PlanItemApi{ public class PlanItemApiImpl implements PlanItemApi { @Autowired private PlanItemCollector planItemCollector; @@ -27,4 +31,17 @@ List<PlanItemValueVO> list = planItemCollector.queryValue(dto.getItemNo(), dto.getStart(), dto.getEnd()); return ConvertUtils.sourceToTarget(list, ApiDataValueDTO.class); } public LinkedHashMap<String, List<ApiPlanDataDTO>> queryPlanItemRecordValue(ApiDataQueryDTO dto) { LinkedHashMap<String, List<ApiPlanDataDTO>> result = new LinkedHashMap<>(); if (CollectionUtils.isEmpty(dto.getItemNos())) { return result; } dto.getItemNos().forEach(item -> { List<PlanItemDataVO> list = planItemCollector.getSourceValue(item, dto.getStart(), dto.getEnd()); result.put(item, ConvertUtils.sourceToTarget(list, ApiPlanDataDTO.class)); }); return result; } } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/plan/item/collection/PlanItemCollector.java
@@ -34,8 +34,6 @@ public List<PlanItemValueVO> queryValue(String itemNo, Date startTime, Date endTime) { List<PlanItemValueVO> result = new ArrayList<>(); PlanItemEntity itemEntity = planItemService.getInfoByNo(itemNo); Map<String, Object> params = new HashMap<String, Object>(); PlanDataSetEntity dataSet = planDataSetService.get(itemEntity.getDataSet()); if (dataSet == null) { log.warn("数据集不能为空"); @@ -45,24 +43,14 @@ log.warn("数据源不能为空"); return null; } Map<String, Object> params = getSqlParams(dataSet, startTime, endTime); DataContextHolder.setDataSourceId(Long.valueOf(dataSet.getDataSource())); params.put("selectSql", " plan_t.start_time, plan_t.end_time"); params.put("viewSql", dataSet.getQuerySql()); StringBuilder whereSql = new StringBuilder(); String startStr = DateUtils.format(startTime, DateUtils.DATE_NUMBER_PATTERN); String endStr = DateUtils.format(endTime, DateUtils.DATE_NUMBER_PATTERN); whereSql.append(" plan_t.start_time <= " ) .append(endStr) .append(" and plan_t.end_time >= ") .append(startStr); params.put("whereSql", whereSql.toString()); List<PlanItemDataVO> dataList = planItemService.getSourceValue(params); Calendar calendar = Calendar.getInstance(); calendar.setTime(startTime); int dataLength = (int)((endTime.getTime() - startTime.getTime()) / (1000 * 60)) + 1; for (int i = 0; i < dataLength; i ++) { int dataLength = (int) ((endTime.getTime() - startTime.getTime()) / (1000 * 60)) + 1; for (int i = 0; i < dataLength; i++) { PlanItemValueVO itemValue = new PlanItemValueVO(); Date dataTime = calendar.getTime(); itemValue.setDataTime(dataTime); @@ -82,4 +70,34 @@ } return result; } public List<PlanItemDataVO> getSourceValue(String itemNo, Date startTime, Date endTime) { PlanItemEntity itemEntity = planItemService.getInfoByNo(itemNo); PlanDataSetEntity dataSet = planDataSetService.get(itemEntity.getDataSet()); if (dataSet == null) { log.warn("数据集不能为空"); return null; } if (StringUtils.isEmpty(dataSet.getDataSource())) { log.warn("数据源不能为空"); return null; } Map<String, Object> params = getSqlParams(dataSet, startTime, endTime); return planItemService.getSourceValue(params); } private Map<String, Object> getSqlParams(PlanDataSetEntity dataSet, Date startTime, Date endTime) { Map<String, Object> params = new HashMap<String, Object>(); params.put("selectSql", " plan_t.start_time, plan_t.end_time"); params.put("viewSql", dataSet.getQuerySql()); StringBuilder whereSql = new StringBuilder(); String startStr = DateUtils.format(startTime, DateUtils.DATE_NUMBER_PATTERN); String endStr = DateUtils.format(endTime, DateUtils.DATE_NUMBER_PATTERN); whereSql.append(" plan_t.start_time <= ") .append(endStr) .append(" and plan_t.end_time >= ") .append(startStr); params.put("whereSql", whereSql.toString()); return params; } } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/plan/item/vo/PlanItemValueExportVO.java
@@ -5,6 +5,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import java.util.Date; /** * @author PanZhibao * @Description