package com.iailab.module.data.plan.item.collection; import com.iailab.framework.tenant.core.context.DataContextHolder; import com.iailab.module.data.common.utils.DateUtils; import com.iailab.module.data.plan.data.entity.PlanDataSetEntity; import com.iailab.module.data.plan.data.service.PlanDataSetService; import com.iailab.module.data.plan.item.entity.PlanItemEntity; import com.iailab.module.data.plan.item.service.PlanItemService; import com.iailab.module.data.plan.item.vo.PlanItemDataVO; import com.iailab.module.data.plan.item.vo.PlanItemValueVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author PanZhibao * @Description * @createTime 2024年11月02日 */ @Slf4j @Component public class PlanItemCollector { @Autowired private PlanItemService planItemService; @Autowired private PlanDataSetService planDataSetService; public List queryValue(String itemNo, Date startTime, Date endTime) { List result = new ArrayList<>(); 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 params = getSqlParams(dataSet, startTime, endTime); DataContextHolder.setDataSourceId(Long.valueOf(dataSet.getDataSource())); List 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++) { PlanItemValueVO itemValue = new PlanItemValueVO(); Date dataTime = calendar.getTime(); itemValue.setDataTime(dataTime); itemValue.setDataValue(0d); if (!CollectionUtils.isEmpty(dataList)) { dataList.forEach(item -> { Date s = DateUtils.parse(item.getStartTime(), DateUtils.DATE_NUMBER_PATTERN); Date e = DateUtils.parse(item.getEndTime(), DateUtils.DATE_NUMBER_PATTERN); if ((dataTime.after(s) && dataTime.before(e)) || dataTime.equals(s) || dataTime.equals(e)) { itemValue.setDataValue(1d); } }); } calendar.add(Calendar.MINUTE, 1); result.add(itemValue); } return result; } public List 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 params = getSqlParams(dataSet, startTime, endTime); return planItemService.getSourceValue(params); } private Map getSqlParams(PlanDataSetEntity dataSet, Date startTime, Date endTime) { Map params = new HashMap(); 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; } }