Explorar o código

优化sop任务分发逻辑

cgp hai 2 semanas
pai
achega
242b74c3f2

+ 214 - 82
fs-admin/src/main/java/com/fs/his/task/Task.java

@@ -2240,112 +2240,244 @@ public class Task {
         }
     }
 
-    /**
-     * 定时向销售和医生分发SOP任务。
-     * */
     public void obtainSopInfoData() {
         List<SopOrderLog> sopOrderLogs = sopOrderLogMapper.queryUnsentSOPData();
         if (CollectionUtils.isEmpty(sopOrderLogs)) {
             return;
         }
 
+        // 1. 提取非空 externalIds
+        List<Long> externalIds = sopOrderLogs.stream()
+                .filter(info -> info.getExId() != null)
+                .map(SopOrderLog::getExId)
+                .collect(Collectors.toList());
+
+        if (CollectionUtils.isEmpty(externalIds)) {
+            return;
+        }
+
+        // 2. 批量查询外部联系人;从qw_external_contact 表获取客户id,销售id
+        List<QwExternalContact> contacts = qwExternalContactMapper.selectQwExternalContactByIds(externalIds);
+        Map<Long, QwExternalContact> contactMap = contacts.stream()
+                .collect(Collectors.toMap(QwExternalContact::getId, contact -> contact));
+
+        // 3. 批量查询所有需要的公司用户
+        Set<Long> companyUserIds = contacts.stream()
+                .map(QwExternalContact::getCompanyUserId)
+                .collect(Collectors.toSet());
+        List<CompanyUser> companyUsers = companyUserMapper.selectCompanyUserByCompanyUserIds(companyUserIds);
+        Map<Long, CompanyUser> companyUserMap = companyUsers.stream()
+                .collect(Collectors.toMap(CompanyUser::getUserId, user -> user));
+
+        // 4. 遍历处理每条 SOP 日志信息
+        List<Long> ids = new ArrayList<>();
         for (SopOrderLog log : sopOrderLogs) {
-            List<SopUserLogsInfo> logsInfo = sopUserLogsInfoMapper.querySopUserLogsRecord(log.getSopId(),log.getIsBuy());
-            if (CollectionUtils.isEmpty(logsInfo)){
+            Long exId = log.getExId();
+            if (exId == null) {
+                logger.warn("跳过 exId 为空的记录");
                 continue;
             }
 
-            // 过滤 externalId 非空
-            List<Long> externalIds = logsInfo.stream()
-                    .filter(info -> info.getExternalId() != null)
-                    .map(SopUserLogsInfo::getExternalId)
-                    .collect(Collectors.toList());
-
-            if (CollectionUtils.isEmpty(externalIds)) {
+            // 获取联系人
+            QwExternalContact qwExternalContact = contactMap.get(exId);
+            if (qwExternalContact == null) {
+                logger.warn("未找到 exId: {} 对应的外部联系人", exId);
                 continue;
             }
 
-            // 构建 productBuyMap:根据 isBuy 类型选择字段
-            Map<Long, Integer> productBuyMap;
-            boolean isUpsell = (log.getIsBuy() == 3 || log.getIsBuy() == 4); // 是否升单
-
-            if (isUpsell) {//升单就取IsUpsellProductBuy属性值
-                productBuyMap = logsInfo.stream()
-                        .filter(info -> info.getExternalId() != null)
-                        .collect(Collectors.toMap(
-                                SopUserLogsInfo::getExternalId,
-                                SopUserLogsInfo::getIsUpsellProductBuy,
-                                (v1, v2) -> v1//防止重复key,一般同一个营期内不会有重复的
-                        ));
-            } else {//未升单就取IsProductBuy属性值
-                productBuyMap = logsInfo.stream()
-                        .filter(info -> info.getExternalId() != null)
-                        .collect(Collectors.toMap(
-                                SopUserLogsInfo::getExternalId,
-                                SopUserLogsInfo::getIsProductBuy,
-                                (v1, v2) -> v1//防止重复key
-                        ));
-            }
+            Long fsUserId = qwExternalContact.getFsUserId(); // 客户ID
+            Long companyUserId = qwExternalContact.getCompanyUserId(); // 销售ID
 
-            // 查询qw_external_contact 表获取客户id,销售id
-            List<QwExternalContact> contacts = qwExternalContactMapper.selectQwExternalContactByIds(externalIds);
-            if (CollectionUtils.isEmpty(contacts)) {
-                continue;
+            CompanyUser companyUser = companyUserMap.get(companyUserId);
+            if (companyUser == null) {
+                logger.warn("未找到 companyUserId: {} 对应的销售人员", companyUserId);
+                continue; // 防空指针
             }
 
-            for (QwExternalContact contact : contacts) {
-                Long fsUserId = contact.getFsUserId(); // 客户ID
-                Long companyUserId = contact.getCompanyUserId();//销售ID
+            Long doctorId = companyUser.getDoctorId(); // 医生ID
 
-                CompanyUser companyUser = companyUserMapper.selectCompanyUserByCompanyUserId(companyUserId);
-                if (companyUser == null) continue; // 防空指针
-
-                Long doctorId = companyUser.getDoctorId();//医生ID
-                //    - 根据当前外部联系人 ID(contact.getId()),从购买sop_user_logs_info表中获取其购买状态
-                //    - 如果是“未升单”场景,buyStatus 来自字段 isProductBuy(1=未购,2=已购)
-                //    - 如果是“升单”场景,buyStatus 来自字段 isUpsellProductBuy(1=未购,2=已购)
-                Integer buyStatus = productBuyMap.get(contact.getId());
+            // 确定 type:1~4
+            Integer isBuy = log.getIsBuy();
+            Integer type = determineType(isBuy, exId);
+            if (type == null) {
+                continue;
+            }
 
+            // 根据 orderSendType 分发任务
+            Integer orderSendType = log.getOrderSendType();
+            if (orderSendType == null) {
+                logger.warn("orderSendType 为空,跳过 exId: {}", exId);
+                continue;
+            }
+            // 处理订单发送类型
+            boolean success = processOrderSendType(orderSendType, fsUserId, companyUserId, doctorId, type, log.getExId());
+            if (success) {
+                ids.add(log.getId());
+            }
+        }
 
-                // 安全校验:确保 buyStatus 为 1(未购)或 2(已购)
-                if (buyStatus == null || (buyStatus != 1 && buyStatus != 2)) {
-                    logger.error("方法obtainSopInfoData():buyStatus 异常,externalId: {}, 值: {}", contact.getId(), buyStatus);
-                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
-                    throw new CustomException("分发医生和销售sop任务失败");
-                }
+        // 对于已经推送的数据需要修改sop库的sop_order_log表的status状态为1:已推送
+        if (!ids.isEmpty()) {
+            int i = sopOrderLogMapper.updateIsSentSOPDataByIds(ids);
+            if (i > 0) {
+                logger.info("修改了{}条sop_order_log表的status状态为1:已推送", i);
+            }
+        }
+    }
 
-                //核心映射:1/2 → 1/2(未升单),1/2 → 3/4(升单)
-                int type = isUpsell ? (buyStatus + 2) : buyStatus;
-
-                // 分发任务
-                switch (log.getOrderSendType()) {
-                    case 1:
-                        //分发给销售sop数据
-                        obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
-                        break;
-                    case 2:
-                        //分发给医生sop数据
-                        obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
-                        break;
-                    case 3:
-                        //分发给销售和医生sop数据
-                        obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
-                        obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
-                        break;
-                    default:
-                        logger.error("未知的type: {}", type);
-                        break;
-                }
+    /**
+     * 确定类型值
+     */
+    private Integer determineType(Integer isBuy, Long exId) {
+        if (isBuy == null) {
+            logger.warn("isBuy 为空,跳过 exId: {}", exId);
+            return null;
+        }
 
-            }
+        switch (isBuy) {
+            case 1:
+                return 1; // 未升单未购
+            case 2:
+                return 2; // 未升单已购
+            case 3:
+                return 3; // 升单未购
+            case 4:
+                return 4; // 升单已购
+            default:
+                logger.warn("未知 isBuy 值: {}, 跳过 exId: {}", isBuy, exId);
+                return null;
         }
-        //对于已经推送的数据需要修改sop库的sop_order_log表的status状态为1:已推送
-        List<Long> ids = sopOrderLogs.stream().map(SopOrderLog::getId).collect(Collectors.toList());
-        int i = sopOrderLogMapper.updateIsSentSOPDataByIds(ids);
-        if (i > 0){
-            logger.info("修改了{}条sop_order_log表的status状态为1:已推送", i);
+    }
+
+    /**
+     * 处理订单发送类型
+     */
+    private boolean processOrderSendType(Integer orderSendType, Long fsUserId, Long companyUserId, Long doctorId, Integer type, Long exId) {
+        switch (orderSendType) {
+            case 1:
+                // 分发给销售sop数据
+                return obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type, exId) == 1;
+            case 2:
+                // 分发给医生sop数据
+                return obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type, exId) == 1;
+            case 3:
+                // 分发给销售和医生sop数据
+                int result1 = obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type, exId);
+                int result2 = obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type, exId);
+                return result1 == 1 && result2 == 1;
+            default:
+                logger.error("未知的orderSendType: {}", orderSendType);
+                return false;
         }
     }
+
+
+    /**
+     * 定时向销售和医生分发SOP任务。
+     * */
+//    public void obtainSopInfoData2() {
+//        List<SopOrderLog> sopOrderLogs = sopOrderLogMapper.queryUnsentSOPData();
+//        if (CollectionUtils.isEmpty(sopOrderLogs)) {
+//            return;
+//        }
+//
+//        for (SopOrderLog log : sopOrderLogs) {
+//            List<SopUserLogsInfo> logsInfo = sopUserLogsInfoMapper.querySopUserLogsRecord(log.getSopId(),log.getIsBuy());
+//            if (CollectionUtils.isEmpty(logsInfo)){
+//                continue;
+//            }
+//
+//            // 过滤 externalId 非空
+//            List<Long> externalIds = logsInfo.stream()
+//                    .filter(info -> info.getExternalId() != null)
+//                    .map(SopUserLogsInfo::getExternalId)
+//                    .collect(Collectors.toList());
+//
+//            if (CollectionUtils.isEmpty(externalIds)) {
+//                continue;
+//            }
+//
+//            // 构建 productBuyMap:根据 isBuy 类型选择字段
+//            Map<Long, Integer> productBuyMap;
+//            boolean isUpsell = (log.getIsBuy() == 3 || log.getIsBuy() == 4); // 是否升单
+//
+//            if (isUpsell) {//升单就取IsUpsellProductBuy属性值
+//                productBuyMap = logsInfo.stream()
+//                        .filter(info -> info.getExternalId() != null)
+//                        .collect(Collectors.toMap(
+//                                SopUserLogsInfo::getExternalId,
+//                                SopUserLogsInfo::getIsUpsellProductBuy,
+//                                (v1, v2) -> v1//防止重复key,一般同一个营期内不会有重复的
+//                        ));
+//            } else {//未升单就取IsProductBuy属性值
+//                productBuyMap = logsInfo.stream()
+//                        .filter(info -> info.getExternalId() != null)
+//                        .collect(Collectors.toMap(
+//                                SopUserLogsInfo::getExternalId,
+//                                SopUserLogsInfo::getIsProductBuy,
+//                                (v1, v2) -> v1//防止重复key
+//                        ));
+//            }
+//
+//            // 查询qw_external_contact 表获取客户id,销售id
+//            List<QwExternalContact> contacts = qwExternalContactMapper.selectQwExternalContactByIds(externalIds);
+//            if (CollectionUtils.isEmpty(contacts)) {
+//                continue;
+//            }
+//
+//            for (QwExternalContact contact : contacts) {
+//                Long fsUserId = contact.getFsUserId(); // 客户ID
+//                Long companyUserId = contact.getCompanyUserId();//销售ID
+//
+//                CompanyUser companyUser = companyUserMapper.selectCompanyUserByCompanyUserId(companyUserId);
+//                if (companyUser == null) continue; // 防空指针
+//
+//                Long doctorId = companyUser.getDoctorId();//医生ID
+//                //    - 根据当前外部联系人 ID(contact.getId()),从购买sop_user_logs_info表中获取其购买状态
+//                //    - 如果是“未升单”场景,buyStatus 来自字段 isProductBuy(1=未购,2=已购)
+//                //    - 如果是“升单”场景,buyStatus 来自字段 isUpsellProductBuy(1=未购,2=已购)
+//                Integer buyStatus = productBuyMap.get(contact.getId());
+//
+//
+//                // 安全校验:确保 buyStatus 为 1(未购)或 2(已购)
+//                if (buyStatus == null || (buyStatus != 1 && buyStatus != 2)) {
+//                    logger.error("方法obtainSopInfoData():buyStatus 异常,externalId: {}, 值: {}", contact.getId(), buyStatus);
+//                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+//                    throw new CustomException("分发医生和销售sop任务失败");
+//                }
+//
+//                //核心映射:1/2 → 1/2(未升单),1/2 → 3/4(升单)
+//                int type = isUpsell ? (buyStatus + 2) : buyStatus;
+//
+//                // 分发任务
+//                switch (log.getOrderSendType()) {
+//                    case 1:
+//                        //分发给销售sop数据
+//                        obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
+//                        break;
+//                    case 2:
+//                        //分发给医生sop数据
+//                        obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
+//                        break;
+//                    case 3:
+//                        //分发给销售和医生sop数据
+//                        obtainSopCompanyUserTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
+//                        obtainSopDoctorTaskData(fsUserId, companyUserId, doctorId, type,contact.getId());
+//                        break;
+//                    default:
+//                        logger.error("未知的type: {}", type);
+//                        break;
+//                }
+//
+//            }
+//        }
+//        //对于已经推送的数据需要修改sop库的sop_order_log表的status状态为1:已推送
+//        List<Long> ids = sopOrderLogs.stream().map(SopOrderLog::getId).collect(Collectors.toList());
+//        int i = sopOrderLogMapper.updateIsSentSOPDataByIds(ids);
+//        if (i > 0){
+//            logger.info("修改了{}条sop_order_log表的status状态为1:已推送", i);
+//        }
+//    }
     //辅助方法 增加医生和销售的sop推送数据
     /**
      *  增加sop医生推送数据

+ 2 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyUserMapper.java

@@ -328,4 +328,6 @@ public interface CompanyUserMapper
     Boolean bindCompanyUserReplyTxt(@Param("replyText") String replyText,@Param("userIds") List<Long> userIds);
 
     Boolean unbindCompanyUserReplyTxt(@Param("userIds")List<Long> userIds);
+
+    List<CompanyUser> selectCompanyUserByCompanyUserIds(Set<Long> companyUserIds);
 }

+ 8 - 0
fs-service/src/main/resources/mapper/company/CompanyUserMapper.xml

@@ -607,5 +607,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         order by create_time desc
         limit 1
     </select>
+    <select id="selectCompanyUserByCompanyUserIds" resultType="com.fs.company.domain.CompanyUser">
+        SELECT *
+        FROM company_user
+        WHERE user_id IN
+        <foreach collection="companyUserIds" item="id" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </select>
 
 </mapper>