Browse Source

客户抄表率

lin 4 years ago
parent
commit
4632675bc8

+ 108 - 0
smart-city-platform/src/main/java/com/bz/smart_city/commom/util/TreeUtil.java

@@ -6,10 +6,13 @@ import com.bz.smart_city.dto.*;
 import com.bz.smart_city.dto.assistant.InstallPlanDataDTO;
 import com.bz.smart_city.entity.MeterRecordStat;
 import com.bz.smart_city.entity.Permission;
+import io.swagger.models.auth.In;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 
+import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -258,4 +261,109 @@ public class TreeUtil {
             return null;
         }
     }
+
+    public static List<CustomerRateDto> getCustomerRateTree(List<CustomerRateDto> list, Integer id, Integer leve) {
+        List<CustomerRateDto> temList = newArrayList();
+        if (list != null) {
+            for (CustomerRateDto customerRateDto : list) {
+                if (id.equals(customerRateDto.getParentId())) {
+                    List<CustomerRateDto> chidren = getCustomerRateTree(list, customerRateDto.getCustomerId(), ++leve);
+                    CustomerRateDto tempCustomerRateDto = new CustomerRateDto();
+                    BeanUtils.copyProperties(customerRateDto, tempCustomerRateDto);
+                    tempCustomerRateDto.setChildren(chidren);
+                    temList.add(tempCustomerRateDto);
+                    leve--;
+                }
+
+            }
+        }
+        if(temList.size() > 0){
+            return temList;
+        }else {
+            return null;
+        }
+    }
+
+    //递归后续遍历多叉树
+    public static void  postorderTraversalCustomerRate(List<CustomerRateDto> list){
+        if (list != null && list.size()>0) {
+            for (CustomerRateDto customerRateDto : list) {
+                postorderTraversalCustomerRate(customerRateDto.getChildren());
+
+                if(customerRateDto.getChildren() != null && customerRateDto.getChildren().size() > 0){
+                    for (CustomerRateDto child : customerRateDto.getChildren()) {
+                        if(child.getList() != null && child.getList().size() > 0){
+                            for (WaterReadRateListDto waterReadRateListDto : child.getList()) {
+                                if(customerRateDto.getMap().containsKey(waterReadRateListDto.getChannelId())){
+                                    List<WaterReadRateListDto> tempList = customerRateDto.getMap().get(waterReadRateListDto.getChannelId());
+                                    if (tempList != null) {
+                                        tempList.add(waterReadRateListDto);
+                                    }else {
+                                        customerRateDto.getMap().get(waterReadRateListDto.getChannelId()).addAll(newArrayList(waterReadRateListDto));
+                                    }
+                                }else {
+                                    customerRateDto.getMap().put(waterReadRateListDto.getChannelId(),newArrayList(waterReadRateListDto));
+                                }
+                            }
+                        }
+
+
+                        customerRateDto.getMap().putAll(child.getMap());
+
+                    }
+                }
+
+                if (customerRateDto.getMap() != null && customerRateDto.getMap().size() > 0) {
+                    if (customerRateDto.getList() != null && customerRateDto.getList().size() > 0) {
+                        for (WaterReadRateListDto waterReadRateListDto : customerRateDto.getList()) {
+                            if(customerRateDto.getMap().containsKey(waterReadRateListDto.getChannelId())){
+                                List<WaterReadRateListDto> tempList = customerRateDto.getMap().get(waterReadRateListDto.getChannelId());
+                                if (tempList != null) {
+                                    tempList.add(waterReadRateListDto);
+                                }else {
+                                    customerRateDto.getMap().get(waterReadRateListDto.getChannelId()).addAll(newArrayList(waterReadRateListDto));
+                                }
+                            }else {
+                                customerRateDto.getMap().put(waterReadRateListDto.getChannelId(),newArrayList(waterReadRateListDto));
+                            }
+                        }
+                    }
+                }
+
+
+            }
+        }
+    }
+
+    public static void  postorderTraversalCustomerRateCalculate(List<CustomerRateDto> list){
+        if (list != null && list.size()>0) {
+            for (CustomerRateDto customerRateDto : list) {
+                postorderTraversalCustomerRateCalculate(customerRateDto.getChildren());
+                if (customerRateDto.getMap() != null && customerRateDto.getMap().size() > 0) {
+                    List<WaterReadRateListDto> tempList = newArrayList();
+                    for (Map.Entry<Integer, List<WaterReadRateListDto>> m : customerRateDto.getMap().entrySet()) {
+                        WaterReadRateListDto temp = new WaterReadRateListDto();
+                        if (m.getValue() != null && m.getValue().size() > 0) {
+                            for (WaterReadRateListDto waterReadRateListDto : m.getValue()) {
+
+                                temp.setDeviceCount(temp.getDeviceCount() + waterReadRateListDto.getDeviceCount());
+                                temp.setReadTimes(temp.getReadTimes() + waterReadRateListDto.getReadTimes());
+                                temp.setRealReadTimes(temp.getRealReadTimes() + waterReadRateListDto.getRealReadTimes());
+                                temp.setUnReadTimes(temp.getUnReadTimes() + waterReadRateListDto.getUnReadTimes());
+
+                            }
+                            temp.setChannelId(m.getValue().get(0).getChannelId());
+                            temp.setChannelName(m.getValue().get(0).getChannelName());
+                            temp.setCustomerId(m.getValue().get(0).getCustomerId());
+                            temp.setCustomerName(m.getValue().get(0).getCustomerName());
+                            temp.setReadRate(new BigDecimal((float)temp.getRealReadTimes()/temp.getDeviceCount()*100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
+                        }
+                        tempList.add(temp);
+                    }
+                    customerRateDto.setList(tempList);
+                }
+
+            }
+        }
+    }
 }

+ 1 - 1
smart-city-platform/src/main/java/com/bz/smart_city/controller/water/WaterReadController.java

@@ -132,7 +132,7 @@ public class WaterReadController {
     @ResponseBody
     @GetMapping("getListByCustomer")
     @ApiOperation(value = "根据客户获取抄表率列表数据")
-    public AjaxMessage<Pagination<CustomerRateDto>> getListByCustomer(
+    public AjaxMessage<List<CustomerRateDto>> getListByCustomer(
             @ApiParam(value = "查询范围,2:昨天,7:近7日,15:近15日,99:上月", required = false) @RequestParam(required = false) Integer period,
             @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Integer channelId,
             @ApiParam(value = "客户id", required = false) @RequestParam(required = false) Integer customerId,

+ 7 - 1
smart-city-platform/src/main/java/com/bz/smart_city/dto/CustomerRateDto.java

@@ -1,14 +1,20 @@
 package com.bz.smart_city.dto;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import lombok.Data;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 @Data
 public class CustomerRateDto {
 
     private Integer customerId;
+    private Integer parentId;
     private String customerName;
-    private Integer childrenNum;
     private List<WaterReadRateListDto> list;
+    private List<CustomerRateDto> children;
+    @JsonIgnore
+    private Map<Integer,List<WaterReadRateListDto>> map = new HashMap<>();;
 }

+ 1 - 1
smart-city-platform/src/main/java/com/bz/smart_city/service/StatMeterReadRateByBuildingService.java

@@ -25,7 +25,7 @@ public interface StatMeterReadRateByBuildingService{
 
     void getRateExcelByDeviceType(Integer period, HttpServletResponse httpServletResponse);
 
-    Pagination<CustomerRateDto> getListByCustomer(Integer period, Integer channelId, Integer customerId, String sortColumn, String sortOrder, int pageNum, int pageSize);
+    List<CustomerRateDto> getListByCustomer(Integer period, Integer channelId, Integer customerId, String sortColumn, String sortOrder, int pageNum, int pageSize);
 
     List<CustomerRateDto> getSubListByCustomer(Integer period, Integer channelId, Integer customerId);
 

+ 34 - 7
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/StatMeterReadRateByBuildingServiceImpl.java

@@ -6,8 +6,10 @@ import com.bz.smart_city.commom.model.CommonQueryCondition;
 import com.bz.smart_city.commom.model.Pagination;
 import com.bz.smart_city.commom.util.DateTimeUtil;
 import com.bz.smart_city.commom.util.ExcelUtil;
+import com.bz.smart_city.commom.util.TreeUtil;
 import com.bz.smart_city.commom.util.UserUtil;
 import com.bz.smart_city.dto.*;
+import com.bz.smart_city.dto.assistant.InstallPlanDataDTO;
 import com.bz.smart_city.entity.Customer;
 import com.bz.smart_city.service.BuildingService;
 import com.bz.smart_city.service.CustomerService;
@@ -22,6 +24,8 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 import com.bz.smart_city.entity.StatMeterReadRateByBuilding;
 import com.bz.smart_city.dao.MeterReadRecordMapper;
@@ -322,7 +326,7 @@ public class StatMeterReadRateByBuildingServiceImpl implements StatMeterReadRate
     }
 
     @Override
-    public Pagination<CustomerRateDto> getListByCustomer(Integer period, Integer channelId, Integer customerId, String sortColumn, String sortOrder, int pageNum, int pageSize) {
+    public List<CustomerRateDto> getListByCustomer(Integer period, Integer channelId, Integer customerId, String sortColumn, String sortOrder, int pageNum, int pageSize) {
         LoginUser loginUser = UserUtil.getCurrentUser();
         ///查询数据权限的建筑ids
         List<Integer> buildingIds = buildingService.getIdsByDataPermission();
@@ -340,27 +344,50 @@ public class StatMeterReadRateByBuildingServiceImpl implements StatMeterReadRate
         List<WaterReadRateListDto> waterReadRateListDtoList = newArrayList();
 
         if (period == 2) { // 昨天,查询天表
-            PageHelper.startPage(pageNum, pageSize);
+            //PageHelper.startPage(pageNum, pageSize);
             customerRateDtoList = statMeterReadRateByBuildingMapper.getRateListByCustomerV2("sc_stat_meter_read_rate_by_building",loginUser.getSiteId(), startDate, buildingIds,channelId,customerIds);
         } else if (period == 7) { // 近7天,查询7天表
-            PageHelper.startPage(pageNum, pageSize);
+            //PageHelper.startPage(pageNum, pageSize);
             customerRateDtoList = statMeterReadRateByBuildingMapper.getRateListByCustomerV2("sc_stat_meter_read_rate_by_building_7day",loginUser.getSiteId(), startDate, buildingIds,channelId,customerIds);
         } else if (period == 15) {// 近15天,查询15天表
-            PageHelper.startPage(pageNum, pageSize);
+            //PageHelper.startPage(pageNum, pageSize);
             customerRateDtoList = statMeterReadRateByBuildingMapper.getRateListByCustomerV2("sc_stat_meter_read_rate_by_building_15day",loginUser.getSiteId(), startDate, buildingIds,channelId,customerIds);
         }else if (period == 99) {// 上个月,查询15天表
             DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMM");
             startDate = Integer.parseInt(LocalDateTime.now().plusMonths(-1).format(df));
-            PageHelper.startPage(pageNum, pageSize);
+            //PageHelper.startPage(pageNum, pageSize);
             customerRateDtoList = statMeterReadRateByBuildingMapper.getRateListByCustomerV2("sc_stat_meter_read_rate_by_building_month",loginUser.getSiteId(), startDate, buildingIds,channelId,customerIds);
         }
-        if (customerRateDtoList != null && customerRateDtoList.size()>0) {
+        /*if (customerRateDtoList != null && customerRateDtoList.size()>0) {
             for (CustomerRateDto customerRateDto : customerRateDtoList) {
                 customerRateDto.setList(statMeterReadRateByBuildingMapper.getRateListByCustomerV2AndDeviceType("sc_stat_meter_read_rate_by_building_month",loginUser.getSiteId(), startDate, buildingIds,channelId,customerRateDto.getCustomerId()));
             }
+        }*/
+        List<WaterReadRateListDto> dataList = statMeterReadRateByBuildingMapper.getRateListByCustomerV2AndDeviceType("sc_stat_meter_read_rate_by_building_month",loginUser.getSiteId(), startDate, buildingIds,channelId,null);
+        if (dataList != null && dataList.size() > 0) {
+            for (WaterReadRateListDto waterReadRateListDto : dataList) {
+                if (customerRateDtoList != null && customerRateDtoList.size() > 0) {
+                    for (CustomerRateDto customerRateDto : customerRateDtoList) {
+                        if(customerRateDto.getCustomerId().equals(waterReadRateListDto.getCustomerId())){
+                            if (customerRateDto.getList() != null) {
+                                customerRateDto.getList().add(waterReadRateListDto);
+                            }else {
+                                customerRateDto.setList(newArrayList(waterReadRateListDto));
+                            }
+                            continue;
+                        }
+                    }
+                }
+            }
         }
 
-        return new Pagination<>(customerRateDtoList);
+        //4、组装树形数据
+        List<CustomerRateDto> list = TreeUtil.getCustomerRateTree(customerRateDtoList, 0, 1);
+        List<CustomerRateDto> newList = list.stream().filter(p -> (p.getList() != null && p.getList().size() > 0) || (p.getChildren() != null && p.getChildren().size() > 0)).collect(Collectors.toList());
+        //List<CustomerRateDto> newList = list.stream().filter(p -> p.getCustomerId()==121).collect(Collectors.toList());
+        TreeUtil.postorderTraversalCustomerRate(newList);
+        TreeUtil.postorderTraversalCustomerRateCalculate(newList);
+        return newList;
     }
 
     @Override

+ 0 - 2
smart-city-platform/src/main/java/com/bz/smart_city/service/importfile/AsyncTaskImportService.java

@@ -374,8 +374,6 @@ public class AsyncTaskImportService {
 
 
 
-
-
         //更新完成生成消息
         Message message = new Message();
         message.setSiteId(record.getUserId());

BIN
smart-city-platform/src/main/resources/excel/installPlanTemplate20200716.xlsx


+ 5 - 5
smart-city-platform/src/main/resources/mapper/StatMeterReadRateByBuildingMapper.xml

@@ -493,7 +493,7 @@
     </select>
 
     <select id="getRateListByCustomerV2" resultType="com.bz.smart_city.dto.CustomerRateDto">
-        SELECT sc.id as customer_id,sc.customer_name,sc.children_num FROM sc_customer sc
+        SELECT sc.id as customer_id,sc.parent_id,sc.customer_name,sc.children_num FROM sc_customer sc
         LEFT JOIN(
             SELECT
                 ssmrrbb.customer_id,
@@ -512,11 +512,11 @@
         )t1 on(t1.customer_id = sc.id)
         WHERE sc.`status` = 1 and sc.site_id = #{siteId}
         <if test="customerIds != null and customerIds.size() != 0"> and sc.id in <foreach collection="customerIds" item="item" open="(" separator="," close=")">#{item}</foreach> </if>
-        <if test="customerIds == null "> and sc.parent_id = 0 </if>
         ORDER BY device_count desc
     </select>
     <select id="getRateListByCustomerV2AndDeviceType" resultType="com.bz.smart_city.dto.WaterReadRateListDto">
         SELECT
+        ssmrrbb.customer_id,
         ssmrrbb.channel_id,
         sc.channel_name,
         SUM(device_count) as device_count,
@@ -527,12 +527,12 @@
         FROM sc_stat_meter_read_rate_by_building ssmrrbb
         left join sc_channel sc on(sc.id = ssmrrbb.channel_id)
         WHERE
-        ssmrrbb.customer_id = #{customerId}
-        and ssmrrbb.site_id = #{siteId}
+        ssmrrbb.site_id = #{siteId}
         and ssmrrbb.stat_day = #{startDate}
         AND ssmrrbb.channel_id <![CDATA[ <> ]]> - 99
+        <if test="customerId != null"> and ssmrrbb.customer_id = #{customerId} </if>
         <if test="channelId != null"> and ssmrrbb.channel_id = #{channelId} </if>
-        GROUP BY ssmrrbb.channel_id,sc.channel_name
+        GROUP BY ssmrrbb.customer_id,ssmrrbb.channel_id,sc.channel_name
         order by read_rate desc
     </select>