Forráskód Böngészése

获取灯控流量信息统计图

zhj 10 hónapja
szülő
commit
f9c0c8c6e7

+ 13 - 4
src/main/java/com/welampiot/controller/NetworkController.java

@@ -6,12 +6,10 @@ import com.welampiot.dto.LoopDTO;
 import com.welampiot.dto.NetInfoDTO;
 import com.welampiot.dto.NetworkDTO;
 import com.welampiot.dto.UserDTO;
-import com.welampiot.service.LoopService;
-import com.welampiot.service.NetInfoService;
-import com.welampiot.service.NetworkService;
-import com.welampiot.service.UserService;
+import com.welampiot.service.*;
 import com.welampiot.utils.ExcelUtil;
 import com.welampiot.utils.ToolUtils;
+import com.welampiot.vo.NetCardLogVO;
 import com.welampiot.vo.NetInfoDetailVO;
 import com.welampiot.vo.NetInfoVO;
 import com.welampiot.vo.NetworkVO;
@@ -48,6 +46,8 @@ public class NetworkController {
     private UserService userService;
     @Autowired
     private LoopService loopService;
+    @Autowired
+    private NetCardInfoLogService netCardInfoLogService;
 
     /**
      * 网关详情
@@ -565,4 +565,13 @@ public class NetworkController {
 
         return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
     }
+
+    @PostMapping("/simLogList")
+    public BaseResult<?> getMonthlyUsage(NetCardLogVO netCardLogVO) {
+        if (netCardLogVO.getLampId() == null || netCardLogVO.getLampId() == 0)
+            return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,netCardLogVO.getVersion());
+        if (netCardLogVO.getDataType() == null) netCardLogVO.setDataType(0);
+        NetCardLogVO netCardLogVO1 = netCardInfoLogService.getNetCardLogListByLogVO(netCardLogVO);
+        return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,netCardLogVO.getVersion(),netCardLogVO1);
+    }
 }

+ 11 - 0
src/main/java/com/welampiot/dao/NetCardInfoLogDao.java

@@ -0,0 +1,11 @@
+package com.welampiot.dao;
+
+import com.welampiot.dto.NetCardInfoLogDTO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface NetCardInfoLogDao {
+    List<NetCardInfoLogDTO> getNetCardLogListByLampIdAndMonth(@Param("lampId") Integer lampId, @Param("month") String month);
+    List<NetCardInfoLogDTO> getNetCardLogListByLampIdAndYear(@Param("lampId") Integer lampId, @Param("year") String year);
+}

+ 17 - 0
src/main/java/com/welampiot/dto/NetCardInfoLogDTO.java

@@ -0,0 +1,17 @@
+package com.welampiot.dto;
+
+import lombok.Data;
+
+@Data
+public class NetCardInfoLogDTO {
+    private Integer id;
+    private Integer netId;
+    /** 余额 (MB)*/
+    private String remainGprs;
+    /** 当月用量(MB) */
+    private Double useGprs;
+    private Double totalGprs;
+    /** 0时区 */
+    private String updateTime;
+    private String month;
+}

+ 9 - 0
src/main/java/com/welampiot/service/NetCardInfoLogService.java

@@ -0,0 +1,9 @@
+package com.welampiot.service;
+
+import com.welampiot.vo.NetCardLogVO;
+
+public interface NetCardInfoLogService {
+    NetCardLogVO getNetCardLogListByLogVO(NetCardLogVO netCardLogVO);
+    NetCardLogVO getNetCardLogListByLampIdAndMonth(Integer lampId, String year, String month);
+    NetCardLogVO getNetCardLogListByLampIdAndYear(Integer lampId, String year, String month);
+}

+ 120 - 0
src/main/java/com/welampiot/service/impl/NetCardInfoLogServiceImpl.java

@@ -0,0 +1,120 @@
+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];
+        if (netCardLogVO.getDataType() == 1) {
+            return getNetCardLogListByLampIdAndYear(netCardLogVO.getLampId(), year, month);
+        }
+        return getNetCardLogListByLampIdAndMonth(netCardLogVO.getLampId(), year, month);
+    }
+
+    @Override
+    public NetCardLogVO getNetCardLogListByLampIdAndMonth(Integer lampId, String year, String month) {
+        List<NetCardInfoLogDTO> logs = netCardInfoLogDao.getNetCardLogListByLampIdAndMonth(lampId, month);
+
+        // 按日期分组,取每个日期的最后一笔记录
+        Map<LocalDate, NetCardInfoLogDTO> dailyLogs = logs.stream()
+                .collect(Collectors.toMap(
+                        log -> parseLocalDate(log.getUpdateTime()),
+                        log -> log,
+                        (existing, replacement) -> existing.getUpdateTime().compareTo(replacement.getUpdateTime()) > 0 ? existing : replacement
+                ));
+
+        // 计算每天的流量使用量
+        List<Object> dataList = new ArrayList<>();
+        List<Object> dateList = new ArrayList<>();
+
+        LocalDate firstDayOfMonth = LocalDate.parse(year + "-" + 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<NetCardInfoLogDTO> logs = netCardInfoLogDao.getNetCardLogListByLampIdAndYear(lampId, year);
+
+        // 创建一个包含所有月份的列表
+        List<String> allMonths = new ArrayList<>();
+        for (int i = 1; i <= 12; i++) {
+            allMonths.add(String.format("%s-%02d", year, i));
+        }
+
+        // 按月份分组,取每个月的最后一笔记录
+        Map<String, NetCardInfoLogDTO> 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<Object> dataList = new ArrayList<>();
+        List<Object> dateList = new ArrayList<>();
+
+        DecimalFormat df = new DecimalFormat("0.##");
+        // 遍历所有月份,填充 dataList 和 dateList
+        for (String months : allMonths) {
+            NetCardInfoLogDTO log = monthlyLogs.get(month);
+            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);
+    }
+}

+ 8 - 0
src/main/java/com/welampiot/utils/ToolUtils.java

@@ -2158,6 +2158,14 @@ System.out.println(res);
         return sdf.format(new Date());
     }
 
+    /**
+     * 获取当前日期
+     */
+    public static String getNowDate() {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(new Date());
+    }
+
     public static int getSeq() {
         Random random = new Random();
         int min = 1;

+ 17 - 0
src/main/java/com/welampiot/vo/NetCardLogVO.java

@@ -0,0 +1,17 @@
+package com.welampiot.vo;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.util.List;
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class NetCardLogVO extends BaseVO {
+    private Integer lampId;
+    /** 数据类型(0 月份,1 年份) */
+    private Integer dataType;
+
+    List<Object> dataList;
+    List<Object> dateList;
+}

+ 37 - 0
src/main/resources/mapper/NetCardInfoLogMapper.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.welampiot.dao.NetCardInfoLogDao">
+
+    <select id="getNetCardLogListByLampIdAndMonth" resultType="NetCardInfoLogDTO">
+        select nl.id,nl.net_id as netId,nl.useGprs,nl.remainGprs,nl.totalGprs,nl.updatetime AS updateTime
+        from net_card_info_log nl
+        left join network n on n.id = nl.net_id
+        left join lampinfo l on l.networkid = n.id
+        where l.id = #{lampId}
+          and DATE_FORMAT(nl.updatetime, '%Y-%m') = #{month}
+        order by nl.updatetime
+    </select>
+
+    <select id="getNetCardLogListByLampIdAndYear" resultType="NetCardInfoLogDTO">
+        SELECT
+            DATE_FORMAT(nl.updatetime, '%Y-%m') AS month,
+            nl.useGprs,
+            nl.updatetime AS updateTime
+        FROM net_card_info_log nl
+        left join network n on n.id = nl.net_id
+        left join lampinfo l on l.networkid = n.id
+        WHERE l.id = #{lampId}
+          AND YEAR(nl.updatetime) = #{year}
+          AND (nl.updatetime, nl.net_id) IN (
+            SELECT MAX(a.updatetime) AS maxUpdateTime, a.net_id
+            FROM net_card_info_log a
+            left join network b on b.id = a.net_id
+            left join lampinfo c on c.networkid = b.id
+            WHERE c.id = #{lampId}
+              AND YEAR(a.updatetime) = #{year}
+            GROUP BY DATE_FORMAT(a.updatetime, '%Y-%m'), a.net_id
+        )
+        ORDER BY month
+    </select>
+
+</mapper>