| | |
| | | package com.whyc.service; |
| | | |
| | | import com.whyc.dto.NameValueDto; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.factory.ThreadPoolExecutorFactory; |
| | | import com.whyc.pojo.db_batt_testdata.BatttestdataInf; |
| | | import com.whyc.pojo.db_station.BattInf; |
| | | import com.whyc.pojo.db_station.PowerInf; |
| | | import com.whyc.pojo.db_station.StationInf; |
| | | import com.whyc.util.DateUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.concurrent.CountDownLatch; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalDate; |
| | | import java.time.ZoneId; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.whyc.util.DateUtil.convertToLocalDateTime; |
| | | |
| | | |
| | | @Service |
| | | public class LeaderHomeService { |
| | |
| | | private StationInfService stationInfService; |
| | | |
| | | @Autowired |
| | | private BatttestdataInfService bdTestDataInfService; |
| | | private BatttestdataInfService battTestDataInfService; |
| | | |
| | | public Response getAll() { |
| | | public Response getAll(Integer userId) { |
| | | Response response = new Response(); |
| | | Map<String,Object> map = new HashMap<>(); |
| | | |
| | | CountDownLatch latch = new CountDownLatch(1); |
| | | CountDownLatch latch = new CountDownLatch(9); |
| | | ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); |
| | | |
| | | //站点信息统计,按站点类型和站点电压 TODO 待李军明确站点类型是什么字段,目前没有 |
| | | //站点信息统计, |
| | | poolExecutor.execute(() -> { |
| | | List<StationInf> stationInfList =stationInfService.getAllWithFields("station_type"); |
| | | Map<String, List<StationInf>> typeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType)); |
| | | stationInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | |
| | | //电源信息(品牌及电压)统计 |
| | | poolExecutor.execute(() -> { |
| | | powerInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //蓄电池信息(品牌及电压)统计 |
| | | poolExecutor.execute(() -> { |
| | | batteryInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //测试数据信息统计 |
| | | poolExecutor.execute(() -> { |
| | | testDataInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //TODO 设备数量信息统计 |
| | | poolExecutor.execute(() -> { |
| | | //deviceInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //TODO 实时告警信息统计 |
| | | poolExecutor.execute(() -> { |
| | | //alarmRealtimeStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //TODO 不同时间同一品牌蓄电池性能统计 |
| | | poolExecutor.execute(() -> { |
| | | //performanceInfoStatistics(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //TODO 同一时间不同品牌蓄电池性能统计 |
| | | poolExecutor.execute(() -> { |
| | | //performanceInfoStatistics2(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | //TODO 同一时间同一品牌蓄电池性能统计 |
| | | poolExecutor.execute(() -> { |
| | | //performanceInfoStatistics3(userId, map); |
| | | latch.countDown(); |
| | | }); |
| | | |
| | | return response; |
| | | } |
| | | |
| | | private void testDataInfoStatistics(Integer userId, Map<String, Object> map) { |
| | | List<BatttestdataInf> testInfList = battTestDataInfService.getListByUserId(userId); |
| | | //过滤出本月的 |
| | | LocalDateTime startOfMonth = DateUtil.getStartOfMonth(); |
| | | List<BatttestdataInf> testInfListOfMonth = testInfList.stream() |
| | | .filter(testData -> { |
| | | LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime()); |
| | | return !startTime.isBefore(startOfMonth); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //统计本月各种充放电类型及数量 |
| | | List<NameValueDto> testInfOfMonth = getChargeAndDischargeStatistics(testInfListOfMonth); |
| | | map.put("testInf_month",testInfOfMonth); |
| | | |
| | | //过滤出本季度的 |
| | | LocalDateTime startOfQuarter = DateUtil.getStartOfQuarter(); |
| | | List<BatttestdataInf> testInfListOfQuarter = testInfList.stream() |
| | | .filter(testData -> { |
| | | LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime()); |
| | | return !startTime.isBefore(startOfQuarter); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //统计本季度各种充放电类型及数量 |
| | | List<NameValueDto> testInfOfQuarter = getChargeAndDischargeStatistics(testInfListOfQuarter); |
| | | map.put("testInf_quarter",testInfOfQuarter); |
| | | |
| | | //过滤出本年的 |
| | | LocalDateTime startOfYear = DateUtil.getStartOfYear(); |
| | | List<BatttestdataInf> testInfListOfYear = testInfList.stream() |
| | | .filter(testData -> { |
| | | LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime()); |
| | | return !startTime.isBefore(startOfYear); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //统计本年各种充放电类型及数量 |
| | | List<NameValueDto> testInfOfYear = getChargeAndDischargeStatistics(testInfListOfYear); |
| | | map.put("testInf_year",testInfOfYear); |
| | | } |
| | | |
| | | /** |
| | | * 统计各种充放电类型及数量 |
| | | * @param testInfListOfMonth 被统计的列表 |
| | | * @return |
| | | */ |
| | | private List<NameValueDto> getChargeAndDischargeStatistics(List<BatttestdataInf> testInfListOfMonth) { |
| | | List<NameValueDto> testInfOfMonth = new ArrayList<>(); |
| | | int chargeHr = 0; |
| | | int chargeJc = 0; |
| | | int dischargeHr = 0; |
| | | int dischargeJc = 0; |
| | | for (int i = 0; i < testInfListOfMonth.size(); i++) { |
| | | BatttestdataInf testData = testInfListOfMonth.get(i); |
| | | if (testData.getTestType() == 3) { // 测试类型为放电 |
| | | if (testData.getTestStarttype() == 3) { //核容放电 |
| | | dischargeHr++; |
| | | } else if(testData.getTestStarttype() != 4){ //监测放电 |
| | | dischargeJc++; |
| | | } |
| | | } else if (testData.getTestType() == 2) { // 测试类型为充电 |
| | | if (testData.getTestStarttype() == 3) { //核容充电 |
| | | chargeHr++; |
| | | } else { //监测充电 |
| | | chargeJc++; |
| | | } |
| | | } |
| | | } |
| | | //核容放电,核容充电,监测放电,检测充电 |
| | | testInfOfMonth.add(new NameValueDto("核容放电",String.valueOf(dischargeHr))); |
| | | testInfOfMonth.add(new NameValueDto("核容充电",String.valueOf(chargeHr))); |
| | | testInfOfMonth.add(new NameValueDto("监测放电",String.valueOf(dischargeJc))); |
| | | testInfOfMonth.add(new NameValueDto("监测充电",String.valueOf(chargeJc))); |
| | | return testInfOfMonth; |
| | | } |
| | | |
| | | private void batteryInfoStatistics(Integer userId, Map<String, Object> map) { |
| | | List<BattInf> battInfList = battInfService.getListByUserId(userId); |
| | | Map<String, List<BattInf>> brandMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getProduct)); |
| | | Map<Float, List<BattInf>> volMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getMonvolstd)); |
| | | |
| | | List<NameValueDto> brandList = new ArrayList<>(); |
| | | brandMap.forEach((k,v)->{ |
| | | NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size())); |
| | | brandList.add(brandDto); |
| | | }); |
| | | map.put("battGroupInfo_brand",brandList); |
| | | |
| | | List<NameValueDto> volList = new ArrayList<>(); |
| | | volMap.forEach((k,v)->{ |
| | | NameValueDto volDto = new NameValueDto(String.valueOf(k), String.valueOf(v.size())); |
| | | volList.add(volDto); |
| | | }); |
| | | map.put("battGroupInfo_vol",volList); |
| | | } |
| | | |
| | | private void powerInfoStatistics(Integer userId, Map<String, Object> map) { |
| | | List<PowerInf> powerInfList = powerInfService.getListByUserId(userId); |
| | | Map<String, List<PowerInf>> brandMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getCompany)); |
| | | Map<Integer, List<PowerInf>> typeMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerType)); |
| | | |
| | | List<NameValueDto> brandList = new ArrayList<>(); |
| | | brandMap.forEach((k,v)->{ |
| | | NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size())); |
| | | brandList.add(brandDto); |
| | | }); |
| | | map.put("powerInfo_brand",brandList); |
| | | |
| | | List<NameValueDto> typeList = new ArrayList<>(); |
| | | //电源类型:1:直流,2:通讯,3:配网 |
| | | typeMap.forEach((k,v)->{ |
| | | NameValueDto typeDto = new NameValueDto(); |
| | | switch (k){ |
| | | case 1: |
| | | typeDto.setName("直流"); |
| | | break; |
| | | case 2: |
| | | typeDto.setName("通讯"); |
| | | break; |
| | | case 3: |
| | | typeDto.setName("配网"); |
| | | break; |
| | | } |
| | | typeDto.setValue(String.valueOf(v.size())); |
| | | typeList.add(typeDto); |
| | | }); |
| | | map.put("powerInfo_type",typeList); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 按站点类型和站点电压 |
| | | * 站点类型:节点/非节点 |
| | | */ |
| | | private void stationInfoStatistics(Integer userId, Map<String, Object> map) { |
| | | List<StationInf> stationInfList =stationInfService.getListByUserId(userId); |
| | | Map<Integer, List<StationInf>> nodeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation)); |
| | | Map<String, List<StationInf>> volMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType)); |
| | | |
| | | List<NameValueDto> stationTypeList = new ArrayList<>(); |
| | | List<NameValueDto> stationVolList = new ArrayList<>(); |
| | | nodeMap.forEach((k,v)->{ |
| | | NameValueDto stationTypeDto = new NameValueDto(k == 0 ? "非节点" : "节点", String.valueOf(v.size())); |
| | | stationTypeList.add(stationTypeDto); |
| | | }); |
| | | volMap.forEach((k,v)->{ |
| | | NameValueDto stationVolDto = new NameValueDto(k, String.valueOf(v.size())); |
| | | stationVolList.add(stationVolDto); |
| | | }); |
| | | |
| | | map.put("stationInfo_stationType",stationTypeList); |
| | | map.put("stationInfo_stationVol",stationVolList); |
| | | } |
| | | |
| | | } |