|
|
@@ -14,6 +14,7 @@ import com.jjt.common.utils.DateUtils;
|
|
|
import com.jjt.common.utils.StringUtils;
|
|
|
import com.jjt.common.utils.bean.BeanUtils;
|
|
|
import com.jjt.system.service.ISysConfigService;
|
|
|
+import com.jjt.biz.mapper.TwinDeviceMapper;
|
|
|
import org.apache.ibatis.session.ExecutorType;
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
@@ -42,6 +43,8 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
private SqlSessionFactory factory;
|
|
|
@Resource
|
|
|
private ITwinCalcHourService calcHourService;
|
|
|
+ @Resource
|
|
|
+ private TwinDeviceMapper twinDeviceMapper;
|
|
|
|
|
|
/**
|
|
|
* 查询停机数据统计
|
|
|
@@ -307,6 +310,200 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
*/
|
|
|
@Override
|
|
|
public List<StopDetail> selectTwinCalcStopDetailList(TwinCalcStop twinCalcStop) {
|
|
|
- return twinCalcStopMapper.selectTwinCalcStopDetailList(twinCalcStop);
|
|
|
+ String periodType = "day";
|
|
|
+ if (twinCalcStop.getParams() != null && twinCalcStop.getParams().get("periodType") != null) {
|
|
|
+ periodType = (String) twinCalcStop.getParams().get("periodType");
|
|
|
+ }
|
|
|
+
|
|
|
+ Date queryDate = twinCalcStop.getDataDate();
|
|
|
+ if ("month".equals(periodType)) {
|
|
|
+ // 按月查询,返回当月所有日期的数据
|
|
|
+ return selectMonthlyStopDetailList(twinCalcStop);
|
|
|
+ } else {
|
|
|
+ // 按天查询,返回24小时的数据
|
|
|
+ return selectDailyStopDetailList(twinCalcStop);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询月度停机详情列表(按日统计)
|
|
|
+ *
|
|
|
+ * @param twinCalcStop 查询条件
|
|
|
+ * @return 停机详情列表
|
|
|
+ */
|
|
|
+ private List<StopDetail> selectMonthlyStopDetailList(TwinCalcStop twinCalcStop) {
|
|
|
+ // 获取当月第一天和最后一天
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ if (twinCalcStop.getDataDate() != null) {
|
|
|
+ cal.setTime(twinCalcStop.getDataDate());
|
|
|
+ }
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ Date firstDay = cal.getTime();
|
|
|
+
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ Date lastDay = cal.getTime();
|
|
|
+
|
|
|
+ // 结束日期为当前时间减去31小时
|
|
|
+ Calendar endCal = Calendar.getInstance();
|
|
|
+ endCal.add(Calendar.HOUR_OF_DAY, -31);
|
|
|
+ Date endDate = endCal.getTime();
|
|
|
+
|
|
|
+ // 取 endDate 和 lastDay 中较早的日期作为实际结束日期
|
|
|
+ Date actualEndDate = endDate.before(lastDay) ? endDate : lastDay;
|
|
|
+
|
|
|
+ // 设置查询条件
|
|
|
+ twinCalcStop.setWorkDay(firstDay);
|
|
|
+ Map<String, Object> params = twinCalcStop.getParams();
|
|
|
+ if (params == null) {
|
|
|
+ params = new HashMap<>();
|
|
|
+ twinCalcStop.setParams(params);
|
|
|
+ }
|
|
|
+ params.put("endDate", actualEndDate);
|
|
|
+
|
|
|
+ // 查询数据库中的实际数据
|
|
|
+ List<StopDetail> actualList = twinCalcStopMapper.selectTwinCalcStopDetailListByMonth(twinCalcStop);
|
|
|
+
|
|
|
+ // 获取所有涉及的设备
|
|
|
+ Set<Long> deviceIds = actualList.stream().map(StopDetail::getDeviceId).collect(Collectors.toSet());
|
|
|
+ Map<Long, String> deviceNames = new HashMap<>();
|
|
|
+ if (!deviceIds.isEmpty()) {
|
|
|
+ // 这里可以查询设备名称,简化处理,从实际数据中获取
|
|
|
+ for (StopDetail detail : actualList) {
|
|
|
+ deviceNames.put(detail.getDeviceId(), detail.getDeviceName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有任何设备数据,直接返回空列表
|
|
|
+ if (deviceIds.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整的日期列表(从当月第一天到实际结束日期)
|
|
|
+ List<Date> dateList = new ArrayList<>();
|
|
|
+ Calendar dateCal = Calendar.getInstance();
|
|
|
+ dateCal.setTime(firstDay);
|
|
|
+
|
|
|
+ while (!dateCal.getTime().after(actualEndDate)) {
|
|
|
+ dateList.add(dateCal.getTime());
|
|
|
+ dateCal.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整的结果列表,没有数据的日期补0
|
|
|
+ List<StopDetail> resultList = new ArrayList<>();
|
|
|
+ for (Long deviceId : deviceIds) {
|
|
|
+ String deviceName = deviceNames.get(deviceId);
|
|
|
+ Map<Date, StopDetail> dateMap = actualList.stream()
|
|
|
+ .filter(d -> d.getDeviceId().equals(deviceId))
|
|
|
+ .collect(Collectors.toMap(StopDetail::getDate, d -> d));
|
|
|
+
|
|
|
+ // 如果该设备在整个月内没有任何数据记录,则不加入结果列表
|
|
|
+ if (dateMap.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Date date : dateList) {
|
|
|
+ StopDetail detail;
|
|
|
+ if (dateMap.containsKey(date)) {
|
|
|
+ detail = dateMap.get(date);
|
|
|
+ } else {
|
|
|
+ detail = new StopDetail();
|
|
|
+ detail.setDeviceId(deviceId);
|
|
|
+ detail.setDeviceName(deviceName);
|
|
|
+ detail.setDate(date);
|
|
|
+ detail.setTimes(0);
|
|
|
+ }
|
|
|
+ resultList.add(detail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询日停机详情列表(按时统计)
|
|
|
+ *
|
|
|
+ * @param twinCalcStop 查询条件
|
|
|
+ * @return 停机详情列表
|
|
|
+ */
|
|
|
+ private List<StopDetail> selectDailyStopDetailList(TwinCalcStop twinCalcStop) {
|
|
|
+ // 如果没有指定日期,则使用workDay字段
|
|
|
+ Date queryDate = twinCalcStop.getDataDate() != null ? twinCalcStop.getDataDate() : twinCalcStop.getWorkDay();
|
|
|
+ if (queryDate == null) {
|
|
|
+ // 默认查询最近90天
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, -90);
|
|
|
+ queryDate = cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ twinCalcStop.setWorkDay(queryDate);
|
|
|
+
|
|
|
+ // 查询数据库中的实际数据
|
|
|
+ List<StopDetail> actualList = twinCalcStopMapper.selectTwinCalcStopDetailList(twinCalcStop);
|
|
|
+
|
|
|
+ // 获取所有涉及的设备
|
|
|
+ Set<Long> deviceIds = actualList.stream().map(StopDetail::getDeviceId).collect(Collectors.toSet());
|
|
|
+ Map<Long, String> deviceNames = new HashMap<>();
|
|
|
+ if (!deviceIds.isEmpty()) {
|
|
|
+ for (StopDetail detail : actualList) {
|
|
|
+ deviceNames.put(detail.getDeviceId(), detail.getDeviceName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有任何设备数据,直接返回空列表
|
|
|
+ if (deviceIds.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整的结果列表,没有数据的小时补0(从7点到第二天7点,共24小时)
|
|
|
+ List<StopDetail> resultList = new ArrayList<>();
|
|
|
+ for (Long deviceId : deviceIds) {
|
|
|
+ String deviceName = deviceNames.get(deviceId);
|
|
|
+ // 构建该设备的时间点映射
|
|
|
+ Map<String, StopDetail> hourMap = new HashMap<>();
|
|
|
+ actualList.stream()
|
|
|
+ .filter(d -> d.getDeviceId().equals(deviceId))
|
|
|
+ .forEach(d -> {
|
|
|
+ // 使用date和hour组合作为key
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(d.getDate());
|
|
|
+ String key = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, d.getDate()) + "-" + d.getHour();
|
|
|
+ hourMap.put(key, d);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 如果该设备在24小时内没有任何数据记录,则不加入结果列表
|
|
|
+ if (hourMap.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建从7点到第二天7点的24小时数据
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(queryDate);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 7); // 起始时间为当天7点
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+
|
|
|
+ for (int i = 0; i < 24; i++) {
|
|
|
+ // 构造key查找是否有数据
|
|
|
+ String key = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cal.getTime()) + "-" + cal.get(Calendar.HOUR_OF_DAY);
|
|
|
+
|
|
|
+ StopDetail detail;
|
|
|
+ if (hourMap.containsKey(key)) {
|
|
|
+ detail = hourMap.get(key);
|
|
|
+ } else {
|
|
|
+ detail = new StopDetail();
|
|
|
+ detail.setDeviceId(deviceId);
|
|
|
+ detail.setDeviceName(deviceName);
|
|
|
+ detail.setDate(cal.getTime());
|
|
|
+ detail.setHour(cal.get(Calendar.HOUR_OF_DAY));
|
|
|
+ detail.setTimes(0);
|
|
|
+ }
|
|
|
+ resultList.add(detail);
|
|
|
+
|
|
|
+ cal.add(Calendar.HOUR_OF_DAY, 1); // 增加1小时
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultList;
|
|
|
}
|
|
|
}
|