whyclxw
2024-09-06 324e8c53f10026232926185d71373093a13e79d8
首页统计staticSocket
6个文件已添加
6个文件已修改
544 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/BatttestdataInfController.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/DevInfController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/BatttestdataInfMapper.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/DevInfMapper.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/db_alarm/AlarmParam.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/db_batt_testdata/BatttestdataInf.java 93 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/BatttestdataInfService.java 137 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/DevInfService.java 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/DevLineSocket.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/DevStaticSocket.java 124 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/BatttestdataInfMapper.xml 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/DevInfMapper.xml 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/BatttestdataInfController.java
New file
@@ -0,0 +1,36 @@
package com.whyc.controller;
import com.whyc.dto.Response;
import com.whyc.service.BattgroupBaojigroupService;
import com.whyc.service.BatttestdataInfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.util.Map;
@RestController
@Api(tags = "测试管理")
@RequestMapping("tinf")
public class BatttestdataInfController {
    @Autowired
    private BatttestdataInfService service;
    @ApiOperation(value = "测试统计本年/本月")
    @GetMapping("getDevTinfByYearMonth")
    public Map<String, Object> getDevTinfByYearMonth(@RequestParam Integer uid){
        return service.getDevTinfByYearMonth(uid);
    }
    @ApiOperation(value = "测试统计本周")
    @GetMapping("getDevTinfByWeek")
    public Map<String, Object> getDevTinfByWeek(@RequestParam Integer uid){
        return service.getDevTinfByWeek(uid);
    }
}
src/main/java/com/whyc/controller/DevInfController.java
@@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@Api(tags = "设备")
@@ -66,4 +67,10 @@
    public Response cancelContPl(@RequestBody List<Integer> devIds){
        return service.cancelContPl(devIds);
    }
    @ApiOperation(value = "测试设备统计")
    @GetMapping("getDevStaticByType")
    public Map<String,Object>  getDevStaticByType(){
        return service.getDevStaticByType(101);
    }
}
src/main/java/com/whyc/mapper/BatttestdataInfMapper.java
New file
@@ -0,0 +1,19 @@
package com.whyc.mapper;
import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BatttestdataInfMapper extends CustomMapper<BatttestdataInf>{
    //充放电一体机测试统计
    List<BatttestdataInf> getDevTinf(@Param("uid") int uid);
    //本年
    List<BatttestdataInf> getDevTinfByYear(@Param("uid")int userId);
    //本月
    List<BatttestdataInf> getDevTinfByMonth(@Param("uid")int userId);
    //近一周(mysql中不会算上今天)
    List<BatttestdataInf> getDevTinfByWeek(@Param("uid")int userId,@Param("type")int type);
}
src/main/java/com/whyc/mapper/DevInfMapper.java
@@ -18,4 +18,8 @@
    //获取左侧泪飙
    List<DevInf> getDevType(@Param("uid") Integer uid,@Param("devType") Integer devType);
    //设备分类总数统计
    List<DevInf> getDevStaticByType(@Param("uid") Integer userId);
}
src/main/java/com/whyc/pojo/db_alarm/AlarmParam.java
@@ -22,7 +22,7 @@
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tb_alarm_param")
@TableName(schema = "db_alarm",value ="tb_alarm_param")
@ApiModel(value="AlarmParam对象", description="设备告警参数表")
public class AlarmParam implements Serializable {
src/main/java/com/whyc/pojo/db_batt_testdata/BatttestdataInf.java
New file
@@ -0,0 +1,93 @@
package com.whyc.pojo.db_batt_testdata;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
 * <p>
 * 电池组历史数据总表
 * </p>
 *
 * @author lxw
 * @since 2024-09-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(schema = "db_batt_testdata",value ="tb_batttestdata_inf")
@ApiModel(value="BatttestdataInf对象", description="电池组历史数据总表")
public class BatttestdataInf implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "自增主键")
    @TableId(value = "num", type = IdType.AUTO)
    private Long num;
    @ApiModelProperty(value = "设备ID")
    private Integer devId;
    @ApiModelProperty(value = "电池组编号[0-组1 1-组2 ... 4-组4]")
    private Integer battIdx;
    @ApiModelProperty(value = "测试类型")
    private Integer testType;
    @ApiModelProperty(value = "测试记录次数")
    private Integer testRecordCount;
    @ApiModelProperty(value = "记录笔数")
    private Integer recordNum;
    @ApiModelProperty(value = "测试开始时间")
    private Date testStarttime;
    @ApiModelProperty(value = "测试时长")
    private Long testTimelong;
    @ApiModelProperty(value = "记录时间")
    private Date recordTime;
    @ApiModelProperty(value = "停止原因")
    private Integer testStoptype;
    @ApiModelProperty(value = "组端电压")
    private Float groupVol;
    @ApiModelProperty(value = "测试电流")
    private Float testCurr;
    @ApiModelProperty(value = "测试容量")
    private Float testCap;
    @ApiModelProperty(value = "最大单体电压")
    private Float maxMonvol;
    @ApiModelProperty(value = "最大单体电压编号")
    private Integer maxMonvolnum;
    @ApiModelProperty(value = "最小单体电压")
    private Float minMonvol;
    @ApiModelProperty(value = "最小单体电压编号")
    private Integer minMonvolnum;
    @ApiModelProperty(value = "设备类型")
    @TableField(exist = false)
    private Integer devType;
    @ApiModelProperty(value = "天数")
    @TableField(exist = false)
    private String weekDay;
}
src/main/java/com/whyc/service/BatttestdataInfService.java
New file
@@ -0,0 +1,137 @@
package com.whyc.service;
import com.whyc.dto.Response;
import com.whyc.mapper.BatttestdataInfMapper;
import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
import com.whyc.pojo.db_lithium_ram_db.DevInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class BatttestdataInfService {
    @Autowired(required = false)
    private BatttestdataInfMapper mapper;
    //充放电一体机测试统计
    public Map<String, Object> getDevTinfByYearMonth(int userId) {
        Map<String,Object> map=new HashMap<>();
        //本年
        List<BatttestdataInf> listYear=mapper.getDevTinfByYear(userId);
        Map<String,Object> a200Map=new HashMap<>();
        Map<String,Object> actmMap=new HashMap<>();
        Map<Integer,Integer> a200YearMap=new HashMap<>();
        a200YearMap.put(2,0);
        a200YearMap.put(3,0);
        Map<Integer,Integer> actmYearMap=new HashMap<>();
        actmYearMap.put(2,0);
        actmYearMap.put(3,0);
        //本年
        Map<Integer, List<BatttestdataInf>> typeMapY = listYear.stream().collect(Collectors.groupingBy(BatttestdataInf::getDevType));
        for (Integer type : typeMapY.keySet()) {
            List<BatttestdataInf> list=typeMapY.get(type);
            Map<Integer, List<BatttestdataInf>> testMapY = list.stream().collect(Collectors.groupingBy(BatttestdataInf::getTestType));
            for (Integer test : testMapY.keySet()) {
                if(type==1){
                    a200YearMap.put(test, testMapY.get(test).size());
                }
                if(type==2){
                    actmYearMap.put(test, testMapY.get(test).size());
                }
            }
        }
        a200Map.put("a200YearMap",a200YearMap);
        actmMap.put("actmYearMap",actmYearMap);
        //本月
        Map<Integer,Integer> a200MonthMap=new HashMap<>();
        a200MonthMap.put(2,0);
        a200MonthMap.put(3,0);
        Map<Integer,Integer> actmMonthMap=new HashMap<>();
        actmMonthMap.put(2,0);
        actmMonthMap.put(3,0);
        List<BatttestdataInf> listMonth=mapper.getDevTinfByMonth(userId);
        Map<Integer, List<BatttestdataInf>> typeMapM = listMonth.stream().collect(Collectors.groupingBy(BatttestdataInf::getDevType));
        for (Integer type : typeMapM.keySet()) {
            List<BatttestdataInf> list=typeMapM.get(type);
            Map<Integer, List<BatttestdataInf>> testMapM = list.stream().collect(Collectors.groupingBy(BatttestdataInf::getTestType));
            for (Integer test : testMapM.keySet()) {
                if(type==1){
                    a200MonthMap.put(test, testMapM.get(test).size());
                }
                if(type==2){
                    actmMonthMap.put(test, testMapM.get(test).size());
                }
            }
        }
        a200Map.put("a200MonthMap",a200MonthMap);
        actmMap.put("actmMonthMap",actmMonthMap);
        map.put("a200Map",a200Map);
        map.put("actmMap",actmMap);
        return map;
    }
    //近一周电池测试趋势统计(从当前时间开始)
    public Map<String, Object> getDevTinfByWeek(int userId) {
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> a200dataMap=new HashMap<>();
        Map<String,Object> actmdataMap=new HashMap<>();
        Map<Integer,Integer> a200weekDateMap=new HashMap<>();
        a200weekDateMap.put(2,0);
        a200weekDateMap.put(3,0);
        Map<Integer,Integer> actmweekDataMap=new HashMap<>();
        actmweekDataMap.put(2,0);
        actmweekDataMap.put(3,0);
        // 当前日期
        LocalDate today = LocalDate.now();
        for(int i=0;i<7;i++){
            LocalDate resultDate = today.minusDays(i);
            a200dataMap.put(resultDate.toString(),a200weekDateMap);
            actmdataMap.put(resultDate.toString(),actmweekDataMap);
        }
        //近一周a200
        Map<Integer,Integer> a200weekMap=new HashMap<>();
        a200weekMap.put(2,0);
        a200weekMap.put(3,0);
        List<BatttestdataInf> listW1=mapper.getDevTinfByWeek(userId,1);
        Map<String, List<BatttestdataInf>> typeMapW1 = listW1.stream().collect(Collectors.groupingBy(BatttestdataInf::getWeekDay));
        for (String day : typeMapW1.keySet()) {
            List<BatttestdataInf> list1=typeMapW1.get(day);
            Map<Integer, List<BatttestdataInf>> testMapM1 = list1.stream().collect(Collectors.groupingBy(BatttestdataInf::getTestType));
            for (Integer test : testMapM1.keySet()) {
                a200weekMap.put(test, testMapM1.get(test).size());
            }
            a200dataMap.put(day,a200weekMap);
        }
        //近一周actm
        Map<Integer,Integer> actmweekMap=new HashMap<>();
        actmweekMap.put(2,0);
        actmweekMap.put(3,0);
        List<BatttestdataInf> listW2=mapper.getDevTinfByWeek(userId,2);
        Map<String, List<BatttestdataInf>> typeMapW2= listW2.stream().collect(Collectors.groupingBy(BatttestdataInf::getWeekDay));
        for (String day : typeMapW2.keySet()) {
            List<BatttestdataInf> list2=typeMapW2.get(day);
            Map<Integer, List<BatttestdataInf>> testMapM2 = list2.stream().collect(Collectors.groupingBy(BatttestdataInf::getTestType));
            for (Integer test : testMapM2.keySet()) {
                actmweekMap.put(test, testMapM2.get(test).size());
            }
            actmdataMap.put(day,actmweekMap);
        }
        map.put("a200",a200dataMap);
        map.put("actm",actmdataMap);
        return map;
    }
}
src/main/java/com/whyc/service/DevInfService.java
@@ -189,4 +189,32 @@
        }
        return new Response().set(1,true,"强制移除批量设备");
    }
    //设备分类总数统计
    public Map<String,Object> getDevStaticByType(int userId) {
        Map<String,Object> map=new HashMap<>();
        Map<Integer,Integer> staticTypeMap=new HashMap<>();
        staticTypeMap.put(1,0);
        staticTypeMap.put(2,0);
        Map<Integer,Integer> staticStateMap=new HashMap<>();
        staticStateMap.put(0,0);
        staticStateMap.put(1,0);
        List<DevInf> list=mapper.getDevStaticByType(userId);
        Map<Integer, List<DevInf>> typeMap = list.stream().collect(Collectors.groupingBy(DevInf::getDevType));
        for (Integer type : typeMap.keySet()) {
            staticTypeMap.put(type, typeMap.get(type).size());
        }
        Map<Integer, List<DevInf>> stateMap = list.stream().collect(Collectors.groupingBy(DevInf::getDevOnline));
        for (Integer state : stateMap.keySet()) {
            staticStateMap.put(state, stateMap.get(state).size());
        }
        map.put("dinf",list!=null?list:null);
        map.put("type",staticTypeMap);
        map.put("state",staticStateMap);
        return map;
    }
}
src/main/java/com/whyc/webSocket/DevLineSocket.java
@@ -15,7 +15,7 @@
import java.util.Map;
/**
 * 账号重复登录检查
 * 左侧列表
 */
@Component
@ServerEndpoint(value = "/lineSocket",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
src/main/java/com/whyc/webSocket/DevStaticSocket.java
New file
@@ -0,0 +1,124 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.dto.ResultA200Dto;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.db_lithium_ram_db.DevInf;
import com.whyc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
 * 左侧列表
 */
@Component
@ServerEndpoint(value = "/staticSocket",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class DevStaticSocket{
    private Session session;
    private Thread thread;
    private static DevInfService dinfService;
    private static BatttestdataInfService tinfService;
    private HttpSession httpSession;
    @Autowired
    public void setDevInfService(DevInfService dinfService) {
        DevStaticSocket.dinfService = dinfService;
    }
    @Autowired
    public void setBatttestdataInfService(BatttestdataInfService tinfService) {
        DevStaticSocket.tinfService = tinfService;
    }
    @OnOpen
    public void onOpen(Session session, EndpointConfig config){
        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        //UserInf user = (UserInf) this.httpSession.getAttribute("user");
        //final int userId = user.getUid();
        final int userId = 101;
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    while (!currentThread().isInterrupted()) {
                        Response res = getDevStatic(userId);
                        session.getBasicRemote().sendObject(res);
                        sleep(4000);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
    }
    //获取统计信息
    private Response getDevStatic(int userId) {
        Map<String,Object> map=new HashMap<>();
        try {
            ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
            CountDownLatch latch = new CountDownLatch(3);
            //充放电一体机测试统计/锂电均衡仪测试统计
            poolExecutor.execute(() -> {
                Map<String,Object> devTinfMap=tinfService.getDevTinfByYearMonth(userId);
                map.put("devTinf",devTinfMap);
                latch.countDown();
            });
            //设备分类总数统计/设备状态统计/地图信息
            poolExecutor.execute(() -> {
                Map<String,Object> devSaticMap=dinfService.getDevStaticByType(userId);
                map.put("devSatic",devSaticMap);
                latch.countDown();
            });
            //近一周电池测试趋势统计(从当前时间开始)
            poolExecutor.execute(() -> {
                Map<String,Object> devTestByWeekMap=tinfService.getDevTinfByWeek(userId);
                map.put("devTestByWeek",devTestByWeekMap);
                latch.countDown();
            });
            latch.await(10, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Response().setII(1,true,map,"");
    }
    @OnClose
    public void onClose(CloseReason closeReason) throws IOException {
        System.err.println("closeReason = " + closeReason);
        if(session.isOpen()){
            session.close();
        }
    }
    @OnError
    public void onError(Throwable error) throws IOException {
        error.printStackTrace();
        thread.isInterrupted();
        if(session.isOpen()){
            session.close();
        }
    }
}
src/main/resources/mapper/BatttestdataInfMapper.xml
New file
@@ -0,0 +1,78 @@
<?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.whyc.mapper.BatttestdataInfMapper">
    <resultMap id="staticTinf" type="batttestdataInf">
        <id property="num" column="num"></id>
        <result property="devId" column="dev_id"></result>
        <result property="testType" column="test_type"></result>
        <result property="devType" column="dev_type"></result>
    </resultMap>
    <select id="getDevTinf" resultMap="staticTinf">
        select num,dev_id,test_type,floor(dev_id/100000000) as dev_type from db_batt_testdata.tb_batttestdata_inf
        <where>
            dev_id in (
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    and uid=#{uid}
                </if>
            </where>
            )
        </where>
    </select>
    <select id="getDevTinfByYear" resultMap="staticTinf">
        select num,dev_id,test_type,floor(dev_id/100000000) as dev_type from db_batt_testdata.tb_batttestdata_inf
        <where>
            YEAR(test_starttime)=YEAR(NOW())
            and dev_id in (
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    and uid=#{uid}
                </if>
            </where>
            )
        </where>
    </select>
    <select id="getDevTinfByMonth" resultMap="staticTinf">
        select num,dev_id,test_type,floor(dev_id/100000000) as dev_type from db_batt_testdata.tb_batttestdata_inf
        <where>
            DATE_FORMAT(test_starttime,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
            and dev_id in (
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    and uid=#{uid}
                </if>
            </where>
            )
        </where>
    </select>
    <resultMap id="staticTinfWeek" type="batttestdataInf">
        <id property="num" column="num"></id>
        <result property="devId" column="dev_id"></result>
        <result property="testType" column="test_type"></result>
        <result property="devType" column="dev_type"></result>
        <result property="weekDay" column="week_day"></result>
    </resultMap>
    <select id="getDevTinfByWeek"  resultMap="staticTinfWeek">
        select num,dev_id,test_type,floor(dev_id/100000000) as dev_type,DATE_FORMAT(test_starttime, '%Y-%m-%d') AS week_day
        from db_batt_testdata.tb_batttestdata_inf
        <where>
            DATE_SUB(CURDATE(), INTERVAL 6 DAY) &lt;= DATE(test_starttime)
            and floor(dev_id/100000000)=#{type}
            and dev_id in (
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    and uid=#{uid}
                </if>
            </where>
            )
        </where>
    </select>
</mapper>
src/main/resources/mapper/DevInfMapper.xml
@@ -61,5 +61,19 @@
            )
        </where>
    </select>
    <select id="getDevStaticByType" resultType="com.whyc.pojo.db_lithium_ram_db.DevInf">
        select * from db_lithium_ram_db.tb_dev_inf
        <where>
            dev_id in (
            SELECT distinct dev_id from db_user.tb_battgroup_baojigroup,db_user.tb_battgroup_usr
            <where>
                tb_battgroup_baojigroup.baoji_group_id=tb_battgroup_usr.baoji_group_id
                <if test="uid!=null">
                    and uid=#{uid}
                </if>
            </where>
            )
        </where>
    </select>
</mapper>