chengxiaowen пре 3 месеци
родитељ
комит
2dbb7b7716

+ 5 - 1
src/main/java/com/poteviohealth/cgp/statistics/mapper/OrderMapper.java

@@ -17,6 +17,7 @@ import com.poteviohealth.cgp.statistics.model.outdto.*;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -114,7 +115,10 @@ public interface OrderMapper extends BaseMapper<Order>{
      * @return
      */
     @SqlParser(filter = true)
-    List<OrderIntegrationData> queryOrderIntegrationData(@Param("operatorId")Integer operatorId, @Param("dbName")String dbName);
+    List<OrderIntegrationData> queryOrderIntegrationData(@Param("operatorId")Integer operatorId, @Param("dbName")String dbName,
+                                                         @Param("beginDay") Date beginDay, @Param("endDay")Date endDay,
+                                                         @Param("beginWeek") Date beginWeek, @Param("endWeek")Date endWeek,
+                                                         @Param("beginMonth") Date beginMonth, @Param("endMonth")Date endMonth);
 
     /**
      * 获取订单数据

+ 61 - 47
src/main/java/com/poteviohealth/cgp/statistics/service/impl/OrderServiceImpl.java

@@ -35,6 +35,7 @@ import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.ParseException;
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -1357,11 +1358,11 @@ public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implem
             Long exchangeSum = dataList.stream().filter(t -> t.getTransType().equals("1")).mapToLong(t -> t.getAmount()).sum();
             face.setExchangeNum(exchangeSum);
 
-            Long grantWmSum = dataList.stream().filter(t -> t.getTransType().equals("0") && t.getRemark().contains("文明积分")).mapToLong(t -> t.getAmount()).sum();
+            Long grantWmSum = dataList.stream().filter(t -> t.getTransType().equals("0") && t.getRemark() !=null && t.getRemark().contains("文明积分")).mapToLong(t -> t.getAmount()).sum();
             face.setGrantNumWm(grantWmSum);
-            Long grantYlSum = dataList.stream().filter(t -> t.getTransType().equals("0") && t.getRemark().contains("银龄积分")).mapToLong(t -> t.getAmount()).sum();
+            Long grantYlSum = dataList.stream().filter(t -> t.getTransType().equals("0") && t.getRemark() !=null && t.getRemark().contains("银龄积分")).mapToLong(t -> t.getAmount()).sum();
             face.setGrantNumYl(grantYlSum);
-            Long exchangeWmSum = dataList.stream().filter(t -> t.getTransType().equals("1") && t.getRemark().contains("文明积分")).mapToLong(t -> t.getAmount()).sum();
+            Long exchangeWmSum = dataList.stream().filter(t -> t.getTransType().equals("1") && t.getRemark() !=null && t.getRemark().contains("文明积分")).mapToLong(t -> t.getAmount()).sum();
             face.setExchangeNumWm(exchangeWmSum);
             List<MonthData> goodsList = Lists.newArrayList();
             Map<Integer, List<IntegralTrans>> maps = dataList.stream().filter(i -> i.getParentId() != null).collect(Collectors.groupingBy(IntegralTrans::getParentId));
@@ -1417,8 +1418,8 @@ public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implem
                     data.setUseValue(0L);
                 }else{
                     if(StringUtils.isNotEmpty(remark)){
-                        data.setAddValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("0") && c.getRemark().contains(remark)).mapToLong(c->c.getAmount()).sum());
-                        data.setUseValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("1") && c.getRemark().contains(remark)).mapToLong(c->c.getAmount()).sum());
+                        data.setAddValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("0") && c.getRemark() !=null && c.getRemark().contains(remark)).mapToLong(c->c.getAmount()).sum());
+                        data.setUseValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("1") && c.getRemark() !=null && c.getRemark().contains(remark)).mapToLong(c->c.getAmount()).sum());
                     }else{
                         data.setAddValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("0")).mapToLong(c->c.getAmount()).sum());
                         data.setUseValue(transList.stream().filter(c->c.getBuildTime().after(calendar2.getTime()) && c.getBuildTime().before(com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(calendar2.getTime())) && c.getTransType().equals("1")).mapToLong(c->c.getAmount()).sum());
@@ -1671,14 +1672,28 @@ public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implem
     private List<OrderStatisticsData> getOrderStatisticsDataList(List<Integer> ids,List<ListParams> categoryList){
         List<OrderStatisticsData> returnList = Lists.newArrayList();
         List<OrderIntegrationData> list = Lists.newArrayList();
+        Date beginMonth = com.poteviohealth.cgp.common.utils.DateUtils.getFirstDayOfMonth(new Date());
+        Date endMonth=         com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(new Date());
+        Map<String, String> dateMap = com.poteviohealth.cgp.common.utils.DateUtils.getWeekDate();
+        Date monday ;
+        Date sunday ;
+        try {
+            monday = com.poteviohealth.cgp.common.utils.DateUtils.startOfDay(com.alibaba.excel.util.DateUtils.parseDate(dateMap.get("mondayDate")));
+            sunday = com.poteviohealth.cgp.common.utils.DateUtils.endOfDay(com.alibaba.excel.util.DateUtils.parseDate(dateMap.get("sundayDate")));
+        } catch (ParseException e) {
+            log.error("日期解析失败",e);
+            return Collections.emptyList();
+        }
 
+        Date beginDay = com.poteviohealth.cgp.common.utils.DateUtils.startOfDay(new Date());
+        Date endDay = com.poteviohealth.cgp.common.utils.DateUtils.endOfDay(new Date());
         for (Integer id : ids) {
             //获取已完成的服务工单
-            list.addAll(baseMapper.queryOrderIntegrationData(id,dbName));
+            list.addAll(baseMapper.queryOrderIntegrationData(id,dbName, beginDay, endDay, monday, sunday, beginMonth, endMonth));
         }
 
 
-        Map<Integer,List<ListParams>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(ListParams::getId));
+        Map<Integer,ListParams> categoryMap = categoryList.stream().collect(Collectors.toMap(ListParams::getId, Function.identity()));
         //按照市统计
         Map<Integer,List<OrderIntegrationData>> cityMaps = list.stream().collect(Collectors.groupingBy(OrderIntegrationData::getCityId));
         //按照区统计
@@ -1729,47 +1744,44 @@ public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implem
 
         OrderStatisticsData data = new OrderStatisticsData();
         data.setAreaId(areaId);
-        List<Long> visitNumList = orderList.stream().map(o->o.getCustomerId()).distinct().collect(Collectors.toList());
-        data.setVisitNum(visitNumList.size());
-        data.setOrderNum(orderList.size());
+        int visitNum = 0;
+        int orderNum = 0;
+        int satisfactionNum = 0;
+        int qualifiedNum = 0;
+        for (OrderIntegrationData item:orderList){
+            orderNum += item.getOrderId();
+            visitNum += item.getCustomerId();
+            satisfactionNum += item.getResult();
+            qualifiedNum += item.getQualified();
+        }
+        data.setVisitNum(visitNum);
+        data.setOrderNum(orderNum);
         data.setVisitRate(new BigDecimal(100));
-        if(data.getVisitNum()>0){
-            List<Long> satisfactionList = orderList.stream().filter(v->v.getResult().equals(1)).map(o->o.getOrderId()).collect(Collectors.toList());
-            data.setSatisfactionNum(satisfactionList.size());
-            List<Long> qualifiedList = orderList.stream().filter(v->v.getQualified().equals(1)).map(o->o.getOrderId()).collect(Collectors.toList());
-            data.setQualifiedNum(qualifiedList.size());
-        }else{
-            data.setSatisfactionNum(0);
-            data.setQualifiedNum(0);
-        }
+        data.setSatisfactionNum(satisfactionNum);
+        data.setQualifiedNum(qualifiedNum);
 
         returnList.add(data);
     }
 
 
     private void integrationData(List<OrderStatisticsData> returnList,List<OrderIntegrationData> orderList,
-                                 Integer areaId,Map<Integer,List<ListParams>> categoryMap){
+                                 Integer areaId,Map<Integer,ListParams> categoryMap){
         OrderStatisticsData data = new OrderStatisticsData();
         data.setAreaId(areaId);
-        data.setOrderNum(orderList.size());
-        List<OrderIntegrationData> monthOrderList = orderList.stream()
-                .filter(o-> DateUtil.isIn(o.getFinishTime(), com.poteviohealth.cgp.common.utils.DateUtils.getFirstDayOfMonth(new Date()), com.poteviohealth.cgp.common.utils.DateUtils.getLastDayOfMonth(new Date())))
-                .collect(Collectors.toList());
-        data.setOrderNumMonth(monthOrderList.size());
-        Map<String, String> dateMap = com.poteviohealth.cgp.common.utils.DateUtils.getWeekDate();
-        try {
-            Date monday = com.poteviohealth.cgp.common.utils.DateUtils.startOfDay(com.alibaba.excel.util.DateUtils.parseDate(dateMap.get("mondayDate")));
-            Date sunday = com.poteviohealth.cgp.common.utils.DateUtils.endOfDay(com.alibaba.excel.util.DateUtils.parseDate(dateMap.get("sundayDate")));
-            List<OrderIntegrationData> weekOrderList = orderList.stream().filter(o->DateUtil.isIn(o.getFinishTime(),monday,sunday)).collect(Collectors.toList());
-            data.setOrderNumWeek(weekOrderList.size());
-        } catch (ParseException e) {
-            e.printStackTrace();
-        }
-
-        List<OrderIntegrationData> dayOrderList = orderList.stream()
-                .filter(o->DateUtil.isIn(o.getFinishTime(), com.poteviohealth.cgp.common.utils.DateUtils.startOfDay(new Date()), com.poteviohealth.cgp.common.utils.DateUtils.endOfDay(new Date())))
-                .collect(Collectors.toList());
-        data.setOrderNumDay(dayOrderList.size());
+        int orderNum = 0;
+        int dayNum = 0;
+        int weekNum = 0;
+        int monthNum = 0;
+        for (OrderIntegrationData item:orderList){
+            orderNum += item.getOrderId();
+            dayNum += item.getVisitStatus();
+            weekNum += item.getResult();
+            monthNum += item.getQualified();
+        }
+        data.setOrderNum(orderNum);
+        data.setOrderNumMonth(monthNum);
+        data.setOrderNumWeek(weekNum);
+        data.setOrderNumDay(dayNum);
 
         //服务类型统计
         List<OrderServiceTypeDTO> typeList = Lists.newArrayList();
@@ -1777,24 +1789,26 @@ public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implem
 
         Map<Integer,Integer> maps = Maps.newHashMap();
         for (Integer integer : orderCategoryMaps.keySet()) {
-            List<ListParams> list = categoryMap.get(integer);
-            if(list != null && !list.isEmpty()){
-                ListParams listParent = categoryMap.get(list.get(0).getKey()).get(0);
-                if(maps.containsKey(listParent.getId())){
-                    maps.put(listParent.getId(),maps.get(listParent.getId())+orderCategoryMaps.get(integer).size());
+            ListParams list = categoryMap.get(integer);
+            if(list != null ){
+                ListParams listParent = categoryMap.get(list.getKey());
+                long num = orderCategoryMaps.get(integer).stream().mapToLong(OrderIntegrationData::getOrderId).sum();
+                Integer parentId = listParent.getId();
+                if(maps.containsKey(parentId)){
+                    maps.put(parentId,maps.get(parentId)+ (int)num);
                 }else{
-                    maps.put(listParent.getId(),orderCategoryMaps.get(integer).size());
+                    maps.put(parentId, (int)num);
                 }
             }
         }
         DecimalFormat df = new DecimalFormat("#0.00");
         if(!maps.isEmpty()){
-            Integer sum =  maps.values().stream().mapToInt(Integer::intValue).sum();
+            int sum =  maps.values().stream().mapToInt(Integer::intValue).sum();
             for (Integer integer : maps.keySet()) {
                 OrderServiceTypeDTO dto = new OrderServiceTypeDTO();
                 dto.setId(integer);
                 dto.setCount(maps.get(integer));
-                dto.setName(categoryMap.get(integer).get(0).getName());
+                dto.setName(categoryMap.get(integer).getName());
                 if(sum == 0){
                     dto.setRate("0");
                 }else{

+ 22 - 21
src/main/resources/mapper/statistics/OrderMapper.xml

@@ -159,9 +159,11 @@
 
     <select id="queryOrderIntegrationData"
             resultType="com.poteviohealth.cgp.statistics.model.outdto.OrderIntegrationData">
-        SELECT o.order_id as orderId,o.city_id as cityId,o.country_id as countryId,o.street_id as streetId,o.visit_status as visitStatus
-                ,o.customer_id as  customerId,o.finish_time as  finishTime,od.product_category_id as productCategoryId
-                FROM ${dbName}_order.o_order_${operatorId} o,${dbName}_order.o_order_detail_${operatorId} od
+        SELECT o.city_id as cityId,o.country_id as countryId,o.street_id as streetId,od.product_category_id as productCategoryId, count(o.order_id) as orderId,
+            sum(if (o.finish_time BETWEEN #{beginDay} and #{endDay}, 1,0)) as visitStatus,
+            sum(if (o.finish_time BETWEEN #{beginWeek} and #{endWeek}, 1,0)) as result,
+            sum(if (o.finish_time BETWEEN #{beginMonth} and #{endMonth}, 1,0)) as qualified
+        FROM ${dbName}_order.o_order_${operatorId} o,${dbName}_order.o_order_detail_${operatorId} od
         where o.order_id = od.order_id
           and o.yn= 0
           and o.order_status = 6
@@ -171,41 +173,40 @@
           and o.street_id is not null
           and o.finish_time is not null
           and od.product_category_id is not null
-        group by o.order_id
+        group by o.street_id, od.product_category_id
     </select>
 
     <select id="queryOrderVisitData"
             resultType="com.poteviohealth.cgp.statistics.model.outdto.OrderIntegrationData">
         SELECT
-            o.order_id AS orderId,
             o.city_id AS cityId,
             o.country_id AS countryId,
             o.street_id AS streetId,
-            o.customer_id AS customerId,
-            v.qualified AS qualified,
-            v.result AS result
+            count(o.order_id) AS order_id,
+            count(DISTINCT o.customer_id) AS customer_id,
+            sum(IF(v.qualified = 1, 1, 0)) AS qualified,
+            sum(IF(v.result = 1, 1, 0)) AS result
         FROM
-            ${dbName}_order.o_order_${operatorId} o,
-            ${dbName}_order.o_visit_${operatorId} v
-                JOIN (
+            (
                 SELECT
-                    MAX( t2.id ) AS maxv
+                    MAX(t2.id) AS maxv
                 FROM
                     ${dbName}_order.o_visit_${operatorId} t2
                 WHERE
                     t2.yn = 0
                 GROUP BY
                     t2.order_id
-            ) t ON t.maxv = v.id
+            ) t
+        JOIN ${dbName}_order.o_visit_${operatorId} v ON t.maxv = v.id
+        JOIN ${dbName}_order.o_order_${operatorId} o ON v.order_id = o.order_id
         WHERE
-            v.order_id = o.order_id
-          AND o.yn = 0
-          AND v.yn = 0
-          AND o.order_type = 2
-          AND o.city_id IS NOT NULL
-          AND o.country_id IS NOT NULL
-          AND o.street_id IS NOT NULL
-        GROUP BY o.order_id
+            o.yn = 0
+        AND o.order_type = 2
+        AND o.city_id IS NOT NULL
+        AND o.country_id IS NOT NULL
+        AND o.street_id IS NOT NULL
+        GROUP BY
+            o.street_id
     </select>
 
     <select id="getAllCategory" resultType="com.poteviohealth.cgp.common.integrated.ListParams">