xw il y a 1 jour
Parent
commit
e37446c2b1

+ 19 - 3
fs-qw-mq/src/main/java/com/fs/framework/config/DataSourceConfig.java

@@ -40,15 +40,31 @@ public class DataSourceConfig {
         return new DruidDataSource();
     }
 
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.master")
+    public DataSource sopDataSource() {
+        return new DruidDataSource();
+    }
+
+    @Bean
+    @ConfigurationProperties(prefix = "spring.datasource.sop.druid.read")
+    public DataSource sopReadDataSource() {
+        return new DruidDataSource();
+    }
+
     @Bean
     @Primary
     public DynamicDataSource dataSource(@Qualifier("clickhouseDataSource") DataSource clickhouseDataSource,
                                         @Qualifier("masterDataSource") DataSource masterDataSource,
-                                        @Qualifier("slaveDataSource") DataSource slaveDataSource) {
+                                        @Qualifier("slaveDataSource") DataSource slaveDataSource,
+                                        @Qualifier("sopDataSource") DataSource sopDataSource,
+                                        @Qualifier("sopReadDataSource") DataSource sopReadDataSource) {
         Map<Object, Object> targetDataSources = new HashMap<>();
-        targetDataSources.put(DataSourceType.MASTER, masterDataSource);
+        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
         targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
-        targetDataSources.put(DataSourceType.CLICKHOUSE.name(), clickhouseDataSource); // Ensure matching key
+        targetDataSources.put(DataSourceType.CLICKHOUSE.name(), clickhouseDataSource);
+        targetDataSources.put(DataSourceType.SOP.name(), sopDataSource);
+        targetDataSources.put(DataSourceType.SopREAD.name(), sopReadDataSource);
         return new DynamicDataSource(masterDataSource, targetDataSources);
     }
 

+ 47 - 53
fs-service/src/main/java/com/fs/course/service/impl/FsCourseFinishTempServiceImpl.java

@@ -27,9 +27,9 @@ import com.fs.qw.vo.QwUserVO;
 import com.fs.qwApi.domain.QwExternalContactRemarkResult;
 import com.fs.qwApi.param.QwExternalContactRemarkParam;
 import com.fs.qwApi.service.QwApiService;
-import com.fs.sop.domain.SopUserLogsInfo;
-import com.fs.sop.mapper.SopUserLogsInfoMapper;
-import com.fs.sop.vo.SopUserLogsInfoVOE;
+import com.fs.sop.mapper.SopUserLogsMapper;
+import com.fs.sop.params.SopUserLogsParam;
+import com.fs.qw.param.SopUserLogsVO;
 import com.fs.voice.utils.StringUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -39,7 +39,6 @@ import org.springframework.stereotype.Service;
 import java.time.LocalDate;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
-import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.function.Consumer;
 import java.util.regex.Matcher;
@@ -83,7 +82,7 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
     private OpenIMService openIMService;
 
     @Autowired
-    private SopUserLogsInfoMapper sopUserLogsInfoMapper;
+    private SopUserLogsMapper sopUserLogsMapper;
 
     /**
      * 查询完课模板
@@ -256,7 +255,42 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
         Integer campDays = null;
         if (isSendMsg == 6) {
             log.info("【isSendMsg=6】开始计算营期天数,客户ID: {}", qwExternalContactId);
-            campDays = calculateCampDays(qwExternalContactId);
+            try {
+                if (watchLog == null || watchLog.getSopId() == null || watchLog.getQwExternalContactId() == null) {
+                    log.error("计算营期天数参数不完整,sopId={} externalId={}", watchLog == null ? null : watchLog.getSopId(), qwExternalContactId);
+                    return;
+                }
+
+                SopUserLogsParam param = new SopUserLogsParam();
+                param.setSopId(watchLog.getSopId());
+                param.setExternalId(qwExternalContactId);
+                param.setStatus(1);
+                if (qwUserByRedisForId != null) {
+                    param.setQwUserId(qwUserByRedisForId.getQwUserId());
+                    param.setCorpId(qwUserByRedisForId.getCorpId());
+                }
+
+                List<SopUserLogsVO> list = sopUserLogsMapper.selectSopUserLogsListByParam(param);
+                if (list == null || list.isEmpty()) {
+                    log.error("未找到完课对应的SOP营期countDays,sopId={} externalId={}",
+                            watchLog.getSopId(), qwExternalContactId);
+                    return;
+                }
+
+                SopUserLogsVO first = list.get(0);
+                campDays = first.getCountDays();
+                if (campDays == null || campDays <= 0) {
+                    log.error("查询到SOP营期记录但countDays无效,sopId={} externalId={} countDays={}",
+                            watchLog.getSopId(), qwExternalContactId, campDays);
+                    return;
+                }
+
+                log.info("读取SOP营期countDays成功,客户ID: {}, countDays: {}, startTime: {}",
+                        qwExternalContactId, campDays, first.getStartTime());
+            } catch (Exception e) {
+                log.error("计算营期天数异常,客户ID: {}", qwExternalContactId, e);
+                return;
+            }
             if (campDays == null) {
                 log.error("【isSendMsg=6】无法获取营期天数,客户ID: {},跳过处理", qwExternalContactId);
                 return;
@@ -284,9 +318,9 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
             boolean shouldUpdate = true;
 
             if (isSendMsg == 6) {
-                // 检查是否已有相同的"*第N课 MMDD完"标记
-                String expectedMark = "*第" + campDays + "课 " + monthDay + "完";
-                if (oldRemark.contains(expectedMark)) {
+                // 检查是否已有相同的「第N课完」标记(不含日期,与历史「*第N课MMDD完」区分后统一为新格式)
+                Pattern expectedPattern = Pattern.compile("\\*第" + campDays + "课完");
+                if (expectedPattern.matcher(oldRemark).find()) {
                     shouldUpdate = false;
                 }
             } else {
@@ -315,8 +349,8 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
             // 根据 isSendMsg 决定标记格式
             String markToAdd;
             if (isSendMsg == 6) {
-                // isSendMsg = 6: 客户名*第N课 MMDD
-                markToAdd = "*第" + campDays + "课 " + monthDay + "完";
+                // isSendMsg = 6: *第N课完(不含月日)
+                markToAdd = "*第" + campDays + "课完";
             } else {
                 markToAdd = (isSendMsg == 3 || isSendMsg == 4) ? newNotesDay : newNotes;
             }
@@ -324,8 +358,8 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
             // 先移除现有标记
             String remarkWithoutMark;
             if (isSendMsg == 6) {
-                // 移除 "*第N课 MMDD完" 格式的标记
-                remarkWithoutMark = oldRemark.replaceAll("\\*第\\d+课 \\d{4}完", "").trim();
+                // 移除「*第N课完」「*第N课MMDD完」(含历史带月日)及「*MMdd完」
+                remarkWithoutMark = oldRemark.replaceAll("\\*第\\d+课(?:\\s*\\d{4})?完|\\*\\d{2,4}完", "").trim();
             } else {
                 remarkWithoutMark = oldRemark.replaceAll("\\*\\d{2,4}完", "").trim();
             }
@@ -577,46 +611,6 @@ public class FsCourseFinishTempServiceImpl implements IFsCourseFinishTempService
         return fsCourseFinishTempMapper.updateFsCourseFinishTempBatch(fsCourseFinishTemp);
     }
 
-    /**
-     * 计算营期天数(从SOP开始时间到今天的天数)
-     * @param qwExternalContactId 企微外部联系人ID
-     * @return 营期天数,如果查询失败返回null
-     */
-    private Integer calculateCampDays(Long qwExternalContactId) {
-        try {
-            // 查询SOP信息,获取start_time
-            SopUserLogsInfo queryParam = new SopUserLogsInfo();
-            queryParam.setExternalId(qwExternalContactId);
-
-            List<SopUserLogsInfoVOE> sopList = sopUserLogsInfoMapper.selectSopUserLogsInfoListVO(queryParam);
-
-            if (sopList == null || sopList.isEmpty()) {
-                log.error("未找到客户的SOP信息,客户ID: {}", qwExternalContactId);
-                return null;
-            }
-
-            // 取第一条SOP记录的开始时间
-            String startTimeStr = sopList.get(0).getStartTime();
-            if (StringUtil.strIsNullOrEmpty(startTimeStr)) {
-                log.error("SOP开始时间为空,客户ID: {}", qwExternalContactId);
-                return null;
-            }
 
-            // 解析开始时间
-            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-            LocalDate startDate = LocalDate.parse(startTimeStr, formatter);
-            LocalDate today = LocalDate.now();
 
-            // 计算天数差(从开始日期到今天,包含开始日期,所以+1)
-            long daysDiff = java.time.temporal.ChronoUnit.DAYS.between(startDate, today) + 1;
-
-            log.info("计算营期天数成功,客户ID: {}, 开始时间: {}, 营期天数: {}",
-                    qwExternalContactId, startTimeStr, daysDiff);
-
-            return (int) daysDiff;
-        } catch (Exception e) {
-            log.error("计算营期天数异常,客户ID: {}", qwExternalContactId, e);
-            return null;
-        }
-    }
 }