| 
					
				 | 
			
			
				@@ -0,0 +1,420 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.yonge.cooleshow.biz.dal.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.core.metadata.IPage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.core.toolkit.IdWorker; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.core.toolkit.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.core.toolkit.Wrappers; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.auth.api.entity.SysUser; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.dao.StudentDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.dao.SubjectDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.dao.TeacherDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.dto.search.StudentSearch; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.ImGroup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.ImGroupMember; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.Student; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.Subject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.Teacher; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.entity.TenantGroup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.enums.ClientEnum; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.enums.ImGroupMemberRoleType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.enums.ImGroupType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.mapper.TenantGroupMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.service.ImGroupMemberService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.service.ImGroupService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.service.SysUserService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.service.TenantGroupService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.service.im.ImGroupCoreService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.vo.StudentVo; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.wrapper.StudentWrapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.wrapper.TenantGroupWrapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.cooleshow.biz.dal.wrapper.im.ImGroupWrapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.toolset.base.exception.BizException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.yonge.toolset.mybatis.support.PageUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.transaction.annotation.Transactional; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Arrays; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Collections; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.HashSet; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Set; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 机构小组表 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 2023-10-11 09:24:50 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class TenantGroupServiceImpl extends ServiceImpl<TenantGroupMapper, TenantGroup> implements TenantGroupService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ImGroupService imGroupService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ImGroupCoreService imGroupCoreService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private StudentDao studentDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private SubjectDao subjectDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private TeacherDao teacherDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ImGroupMemberService imGroupMemberService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private SysUserService sysUserService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 查询详情 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param id 详情ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return TenantGroup 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public TenantGroup detail(Long id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return baseMapper.selectById(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 分页查询 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param page  IPage<TenantGroup> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param query TenantGroupWrapper.TenantGroupQuery 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return IPage<TenantGroup> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public IPage<TenantGroupWrapper.TenantGroup> selectPage(IPage<TenantGroupWrapper.TenantGroup> page, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                                            TenantGroupWrapper.TenantGroupQuery query) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        IPage<TenantGroupWrapper.TenantGroup> iPage = page.setRecords(baseMapper.selectPage(page, query)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<TenantGroupWrapper.TenantGroup> records = iPage.getRecords(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 设置小组成员数量 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!records.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<Long> groupIdList = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    records.stream().map(TenantGroupWrapper.TenantGroup::getId).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<StudentWrapper.UserCount> userCounts = studentDao.countStudentByTenantGroupIds(query.getTenantId(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    groupIdList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Map<Long, Integer> groupBy = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    userCounts.stream().collect(Collectors.toMap(StudentWrapper.UserCount::getTenantGroupId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            , StudentWrapper.UserCount::getCount)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (TenantGroupWrapper.TenantGroup record : records) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                record.setGroupUserCount(groupBy.getOrDefault(record.getId(), 0)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return iPage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 添加 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param tenantGroup TenantGroupWrapper.TenantGroup 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return Boolean 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public Boolean add(TenantGroupWrapper.TenantGroup tenantGroup) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        TenantGroup entity = JSON.parseObject(tenantGroup.jsonString(), TenantGroup.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Integer count = this.lambdaQuery() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(TenantGroup::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(TenantGroup::getName, entity.getName()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .count(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (count > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("小组名称已存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.save(entity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> userIds = tenantGroup.getUserIds(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 有群成员才建立机构小组群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!userIds.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 建群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ImGroupWrapper.ImGroup imGroup = new ImGroupWrapper.ImGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroup.setId(IdWorker.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroup.setCreateBy(tenantGroup.getAdminId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroup.setType(ImGroupType.ORG.getCode()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroup.setName(tenantGroup.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                String imGroupId = imGroupService.create(imGroup); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                entity.setImGroupId(imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.updateById(entity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("创建机构小组群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new BizException("创建机构小组群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 加群成员 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroupService.addGroupMember(entity.getImGroupId(), new HashSet<>(userIds)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("加入机构小组群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new BizException("加入机构小组群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            studentDao.update(null, Wrappers.<Student>lambdaUpdate() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .in(Student::getUserId, userIds) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .eq(Student::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .set(Student::getTenantGroupId, entity.getId())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 更新 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param tenantGroup TenantGroupWrapper.TenantGroup 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return Boolean 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public Boolean update(TenantGroupWrapper.TenantGroup tenantGroup) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        TenantGroup oldGroup = this.getById(tenantGroup.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Integer count = this.lambdaQuery() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .ne(TenantGroup::getId, tenantGroup.getId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(TenantGroup::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(TenantGroup::getName, tenantGroup.getName()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .count(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (count > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("小组名称已经存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        TenantGroup entity = JSON.parseObject(tenantGroup.jsonString(), TenantGroup.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 修改小组负责人,调整群主,更换群主需要新的群主为该群成员 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (oldGroup.getImGroupId() != null && !oldGroup.getAdminId().equals(entity.getAdminId())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 如果新负责人不在群,先加入群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Integer toAdminInThisGroup = imGroupMemberService.lambdaQuery() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .eq(ImGroupMember::getGroupId, oldGroup.getImGroupId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .eq(ImGroupMember::getUserId, entity.getAdminId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .count(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (toAdminInThisGroup == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                // 新负责人加入群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    SysUser newTeacher = sysUserService.getByUserId(entity.getAdminId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ImGroupMember imGroupMember = new ImGroupMember(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setGroupId(oldGroup.getImGroupId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setUserId(entity.getAdminId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setRoleType(ImGroupMemberRoleType.TEACHER); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setAvatar(newTeacher.getAvatar()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setNickname(newTeacher.getUsername()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setIsAdmin(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMember.setRoleType(ImGroupMemberRoleType.TEACHER); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupMemberService.join(Collections.singletonList(imGroupMember), oldGroup.getImGroupId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    log.error("负责人加入群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new BizException("负责人加入群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ImGroup oldImGroup = imGroupService.getById(oldGroup.getImGroupId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (oldImGroup != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupCoreService.changeGroupOwner(oldGroup.getImGroupId(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            String.valueOf(tenantGroup.getAdminId()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            String.valueOf(oldGroup.getAdminId())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupCoreService.groupQuit(oldGroup.getAdminId(), ClientEnum.TEACHER.getCode(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            oldGroup.getImGroupId(), true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    log.error("更换群主失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new BizException("更换群主失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        queryWrapper.lambda() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(Student::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(Student::getTenantGroupId, tenantGroup.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> oldGroupUsers = studentDao.selectList(queryWrapper).stream().map(Student::getUserId) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> newGroupUsers = tenantGroup.getUserIds(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> removeGroupUsers = oldGroupUsers.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .filter(next -> !newGroupUsers.contains(next)).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 移除的群成员,退群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!removeGroupUsers.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (Long removeUserId : removeGroupUsers) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupCoreService.groupQuit(removeUserId, ClientEnum.STUDENT.getCode(), oldGroup.getImGroupId(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("群成员移出群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new BizException("群成员移出群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            studentDao.update(null, Wrappers.<Student>lambdaUpdate() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .in(Student::getUserId, removeGroupUsers) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .eq(Student::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .set(Student::getTenantGroupId, -1L)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 新增的群成员 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> newAddGroupUsers = newGroupUsers.stream().filter(next -> !oldGroupUsers.contains(next)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!newAddGroupUsers.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 加群成员 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                imGroupService.addGroupMember(oldGroup.getImGroupId(), new HashSet<>(newAddGroupUsers)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("加入机构小组群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new BizException("加入机构小组群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            studentDao.update(null, Wrappers.<Student>lambdaUpdate() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .in(Student::getUserId, newAddGroupUsers) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .eq(Student::getTenantId, tenantGroup.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    .set(Student::getTenantGroupId, entity.getId())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return this.updateById(entity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public Boolean delete(Long id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        TenantGroup group = this.getById(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (group == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("机构小组不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String imGroupId = group.getImGroupId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (imGroupId != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ImGroup imGroup = imGroupService.getById(imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (imGroup != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    imGroupCoreService.groupDismiss(imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    log.error("解散群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        studentDao.update(null, Wrappers.<Student>lambdaUpdate() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .eq(Student::getTenantId, group.getTenantId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .set(Student::getTenantGroupId, -1L)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return removeById(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 查询机构小组群成员 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param tenantId      机构ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param tenantGroupId 机构小组ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return 机构小组成员列表 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<TenantGroupWrapper.TenantGroupMember> queryGroupMember(Long tenantId, Long tenantGroupId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        StudentSearch studentSearch = new StudentSearch(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        studentSearch.setRows(1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        studentSearch.setPage(9999); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<StudentVo> studentVos = studentDao.selectPage(PageUtil.getPage(studentSearch), studentSearch); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (studentVos.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, String> subjectIdNameMap = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<String> subjectIds = studentVos.stream().map(Student::getSubjectId).filter(StringUtils::isNotEmpty) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .distinct().collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!subjectIds.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<Subject> subject = subjectDao.findBySubjectByIdList(String.join(",", subjectIds)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Map<String, String> map = subject.stream().collect(Collectors.toMap(next -> next.getId().toString(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Subject::getName)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            subjectIdNameMap.putAll(map); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return studentVos.stream().map(next -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            TenantGroupWrapper.TenantGroupMember member = new TenantGroupWrapper.TenantGroupMember(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setUserId(next.getUserId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setTenantId(next.getTenantId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setName(next.getUsername()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setGender(next.getGender()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setBirthdate(next.getBirthdate()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setPhone(next.getPhone()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setAvatar(next.getAvatar()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setSubjectId(next.getSubjectId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            member.setSubjectName(subjectIdNameMap.getOrDefault(next.getSubjectId(), "")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return member; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 小组交接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param adjustTenantGroup 入参 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return 成功/失败 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public Boolean adjustTenantGroup(TenantGroupWrapper.AdjustTenantGroup adjustTenantGroup) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Long teacherId = adjustTenantGroup.getTeacherId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Teacher teacher = teacherDao.selectById(teacherId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (teacher == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("移交的老师不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<TenantGroupWrapper.AdjustTenantGroupDetail> details = adjustTenantGroup.getAdjustTenantGroupDetails(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (details.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("未选择移交信息"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> tenantGroupIds = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                details.stream().map(TenantGroupWrapper.AdjustTenantGroupDetail::getTenantGroupId).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<TenantGroup> tenantGroups = this.lambdaQuery().in(TenantGroup::getId, tenantGroupIds).list(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<Long, String> groupBy = tenantGroups.stream().collect(Collectors.toMap(TenantGroup::getId, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                TenantGroup::getImGroupId)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (TenantGroupWrapper.AdjustTenantGroupDetail detail : details) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Long toTeacher = detail.getTeacherId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (teacherId.equals(toTeacher) && Boolean.FALSE.equals(detail.getDismiss())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String imGroupId = groupBy.get(detail.getTenantGroupId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (toTeacher != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                // 更换群主 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!StringUtils.isEmpty(imGroupId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    // 新负责人加入群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        SysUser newTeacher = sysUserService.getByUserId(toTeacher); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        ImGroupMember imGroupMember = new ImGroupMember(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setGroupId(imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setUserId(toTeacher); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setRoleType(ImGroupMemberRoleType.TEACHER); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setAvatar(newTeacher.getAvatar()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setNickname(newTeacher.getUsername()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setIsAdmin(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMember.setRoleType(ImGroupMemberRoleType.TEACHER); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupMemberService.join(Collections.singletonList(imGroupMember), imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        log.error("负责人加入群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        throw new BizException("负责人加入群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupCoreService.changeGroupOwner(imGroupId, String.valueOf(toTeacher), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                String.valueOf(teacherId)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupCoreService.groupQuit(teacherId, ClientEnum.TEACHER.getCode(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                imGroupId, true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        log.error("移交负责人失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        throw new BizException("移交负责人失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.lambdaUpdate() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .set(TenantGroup::getAdminId, toTeacher) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .eq(TenantGroup::getId, detail.getTenantGroupId()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .update(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else if (Boolean.TRUE.equals(detail.getDismiss())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                // 解散群 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!StringUtils.isEmpty(imGroupId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        imGroupCoreService.groupDismiss(imGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        log.error("解散群失败", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        throw new BizException("解散群失败"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                this.removeById(detail.getTenantGroupId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |