Ver código fonte

优化群通链的销售关系

xw 1 semana atrás
pai
commit
c77258b72c

+ 157 - 92
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -1064,101 +1064,142 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
         return CollectionUtils.isEmpty(list) ? null : list.get(0);
     }
 
-    private R handleRoom(FsUserCourseVideoAddKfUParam param,FsUser user,String noRegisterMsg) {
-        //查询客户列表
-        List<QwExternalContact> contacts = qwExternalContactMapper.selectQwExternalContactListVOByfsUserId(user.getUserId());
-        List<QwExternalContact> nonNullContacts = contacts.stream()
-                .filter(Objects::nonNull)
-                .collect(Collectors.toList());
-        log.info("查出来的企微客户数量:" + nonNullContacts.size());
-        if (!nonNullContacts.isEmpty()) {
-            //找出对应销售匹配的客户
-            QwExternalContact matchedContact = nonNullContacts.stream()
-                    .filter(contact -> contact.getQwUserId().equals(Long.parseLong(param.getQwUserId())))
-                    .findFirst()
-                    .orElse(null);
-
-            if (matchedContact != null) {
-                log.info("匹配到的第一个企微用户:" + matchedContact.getUserId());
-                log.info("企微id:" + matchedContact.getId());
-                log.info("用户:" + param.getVideoId());
-                log.info("企微用户:" + param.getQwUserId());
-                param.setQwExternalId(matchedContact.getId());
-                FsCourseWatchLog log = courseWatchLogMapper.getWatchCourseVideoByExt(matchedContact.getId(), param.getVideoId(), param.getQwUserId());
-                if (log == null) {
-                    createWatchLog(param);
-                }
-                return R.error(567, "群聊通用链接").put("qwExternalId", matchedContact.getId());
-            } else {
-                QwExternalContact contact = nonNullContacts.get(0);
-                log.info("匹配到的第一个企微用户:" + contact.getUserId());
-                log.info("企微id:" + contact.getId());
-                log.info("用户:" + param.getVideoId());
-                log.info("企微用户:" + param.getQwUserId());
-                param.setQwExternalId(contact.getId());
-                FsCourseWatchLog log = courseWatchLogMapper.getWatchCourseVideoByExt(contact.getId(), param.getVideoId(), param.getQwUserId());
-                if (log == null) {
-                    createWatchLog(param);
-                }
-                return R.error(567, "群聊通用链接").put("qwExternalId", contact.getId());
+    /**
+     * 解析当前销售对应的外部联系人,避免重粉场景 externalId 与 qwUser 错配。
+     */
+    private QwExternalContact resolveExternalContactForQwUser(Long externalId, QwUser qwUser, Long fsUserId) {
+        if (qwUser == null || qwUser.getId() == null) {
+            return externalId != null ? qwExternalContactMapper.selectQwExternalContactById(externalId) : null;
+        }
+        if (externalId != null) {
+            QwExternalContact contact = qwExternalContactMapper.selectQwExternalContactById(externalId);
+            if (contact != null && qwUser.getId().equals(contact.getQwUserId())
+                    && isValidExternalContactStatus(contact.getStatus())) {
+                return contact;
+            }
+            if (contact != null && !qwUser.getId().equals(contact.getQwUserId())) {
+                log.warn("外部联系人与当前销售不匹配,尝试纠正: externalId={}, qwUserId={}, contactQwUserId={}",
+                        externalId, qwUser.getId(), contact.getQwUserId());
             }
         }
-
-        boolean bool1 = "今正科技".equals(cloudHostProper.getCompanyName());
-        boolean bool2 = "弘珍医药".equals(cloudHostProper.getCompanyName());
-        if (bool1 || bool2) {
-            //一个都找不到
-            List<QwExternalContact> qwExternalContactList = qwExternalContactMapper.selectQwExternalByUnionID(user.getUnionId());
-
-            if (qwExternalContactList != null && !qwExternalContactList.isEmpty()) {
-                QwExternalContact UnionEXt = qwExternalContactList.get(0);
-                for (QwExternalContact qwExternalContact : qwExternalContactList) {
-                    if (qwExternalContact.getFsUserId() == null || qwExternalContact.getFsUserId() != 0L) {
-                        qwExternalContact.setFsUserId(user.getUserId());
-                        qwExternalContactMapper.updateQwExternalContact(qwExternalContact);
-                    }
-                    try {
-                        if (qwExternalContact.getQwUserId().equals(Long.parseLong(param.getQwUserId()))) {
-                            UnionEXt = qwExternalContact;
-                        }
-                    } catch (Exception e) {
-                        log.error("群聊链接匹配销售失败");
-                    }
-
-                    log.info("匹配到的第一个企微用户:" + UnionEXt.getUserId());
-                    log.info("企微id:" + UnionEXt.getId());
-                    log.info("用户:" + param.getVideoId());
-                    log.info("企微用户:" + param.getQwUserId());
-                    param.setQwExternalId(UnionEXt.getId());
-                    FsCourseWatchLog log = courseWatchLogMapper.getWatchCourseVideoByExt(UnionEXt.getId(), param.getVideoId(), param.getQwUserId());
-                    if (log == null) {
-                        param.setUserId(user.getUserId());
-                        createWatchLog(param);
-                    } else {
-                        if (log.getUserId() == null || log.getUserId().equals(0L) || !log.getUserId().equals(param.getUserId())) {
-                            log.setUserId(param.getUserId());
-                        }
-                        log.setUpdateTime(new Date());
-                        log.setAppId(param.getAppId());
-                        courseWatchLogMapper.updateFsCourseWatchLog(log);
-                    }
-                    return R.error(567, "群聊通用链接").put("qwExternalId", UnionEXt.getId());
-                }
+        QueryWrapper<QwExternalContact> wrapper = new QueryWrapper<QwExternalContact>()
+                .eq("qw_user_id", qwUser.getId());
+        if (StringUtils.isNotEmpty(qwUser.getCorpId())) {
+            wrapper.eq("corp_id", qwUser.getCorpId());
+        }
+        if (fsUserId != null && fsUserId > 0) {
+            wrapper.eq("fs_user_id", fsUserId);
+        } else if (externalId != null) {
+            QwExternalContact wrongContact = qwExternalContactMapper.selectQwExternalContactById(externalId);
+            if (wrongContact != null && wrongContact.getFsUserId() != null && wrongContact.getFsUserId() > 0) {
+                wrapper.eq("fs_user_id", wrongContact.getFsUserId());
             }
         }
+        return selectValidQwExternalContact(wrapper);
+    }
 
-        // ========== 【handleRoom-记录用户-项目-销售绑定关系】 ==========
-//        if (param.getQwExternalId() != null) {
-//            recordUserProjectBind(param, param.getQwExternalId(), "handleRoom");
-//        }
-        // ========== 【绑定关系记录结束】 ==========
+    private boolean isValidExternalContactStatus(Integer status) {
+        return status != null && Arrays.asList(0, 1, 2).contains(status);
+    }
 
-     /*   String msg = "<div style=\"color: red;margin-bottom: 15px;font-weight: bold;\">本课程为群会员独享<br>请长按二维码</div>\n" +
-                "\t\t\t\t\t<div style=\"color: #999;font-size: 14px;font-weight: bold;\">添加伴学助手免费领取会员权限</div>";*/
+    private QwExternalContact resolveExternalContactForRoom(FsUser user, FsUserCourseVideoAddKfUParam param) {
+        if (user == null || StringUtils.isEmpty(param.getQwUserId())) {
+            return null;
+        }
+        try {
+            Long qwUserId = Long.parseLong(param.getQwUserId());
+            QueryWrapper<QwExternalContact> boundWrapper = new QueryWrapper<QwExternalContact>()
+                    .eq("qw_user_id", qwUserId)
+                    .eq("fs_user_id", user.getUserId());
+            if (StringUtils.isNotEmpty(param.getCorpId())) {
+                boundWrapper.eq("corp_id", param.getCorpId());
+            }
+            QwExternalContact matched = selectValidQwExternalContact(boundWrapper);
+            if (matched != null) {
+                return matched;
+            }
+            QueryWrapper<QwExternalContact> unboundWrapper = new QueryWrapper<QwExternalContact>()
+                    .eq("qw_user_id", qwUserId);
+            if (StringUtils.isNotEmpty(param.getCorpId())) {
+                unboundWrapper.eq("corp_id", param.getCorpId());
+            }
+            if (StringUtils.isNotEmpty(user.getUnionId())) {
+                unboundWrapper.eq("unionid", user.getUnionId());
+            }
+            return selectValidQwExternalContact(unboundWrapper);
+        } catch (NumberFormatException e) {
+            log.error("群聊通用链接匹配销售失败,qwUserId 非法: {}", param.getQwUserId());
+            return null;
+        }
+    }
 
-        return R.error(noRegisterMsg);
+    private QwExternalContact resolveExternalContactByUnionId(FsUser user, FsUserCourseVideoAddKfUParam param) {
+        if (user == null || StringUtils.isEmpty(user.getUnionId()) || StringUtils.isEmpty(param.getQwUserId())) {
+            return null;
+        }
+        List<QwExternalContact> contactList = qwExternalContactMapper.selectQwExternalByUnionID(user.getUnionId());
+        if (CollectionUtils.isEmpty(contactList)) {
+            return null;
+        }
+        Long qwUserId;
+        try {
+            qwUserId = Long.parseLong(param.getQwUserId());
+        } catch (NumberFormatException e) {
+            log.error("群聊链接匹配销售失败,qwUserId 非法: {}", param.getQwUserId());
+            return null;
+        }
+        QwExternalContact matched = null;
+        for (QwExternalContact contact : contactList) {
+            if (contact == null) {
+                continue;
+            }
+            if (contact.getFsUserId() == null || contact.getFsUserId() == 0L) {
+                contact.setFsUserId(user.getUserId());
+                qwExternalContactMapper.updateQwExternalContact(contact);
+            }
+            if (qwUserId.equals(contact.getQwUserId()) && isValidExternalContactStatus(contact.getStatus())) {
+                matched = contact;
+            }
+        }
+        return matched;
+    }
 
+    private R processRoomExternalContact(FsUserCourseVideoAddKfUParam param, FsUser user, QwExternalContact matchedContact) {
+        log.info("群聊通用链接匹配外部联系人: extId={}, qwUserId={}, videoId={}",
+                matchedContact.getId(), param.getQwUserId(), param.getVideoId());
+        param.setQwExternalId(matchedContact.getId());
+        if (param.getUserId() == null) {
+            param.setUserId(user.getUserId());
+        }
+        FsCourseWatchLog watchLog = courseWatchLogMapper.getWatchCourseVideoByExt(
+                matchedContact.getId(), param.getVideoId(), param.getQwUserId());
+        if (watchLog == null) {
+            createWatchLog(param);
+        } else if (watchLog.getUserId() == null || watchLog.getUserId().equals(0L)
+                || !watchLog.getUserId().equals(user.getUserId())) {
+            watchLog.setUserId(user.getUserId());
+            watchLog.setUpdateTime(new Date());
+            watchLog.setAppId(param.getAppId());
+            courseWatchLogMapper.updateFsCourseWatchLog(watchLog);
+        }
+        return R.error(567, "群聊通用链接").put("qwExternalId", matchedContact.getId());
+    }
 
+    private R handleRoom(FsUserCourseVideoAddKfUParam param,FsUser user,String noRegisterMsg) {
+        QwExternalContact matchedContact = resolveExternalContactForRoom(user, param);
+        if (matchedContact == null) {
+            boolean bool1 = "今正科技".equals(cloudHostProper.getCompanyName());
+            boolean bool2 = "弘珍医药".equals(cloudHostProper.getCompanyName());
+            if (bool1 || bool2) {
+                matchedContact = resolveExternalContactByUnionId(user, param);
+            }
+        }
+        if (matchedContact == null) {
+            log.warn("群聊通用链接未匹配到当前销售的外部联系人: userId={}, qwUserId={}, corpId={}",
+                    user.getUserId(), param.getQwUserId(), param.getCorpId());
+            return R.error(noRegisterMsg);
+        }
+        return processRoomExternalContact(param, user, matchedContact);
     }
 
     private void createWatchLog(FsUserCourseVideoAddKfUParam param) {
@@ -3555,11 +3596,17 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
             return R.error().put("msg", "企业不存在,请联系管理员");
         }
 
+        QwExternalContact externalContact = resolveExternalContactForQwUser(param.getExternalUserId(), qwUser, param.getFsUserId());
+        if (externalContact == null) {
+            return R.error("客户与当前销售关系未建立,请确认客户已添加后再发课");
+        }
+        Long externalContactId = externalContact.getId();
+
         //看课记录
-        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, param.getExternalUserId(),2);
+        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, externalContactId,2);
 
         //生成小程序链接
-        String linkByMiniApp = createLinkByMiniApp(new Date(), param.getCourseId(), param.getVideoId(), qwUser, param.getExternalUserId(), 2, null, 0);
+        String linkByMiniApp = createLinkByMiniApp(new Date(), param.getCourseId(), param.getVideoId(), qwUser, externalContactId, 2, null, 0);
 
         JSONObject news = new JSONObject(true);
         news.put("miniprogramAppid", qwCompany.getMiniAppId());
@@ -3599,9 +3646,15 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
             domainName = config.getRealLinkDomainName();
         }
 
-        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, param.getExternalUserId(),2);
+        QwExternalContact externalContact = resolveExternalContactForQwUser(param.getExternalUserId(), qwUser, param.getFsUserId());
+        if (externalContact == null) {
+            return R.error("客户与当前销售关系未建立,请确认客户已添加后再发课");
+        }
+        Long externalContactId = externalContact.getId();
+
+        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, externalContactId,2);
 
-        String linkByCartLink = createLinkByMiniApp(new Date(), param.getCourseId(), param.getVideoId(), qwUser, param.getExternalUserId(), 1, domainName, 0);
+        String linkByCartLink = createLinkByMiniApp(new Date(), param.getCourseId(), param.getVideoId(), qwUser, externalContactId, 1, domainName, 0);
 
 
         //生成卡片链接
@@ -3640,12 +3693,17 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
         Long courseId = param.getCourseId();
         Long qwUserId = qwUser.getId();
         Date createTime = DateUtils.getNowDate();
-        Long externalUserId = param.getExternalUserId();
         Long videoId = param.getVideoId();
         Long companyId = qwUser.getCompanyId();
         Long companyUserId = qwUser.getCompanyUserId();
 
-        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, param.getExternalUserId(), 2);
+        QwExternalContact externalContact = resolveExternalContactForQwUser(param.getExternalUserId(), qwUser, param.getFsUserId());
+        if (externalContact == null) {
+            return R.error("客户与当前销售关系未建立,请确认客户已添加后再发课");
+        }
+        Long externalUserId = externalContact.getId();
+
+        addWatchLogIfNeeded(param.getVideoId(), param.getCourseId(), param.getFsUserId(), qwUser, externalUserId, 2);
 
         Map<String, String> feishuH5Link = createFeishuH5Link(param.getCorpId(), createTime, courseId.intValue(), videoId.intValue(), String.valueOf(qwUserId), companyUserId, companyId, externalUserId, config, null);
         String shortCode = feishuH5Link.get("link");
@@ -3667,6 +3725,13 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
                                      Long fsUserId, QwUser qwUser, Long externalId,Integer watchType) {
 
         try {
+            QwExternalContact resolvedContact = resolveExternalContactForQwUser(externalId, qwUser, fsUserId);
+            if (resolvedContact == null) {
+                logger.error("插入观看记录失败,未找到当前销售对应的外部联系人: externalId={}, qwUserId={}, fsUserId={}",
+                        externalId, qwUser != null ? qwUser.getId() : null, fsUserId);
+                return;
+            }
+            externalId = resolvedContact.getId();
 
             FsCourseWatchLog watchLog = new FsCourseWatchLog();
             watchLog.setVideoId(videoId);