package com.welampiot.service.impl; import com.welampiot.dao.NetCardInfoLogDao; import com.welampiot.dto.NetCardInfoLogDTO; import com.welampiot.service.NetCardInfoLogService; import com.welampiot.utils.ToolUtils; import com.welampiot.vo.NetCardLogVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service public class NetCardInfoLogServiceImpl implements NetCardInfoLogService { @Autowired private NetCardInfoLogDao netCardInfoLogDao; @Override public NetCardLogVO getNetCardLogListByLogVO(NetCardLogVO netCardLogVO) { String nowDate = ToolUtils.getNowDate(); String[] split = nowDate.split("-"); String year = split[0]; String month = split[1]; String monthStr = year + "-" + month; if (netCardLogVO.getDataType() == 1) { return getNetCardLogListByLampIdAndYear(netCardLogVO.getLampId(), year, month); } return getNetCardLogListByLampIdAndMonth(netCardLogVO.getLampId(), year, monthStr); } @Override public NetCardLogVO getNetCardLogListByLampIdAndMonth(Integer lampId, String year, String month) { List logs = netCardInfoLogDao.getNetCardLogListByLampIdAndMonth(lampId, month); // 按日期分组,取每个日期的最后一笔记录 Map dailyLogs = logs.stream() .collect(Collectors.toMap( log -> parseLocalDate(log.getUpdateTime()), log -> log, (existing, replacement) -> existing.getUpdateTime().compareTo(replacement.getUpdateTime()) > 0 ? existing : replacement )); // 计算每天的流量使用量 List dataList = new ArrayList<>(); List dateList = new ArrayList<>(); LocalDate firstDayOfMonth = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDate lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.lengthOfMonth()); DecimalFormat df = new DecimalFormat("0.##"); for (LocalDate date = firstDayOfMonth; !date.isAfter(lastDayOfMonth); date = date.plusDays(1)) { NetCardInfoLogDTO currentLog = dailyLogs.get(date); NetCardInfoLogDTO previousLog = dailyLogs.get(date.minusDays(1)); double usage = 0.0; if (currentLog != null) { if (previousLog == null) { usage = currentLog.getUseGprs(); // 第一天的流量 } else { usage = currentLog.getUseGprs() - previousLog.getUseGprs(); } } dataList.add(df.format(usage)); dateList.add(date.toString()); } NetCardLogVO netCardLogVO1 = new NetCardLogVO(); netCardLogVO1.setDateList(dateList); netCardLogVO1.setDataList(dataList); return netCardLogVO1; } @Override public NetCardLogVO getNetCardLogListByLampIdAndYear(Integer lampId, String year, String month) { List logs = netCardInfoLogDao.getNetCardLogListByLampIdAndYear(lampId, year); // 创建一个包含所有月份的列表 List allMonths = new ArrayList<>(); for (int i = 1; i <= 12; i++) { allMonths.add(String.format("%s-%02d", year, i)); } // 将 logs 转换为 Map,键为月份,值为 NetCardInfoLogDTO Map monthlyLogs = logs.stream() .collect(Collectors.toMap( log -> log.getUpdateTime().substring(0, 7), // 取前7个字符,即 "YYYY-MM" log -> log, (existing, replacement) -> existing.getUpdateTime().compareTo(replacement.getUpdateTime()) > 0 ? existing : replacement )); // 初始化 dataList 和 dateList List dataList = new ArrayList<>(); List dateList = new ArrayList<>(); DecimalFormat df = new DecimalFormat("0.##"); // 遍历所有月份,填充 dataList 和 dateList for (String months : allMonths) { NetCardInfoLogDTO log = monthlyLogs.get(months); double usage = log != null ? log.getUseGprs() : 0.0; dataList.add(df.format(usage)); dateList.add(months); } NetCardLogVO netCardLogVO = new NetCardLogVO(); netCardLogVO.setDateList(dateList); netCardLogVO.setDataList(dataList); return netCardLogVO; } private LocalDate parseLocalDate(String dateStr) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return LocalDate.parse(dateStr, formatter); } }