Browse Source

增加统计数据

xueyiming 3 weeks ago
parent
commit
c3097ec52d

+ 38 - 38
offline-module/src/main/java/com/tzld/piaoquan/offline/job/WeComStaffDataJob.java

@@ -27,6 +27,7 @@ import org.springframework.util.CollectionUtils;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.*;
@@ -120,10 +121,10 @@ public class WeComStaffDataJob {
         if (StringUtils.isNotEmpty(param)) {
             date = param;
         } else {
-            date = DateUtil.getThatDayDateString();
+            date = DateUtil.getBeforeDayDateString1();
         }
         long startTime = DateUtil.dateStrToTimestamp(date, "yyyy-MM-dd");
-        long endTime = startTime + TimeEnum.DAY.getTime();
+        long endTime = startTime + TimeEnum.DAY.getTime() - 1;
         StaffExample staffExample = new StaffExample();
         staffExample.createCriteria().andIsDeleteEqualTo(0);
         List<Staff> staffs = staffMapper.selectByExample(staffExample);
@@ -135,41 +136,7 @@ public class WeComStaffDataJob {
         List<StaffStatisticsTotal> staffStatisticsTotals = staffStatisticsTotalMapper.selectByExample(staffStatisticsTotalExample);
         if (!CollectionUtils.isEmpty(staffStatisticsTotals)) {
             // 2. 分组并多字段求和(不使用构造方法)
-            List<CorpStatisticsTotal> results = new ArrayList<>(staffStatisticsTotals.stream()
-                    // 按产品类型分组
-                    .collect(Collectors.groupingBy(
-                            StaffStatisticsTotal::getCorpId,
-                            // 对每组数据累加:使用默认构造的结果对象,通过setter更新值
-                            Collectors.reducing(
-                                    new CorpStatisticsTotal(), // 初始值:默认构造的空对象
-                                    // 转换函数:将Order转换为临时结果对象(用setter设置初始值)
-                                    s -> {
-                                        CorpStatisticsTotal temp = new CorpStatisticsTotal();
-                                        temp.setCorpId(s.getCorpId());
-                                        temp.setChatCnt(s.getChatCnt());
-                                        temp.setMessageCnt(s.getMessageCnt());
-                                        temp.setNegativeFeedbackCnt(s.getNegativeFeedbackCnt());
-                                        temp.setNewApplyCnt(s.getNewApplyCnt());
-                                        temp.setNewContactCnt(s.getNewContactCnt());
-                                        return temp;
-                                    },
-                                    // 累加函数:合并两个结果对象(用setter更新总和)
-                                    (sum1, sum2) -> {
-                                        // 确保分组类型一致(同组数据type相同,取一个即可)
-                                        sum1.setCorpId(sum2.getCorpId());
-                                        // 累加数量
-                                        sum1.setChatCnt(sum1.getChatCnt() + sum2.getChatCnt());
-                                        sum1.setMessageCnt(sum1.getMessageCnt() + sum2.getMessageCnt());
-                                        sum1.setNegativeFeedbackCnt(sum1.getNegativeFeedbackCnt() + sum2.getNegativeFeedbackCnt());
-                                        sum1.setNewApplyCnt(sum1.getNewApplyCnt() + sum2.getNewApplyCnt());
-                                        sum1.setNewContactCnt(sum1.getNewContactCnt() + sum2.getNewContactCnt());
-                                        return sum1;
-                                    }
-                            )
-                    ))
-                    // 将Map的值(结果对象)转换为List
-                    .values());
-
+            List<CorpStatisticsTotal> results = processStaffStatistics(staffStatisticsTotals);
             for (CorpStatisticsTotal corpStatisticsTotal : results) {
                 corpStatisticsTotal.setDate(date);
                 corpStatisticsTotalMapper.insertSelective(corpStatisticsTotal);
@@ -180,6 +147,39 @@ public class WeComStaffDataJob {
         return ReturnT.SUCCESS;
     }
 
+    public List<CorpStatisticsTotal> processStaffStatistics(List<StaffStatisticsTotal> staffStatisticsTotals) {
+        // 使用 toMap 收集器高效分组和聚合
+        Map<Long, CorpStatisticsTotal> resultMap = staffStatisticsTotals.stream()
+                .collect(Collectors.toMap(
+                        StaffStatisticsTotal::getCorpId,
+                        this::convertToCorpStats,  // 方法引用转换
+                        this::mergeCorpStats       // 方法引用合并
+                ));
+
+        return new ArrayList<>(resultMap.values());
+    }
+
+    private CorpStatisticsTotal mergeCorpStats(CorpStatisticsTotal existing, CorpStatisticsTotal newcomer) {
+        existing.setChatCnt(existing.getChatCnt() + newcomer.getChatCnt());
+        existing.setMessageCnt(existing.getMessageCnt() + newcomer.getMessageCnt());
+        existing.setNegativeFeedbackCnt(existing.getNegativeFeedbackCnt() + newcomer.getNegativeFeedbackCnt());
+        existing.setNewApplyCnt(existing.getNewApplyCnt() + newcomer.getNewApplyCnt());
+        existing.setNewContactCnt(existing.getNewContactCnt() + newcomer.getNewContactCnt());
+        return existing;
+    }
+
+    // 转换方法:将 Staff 对象转换为 Corp 对象
+    private CorpStatisticsTotal convertToCorpStats(StaffStatisticsTotal staff) {
+        CorpStatisticsTotal corp = new CorpStatisticsTotal();
+        corp.setCorpId(staff.getCorpId());
+        corp.setChatCnt(staff.getChatCnt());
+        corp.setMessageCnt(staff.getMessageCnt());
+        corp.setNegativeFeedbackCnt(staff.getNegativeFeedbackCnt());
+        corp.setNewApplyCnt(staff.getNewApplyCnt());
+        corp.setNewContactCnt(staff.getNewContactCnt());
+        return corp;
+    }
+
     private void statisticsStaffTotal(Staff staff, long startTime, long endTime, String date) throws IOException {
         Long corpId = staff.getCorpId();
         String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
@@ -195,7 +195,7 @@ public class WeComStaffDataJob {
         if (staff.getCorpId() == 1L) {
             res = httpPoolClient.post(url, params.toJSONString());
         } else {
-            res = proxyHttpPoolClient.post(url);
+            res = proxyHttpPoolClient.post(url, params.toJSONString());
         }
         if (StringUtils.isNotEmpty(res)) {
             JSONObject jsonObject = JSONObject.parseObject(res);