Просмотр исходного кода

部门发课可优先走主备小程序

xw 1 день назад
Родитель
Сommit
a127ff1e62

+ 8 - 0
fs-company/src/main/java/com/fs/company/controller/company/CompanyDeptController.java

@@ -139,6 +139,10 @@ public class CompanyDeptController extends BaseController
         {
         {
             return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
             return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
         }
         }
+        String miniAppError = deptService.validateDeptMiniApp(dept.getCompanyId(), dept.getMiniAppMaster(), dept.getMiniAppServer());
+        if (miniAppError != null) {
+            return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败," + miniAppError);
+        }
         dept.setCreateBy(SecurityUtils.getUsername());
         dept.setCreateBy(SecurityUtils.getUsername());
         return toAjax(deptService.insertCompanyDept(dept));
         return toAjax(deptService.insertCompanyDept(dept));
     }
     }
@@ -161,6 +165,10 @@ public class CompanyDeptController extends BaseController
                 && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
                 && deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
             return AjaxResult.error("该部门包含未停用的子部门!");
             return AjaxResult.error("该部门包含未停用的子部门!");
         }
         }
+        String miniAppError = deptService.validateDeptMiniApp(dept.getCompanyId(), dept.getMiniAppMaster(), dept.getMiniAppServer());
+        if (miniAppError != null) {
+            return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败," + miniAppError);
+        }
         dept.setUpdateBy(SecurityUtils.getUsername());
         dept.setUpdateBy(SecurityUtils.getUsername());
         return toAjax(deptService.updateCompanyDept(dept));
         return toAjax(deptService.updateCompanyDept(dept));
     }
     }

+ 77 - 29
fs-ipad-task/src/main/java/com/fs/app/service/IpadSendServer.java

@@ -9,7 +9,11 @@ import com.fs.common.exception.base.BaseException;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.common.utils.spring.SpringUtils;
 import com.fs.common.utils.spring.SpringUtils;
+import com.fs.company.domain.CompanyDept;
 import com.fs.company.domain.CompanyMiniapp;
 import com.fs.company.domain.CompanyMiniapp;
+import com.fs.company.domain.CompanyUser;
+import com.fs.company.mapper.CompanyDeptMapper;
+import com.fs.company.mapper.CompanyUserMapper;
 import com.fs.company.service.ICompanyMiniappService;
 import com.fs.company.service.ICompanyMiniappService;
 import com.fs.course.domain.FsCoursePlaySourceConfig;
 import com.fs.course.domain.FsCoursePlaySourceConfig;
 import com.fs.course.domain.FsCourseWatchLog;
 import com.fs.course.domain.FsCourseWatchLog;
@@ -60,6 +64,8 @@ public class IpadSendServer {
     private final IQwUserVideoService qwUserVideoService;
     private final IQwUserVideoService qwUserVideoService;
     private final RedisCache redisCache;
     private final RedisCache redisCache;
     private final ICompanyMiniappService companyMiniappService;
     private final ICompanyMiniappService companyMiniappService;
+    private final CompanyUserMapper companyUserMapper;
+    private final CompanyDeptMapper companyDeptMapper;
     private final IFsCoursePlaySourceConfigService playSourceConfigService;
     private final IFsCoursePlaySourceConfigService playSourceConfigService;
     private final FsUserMapper fsUserMapper;
     private final FsUserMapper fsUserMapper;
     private final FsUserCourseVideoMapper fsUserCourseVideoMapper;
     private final FsUserCourseVideoMapper fsUserCourseVideoMapper;
@@ -68,7 +74,7 @@ public class IpadSendServer {
     private static final List<String> PROJECT_NAMES = Arrays.asList("济南联志健康", "北京存在文化", "宽益堂");
     private static final List<String> PROJECT_NAMES = Arrays.asList("济南联志健康", "北京存在文化", "宽益堂");
     private final LiveWatchLogMapper liveWatchLogMapper;
     private final LiveWatchLogMapper liveWatchLogMapper;
 
 
-    private void sendMiniProgram(BaseVo vo, QwSopCourseFinishTempSetting.Setting content, Map<String, FsCoursePlaySourceConfig> miniMap, Long companyId) {
+    private void sendMiniProgram(BaseVo vo, QwSopCourseFinishTempSetting.Setting content, Map<String, FsCoursePlaySourceConfig> miniMap, Long companyId, Long companyUserId) {
         log.info("ID:{} 小程序发送前关键字段 - miniprogramPage:{}, miniprogramTitle:{}, miniprogramAppid:{}, articleUrl:{}",
         log.info("ID:{} 小程序发送前关键字段 - miniprogramPage:{}, miniprogramTitle:{}, miniprogramAppid:{}, articleUrl:{}",
             vo.getId(), content.getMiniprogramPage(), content.getMiniprogramTitle(), content.getMiniprogramAppid(), content.getArticleUrl());
             vo.getId(), content.getMiniprogramPage(), content.getMiniprogramTitle(), content.getMiniprogramAppid(), content.getArticleUrl());
 
 
@@ -87,14 +93,14 @@ public class IpadSendServer {
             forceCompanyApp = !companyAppIds.isEmpty();
             forceCompanyApp = !companyAppIds.isEmpty();
         }
         }
         final Set<String> allowedCompanyAppIds = companyAppIds;
         final Set<String> allowedCompanyAppIds = companyAppIds;
+        if (companyId != null) {
+            listAll = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().eq("company_id", companyId));
+        }
         // 判断销售工时ID不为空并且有小程序类型
         // 判断销售工时ID不为空并且有小程序类型
         if (companyId != null && content.getMiniType() != null) {
         if (companyId != null && content.getMiniType() != null) {
-            // 获取销售公司下面绑定的主备小程序,并且根据当前应该发送的主备类型查询出数据
-            listAll = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().eq("company_id", companyId));
-            List<CompanyMiniapp> list = listAll.stream().filter(e -> e.getType().equals(content.getMiniType())).collect(Collectors.toList());
-            // 判断当前绑定的最新的小程序,并且覆盖以前的值(可以达到实时替换小程序的功能)
-            if (!list.isEmpty() && list.get(0) != null && StringUtils.isNotEmpty(list.get(0).getAppId())) {
-                appid = list.get(0).getAppId();
+            String resolvedAppId = resolveAppIdByType(companyUserId, content.getMiniType(), listAll);
+            if (StringUtils.isNotEmpty(resolvedAppId)) {
+                appid = resolvedAppId;
             }
             }
             // 获取配置文件里面的项目名称
             // 获取配置文件里面的项目名称
             String signProjectName = SpringUtils.getProperty("cloud_host.company_name");
             String signProjectName = SpringUtils.getProperty("cloud_host.company_name");
@@ -139,10 +145,9 @@ public class IpadSendServer {
                                     appid = availableList.get(0).getAppid();
                                     appid = availableList.get(0).getAppid();
                                     log.info("ID:{}, qwUserId:{},externalId:{},判断看过一个小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                     log.info("ID:{}, qwUserId:{},externalId:{},判断看过一个小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                 } else {
                                 } else {
-                                    // 用户看过的小程序已完全封禁,使用备用小程序
-                                    Optional<CompanyMiniapp> first = listAll.stream().filter(e -> e.getType() == 1).findFirst();
-                                    if (first.isPresent()) {
-                                        appid = first.get().getAppId();
+                                    String backupAppId = resolveAppIdByType(companyUserId, 1, listAll);
+                                    if (StringUtils.isNotEmpty(backupAppId)) {
+                                        appid = backupAppId;
                                         log.info("ID:{}, qwUserId:{},externalId:{},看过的小程序已封禁,使用备用小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                         log.info("ID:{}, qwUserId:{},externalId:{},看过的小程序已封禁,使用备用小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                     }
                                     }
                                 }
                                 }
@@ -156,10 +161,9 @@ public class IpadSendServer {
                                     appid = optional2.get().getAppid();
                                     appid = optional2.get().getAppid();
                                     log.info("ID:{}, qwUserId:{},externalId:{},找到正常小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                     log.info("ID:{}, qwUserId:{},externalId:{},找到正常小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                 } else {
                                 } else {
-                                    Optional<CompanyMiniapp> first = listAll.stream().filter(e -> e.getType() == 1).findFirst();
-                                    // 查找备用小程序发送备用记录ID
-                                    if (first.isPresent()) {
-                                        appid = first.get().getAppId();
+                                    String backupAppId = resolveAppIdByType(companyUserId, 1, listAll);
+                                    if (StringUtils.isNotEmpty(backupAppId)) {
+                                        appid = backupAppId;
                                         log.info("ID:{}, qwUserId:{},externalId:{},找到备用小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                         log.info("ID:{}, qwUserId:{},externalId:{},找到备用小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                     } else {
                                     } else {
                                         log.info("ID:{}, qwUserId:{},externalId:{},没找到小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                         log.info("ID:{}, qwUserId:{},externalId:{},没找到小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
@@ -174,11 +178,9 @@ public class IpadSendServer {
                             LocalDateTime lastTime = LocalDateTime.of(2025, 11, 10, 23, 59, 59);
                             LocalDateTime lastTime = LocalDateTime.of(2025, 11, 10, 23, 59, 59);
                             // 判断客户创建时间是在这个之前还是之后
                             // 判断客户创建时间是在这个之前还是之后
                             int listIndex = createTime.isAfter(lastTime) ? 1 : 0;
                             int listIndex = createTime.isAfter(lastTime) ? 1 : 0;
-                            // 获取公司里面的主备小程序,根据用户创建时间来发送主备,如果创建时间大于上看的时间就发送备用小程序,如果小于就发送主要小程序
-                            List<CompanyMiniapp> collect2 = listAll.stream().filter(e -> e.getType().equals(listIndex)).collect(Collectors.toList());
-                            // 判断是否获取到了配置
-                            if (!collect2.isEmpty() && collect2.get(0) != null && StringUtils.isNotEmpty(collect2.get(0).getAppId())) {
-                                appid = collect2.get(0).getAppId();
+                            String resolvedByTime = resolveAppIdByType(companyUserId, listIndex, listAll);
+                            if (StringUtils.isNotEmpty(resolvedByTime)) {
+                                appid = resolvedByTime;
                                 log.info("ID:{}, qwUserId:{},externalId:{},发送小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                                 log.info("ID:{}, qwUserId:{},externalId:{},发送小程序:{}", vo.getId(), vo.getQwUserId(), vo.getExId(), appid);
                             }
                             }
                         }
                         }
@@ -193,11 +195,18 @@ public class IpadSendServer {
             }
             }
         }
         }
         if (forceCompanyApp && StringUtils.isNotEmpty(appid) && !allowedCompanyAppIds.contains(appid)) {
         if (forceCompanyApp && StringUtils.isNotEmpty(appid) && !allowedCompanyAppIds.contains(appid)) {
-            Optional<String> preferredAppId = listAll.stream()
-                    .filter(e -> e.getType().equals(content.getMiniType()) && StringUtils.isNotEmpty(e.getAppId()))
-                    .map(CompanyMiniapp::getAppId)
-                    .filter(allowedCompanyAppIds::contains)
-                    .findFirst();
+            String deptAppId = resolveDeptMiniAppId(companyUserId, content.getMiniType());
+            Optional<String> preferredAppId = Optional.empty();
+            if (StringUtils.isNotEmpty(deptAppId) && allowedCompanyAppIds.contains(deptAppId)) {
+                preferredAppId = Optional.of(deptAppId);
+            }
+            if (!preferredAppId.isPresent()) {
+                preferredAppId = listAll.stream()
+                        .filter(e -> e.getType().equals(content.getMiniType()) && StringUtils.isNotEmpty(e.getAppId()))
+                        .map(CompanyMiniapp::getAppId)
+                        .filter(allowedCompanyAppIds::contains)
+                        .findFirst();
+            }
             if (!preferredAppId.isPresent()) {
             if (!preferredAppId.isPresent()) {
                 preferredAppId = allowedCompanyAppIds.stream().findFirst();
                 preferredAppId = allowedCompanyAppIds.stream().findFirst();
             }
             }
@@ -211,9 +220,9 @@ public class IpadSendServer {
             courseMaConfig = null;
             courseMaConfig = null;
         }
         }
         if (courseMaConfig == null) {
         if (courseMaConfig == null) {
-            List<CompanyMiniapp> list = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().eq("company_id", companyId).eq("type", 1));
-            if (!list.isEmpty() && list.get(0) != null && StringUtils.isNotEmpty(list.get(0).getAppId())) {
-                courseMaConfig = miniMap.get(list.get(0).getAppId());
+            String backupAppId = resolveAppIdByType(companyUserId, 1, listAll);
+            if (StringUtils.isNotEmpty(backupAppId)) {
+                courseMaConfig = miniMap.get(backupAppId);
             }
             }
         }
         }
         if (courseMaConfig == null && forceCompanyApp) {
         if (courseMaConfig == null && forceCompanyApp) {
@@ -251,6 +260,45 @@ public class IpadSendServer {
         }
         }
     }
     }
 
 
+    private String resolveDeptMiniAppId(Long companyUserId, Integer miniType) {
+        if (companyUserId == null || miniType == null) {
+            return null;
+        }
+        CompanyUser companyUser = companyUserMapper.selectCompanyUserById(companyUserId);
+        if (companyUser == null || companyUser.getDeptId() == null) {
+            return null;
+        }
+        CompanyDept dept = companyDeptMapper.selectCompanyDeptById(companyUser.getDeptId());
+        if (dept == null) {
+            return null;
+        }
+        if (miniType == 0) {
+            return dept.getMiniAppMaster();
+        }
+        if (miniType == 1) {
+            return dept.getMiniAppServer();
+        }
+        return null;
+    }
+
+    private String resolveAppIdByType(Long companyUserId, Integer miniType, List<CompanyMiniapp> companyMiniapps) {
+        if (miniType == null) {
+            return null;
+        }
+        String deptAppId = resolveDeptMiniAppId(companyUserId, miniType);
+        if (StringUtils.isNotEmpty(deptAppId)) {
+            return deptAppId;
+        }
+        if (companyMiniapps == null || companyMiniapps.isEmpty()) {
+            return null;
+        }
+        return companyMiniapps.stream()
+                .filter(e -> miniType.equals(e.getType()) && StringUtils.isNotEmpty(e.getAppId()))
+                .map(CompanyMiniapp::getAppId)
+                .findFirst()
+                .orElse(null);
+    }
+
     private boolean isConfigBoundToCompany(FsCoursePlaySourceConfig config, Long companyId) {
     private boolean isConfigBoundToCompany(FsCoursePlaySourceConfig config, Long companyId) {
         if (config == null || companyId == null || StringUtils.isEmpty(config.getCompanyIds())) {
         if (config == null || companyId == null || StringUtils.isEmpty(config.getCompanyIds())) {
             return false;
             return false;
@@ -809,7 +857,7 @@ public class IpadSendServer {
                 case "4":
                 case "4":
                 case "10":
                 case "10":
                 case "13":
                 case "13":
-                    sendMiniProgram(vo, content, miniMap, qwUser.getCompanyId());
+                    sendMiniProgram(vo, content, miniMap, qwUser.getCompanyId(), qwUser.getCompanyUserId());
                     break;
                     break;
                 case "5":
                 case "5":
                     // 文件
                     // 文件

+ 43 - 54
fs-qw-task/src/main/java/com/fs/app/taskService/impl/SopLogsTaskServiceImpl.java

@@ -10,11 +10,15 @@ import com.fs.common.utils.CloudHostUtils;
 import com.fs.common.utils.PubFun;
 import com.fs.common.utils.PubFun;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.company.domain.Company;
 import com.fs.company.domain.Company;
+import com.fs.company.domain.CompanyDept;
 import com.fs.company.domain.CompanyMiniapp;
 import com.fs.company.domain.CompanyMiniapp;
 import com.fs.company.domain.CompanyUser;
 import com.fs.company.domain.CompanyUser;
+import com.fs.company.mapper.CompanyDeptMapper;
 import com.fs.company.mapper.CompanyMapper;
 import com.fs.company.mapper.CompanyMapper;
+import com.fs.company.mapper.CompanyUserMapper;
 import com.fs.company.service.ICompanyMiniappService;
 import com.fs.company.service.ICompanyMiniappService;
 import com.fs.company.service.ICompanyUserService;
 import com.fs.company.service.ICompanyUserService;
+import com.fs.company.util.MiniAppIdResolver;
 import com.fs.config.cloud.CloudHostProper;
 import com.fs.config.cloud.CloudHostProper;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.domain.*;
 import com.fs.course.domain.*;
@@ -185,6 +189,12 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
     @Autowired
     @Autowired
     private CompanyMapper companyMapper;
     private CompanyMapper companyMapper;
 
 
+    @Autowired
+    private CompanyDeptMapper companyDeptMapper;
+
+    @Autowired
+    private CompanyUserMapper companyUserMapper;
+
     @Autowired
     @Autowired
     private AsyncCourseWatchFinishService asyncCourseWatchFinishService;
     private AsyncCourseWatchFinishService asyncCourseWatchFinishService;
 
 
@@ -358,7 +368,8 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
 
 
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
-
+        Map<Long, CompanyDept> deptMiniAppMap = MiniAppIdResolver.buildDeptMiniAppMap(companyDeptMapper.queryDeptDataAll());
+        Map<Long, Long> companyUserDeptMap = MiniAppIdResolver.buildCompanyUserDeptMap(companyUserMapper.selectAllCompanyUserList());
 
 
         List<Company> companies = companyMapper.selectCompanyAllList();
         List<Company> companies = companyMapper.selectCompanyAllList();
 
 
@@ -369,7 +380,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
         for (Map.Entry<String, List<SopUserLogsVo>> entry : sopLogsGroupedById.entrySet()) {
         for (Map.Entry<String, List<SopUserLogsVo>> entry : sopLogsGroupedById.entrySet()) {
             String sopId = entry.getKey();
             String sopId = entry.getKey();
             List<SopUserLogsVo> userLogsVos = entry.getValue();
             List<SopUserLogsVo> userLogsVos = entry.getValue();
-            processSopGroupAsync(sopId, userLogsVos, sopGroupLatch,currentTime, groupChatMap,config,miniMap,companies);
+            processSopGroupAsync(sopId, userLogsVos, sopGroupLatch,currentTime, groupChatMap,config,miniMap,companies, deptMiniAppMap, companyUserDeptMap);
         }
         }
 
 
         // 等待所有 SOP 分组处理完成
         // 等待所有 SOP 分组处理完成
@@ -391,9 +402,9 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
     )
     )
     public void processSopGroupAsync(String sopId, List<SopUserLogsVo> userLogsVos, CountDownLatch latch ,LocalDateTime currentTime,
     public void processSopGroupAsync(String sopId, List<SopUserLogsVo> userLogsVos, CountDownLatch latch ,LocalDateTime currentTime,
                                      Map<String, QwGroupChat> groupChatMap,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                      Map<String, QwGroupChat> groupChatMap,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                     List<Company> companies) {
+                                     List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         try {
         try {
-            processSopGroup(sopId, userLogsVos,currentTime, groupChatMap, config,miniMap,companies);
+            processSopGroup(sopId, userLogsVos,currentTime, groupChatMap, config,miniMap,companies, deptMiniAppMap, companyUserDeptMap);
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("处理 SOP ID {} 时发生异常: {}", sopId, e.getMessage(), e);
             log.error("处理 SOP ID {} 时发生异常: {}", sopId, e.getMessage(), e);
         } finally {
         } finally {
@@ -404,7 +415,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
 
 
     private void processSopGroup(String sopId, List<SopUserLogsVo> userLogsVos,LocalDateTime currentTime, Map<String,
     private void processSopGroup(String sopId, List<SopUserLogsVo> userLogsVos,LocalDateTime currentTime, Map<String,
                                          QwGroupChat> groupChatMap,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                          QwGroupChat> groupChatMap,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                 List<Company> companies) throws Exception {
+                                 List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) throws Exception {
         QwSopRuleTimeVO ruleTimeVO = sopMapper.selectQwSopByClickHouseId(sopId);
         QwSopRuleTimeVO ruleTimeVO = sopMapper.selectQwSopByClickHouseId(sopId);
 
 
         if (ruleTimeVO == null) {
         if (ruleTimeVO == null) {
@@ -447,7 +458,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
         CountDownLatch userLogsLatch = new CountDownLatch(userLogsVos.size());
         CountDownLatch userLogsLatch = new CountDownLatch(userLogsVos.size());
         for (SopUserLogsVo logVo : userLogsVos) {
         for (SopUserLogsVo logVo : userLogsVos) {
             processUserLogAsync(logVo, ruleTimeVO, rulesList, userLogsLatch, currentTime, groupChatMap,qwCompany.getMiniAppId(),
             processUserLogAsync(logVo, ruleTimeVO, rulesList, userLogsLatch, currentTime, groupChatMap,qwCompany.getMiniAppId(),
-                    config,miniMap,companies);
+                    config,miniMap,companies, deptMiniAppMap, companyUserDeptMap);
         }
         }
 
 
         // 等待所有用户日志处理完成
         // 等待所有用户日志处理完成
@@ -469,9 +480,9 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
     public void processUserLogAsync(SopUserLogsVo logVo, QwSopRuleTimeVO ruleTimeVO, List<QwSopTempRules> tempSettings,
     public void processUserLogAsync(SopUserLogsVo logVo, QwSopRuleTimeVO ruleTimeVO, List<QwSopTempRules> tempSettings,
                                     CountDownLatch latch, LocalDateTime currentTime, Map<String, QwGroupChat> groupChatMap,
                                     CountDownLatch latch, LocalDateTime currentTime, Map<String, QwGroupChat> groupChatMap,
                                     String miniAppId,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                     String miniAppId,CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                    List<Company> companies) {
+                                    List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         try {
         try {
-            processUserLog(logVo, ruleTimeVO, tempSettings,currentTime, groupChatMap, miniAppId, config,miniMap,companies);
+            processUserLog(logVo, ruleTimeVO, tempSettings,currentTime, groupChatMap, miniAppId, config,miniMap,companies, deptMiniAppMap, companyUserDeptMap);
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("处理用户日志 {} 时发生异常: {}", logVo.getId(), e.getMessage(), e);
             log.error("处理用户日志 {} 时发生异常: {}", logVo.getId(), e.getMessage(), e);
         } finally {
         } finally {
@@ -483,7 +494,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
     private void processUserLog(SopUserLogsVo logVo, QwSopRuleTimeVO ruleTimeVO, List<QwSopTempRules> tempSettings,
     private void processUserLog(SopUserLogsVo logVo, QwSopRuleTimeVO ruleTimeVO, List<QwSopTempRules> tempSettings,
                                 LocalDateTime currentTime, Map<String, QwGroupChat> groupChatMap,String miniAppId,
                                 LocalDateTime currentTime, Map<String, QwGroupChat> groupChatMap,String miniAppId,
                                 CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                 CourseConfig config,Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                List<Company> companies) {
+                                List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         try {
         try {
 
 
             LocalDate startDate = LocalDate.parse(logVo.getStartTime(), DATE_FORMATTER);
             LocalDate startDate = LocalDate.parse(logVo.getStartTime(), DATE_FORMATTER);
@@ -740,7 +751,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
 
 
                         insertSopUserLogs(sopUserLogsInfos, logVo, sendTime, ruleTimeVO, content, qwUserId,
                         insertSopUserLogs(sopUserLogsInfos, logVo, sendTime, ruleTimeVO, content, qwUserId,
                                 companyUserId, companyId, qwUserByRedis.getWelcomeText(),qwUserByRedis.getQwUserName(),
                                 companyUserId, companyId, qwUserByRedis.getWelcomeText(),qwUserByRedis.getQwUserName(),
-                                groupChatMap, miniAppId,config,miniMap, sendMsgType,companies);
+                                groupChatMap, miniAppId,config,miniMap, sendMsgType,companies, deptMiniAppMap, companyUserDeptMap);
 
 
                     }
                     }
                 } catch (Exception e) {
                 } catch (Exception e) {
@@ -786,7 +797,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                                    String qwUserId,String companyUserId,String companyId,String welcomeText,String qwUserName,
                                    String qwUserId,String companyUserId,String companyId,String welcomeText,String qwUserName,
                                    Map<String, QwGroupChat> groupChatMap,String miniAppId,CourseConfig config,
                                    Map<String, QwGroupChat> groupChatMap,String miniAppId,CourseConfig config,
                                    Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap, Integer sendMsgType,
                                    Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap, Integer sendMsgType,
-                                   List<Company> companies) {
+                                   List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         String formattedSendTime = sendTime.toInstant()
         String formattedSendTime = sendTime.toInstant()
                 .atZone(ZoneId.systemDefault())
                 .atZone(ZoneId.systemDefault())
                 .format(DATE_TIME_FORMATTER);
                 .format(DATE_TIME_FORMATTER);
@@ -866,7 +877,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
             QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, groupChat.getChatId(), groupChat.getName(), null, isOfficial, null,null);
             QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, groupChat.getChatId(), groupChat.getName(), null, isOfficial, null,null);
             handleLogBasedOnType(sopLogs, content, logVo, sendTime, courseId, videoId,
             handleLogBasedOnType(sopLogs, content, logVo, sendTime, courseId, videoId,
                     type, qwUserId, companyUserId, companyId, groupChat.getChatId(), welcomeText, qwUserName,
                     type, qwUserId, companyUserId, companyId, groupChat.getChatId(), welcomeText, qwUserName,
-                    null, true, miniAppId, groupChat,config, miniMap, null, sendMsgType,companies,liveId);
+                    null, true, miniAppId, groupChat,config, miniMap, null, sendMsgType,companies,liveId, deptMiniAppMap, companyUserDeptMap);
             }
             }
 //            if (content.getIndex() == 0) {
 //            if (content.getIndex() == 0) {
 //                QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, groupChat.getChatId(), groupChat.getName(), null, isOfficial, null);
 //                QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, groupChat.getChatId(), groupChat.getName(), null, isOfficial, null);
@@ -915,7 +926,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                     QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, contactId.getExternalContactId(), externalUserName, fsUserId, isOfficial, contactId.getExternalId(),contactId.getIsDaysNotStudy());
                     QwSopLogs sopLogs = createBaseLog(formattedSendTime, logVo, ruleTimeVO, contactId.getExternalContactId(), externalUserName, fsUserId, isOfficial, contactId.getExternalId(),contactId.getIsDaysNotStudy());
                     handleLogBasedOnType(sopLogs, content, logVo, sendTime, courseId, videoId,
                     handleLogBasedOnType(sopLogs, content, logVo, sendTime, courseId, videoId,
                             type, qwUserId, companyUserId, companyId, externalId, welcomeText, qwUserName, fsUserId, false, miniAppId,
                             type, qwUserId, companyUserId, companyId, externalId, welcomeText, qwUserName, fsUserId, false, miniAppId,
-                            null,config, miniMap, grade, sendMsgType,companies,liveId);
+                            null,config, miniMap, grade, sendMsgType,companies,liveId, deptMiniAppMap, companyUserDeptMap);
                 } catch (Exception e) {
                 } catch (Exception e) {
                     e.printStackTrace();
                     e.printStackTrace();
                     log.error("处理 externalContactId {} 时发生异常", contactId, e);
                     log.error("处理 externalContactId {} 时发生异常", contactId, e);
@@ -1074,7 +1085,8 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                                       String qwUserName, Long fsUserId, boolean isGroupChat, String miniAppId,
                                       String qwUserName, Long fsUserId, boolean isGroupChat, String miniAppId,
                                       QwGroupChat groupChat,CourseConfig config,
                                       QwGroupChat groupChat,CourseConfig config,
                                       Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                       Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                      Integer grade, Integer sendMsgType ,List<Company> companies ,Long liveId) {
+                                      Integer grade, Integer sendMsgType ,List<Company> companies ,Long liveId,
+                                      Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         switch (type) {
         switch (type) {
             case 1:
             case 1:
                 handleNormalMessage(sopLogs, content,companyUserId,companyId,isGroupChat,qwUserId,groupChat,externalId,logVo);
                 handleNormalMessage(sopLogs, content,companyUserId,companyId,isGroupChat,qwUserId,groupChat,externalId,logVo);
@@ -1082,7 +1094,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
             case 2:
             case 2:
                 handleCourseMessage(sopLogs, content, logVo, sendTime, courseId, videoId,
                 handleCourseMessage(sopLogs, content, logVo, sendTime, courseId, videoId,
                         qwUserId, companyUserId, companyId, externalId, welcomeText,qwUserName, fsUserId,
                         qwUserId, companyUserId, companyId, externalId, welcomeText,qwUserName, fsUserId,
-                        isGroupChat, miniAppId, groupChat,config,miniMap, grade, sendMsgType,companies);
+                        isGroupChat, miniAppId, groupChat,config,miniMap, grade, sendMsgType,companies, deptMiniAppMap, companyUserDeptMap);
                 break;
                 break;
             case 3:
             case 3:
                 handleOrderMessage(sopLogs, content);
                 handleOrderMessage(sopLogs, content);
@@ -1300,7 +1312,7 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                                      String companyId, String externalId, String welcomeText, String qwUserName,
                                      String companyId, String externalId, String welcomeText, String qwUserName,
                                      Long fsUserId, boolean isGroupChat, String miniAppId, QwGroupChat groupChat,CourseConfig config,Map<Long,
                                      Long fsUserId, boolean isGroupChat, String miniAppId, QwGroupChat groupChat,CourseConfig config,Map<Long,
                     Map<Integer, List<CompanyMiniapp>>> miniMap,Integer grade, Integer sendMsgType,
                     Map<Integer, List<CompanyMiniapp>>> miniMap,Integer grade, Integer sendMsgType,
-                                     List<Company> companies) {
+                                     List<Company> companies, Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap) {
         QwExternalContact contact = null;
         QwExternalContact contact = null;
         if(logVo.getExternalId() != null){
         if(logVo.getExternalId() != null){
             contact = qwExternalContactMapper.selectById(logVo.getExternalId());
             contact = qwExternalContactMapper.selectById(logVo.getExternalId());
@@ -1428,8 +1440,8 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                         setting.setMiniprogramAppid(miniAppId);
                         setting.setMiniprogramAppid(miniAppId);
                     }else {
                     }else {
                         int miniType = getLevel(grade);
                         int miniType = getLevel(grade);
-                        //算主备小程序
-                        String finalAppId = getAppIdFromMiniMap(miniMap, companyId, sendMsgType, grade);
+                        Long deptId = MiniAppIdResolver.getDeptId(Long.parseLong(companyUserId), companyUserDeptMap);
+                        String finalAppId = getAppIdFromMiniMap(miniMap, companyId, sendMsgType, grade, deptId, deptMiniAppMap);
 
 
                         if (StringUtil.strIsNullOrEmpty(finalAppId)) {
                         if (StringUtil.strIsNullOrEmpty(finalAppId)) {
                             finalAppId = miniAppId;
                             finalAppId = miniAppId;
@@ -1545,7 +1557,8 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                         setting.setMiniprogramAppid(miniAppId);
                         setting.setMiniprogramAppid(miniAppId);
                     }else {
                     }else {
                         int miniType = getLevel(grade);
                         int miniType = getLevel(grade);
-                        String finalAppId = getAppIdFromMiniMap(miniMap, companyId, sendMsgType, grade);
+                        Long deptIdArticle = MiniAppIdResolver.getDeptId(Long.parseLong(companyUserId), companyUserDeptMap);
+                        String finalAppId = getAppIdFromMiniMap(miniMap, companyId, sendMsgType, grade, deptIdArticle, deptMiniAppMap);
 
 
                         if (StringUtil.strIsNullOrEmpty(finalAppId)) {
                         if (StringUtil.strIsNullOrEmpty(finalAppId)) {
                             finalAppId = articleSysConfig.getAppId();
                             finalAppId = articleSysConfig.getAppId();
@@ -1593,27 +1606,12 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
     private String getAppIdFromMiniMap(Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
     private String getAppIdFromMiniMap(Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                        String companyId,
                                        String companyId,
                                        Integer sendMsgType,
                                        Integer sendMsgType,
-                                       Integer grade) {
-        if (miniMap == null || miniMap.isEmpty() || sendMsgType == null || sendMsgType != 1) {
-            return null;
-        }
-
-        Map<Integer, List<CompanyMiniapp>> gradeMap = miniMap.get(Long.valueOf(companyId));
-        if (gradeMap == null) {
-            return null;
-        }
-
+                                       Integer grade,
+                                       Long deptId,
+                                       Map<Long, CompanyDept> deptMiniAppMap) {
         int listIndex = getLevel(grade);
         int listIndex = getLevel(grade);
-        List<CompanyMiniapp> miniapps = gradeMap.get(listIndex);
-
-        if (miniapps == null || miniapps.isEmpty()) {
-            return null;
-        }
-
-        CompanyMiniapp companyMiniapp = miniapps.get(0);
-        return (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId()))
-                ? companyMiniapp.getAppId()
-                : null;
+        return MiniAppIdResolver.resolveMiniAppId(
+                deptId, Long.valueOf(companyId), listIndex, sendMsgType, miniMap, deptMiniAppMap);
     }
     }
 
 
     /**
     /**
@@ -2779,6 +2777,8 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
         Date dataTime = new Date();
         Date dataTime = new Date();
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
+        Map<Long, CompanyDept> deptMiniAppMap = MiniAppIdResolver.buildDeptMiniAppMap(companyDeptMapper.queryDeptDataAll());
+        Map<Long, Long> companyUserDeptMap = MiniAppIdResolver.buildCompanyUserDeptMap(companyUserMapper.selectAllCompanyUserList());
 
 
         QwCompany qwCompany = iQwCompanyService.getQwCompanyByRedis(sopLogs.getCorpId());
         QwCompany qwCompany = iQwCompanyService.getQwCompanyByRedis(sopLogs.getCorpId());
         QwUser qwUser = qwExternalContactService.getQwUserByRedis(sopLogs.getCorpId(), sopLogs.getQwUserid());
         QwUser qwUser = qwExternalContactService.getQwUserByRedis(sopLogs.getCorpId(), sopLogs.getQwUserid());
@@ -2804,21 +2804,10 @@ public class SopLogsTaskServiceImpl implements SopLogsTaskService {
                             String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), sopLogs.getExternalId(), config);
                             String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), sopLogs.getExternalId(), config);
 
 
 
 
-                    String miniAppId = null;
-                    if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                        Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(qwUser.getCompanyId()));
-                        if (integerListMap != null) {
-                            int listIndex = getLevel(grade);
-                            List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                            if (miniapps != null && !miniapps.isEmpty()) {
-                                CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                    miniAppId = companyMiniapp.getAppId();
-                                }
-                            }
-                        }
-                    }
+                    int listIndex = getLevel(grade);
+                    Long deptId = MiniAppIdResolver.getDeptId(qwUser.getCompanyUserId(), companyUserDeptMap);
+                    String miniAppId = MiniAppIdResolver.resolveMiniAppId(
+                            deptId, qwUser.getCompanyId(), listIndex, qwUser.getSendMsgType(), miniMap, deptMiniAppMap);
 
 
                     if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
                     if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
                         miniAppId = qwCompany.getMiniAppId();
                         miniAppId = qwCompany.getMiniAppId();

+ 22 - 0
fs-service/src/main/java/com/fs/company/domain/CompanyDept.java

@@ -64,6 +64,12 @@ public class CompanyDept extends BaseEntity
     @Excel(name = "是否删除")
     @Excel(name = "是否删除")
     private Integer isDel;
     private Integer isDel;
 
 
+    /** 主小程序appId */
+    private String miniAppMaster;
+
+    /** 备小程序appId */
+    private String miniAppServer;
+
     private List<CompanyDept> children = new ArrayList<CompanyDept>();
     private List<CompanyDept> children = new ArrayList<CompanyDept>();
 
 
     public List<CompanyDept> getChildren() {
     public List<CompanyDept> getChildren() {
@@ -183,6 +189,22 @@ public class CompanyDept extends BaseEntity
         return isDel;
         return isDel;
     }
     }
 
 
+    public String getMiniAppMaster() {
+        return miniAppMaster;
+    }
+
+    public void setMiniAppMaster(String miniAppMaster) {
+        this.miniAppMaster = miniAppMaster;
+    }
+
+    public String getMiniAppServer() {
+        return miniAppServer;
+    }
+
+    public void setMiniAppServer(String miniAppServer) {
+        this.miniAppServer = miniAppServer;
+    }
+
     @Override
     @Override
     public String toString() {
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

+ 5 - 0
fs-service/src/main/java/com/fs/company/service/ICompanyDeptService.java

@@ -91,4 +91,9 @@ public interface ICompanyDeptService
      * @return 部门
      * @return 部门
      */
      */
     CompanyDept getDefaultCompanyDeptByCompanyId(Long companyId);
     CompanyDept getDefaultCompanyDeptByCompanyId(Long companyId);
+
+    /**
+     * 校验部门主备小程序是否属于本公司可配置列表
+     */
+    String validateDeptMiniApp(Long companyId, String miniAppMaster, String miniAppServer);
 }
 }

+ 28 - 0
fs-service/src/main/java/com/fs/company/service/impl/CompanyDeptServiceImpl.java

@@ -10,7 +10,9 @@ import com.fs.company.domain.CompanyDeptTreeSelect;
 import com.fs.company.domain.CompanyRole;
 import com.fs.company.domain.CompanyRole;
 import com.fs.company.mapper.CompanyDeptMapper;
 import com.fs.company.mapper.CompanyDeptMapper;
 import com.fs.company.mapper.CompanyRoleMapper;
 import com.fs.company.mapper.CompanyRoleMapper;
+import com.fs.company.service.ICompanyConfigService;
 import com.fs.company.service.ICompanyDeptService;
 import com.fs.company.service.ICompanyDeptService;
+import com.fs.company.vo.CompanyMiniAppVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
@@ -18,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -34,6 +37,9 @@ public class CompanyDeptServiceImpl implements ICompanyDeptService
 
 
     @Autowired
     @Autowired
     private CompanyRoleMapper roleMapper;
     private CompanyRoleMapper roleMapper;
+
+    @Autowired
+    private ICompanyConfigService companyConfigService;
     /**
     /**
      * 查询部门
      * 查询部门
      *
      *
@@ -291,4 +297,26 @@ public class CompanyDeptServiceImpl implements ICompanyDeptService
     {
     {
         return getChildList(list, t).size() > 0 ? true : false;
         return getChildList(list, t).size() > 0 ? true : false;
     }
     }
+
+    @Override
+    public String validateDeptMiniApp(Long companyId, String miniAppMaster, String miniAppServer) {
+        if (StringUtils.isEmpty(miniAppMaster) && StringUtils.isEmpty(miniAppServer)) {
+            return null;
+        }
+        List<CompanyMiniAppVO> playSourceMiniApps = companyConfigService.getCompanyMiniAppListByCompany(companyId);
+        if (playSourceMiniApps == null || playSourceMiniApps.isEmpty()) {
+            return "未找到本公司的小程序配置,请先在点播播放源配置中维护本公司小程序";
+        }
+        Set<String> validAppIds = playSourceMiniApps.stream()
+                .map(CompanyMiniAppVO::getAppid)
+                .filter(StringUtils::isNotEmpty)
+                .collect(Collectors.toSet());
+        if (StringUtils.isNotEmpty(miniAppMaster) && !validAppIds.contains(miniAppMaster)) {
+            return "主小程序不在本公司可配置列表中";
+        }
+        if (StringUtils.isNotEmpty(miniAppServer) && !validAppIds.contains(miniAppServer)) {
+            return "备小程序不在本公司可配置列表中";
+        }
+        return null;
+    }
 }
 }

+ 122 - 0
fs-service/src/main/java/com/fs/company/util/MiniAppIdResolver.java

@@ -0,0 +1,122 @@
+package com.fs.company.util;
+
+import com.fs.common.utils.StringUtils;
+import com.fs.company.domain.CompanyDept;
+import com.fs.company.domain.CompanyMiniapp;
+import com.fs.company.domain.CompanyUser;
+import com.fs.voice.utils.StringUtil;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * 小程序SOP工具类,根据用户、部门、企业信息解析对应小程序appId,区分主小程序/服务小程序
+ */
+public final class MiniAppIdResolver {
+
+    // 私有构造,禁止实例化工具类
+    private MiniAppIdResolver() {
+    }
+
+    /**
+     * 构建部门ID -> 部门信息映射,只过滤配置了主小程序/服务小程序的部门
+     * @param depts 部门列表
+     * @return Map<部门ID, 部门实体>
+     */
+    public static Map<Long, CompanyDept> buildDeptMiniAppMap(List<CompanyDept> depts) {
+        if (depts == null || depts.isEmpty()) {
+            return new HashMap<>();
+        }
+        return depts.stream()
+                .filter(d -> StringUtils.isNotEmpty(d.getMiniAppMaster()) || StringUtils.isNotEmpty(d.getMiniAppServer()))
+                // key重复保留第一个部门数据
+                .collect(Collectors.toMap(CompanyDept::getDeptId, d -> d, (oldVal, newVal) -> oldVal));
+    }
+
+    /**
+     * 构建用户ID -> 部门ID映射关系
+     * @param users 企业用户列表
+     * @return Map<用户ID, 部门ID>
+     */
+    public static Map<Long, Long> buildCompanyUserDeptMap(List<CompanyUser> users) {
+        if (users == null || users.isEmpty()) {
+            return new HashMap<>();
+        }
+        Map<Long, Long> map = new HashMap<>(users.size());
+        for (CompanyUser user : users) {
+            Long userId = user.getUserId();
+            Long deptId = user.getDeptId();
+            if (userId != null && deptId != null) {
+                map.put(userId, deptId);
+            }
+        }
+        return map;
+    }
+
+    /**
+     * 根据用户ID获取所属部门ID
+     * @param companyUserId 用户ID
+     * @param companyUserDeptMap 用户-部门映射表
+     * @return 部门ID,无匹配返回null
+     */
+    public static Long getDeptId(Long companyUserId, Map<Long, Long> companyUserDeptMap) {
+        if (companyUserId == null || companyUserDeptMap == null) {
+            return null;
+        }
+        return companyUserDeptMap.get(companyUserId);
+    }
+
+    /**
+     * 解析最终可用小程序AppId
+     * 优先级:部门自定义小程序 > 企业级通用小程序
+     * @param deptId 部门ID
+     * @param companyId 企业ID
+     * @param listIndex 小程序类型下标 0=主小程序 1=服务小程序
+     * @param sendMsgType 消息发送类型,仅等于1时读取企业通用小程序
+     * @param miniMap 企业小程序总映射:key企业ID -> key消息类型 -> 小程序列表
+     * @param deptMiniAppMap 带小程序配置的部门映射表
+     * @return 匹配到的小程序AppId,无配置返回null
+     */
+    public static String resolveMiniAppId(Long deptId,
+                                          Long companyId,
+                                          int listIndex,
+                                          Integer sendMsgType,
+                                          Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
+                                          Map<Long, CompanyDept> deptMiniAppMap) {
+        // 1.优先读取部门单独配置的小程序
+        if (deptId != null && deptMiniAppMap != null && !deptMiniAppMap.isEmpty()) {
+            CompanyDept dept = deptMiniAppMap.get(deptId);
+            if (dept != null) {
+                String targetAppId = listIndex == 0 ? dept.getMiniAppMaster() : dept.getMiniAppServer();
+                if (StringUtils.isNotEmpty(targetAppId)) {
+                    return targetAppId;
+                }
+            }
+        }
+
+        // 消息类型非1、企业小程序映射为空、企业ID为空,直接返回空
+        if (miniMap == null || miniMap.isEmpty() || sendMsgType == null || !sendMsgType.equals(1) || companyId == null) {
+            return null;
+        }
+
+        // 2.读取企业统一配置的小程序
+        Map<Integer, List<CompanyMiniapp>> typeMiniAppMap = miniMap.get(companyId);
+        if (typeMiniAppMap == null) {
+            return null;
+        }
+
+        List<CompanyMiniapp> miniAppList = typeMiniAppMap.get(listIndex);
+        if (miniAppList == null || miniAppList.isEmpty()) {
+            return null;
+        }
+
+        // 取列表第一条小程序配置
+        CompanyMiniapp firstMiniApp = miniAppList.get(0);
+        if (firstMiniApp != null && !StringUtil.strIsNullOrEmpty(firstMiniApp.getAppId())) {
+            return firstMiniApp.getAppId();
+        }
+        return null;
+    }
+}

+ 57 - 173
fs-service/src/main/java/com/fs/sop/service/impl/SopUserLogsInfoServiceImpl.java

@@ -14,10 +14,13 @@ import com.fs.common.utils.PubFun;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.common.utils.date.DateUtil;
 import com.fs.company.domain.Company;
 import com.fs.company.domain.Company;
+import com.fs.company.domain.CompanyDept;
 import com.fs.company.domain.CompanyMiniapp;
 import com.fs.company.domain.CompanyMiniapp;
 import com.fs.company.mapper.CompanyMapper;
 import com.fs.company.mapper.CompanyMapper;
+import com.fs.company.mapper.CompanyDeptMapper;
 import com.fs.company.mapper.CompanyUserMapper;
 import com.fs.company.mapper.CompanyUserMapper;
 import com.fs.company.service.ICompanyMiniappService;
 import com.fs.company.service.ICompanyMiniappService;
+import com.fs.company.util.MiniAppIdResolver;
 import com.fs.config.cloud.CloudHostProper;
 import com.fs.config.cloud.CloudHostProper;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.config.CourseConfig;
 import com.fs.course.domain.*;
 import com.fs.course.domain.*;
@@ -176,6 +179,9 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
     @Autowired
     @Autowired
     private CompanyMapper companyMapper;
     private CompanyMapper companyMapper;
 
 
+    @Autowired
+    private CompanyDeptMapper companyDeptMapper;
+
     @Autowired
     @Autowired
     private IQwSopTempVoiceService sopTempVoiceService;
     private IQwSopTempVoiceService sopTempVoiceService;
 
 
@@ -534,6 +540,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
 
 
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
+        Map<Long, CompanyDept> deptMiniAppMap = MiniAppIdResolver.buildDeptMiniAppMap(companyDeptMapper.queryDeptDataAll());
+        Map<Long, Long> companyUserDeptMap = MiniAppIdResolver.buildCompanyUserDeptMap(companyUserMapper.selectAllCompanyUserList());
         QwCompany qwCompany = iQwCompanyService.getQwCompanyByRedis(param.getCorpId());
         QwCompany qwCompany = iQwCompanyService.getQwCompanyByRedis(param.getCorpId());
         if (qwCompany == null ) {
         if (qwCompany == null ) {
             return  R.error().put("msg","企业不存在,请联系管理员");
             return  R.error().put("msg","企业不存在,请联系管理员");
@@ -685,26 +693,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                                 String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                 String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                         qwUser.getId(), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), null, config, qwGroupChat.getChatId());
                                         qwUser.getId(), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), null, config, qwGroupChat.getChatId());
 
 
-                                String miniAppId = null;
-
                                 int listIndex = 1;
                                 int listIndex = 1;
-                                if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                    Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(qwUser.getCompanyId());
-                                    if (integerListMap != null) {
-                                        List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                                        if (miniapps != null && !miniapps.isEmpty()) {
-                                            CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                            if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                                miniAppId = companyMiniapp.getAppId();
-                                            }
-                                        }
-                                    }
-                                }
-
-                                if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                    miniAppId = qwCompany.getMiniAppId();
-                                }
+                                String miniAppId = resolveSopMiniAppId(qwUser, qwUser.getCompanyId(), listIndex, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
                                 st.setMiniType(listIndex);
                                 st.setMiniType(listIndex);
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                     st.setMiniprogramAppid(miniAppId);
                                     st.setMiniprogramAppid(miniAppId);
@@ -744,24 +734,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                                 linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, qwGroupChat.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                 linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, qwGroupChat.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                         String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(),null ,config, groupUser.getChatId());
                                         String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(),null ,config, groupUser.getChatId());
 
 
-                                miniAppId = null;
-                                if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                    Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                                    if (integerListMap != null) {
-                                        int listIndexTemp = 1;
-                                        List<CompanyMiniapp> miniapps = integerListMap.get(listIndexTemp);
-                                        if (miniapps != null && !miniapps.isEmpty()) {
-                                            CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                            if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                                miniAppId = companyMiniapp.getAppId();
-                                            }
-                                        }
-                                    }
-                                }
-
-                                if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                    miniAppId = qwCompany.getMiniAppId();
-                                }
+                                int listIndexTemp = 1;
+                                miniAppId = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndexTemp, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
 
 
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                     st.setMiniprogramAppid(miniAppId);
                                     st.setMiniprogramAppid(miniAppId);
@@ -903,26 +877,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                                 String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                 String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                         qwUser.getId(), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), null, config, groupChat.getChatId());
                                         qwUser.getId(), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(), null, config, groupChat.getChatId());
 
 
-                                String miniAppId = null;
-
                                 int listIndex = 1;
                                 int listIndex = 1;
-                                if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                    Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(qwUser.getCompanyId());
-                                    if (integerListMap != null) {
-                                        List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                                        if (miniapps != null && !miniapps.isEmpty()) {
-                                            CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                            if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                                miniAppId = companyMiniapp.getAppId();
-                                            }
-                                        }
-                                    }
-                                }
-
-                                if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                    miniAppId = qwCompany.getMiniAppId();
-                                }
+                                String miniAppId = resolveSopMiniAppId(qwUser, qwUser.getCompanyId(), listIndex, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
                                 st.setMiniType(listIndex);
                                 st.setMiniType(listIndex);
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                     st.setMiniprogramAppid(miniAppId);
                                     st.setMiniprogramAppid(miniAppId);
@@ -968,24 +924,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                                 String companyId = String.valueOf(qwUser.getCompanyId()).trim();
                                 String companyId = String.valueOf(qwUser.getCompanyId()).trim();
                                 linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, qwUser.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                 linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, qwUser.getCorpId(), new Date(), param.getCourseId(), param.getVideoId(),
                                         String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(),null, config, groupChat.getChatId());
                                         String.valueOf(qwUser.getId()), qwUser.getCompanyUserId().toString(), qwUser.getCompanyId().toString(),null, config, groupChat.getChatId());
-                                miniAppId = null;
-                                if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                    Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                                    if (integerListMap != null) {
-                                        int listIndexTemp = 1;
-                                        List<CompanyMiniapp> miniapps = integerListMap.get(listIndexTemp);
-                                        if (miniapps != null && !miniapps.isEmpty()) {
-                                            CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                            if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                                miniAppId = companyMiniapp.getAppId();
-                                            }
-                                        }
-                                    }
-                                }
-
-                                if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                    miniAppId = qwCompany.getMiniAppId();
-                                }
+                                int listIndexTemp = 1;
+                                miniAppId = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndexTemp, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
 
 
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                 if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                     st.setMiniprogramAppid(miniAppId);
                                     st.setMiniprogramAppid(miniAppId);
@@ -1170,27 +1110,9 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                             String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), createTime, param.getCourseId(), param.getVideoId(),
                             String linkByMiniApp = createLinkByMiniApp(st, param.getCorpId(), createTime, param.getCourseId(), param.getVideoId(),
                                     Long.valueOf(qwUserId), companyUserId, companyId, item.getExternalId(), config, null);
                                     Long.valueOf(qwUserId), companyUserId, companyId, item.getExternalId(), config, null);
 
 
-                            String miniAppId = null;
-
                             int effectiveGrade = (item.getGrade() == null) ? 5 : item.getGrade();
                             int effectiveGrade = (item.getGrade() == null) ? 5 : item.getGrade();
                             int listIndex = (effectiveGrade == 1 || effectiveGrade == 2) ? 0 : 1;
                             int listIndex = (effectiveGrade == 1 || effectiveGrade == 2) ? 0 : 1;
-                            if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                                if (integerListMap != null) {
-                                    List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                                    if (miniapps != null && !miniapps.isEmpty()) {
-                                        CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                        if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                            miniAppId = companyMiniapp.getAppId();
-                                        }
-                                    }
-                                }
-                            }
-
-                            if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                miniAppId = qwCompany.getMiniAppId();
-                            }
+                            String miniAppId = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndex, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
                             st.setMiniType(listIndex);
                             st.setMiniType(listIndex);
                             if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                             if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                                 st.setMiniprogramAppid(miniAppId);
                                 st.setMiniprogramAppid(miniAppId);
@@ -1304,36 +1226,15 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                             linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, param.getCorpId(), createTime, param.getCourseId(), param.getVideoId(),
                             linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, param.getCorpId(), createTime, param.getCourseId(), param.getVideoId(),
                                     qwUserId, companyUserId, companyId, item.getExternalId(), config,null);
                                     qwUserId, companyUserId, companyId, item.getExternalId(), config,null);
 
 
-                            miniAppId = null;
-
-                            if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                                Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                                if (integerListMap != null) {
-                                    int effectiveGradeTemp = (item.getGrade() == null) ? 5 : item.getGrade();
-                                    int listIndexTemp = (effectiveGradeTemp == 1 || effectiveGradeTemp == 2) ? 0 : 1;
-
-                                    //评级是6 S级,则走A类小程序
-                                    if (effectiveGradeTemp==6){
-                                        listIndexTemp=2;
-                                    }
-
-                                    List<CompanyMiniapp> miniapps = integerListMap.get(listIndexTemp);
-
-                                    if (miniapps != null && !miniapps.isEmpty()) {
-                                        CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                        if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                            miniAppId = companyMiniapp.getAppId();
-                                        }
-                                    }
-                                }
-                            }
-
-                            if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                                miniAppId = qwCompany.getMiniAppId();
+                            int effectiveGradeTemp = (item.getGrade() == null) ? 5 : item.getGrade();
+                            int listIndexTemp = (effectiveGradeTemp == 1 || effectiveGradeTemp == 2) ? 0 : 1;
+                            if (effectiveGradeTemp == 6) {
+                                listIndexTemp = 2;
                             }
                             }
+                            String miniAppId14 = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndexTemp, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
 
 
-                            if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
-                                st.setMiniprogramAppid(miniAppId);
+                            if (!StringUtil.strIsNullOrEmpty(miniAppId14)) {
+                                st.setMiniprogramAppid(miniAppId14);
                             } else {
                             } else {
                                 log.error("公司的小程序id为空:采用了前端传的固定值" + sopLogs.getSopId());
                                 log.error("公司的小程序id为空:采用了前端传的固定值" + sopLogs.getSopId());
                             }
                             }
@@ -1506,7 +1407,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
         // 查询公司关联小程序数据
         // 查询公司关联小程序数据
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         List<CompanyMiniapp> miniList = companyMiniappService.list(new QueryWrapper<CompanyMiniapp>().orderByAsc("sort_num"));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap = miniList.stream().collect(Collectors.groupingBy(CompanyMiniapp::getCompanyId, Collectors.groupingBy(CompanyMiniapp::getType)));
-
+        Map<Long, CompanyDept> deptMiniAppMap = MiniAppIdResolver.buildDeptMiniAppMap(companyDeptMapper.queryDeptDataAll());
+        Map<Long, Long> companyUserDeptMap = MiniAppIdResolver.buildCompanyUserDeptMap(companyUserMapper.selectAllCompanyUserList());
 
 
         List<Company> companies = companyMapper.selectCompanyAllList();
         List<Company> companies = companyMapper.selectCompanyAllList();
 
 
@@ -1554,7 +1456,7 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
 
 
                 if (qwUser.getCompanyUserId()!=null && qwUser.getCompanyId()!=null){
                 if (qwUser.getCompanyUserId()!=null && qwUser.getCompanyId()!=null){
                     List<QwSopLogs> sopLogsList = processInsertSopUserLogsInfo(logs, qwUser, param, words, config, qwCompany, finalSort,
                     List<QwSopLogs> sopLogsList = processInsertSopUserLogsInfo(logs, qwUser, param, words, config, qwCompany, finalSort,
-                            finalSendType,miniMap,companies);
+                            finalSendType,miniMap,companies, deptMiniAppMap, companyUserDeptMap);
 
 
                     //批量插入 发送记录
                     //批量插入 发送记录
                     if (!sopLogsList.isEmpty()) {
                     if (!sopLogsList.isEmpty()) {
@@ -1577,7 +1479,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
     private List<QwSopLogs> processInsertSopUserLogsInfo(List<SopUserLogsInfo> sopUserLogsInfos,QwUser qwUser,
     private List<QwSopLogs> processInsertSopUserLogsInfo(List<SopUserLogsInfo> sopUserLogsInfos,QwUser qwUser,
                                                          SendUserLogsInfoMsgParam param,List<FastGptChatReplaceWords> words,
                                                          SendUserLogsInfoMsgParam param,List<FastGptChatReplaceWords> words,
                                                          CourseConfig config,QwCompany qwCompany,int finalSort,int finalSendType,
                                                          CourseConfig config,QwCompany qwCompany,int finalSort,int finalSendType,
-                                                         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,List<Company> companies ){
+                                                         Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,List<Company> companies,
+                                                         Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap ){
 
 
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
 
@@ -1633,7 +1536,7 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                 case 5:
                 case 5:
                 case 20:
                 case 20:
                     List<QwSopCourseFinishTempSetting.Setting> list = processSetting(item,qwUser, param, words, config, qwCompany,companyUserId,companyId,
                     List<QwSopCourseFinishTempSetting.Setting> list = processSetting(item,qwUser, param, words, config, qwCompany,companyUserId,companyId,
-                            contact,dataTime, finalDomainName,miniMap,companies,sopLogs);
+                            contact,dataTime, finalDomainName,miniMap,companies,sopLogs, deptMiniAppMap, companyUserDeptMap);
                     setting.setSetting(list);
                     setting.setSetting(list);
                     break;
                     break;
                 case 9:
                 case 9:
@@ -1698,7 +1601,8 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                                                                       CourseConfig config,QwCompany qwCompany,String companyUserId, String companyId,
                                                                       CourseConfig config,QwCompany qwCompany,String companyUserId, String companyId,
                                                                       QwExternalContact contact,Date dataTime,String domainName,
                                                                       QwExternalContact contact,Date dataTime,String domainName,
                                                                       Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
                                                                       Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
-                                                                      List<Company> companies,QwSopLogs sopLogs ){
+                                                                      List<Company> companies,QwSopLogs sopLogs,
+                                                                      Map<Long, CompanyDept> deptMiniAppMap, Map<Long, Long> companyUserDeptMap ){
         List<QwSopCourseFinishTempSetting.Setting> list = JSONArray.parseArray(param.getSetting(),QwSopCourseFinishTempSetting.Setting.class);
         List<QwSopCourseFinishTempSetting.Setting> list = JSONArray.parseArray(param.getSetting(),QwSopCourseFinishTempSetting.Setting.class);
 
 
         for (QwSopCourseFinishTempSetting.Setting st : list) {
         for (QwSopCourseFinishTempSetting.Setting st : list) {
@@ -1766,27 +1670,9 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                             qwUser.getId(), companyUserId, companyId, item.getExternalId(), config, null);
                             qwUser.getId(), companyUserId, companyId, item.getExternalId(), config, null);
 
 
 
 
-                    String miniAppId = null;
-
                     int effectiveGrade = (item.getGrade() == null) ? 5 : item.getGrade();
                     int effectiveGrade = (item.getGrade() == null) ? 5 : item.getGrade();
                     int listIndex = (effectiveGrade == 1 || effectiveGrade == 2) ? 0 : 1;
                     int listIndex = (effectiveGrade == 1 || effectiveGrade == 2) ? 0 : 1;
-                    if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                        Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                        if (integerListMap != null) {
-                            List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                            if (miniapps != null && !miniapps.isEmpty()) {
-                                CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                    miniAppId = companyMiniapp.getAppId();
-                                }
-                            }
-                        }
-                    }
-
-                    if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                        miniAppId = qwCompany.getMiniAppId();
-                    }
+                    String miniAppId = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndex, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
                     st.setMiniType(listIndex);
                     st.setMiniType(listIndex);
                     if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                     if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
                         st.setMiniprogramAppid(miniAppId);
                         st.setMiniprogramAppid(miniAppId);
@@ -1944,36 +1830,15 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
                     linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, param.getCorpId(), dataTime, param.getCourseId(), param.getVideoId(),
                     linkByMiniApp = createActivityLinkByMiniApp(st,sopLogs, param.getCorpId(), dataTime, param.getCourseId(), param.getVideoId(),
                             String.valueOf(qwUser.getId()), companyUserId, companyId, item.getExternalId(), config,null);
                             String.valueOf(qwUser.getId()), companyUserId, companyId, item.getExternalId(), config,null);
 
 
-                    miniAppId = null;
-
-                    if (!miniMap.isEmpty() && qwUser.getSendMsgType() == 1) {
-                        Map<Integer, List<CompanyMiniapp>> integerListMap = miniMap.get(Long.valueOf(companyId));
-                        if (integerListMap != null) {
-                            effectiveGrade = (item.getGrade() == null) ? 5 : item.getGrade();
-                            listIndex = (effectiveGrade == 1 || effectiveGrade == 2) ? 0 : 1;
-
-                            //评级是6 S级,则走A类小程序
-                            if (effectiveGrade==6){
-                                listIndex=2;
-                            }
-
-                            List<CompanyMiniapp> miniapps = integerListMap.get(listIndex);
-
-                            if (miniapps != null && !miniapps.isEmpty()) {
-                                CompanyMiniapp companyMiniapp = miniapps.get(0);
-                                if (companyMiniapp != null && !StringUtil.strIsNullOrEmpty(companyMiniapp.getAppId())) {
-                                    miniAppId = companyMiniapp.getAppId();
-                                }
-                            }
-                        }
-                    }
-
-                    if (StringUtil.strIsNullOrEmpty(miniAppId) && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
-                        miniAppId = qwCompany.getMiniAppId();
+                    int effectiveGrade14 = (item.getGrade() == null) ? 5 : item.getGrade();
+                    int listIndex14 = (effectiveGrade14 == 1 || effectiveGrade14 == 2) ? 0 : 1;
+                    if (effectiveGrade14 == 6) {
+                        listIndex14 = 2;
                     }
                     }
+                    String miniAppId14 = resolveSopMiniAppId(qwUser, Long.valueOf(companyId), listIndex14, miniMap, deptMiniAppMap, companyUserDeptMap, qwCompany);
 
 
-                    if (!StringUtil.strIsNullOrEmpty(miniAppId)) {
-                        st.setMiniprogramAppid(miniAppId);
+                    if (!StringUtil.strIsNullOrEmpty(miniAppId14)) {
+                        st.setMiniprogramAppid(miniAppId14);
                     } else {
                     } else {
                         log.error("公司的小程序id为空:采用了前端传的固定值" + sopLogs.getSopId());
                         log.error("公司的小程序id为空:采用了前端传的固定值" + sopLogs.getSopId());
                     }
                     }
@@ -2456,4 +2321,23 @@ public class SopUserLogsInfoServiceImpl implements ISopUserLogsInfoService {
         return record;
         return record;
     }
     }
 
 
+    private String resolveSopMiniAppId(QwUser qwUser,
+                                       Long companyId,
+                                       int listIndex,
+                                       Map<Long, Map<Integer, List<CompanyMiniapp>>> miniMap,
+                                       Map<Long, CompanyDept> deptMiniAppMap,
+                                       Map<Long, Long> companyUserDeptMap,
+                                       QwCompany qwCompany) {
+        Long deptId = (listIndex == 0 || listIndex == 1)
+                ? MiniAppIdResolver.getDeptId(qwUser.getCompanyUserId(), companyUserDeptMap)
+                : null;
+        String miniAppId = MiniAppIdResolver.resolveMiniAppId(
+                deptId, companyId, listIndex, qwUser.getSendMsgType(), miniMap, deptMiniAppMap);
+        if (StringUtil.strIsNullOrEmpty(miniAppId) && qwCompany != null
+                && !StringUtil.strIsNullOrEmpty(qwCompany.getMiniAppId())) {
+            miniAppId = qwCompany.getMiniAppId();
+        }
+        return miniAppId;
+    }
+
 }
 }

+ 9 - 1
fs-service/src/main/resources/mapper/company/CompanyDeptMapper.xml

@@ -20,10 +20,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="createTime"    column="create_time"    />
         <result property="createTime"    column="create_time"    />
         <result property="updateBy"    column="update_by"    />
         <result property="updateBy"    column="update_by"    />
         <result property="updateTime"    column="update_time"    />
         <result property="updateTime"    column="update_time"    />
+        <result property="miniAppMaster"    column="mini_app_master"    />
+        <result property="miniAppServer"    column="mini_app_server"    />
     </resultMap>
     </resultMap>
 
 
     <sql id="selectCompanyDeptVo">
     <sql id="selectCompanyDeptVo">
-        select dept_id, parent_id, company_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag, create_by, create_time, update_by, update_time from company_dept
+        select dept_id, parent_id, company_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag, create_by, create_time, update_by, update_time, mini_app_master, mini_app_server from company_dept
     </sql>
     </sql>
 
 
     <select id="selectCompanyDeptList" parameterType="CompanyDept" resultMap="CompanyDeptResult">
     <select id="selectCompanyDeptList" parameterType="CompanyDept" resultMap="CompanyDeptResult">
@@ -63,6 +65,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time,</if>
             <if test="createTime != null">create_time,</if>
             <if test="updateBy != null">update_by,</if>
             <if test="updateBy != null">update_by,</if>
             <if test="updateTime != null">update_time,</if>
             <if test="updateTime != null">update_time,</if>
+            <if test="miniAppMaster != null">mini_app_master,</if>
+            <if test="miniAppServer != null">mini_app_server,</if>
          </trim>
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="parentId != null">#{parentId},</if>
             <if test="parentId != null">#{parentId},</if>
@@ -79,6 +83,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">#{createTime},</if>
             <if test="createTime != null">#{createTime},</if>
             <if test="updateBy != null">#{updateBy},</if>
             <if test="updateBy != null">#{updateBy},</if>
             <if test="updateTime != null">#{updateTime},</if>
             <if test="updateTime != null">#{updateTime},</if>
+            <if test="miniAppMaster != null">#{miniAppMaster},</if>
+            <if test="miniAppServer != null">#{miniAppServer},</if>
          </trim>
          </trim>
     </insert>
     </insert>
 
 
@@ -99,6 +105,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
+            mini_app_master = #{miniAppMaster},
+            mini_app_server = #{miniAppServer},
         </trim>
         </trim>
         where dept_id = #{deptId}
         where dept_id = #{deptId}
     </update>
     </update>