guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSON;
import cn.stylefeng.guns.modular.business.dto.ImGroupUserDto;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.mapper.ImGroupMapper;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.im.api.ImServerApi;
import cn.stylefeng.roses.kernel.rule.enums.ImPushTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.PostIdEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 消息群信息 服务实现类
 * </p>
 *
 * @author goupan
 * @since 2024-01-01
 */
@Service
public class ImGroupServiceImpl extends ServiceImpl<ImGroupMapper, ImGroup> implements IImGroupService {
 
    @Resource
    ImServerApi imServerApi;
 
    @Resource
    ImBizService imBizService;
 
    @Resource
    private IImGroupUserService imGroupUserService;
 
    @Resource
    private ICounsellingOrderService counsellingOrderService;
 
    @Resource
    private ICourseOrderService courseOrderService;
 
    @Resource
    private ICounsellingUserService counsellingUserService;
 
    @Resource
    private ICourseService courseService;
 
 
    @Resource
    private CustomerService customerService;
 
 
 
    @Override
    public void createGroup(Integer groupType, Long consultantUserId, Long companionUserId,
                            Long counsellingUserId, Long courseUserId, Long tutoringUserId,Long userId,Long orderId) {
        ImGroup imGroup = new ImGroup();
        imGroup.setGroupType(groupType);
        imGroup.setStatusFlag(1);
        imGroup.setOrderId(orderId);
        imGroup.setCreateUser(userId);
        LambdaQueryWrapper<ImGroup> lambdaQueryWrapper = new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getIsDelete,0).eq(ImGroup::getStatusFlag,1);
        lambdaQueryWrapper.eq(ImGroup::getOrderId,orderId);
        lambdaQueryWrapper.eq(ImGroup::getCreateUser,userId);
        //群类型:1课程,2咨询
        if (groupType.intValue() ==1 ){
            CourseOrder courseOrder = this.courseOrderService.getById(orderId);
            Course course = this.courseService.getById(courseOrder.getCourseId());
            Customer customer = this.customerService.getById(courseOrder.getUserId());
            imGroup.setName(course.getCourseName()+"_"+customer.getNickName());
            lambdaQueryWrapper.eq(ImGroup::getGroupType,1);
 
        } else if (groupType.intValue() == 2) {
            Customer customer = this.customerService.getById(userId);
            imGroup.setName(DateTime.now().toDateStr()+"_"+customer.getNickName());
            lambdaQueryWrapper.eq(ImGroup::getGroupType,2);
        }
        List<ImGroupUser> imGroupUserList = new ArrayList<>();
        List<Long> olduserIdList =  new ArrayList<>();
        //查询是否存在群聊
        ImGroup imGroup1 = this.baseMapper.selectOne(lambdaQueryWrapper);
 
        if (imGroup1 == null){
            this.save(imGroup);
        }else{
            imGroup = imGroup1;
            olduserIdList = this.imGroupUserService.list(new LambdaQueryWrapper<ImGroupUser>().eq(ImGroupUser::getGroupId,imGroup.getId()).eq(ImGroupUser::getIsDelete,0))
                    .stream().map(ImGroupUser::getUserId).collect(Collectors.toList());
        }
 
 
        List<String> userIdList =  new ArrayList<>();
        //咨询顾问id
        if (consultantUserId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(0);
            imGroupUser.setUserId(consultantUserId);
            imGroupUser.setUserPost(PostIdEnum.PO_21.getCode().longValue());
 
            if (!olduserIdList.contains(consultantUserId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
 
        }
        //陪护教练id
        if (companionUserId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(1);
            imGroupUser.setUserId(companionUserId);
            imGroupUser.setUserPost(PostIdEnum.PO_23.getCode().longValue());
 
            if (!olduserIdList.contains(companionUserId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
 
        }
        //咨询师id
        if (counsellingUserId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(2);
            imGroupUser.setUserId(counsellingUserId);
            imGroupUser.setUserPost(PostIdEnum.PO_22.getCode().longValue());
 
 
            if (!olduserIdList.contains(counsellingUserId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
        }
        //课程顾问id
        if (courseUserId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(3);
            imGroupUser.setUserId(courseUserId);
            imGroupUser.setUserPost(PostIdEnum.PO_11.getCode().longValue());
 
            if (!olduserIdList.contains(courseUserId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
 
        }
        //课程辅导id
        if (tutoringUserId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(4);
            imGroupUser.setUserId(tutoringUserId);
            imGroupUser.setUserPost(PostIdEnum.PO_12.getCode().longValue());
 
            if (!olduserIdList.contains(tutoringUserId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
        }
        if (userId != null){
            ImGroupUser imGroupUser = new ImGroupUser();
            imGroupUser.setGroupId(imGroup.getId());
            imGroupUser.setGroupAlias(imGroup.getName());
            imGroupUser.setSort(5);
            imGroupUser.setUserId(userId);
 
            if (!olduserIdList.contains(userId)){
                imGroupUserList.add(imGroupUser);
                userIdList.add(imGroupUser.getUserId().toString());
            }
        }
        this.imGroupUserService.saveBatch(imGroupUserList);
        if (imGroup1 == null){
            //调用群聊
            this.imServerApi.groupCreate(imGroup.getId().toString(),imGroup.getName(), ArrayUtil.toArray(userIdList,String.class));
        }else {
//            //调用群聊
//            this.imServerApi.groupUpdate(imGroup.getId().toString(),imGroup.getName(), ArrayUtil.toArray(userIdList,String.class));
            if (CollectionUtil.isNotEmpty(userIdList)){
                //添加入群
                this.imServerApi.groupJoin(imGroup.getId().toString(),imGroup.getName(), ArrayUtil.toArray(userIdList,String.class));
 
            }
 
        }
 
        if (imGroup.getGroupType().intValue() == 1){
            Map<String,String> buinessmap = new HashMap<>();
            buinessmap.put("buinessName","心理咨询课程");
            // 推送消息内容
            String pushContent = "我购买了心理咨询课程,需要咨询";
            // IM推送数据json
            ImPushDataDTO pushData = ImPushDataDTO.builder()
                    .type(ImPushTypeEnum.S_TO_W_TIP_COURSE_PAY_GROUP_SUCCESS.getCode())
                    .objId(ObjUtil.toString(orderId))
                    .title("通知")
                    .content(pushContent)
                    .extra(JSONObject.toJSONString(buinessmap))
                    .build();
            // 发送咨询
            imBizService.messageSendGroup(userId+"", new String[]{imGroup.getId()+""}, pushData);
        } else if (imGroup.getGroupType().intValue() == 2) {
            Map<String,String> buinessmap = new HashMap<>();
            buinessmap.put("buinessName","心理咨询疗程");
            // 推送消息内容
            String pushContent = "我购买了心理咨询疗程,需要咨询";
            // IM推送数据json
            ImPushDataDTO pushData = ImPushDataDTO.builder()
                    .type(ImPushTypeEnum.S_TO_W_TIP_CONSULT_PAY_GROUP_SUCCESS.getCode())
                    .objId(ObjUtil.toString(orderId))
                    .title("通知")
                    .content(pushContent)
                    .extra(JSONObject.toJSONString(buinessmap))
                    .build();
            // 发送咨询
            imBizService.messageSendGroup(userId+"", new String[]{imGroup.getId()+""}, pushData);
        }
 
    }
 
    @Override
    public List<ImGroup> getMyGroup(Long userId, String groupName, Integer statusFlag, String nickName, Integer groupType, String orderId) {
        return this.baseMapper.getMyGroup(userId, groupName, statusFlag, nickName, groupType, orderId);
    }
 
    @Override
    public List<ImGroupUserDto> groupUser(Long groupId) {
        return this.baseMapper.groupUser(groupId);
    }
 
    @Override
    public void updateGroupUserForOrderinfo(Long userId, Integer groupType, Long customerId) {
        if (groupType != null ){
            //群类型:1课程,2咨询
            if (groupType.intValue() == 1){
               List<CourseOrder> courseOrderList = this.courseOrderService.list(new LambdaQueryWrapper<CourseOrder>().eq(CourseOrder::getUserId,customerId)
                        .eq(CourseOrder::getStatusFlag,1).eq(CourseOrder::getIsDelete,0).ne(CourseOrder::getCourseUserId,userId));
               if (CollectionUtil.isNotEmpty(courseOrderList)){
                   courseOrderList.stream().forEach(courseOrder -> {
                       courseOrder.setCourseUserId(userId);
                       ImGroup imGroup = this.baseMapper.selectOne(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,courseOrder.getId())
                               .eq(ImGroup::getGroupType,groupType).eq(ImGroup::getStatusFlag,1));
                       if (imGroup != null){
                           //退出老顾问
                           ImGroupUser imGroupUserOld = this.imGroupUserService.getOne(new LambdaQueryWrapper<ImGroupUser>().eq(ImGroupUser::getGroupId,imGroup.getId())
                                   .eq(ImGroupUser::getUserPost,PostIdEnum.PO_11.getCode().longValue()).last(" limit 1 ").eq(ImGroupUser::getIsDelete,0)
                                   .ne(ImGroupUser::getUserId,userId));
                           if (imGroupUserOld != null){
                               this.imGroupUserService.update(new LambdaUpdateWrapper<ImGroupUser>().set(ImGroupUser::getIsDelete,1).eq(ImGroupUser::getId,imGroupUserOld.getId()));
                               this.imServerApi.groupQuit(imGroup.getId()+"",new String[]{imGroupUserOld.getUserId()+""});
                               //邀请进入新群
                               ImGroupUser imGroupUser = new ImGroupUser();
                               imGroupUser.setGroupId(imGroup.getId());
                               imGroupUser.setGroupAlias(imGroup.getName());
                               imGroupUser.setSort(3);
                               imGroupUser.setUserId(userId);
                               imGroupUser.setUserPost(PostIdEnum.PO_11.getCode().longValue());
                               this.imGroupUserService.save(imGroupUser);
                               this.imServerApi.groupJoin(imGroup.getId()+"",imGroup.getName(),new String[]{imGroupUser.getUserId()+""});
 
                           }
 
                       }
                       this.courseOrderService.updateById(courseOrder);
 
                   });
               }
 
            }else if (groupType.intValue() == 2){
                //修改使用的订单咨询顾问
                List<CounsellingOrder> counsellingOrderList = this.counsellingOrderService.list(new LambdaQueryWrapper<CounsellingOrder>().eq(CounsellingOrder::getUserId,customerId)
                        .eq(CounsellingOrder::getStatusFlag,1).eq(CounsellingOrder::getIsDelete,0).ne(CounsellingOrder::getConsultantUserId,userId));
                if (CollectionUtil.isNotEmpty(counsellingOrderList)){
                    counsellingOrderList.stream().forEach(counsellingOrder -> {
                        counsellingOrder.setConsultantUserId(userId);
                        ImGroup imGroup = this.baseMapper.selectOne(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,counsellingOrder.getCounsellingInfoId())
                                .eq(ImGroup::getGroupType,groupType).eq(ImGroup::getStatusFlag,1).eq(ImGroup::getCreateUser,counsellingOrder.getUserId()));
                        if (imGroup != null){
                            //退出老顾问
                            ImGroupUser imGroupUserOld = this.imGroupUserService.getOne(new LambdaQueryWrapper<ImGroupUser>().eq(ImGroupUser::getGroupId,imGroup.getId())
                                    .eq(ImGroupUser::getUserPost,PostIdEnum.PO_21.getCode().longValue()).last(" limit 1 ").eq(ImGroupUser::getIsDelete,0)
                                    .ne(ImGroupUser::getUserId,userId));
                            if (imGroupUserOld != null){
                                this.imGroupUserService.update(new LambdaUpdateWrapper<ImGroupUser>().set(ImGroupUser::getIsDelete,1).eq(ImGroupUser::getId,imGroupUserOld.getId()));
                                this.imServerApi.groupQuit(imGroup.getId()+"",new String[]{imGroupUserOld.getUserId()+""});
                                //邀请进入新群
                                ImGroupUser imGroupUser = new ImGroupUser();
                                imGroupUser.setGroupId(imGroup.getId());
                                imGroupUser.setGroupAlias(imGroup.getName());
                                imGroupUser.setSort(0);
                                imGroupUser.setUserId(userId);
                                imGroupUser.setUserPost(PostIdEnum.PO_21.getCode().longValue());
                                this.imGroupUserService.save(imGroupUser);
                                this.imServerApi.groupJoin(imGroup.getId()+"",imGroup.getName(),new String[]{imGroupUser.getUserId()+""});
 
                            }
                        }
                        this.counsellingOrderService.updateById(counsellingOrder);
                        //修改咨询师的咨询顾问
                        CounsellingUser counsellingUser = this.counsellingUserService.getOne(new LambdaQueryWrapper<CounsellingUser>().eq(CounsellingUser::getUserId,customerId)
                                .eq(CounsellingUser::getCounsellingInfoId,counsellingOrder.getCounsellingInfoId()).eq(CounsellingUser::getIsDelete,0)
                                .last(" limit 1"));
                        if (counsellingUser != null){
                            counsellingUser.setConsultantUserId(userId);
                            this.counsellingUserService.updateById(counsellingUser);
                        }
                    });
                }
 
            }
        }
    }
 
    public static void main(String[] args) {
        List<Long> userIds = new ArrayList<>();
        userIds.add(200L);
        System.out.println(userIds.contains(200L));
    }
}