From e2b0e66b92759eb6a24b0a4e24aa60e76bbdc1a7 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期二, 17 十一月 2026 17:34:30 +0800
Subject: [PATCH] feat: 实现妇幼随访按周期发送

---
 ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java |   85 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java b/ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java
index 58b73f4..f094195 100644
--- a/ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java
+++ b/ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java
@@ -126,6 +126,7 @@
 import com.ruoyi.system.service.ISysDeptService;
 import com.ruoyi.system.mapper.ext.SysDept2Mapper;
 import com.ruoyi.task.api.contract.OperationSubtaskWriter;
+import com.ruoyi.task.event.FollowupCompletedEventPublisher;
 
 /**
  * 鍗曚竴浠诲姟锛堥殢璁匡級Service涓氬姟灞傚鐞�
@@ -145,6 +146,9 @@
      */
     @Autowired
     private OperationSubtaskWriter operationSubtaskWriter;
+
+    @Autowired
+    private FollowupCompletedEventPublisher followupCompletedEventPublisher;
 
     /**
      * 婊℃剰搴︽ā鍧楀澶栭棬闈紙甯搁┗ Bean锛夈��
@@ -885,6 +889,39 @@
     }
 
     /**
+     * 鍏煎銆岀瀹�/鐥呭尯銆嶅瓧娈甸噸鍛藉悕鍓嶇殑鏃у叆鍙傘��
+     *
+     * <p>2026-11 璧凤細{@code deptcode/deptname} = 鍦ㄩ櫌绉戝锛寋@code leaveldeptcode/leaveldeptname} = 绂婚櫌绉戝銆�
+     * 浣嗗墠绔�/鏃ц皟鐢ㄦ柟锛坽@code /serviceSubtask/addSubTask}锛変粛鍙兘鎶婄瀹ゆ斁鍦� {@code deptcode/deptname} 閲屻��
+     * 鑻ヤ笉褰掍綅锛屽瓙浠诲姟浼氳惤鍒般�屽湪闄㈢淮搴︺�嶏紝鍒楄〃鎸夌闄㈢瀹ょ瓫閫夋椂鏌ヤ笉鍒帮紙鏈绾夸笂 2654501/2663019 鍗虫闂锛夈��
+     *
+     * <p>瑙勫垯锛�
+     * <ul>
+     *   <li>鍙闈炲欢缁姢鐞嗭紙{@code service_type != '99'}锛夌敓鏁堬紱99 鍏佽浣跨敤鍦ㄩ櫌缁村害锛�</li>
+     *   <li>绂婚櫌绉戝涓虹┖涓� {@code deptcode} 鏈夊�兼椂锛屾妸 {@code deptcode/deptname} 杩佸埌绂婚櫌缁村害锛�</li>
+     *   <li>娓呯┖鍦ㄩ櫌缁村害锛岄伩鍏嶅悓涓�浠界瀹ゅ悓鏃跺懡涓袱缁勭瓫閫夛紙涓� {@code toSubtaskFromTaskVO} 鐨勫彛寰勪竴鑷达級銆�</li>
+     * </ul>
+     */
+    private void alignLeaveDeptFromLegacyDept(ServiceSubtask serviceSubtask) {
+        if (serviceSubtask == null || "99".equals(serviceSubtask.getServiceType())) {
+            return;
+        }
+        if (StringUtils.isEmpty(serviceSubtask.getLeaveldeptcode())
+                && StringUtils.isNotEmpty(serviceSubtask.getDeptcode())) {
+            serviceSubtask.setLeaveldeptcode(serviceSubtask.getDeptcode());
+        }
+        if (StringUtils.isEmpty(serviceSubtask.getLeaveldeptname())
+                && StringUtils.isNotEmpty(serviceSubtask.getDeptname())) {
+            serviceSubtask.setLeaveldeptname(serviceSubtask.getDeptname());
+        }
+        // 鎵嬪伐鏂板鍏ュ彛娌℃湁銆屽湪闄㈢淮搴︺�嶇殑鍙潬鏉ユ簮锛涗换鍔¢厤缃噷鐨勭瀹や笉鏄偅鑰呮湰浜虹殑鍦ㄩ櫌绉戝銆�
+        serviceSubtask.setDeptcode(null);
+        serviceSubtask.setDeptname(null);
+        serviceSubtask.setHospitaldistrictcode(null);
+        serviceSubtask.setHospitaldistrictname(null);
+    }
+
+    /**
      * 鏂板鍗曚竴浠诲姟锛堥殢璁匡級
      *
      * @param serviceSubtask 鍗曚竴浠诲姟锛堥殢璁匡級
@@ -892,6 +929,8 @@
      */
     @Override
     public Map<String, Object> insertServiceSubtask(ServiceSubtask serviceSubtask) {
+        // 鍏煎 2026-11 瀛楁閲嶅懡鍚嶅墠鐨勬棫鍏ュ弬锛�/addSubTask 浠嶅彲鑳芥妸绉戝鏀惧湪 deptcode/deptname 閲�
+        alignLeaveDeptFromLegacyDept(serviceSubtask);
         List<ServiceSubtask> serviceSubtasks = null;
         //鍏堟煡璇竴涓嬭鎮h�呮槸鍚﹀凡缁忓垱寤烘牴鎹畇erviceType,sendstate,inhospid,patid,orgid
         //杩欐牱鍒ゆ柇涓昏鏄负浜嗘兂璁╂柊鍗庡尰闄㈢殑澶栭儴瀵煎叆鎮h�呭垱寤轰换鍔★紝涔熻兘鐢ㄨ繖涓帴鍙�
@@ -1544,7 +1583,12 @@
         if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() == 6)
             serviceSubtask.setFinishtime(new Date());
 
-        return serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
+        Boolean updated = serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
+        if (Boolean.TRUE.equals(updated) && serviceSubtask.getSendstate() != null
+                && serviceSubtask.getSendstate() == 6 && serviceSubtask.getId() != null) {
+            followupCompletedEventPublisher.publish(serviceSubtask.getId());
+        }
+        return updated;
     }
 
     @Override
@@ -2565,7 +2609,10 @@
             if (serviceSubtaskCountReq.getDrcode() != null && !serviceSubtaskCountReq.getDrcode().isEmpty()) {
                 groupKey = drcode;
             } else {
-                groupKey = "deptcode";
+                // 2026-11 瀛楁閲嶅懡鍚嶅悗锛歞eptcode 鏄湪闄㈢瀹わ紙鍘嗗彶鏁版嵁涓虹┖锛夛紝
+                // 鍑洪櫌绉戝宸叉敼鍚嶄负 leaveldeptcode锛屽垎缁勫繀椤荤敤鍚庤�咃紝
+                // 鍚﹀垯 getSfStatisticsGroupKey 浼氳繑鍥� NULL锛屾嫾鎴� IN (NULL) 瀵艰嚧鏁磋〃鏌ョ┖銆�
+                groupKey = "leaveldeptcode";
             }
         } else if (serviceSubtaskCountReq.getStatisticaltype() != null && serviceSubtaskCountReq.getStatisticaltype() == 3) {
             // 鐥呯缁熻锛氭寜浠诲姟ID鍒嗙粍锛屾瘡涓换鍔″嵆涓�涓梾绉嶉殢璁夸换鍔�
@@ -2576,6 +2623,17 @@
         Map<String, List<ServiceSubtask>> collect = new HashMap<>();
         serviceSubtaskCountReq.setGroupKey(groupKey);
         List<String> groupKeyList = serviceSubtaskMapper.getSfStatisticsGroupKey(serviceSubtaskCountReq);
+        /*
+         * 鍒嗙粍閿彲鑳戒负 NULL锛堝巻鍙叉暟鎹�佹垨閲嶅懡鍚嶅悗灏氭湭鍥炲~鐨勫湪闄㈢淮搴﹀垪锛夛紝
+         * 鐩存帴鎷艰繘 SQL 浼氬彉鎴� `AND IFNULL(鍒�,'Unknown') IN (NULL)`锛屾亽涓嶆垚绔嬶紝鏁村紶缁熻琛ㄤ负绌恒��
+         * 杩欓噷缁熶竴鎴� 'Unknown'锛屼笌鑱氬悎 SQL 鐨� IFNULL(鍒�,'Unknown') 鍙e緞瀵归綈銆�
+         */
+        if (groupKeyList != null) {
+            groupKeyList = groupKeyList.stream()
+                    .map(key -> key == null ? "Unknown" : key)
+                    .distinct()
+                    .collect(Collectors.toList());
+        }
         serviceSubtaskCountReq.setGroupKeyList(groupKeyList);
         List<ServiceSubtask> rawData = serviceSubtaskMapper.getSfStatistics(serviceSubtaskCountReq);
         switch (groupKey) {
@@ -2590,6 +2648,7 @@
                 collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getManagementDoctorCode()).orElse("Unknown")));
                 break;
             case "deptcode":
+            case "leaveldeptcode":
                 collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getLeaveldeptcode()).orElse("Unknown")));
                 break;
             case "leavehospitaldistrictcode":
@@ -3206,7 +3265,7 @@
     }
 
     /**
-     * 鍐嶆闅忚闄勫姞缁熻锛氬緟闅忚銆佸凡鍙戦�併�佷汉宸ョ數璇濊秴鏃跺強鍥為櫌澶嶈瘖澶嶆煡锛坰uggest=6锛夈��
+     * 鍐嶆闅忚闄勫姞缁熻锛氬緟闅忚銆佸凡鍙戦�併�佷汉宸ョ數璇濊秴鏃跺強鍥為櫌澶嶈瘖澶嶆煡锛坰uggest=7锛夈��
      */
     private void applyAgainFollowUpExtraStatistics(ServiceSubtaskStatistic statistic, ServiceSubtask serviceSubtask) {
         if (serviceSubtask.getVisitCount() == null || serviceSubtask.getVisitCount() <= 1) {
@@ -3216,9 +3275,9 @@
             return;
         }
         Long sendstate = serviceSubtask.getSendstate();
-        // 鍥為櫌澶嶈瘖澶嶆煡锛氬啀娆¢殢璁夸腑鎸夊鐞嗘剰瑙� suggest=6 缁熻
-        if (StringUtils.equals("6", StringUtils.trim(serviceSubtask.getSuggest()))) {
-            statistic.setSuggest6CountAgain(statistic.getSuggest6CountAgain() + 1L);
+        // 鍥為櫌澶嶈瘖澶嶆煡锛氬啀娆¢殢璁夸腑鎸夊鐞嗘剰瑙� suggest=7 缁熻
+        if (StringUtils.equals("7", StringUtils.trim(serviceSubtask.getSuggest()))) {
+            statistic.setSuggest7CountAgain(statistic.getSuggest7CountAgain() + 1L);
         }
         if (sendstate.equals(3L)) {
             statistic.setSentAgain(statistic.getSentAgain() + 1L);
@@ -3864,7 +3923,7 @@
      * pendingFollowUpSentAgainInfo - visitCount > 1 && sendstate in (3,7)
      * sentInfo - visitCount = 1 && sendstate = 3
      * sentAgainInfo - visitCount > 1 && sendstate = 3
-     * suggest6CountAgainInfo - visitCount > 1 && suggest = 6
+     * suggest7CountAgainInfo - visitCount > 1 && suggest = 7
      * filterCountList - taskSituation = 6 && sendstate = 4
      *
      */
@@ -4049,10 +4108,10 @@
                 serviceSubtaskCountReq.setVisitCount(2);
                 serviceSubtaskCountReq.setExcep("1");
                 break;
-            case "suggest6CountAgainInfo":
-                // 鍐嶆闅忚鍥為櫌澶嶈瘖澶嶆煡锛歷isitCount > 1 涓� suggest = 6
+            case "suggest7CountAgainInfo":
+                // 鍐嶆闅忚鍥為櫌澶嶈瘖澶嶆煡锛歷isitCount > 1 涓� suggest = 7
                 serviceSubtaskCountReq.setVisitCount(2);
-                serviceSubtaskCountReq.setSuggest("6");
+                serviceSubtaskCountReq.setSuggest("7");
                 break;
             case "taskSituation1Info":
                 serviceSubtaskCountReq.setTaskSituation(1);
@@ -4611,7 +4670,7 @@
         total.setSentAgain(0L);
         total.setFollowUpSuccessAgain(0L);
         total.setFollowUpFailAgain(0L);
-        total.setSuggest6CountAgain(0L);
+        total.setSuggest7CountAgain(0L);
         total.setManualAgain(0L);
         total.setSmsAgain(0L);
         total.setWeChatAgain(0L);
@@ -4656,7 +4715,7 @@
             total.setSentAgain(total.getSentAgain() + (stat.getSentAgain() != null ? stat.getSentAgain() : 0L));
             total.setFollowUpSuccessAgain(total.getFollowUpSuccessAgain() + (stat.getFollowUpSuccessAgain() != null ? stat.getFollowUpSuccessAgain() : 0L));
             total.setFollowUpFailAgain(total.getFollowUpFailAgain() + (stat.getFollowUpFailAgain() != null ? stat.getFollowUpFailAgain() : 0L));
-            total.setSuggest6CountAgain(total.getSuggest6CountAgain() + (stat.getSuggest6CountAgain() != null ? stat.getSuggest6CountAgain() : 0L));
+            total.setSuggest7CountAgain(total.getSuggest7CountAgain() + (stat.getSuggest7CountAgain() != null ? stat.getSuggest7CountAgain() : 0L));
             total.setManualAgain(total.getManualAgain() + (stat.getManualAgain() != null ? stat.getManualAgain() : 0L));
             total.setSmsAgain(total.getSmsAgain() + (stat.getSmsAgain() != null ? stat.getSmsAgain() : 0L));
             total.setWeChatAgain(total.getWeChatAgain() + (stat.getWeChatAgain() != null ? stat.getWeChatAgain() : 0L));
@@ -4908,6 +4967,7 @@
             serviceSubtask.setSendstate(6L);
             serviceSubtask.setPreachform("1");
             serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
+            followupCompletedEventPublisher.publish(serviceSubtask.getId());
         }
     }
 
@@ -5052,6 +5112,7 @@
         if (subtaskUpdated == 0) {
             throw new BaseException("寰俊鍏紬鍙烽粯璁ゅ畬鎴愬け璐ワ細瀛愪换鍔$姸鎬佸凡鍙樺寲锛宻ubid=" + current.getId());
         }
+        followupCompletedEventPublisher.publish(current.getId());
 
         log.info("銆愬井淇″叕浼楀彿榛樿瀹屾垚銆憇ubid锛歿}锛屽凡鎸夐粯璁ら�夐」瀹屾垚锛屽~鍏呴鐩暟锛歿}", current.getId(), filled);
         return true;

--
Gitblit v1.9.3