goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package cn.stylefeng.rest.modular.im.controller;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.CustomerImInfoDTO;
import cn.stylefeng.guns.modular.business.dto.ImEffectiveInfoDTO;
import cn.stylefeng.guns.modular.business.dto.ImGroupUserDto;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.request.ImGroupUpdateRequest;
import cn.stylefeng.guns.modular.business.dto.request.ImTestMessageSendRequest;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.modular.business.service.impl.ImBizService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo;
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.CustomerWorkStatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.ImPushTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.ImUserTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.PostIdEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.rong.messages.TxtMessage;
import io.rong.models.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
@Api(tags = "IM接口")
@RestController
@ApiResource(name = "IM接口")
@RequestMapping("/im")
public class ImController {
 
    @Resource
    ImBizService imBizService;
 
    @Resource
    ImServerApi imServerApi;
 
    @Resource
    CustomerService customerService;
 
    @Resource
    IImGroupService imGroupService;
 
    @Resource
    IImNoticeService imNoticeService;
 
    @Resource
    private ICourseOrderService courseOrderService;
 
    @Resource
    private ICounsellingUserService counsellingUserService;
 
    @Resource
    private IImGroupUserService imGroupUserService;
 
    @ApiOperation(value = "测试发送IM消息")
    @PostResource(name = "测试发送IM消息", path = "/testMessageSend")
    public ResponseData testMessageSend(@RequestBody ImTestMessageSendRequest req) {
        // IM推送数据json
        ImPushDataDTO pushData = req.getPushData();
 
        Result messageSendResult;
        if (req.getSendTpe() == null || req.getSendTpe() == 0) {
            messageSendResult = imBizService.messageSendSystem(
                    req.getFromUserId(),
                    new String[]{req.getToId()},
                    pushData,
                    ImUserTypeEnum.USER,
                    null,
                    false
            );
        } else if (req.getSendTpe() == 1) {
            messageSendResult = imServerApi.messageSendPrivate(req.getFromUserId(), new String[]{req.getToId()}, new TxtMessage(JSONUtil.toJsonStr(pushData), pushData.getExtra()));
        } else {
            messageSendResult = imBizService.messageSendGroup(req.getFromUserId(), new String[]{req.getToId()}, pushData);
        }
        log.error("发送消息结果:{}", messageSendResult);
        return new SuccessResponseData(messageSendResult);
    }
 
    @ApiOperation(value = "即时咨询销售顾问", notes = "即时咨询,获取用户销售顾问,没有绑定随机分配一位当前在上班的咨询顾问")
    @GetResource(name = "即时咨询销售顾问", path = "/realTimeConsult")
    public ResponseData<Long> realTimeConsult() {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 获取C端用户的详细信息
        CustomerInfo customerInfo = customerService.getCustomerInfoById(userId);
        Assert.notNull(customerInfo, "获取用户信息失败!");
        Long consultWorkerId = customerInfo.getConsultWorkerId();
        if (consultWorkerId == null) {
            // 获取销售顾问
            consultWorkerId = customerService.randomWorkerIdByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
            if (consultWorkerId != null) {
                // 设置用户信息销售顾问
                Customer customer = new Customer();
                customer.setCustomerId(userId);
                customer.setConsultWorkerId(consultWorkerId);
                customerService.updateCustomerRemoveCache(customer);
            }
        }
 
        if (consultWorkerId != null) {
            return new SuccessResponseData(consultWorkerId);
        } else {
            return new ErrorResponseData("获取销售顾问失败!");
        }
    }
 
    @ApiOperation(value = "即时课程销售顾问", notes = "即时咨询,获取用户销售顾问,没有绑定随机分配一位当前在上班的咨询顾问")
    @GetResource(name = "即时课程销售顾问", path = "/realTimeClass")
    public ResponseData<Long> realTimeClass() {
        // 获取当前登录用户信息
        LoginUser loginUser = LoginContext.me().getLoginUser();
        Long userId = loginUser.getUserId();
 
        // 获取C端用户的详细信息
        CustomerInfo customerInfo = customerService.getCustomerInfoById(userId);
        Assert.notNull(customerInfo, "获取用户信息失败!");
        Long classWorkerId = customerInfo.getClassWorkerId();
        if (classWorkerId == null) {
            // 获取销售顾问
            classWorkerId = customerService.randomWorkerIdByLineStatusAndPost(null, null, PostIdEnum.PO_11.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
            if (classWorkerId != null) {
                // 设置用户信息销售顾问
                Customer customer = new Customer();
                customer.setCustomerId(userId);
                customer.setClassWorkerId(classWorkerId);
                customerService.updateCustomerRemoveCache(customer);
            }
        }
 
        if (classWorkerId != null) {
            return new SuccessResponseData(classWorkerId);
        } else {
            return new ErrorResponseData("获取销售顾问失败!");
        }
    }
 
    @ApiOperation(value = "获取IM通讯token", notes = "获取指定用户IM通讯token,没有则注册IM")
    @GetResource(name = "获取IM通讯token", path = "/getImToken")
    public ResponseData<String> getImToken(Long userId) {
        Assert.notNull(userId, "用户ID不能为空!");
        // 获取用户IM通讯token
        return new SuccessResponseData(imBizService.getImToken(userId));
    }
 
    @ApiOperation(value = "获取用户IM信息")
    @GetResource(name = "获取用户IM信息", path = "/userListByIds")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "用户ID串,逗号拼接")
    })
    public ResponseData<List<CustomerImInfoDTO>> userListByIds(String ids) {
        List<CustomerImInfoDTO> list = customerService.list(
                Wrappers.<Customer>lambdaQuery()
                        .select(
                                Customer::getCustomerId,
                                Customer::getNickName,
                                Customer::getRealName,
                                Customer::getAvatar,
                                Customer::getImToken,
                                Customer::getPostIds
                        )
                        .in(Customer::getCustomerId, ids.split(","))
        ).stream().map(o -> BeanUtil.toBean(o, CustomerImInfoDTO.class)).collect(Collectors.toList());
        return new SuccessResponseData(list);
    }
 
    @ApiOperation(value = "我的群聊")
    @GetResource(name = "我的群聊", path = "/myGroup")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "groupName", value = "群名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1正常,2冻结(解散)", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "nickName", value = "用户姓名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "groupType", value = "群类型:1课程,2咨询", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataTypeClass = Long.class),
    })
    public ResponseData<List<ImGroup>> myGroup(Long userId, String groupName, Integer statusFlag, String nickName, Integer groupType, String orderId) {
        userId = userId == null ? LoginContext.me().getLoginUser().getUserId() : userId;
        return new SuccessResponseData(imGroupService.getMyGroup(userId, groupName, statusFlag, nickName, groupType, orderId));
    }
 
    @ApiOperation(value = "群聊详情")
    @GetResource(name = "群聊详情", path = "/groupDetail")
    public ResponseData<ImGroup> groupDetail(Long groupId) {
        // 获取用户IM通讯token
        return new SuccessResponseData(imGroupService.getById(groupId));
    }
 
    @ApiOperation(value = "编辑群聊信息")
    @PostResource(name = "编辑群聊信息", path = "/updateGroup")
    public ResponseData<Boolean> updateGroup(@RequestBody ImGroupUpdateRequest req) {
        ImGroup group = BeanUtil.toBean(req, ImGroup.class);
        // 编辑群聊信息
        boolean update = imGroupService.updateById(group);
        if (update && StrUtil.isNotBlank(group.getName())) {
            imServerApi.groupUpdate(group.getId().toString(), group.getName(), new String[]{});
        }
        return new SuccessResponseData(update);
    }
 
    @ApiOperation(value = "群聊用户列表")
    @GetResource(name = "群聊用户列表", path = "/groupUser")
    public ResponseData<List<ImGroupUserDto>> groupUser(Long groupId) {
        // 获取用户IM通讯token
        return new SuccessResponseData(imGroupService.groupUser(groupId));
    }
 
    @ApiOperation(value = "通知列表")
    @GetResource(name = "通知列表", path = "/imNotice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "postId", value = "岗位ID", dataTypeClass = Integer.class),
    })
    public ResponseData<List<ImNotice>> imNotice(Long userId, Integer postId) {
        List<ImNotice> list = imNoticeService.list(
                Wrappers.<ImNotice>lambdaQuery()
                        .eq(userId != null, ImNotice::getUserId, userId)
                        .eq(postId != null, ImNotice::getPostId, postId)
                        .orderByDesc(ImNotice::getCreateTime)
        );
        return new SuccessResponseData(list);
    }
 
    @ApiOperation(value = "已读通知")
    @GetResource(name = "已读通知", path = "/readNotice")
    public ResponseData<List<ImNotice>> readNotice(Long id) {
        boolean update = imNoticeService.update(
                Wrappers.<ImNotice>lambdaUpdate()
                        .set(ImNotice::getReadFlag, 1)
                        .eq(ImNotice::getId, id)
        );
        return new SuccessResponseData(update);
    }
 
    @ApiOperation(value = "全部已读通知")
    @GetResource(name = "全部已读通知", path = "/allReadNotice")
    public ResponseData<List<ImNotice>> allReadNotice(Long userId) {
        boolean update = imNoticeService.update(
                Wrappers.<ImNotice>lambdaUpdate()
                        .set(ImNotice::getReadFlag, 1)
                        .eq(ImNotice::getUserId, userId)
        );
        return new SuccessResponseData(update);
    }
 
    @ApiOperation(value = "群聊禁用和启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群聊id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusFlag", value = "状态:1正常,2冻结", dataTypeClass = Long.class),
    })
    @PostResource(name = "群聊禁用和启用", path = "/updateImGroupState")
    public ResponseData<Boolean> updateImGroupState(Long groupId,Integer statusFlag) {
        LambdaUpdateWrapper<ImGroup> imGroupLambdaUpdateWrapper =  Wrappers.<ImGroup>lambdaUpdate()
                .set(ImGroup::getStatusFlag, statusFlag)
                .eq(ImGroup::getId, groupId);
        if (statusFlag.intValue() == 2 ){
            imGroupLambdaUpdateWrapper.set(ImGroup::getDisbandDate,new Date());
        }
        ImGroup imGroup = this.imGroupService.getById(groupId);
        Result result = imServerApi.groupDismiss(groupId.toString(),LoginContext.me().getLoginUser().getUserId().toString());
        log.info("解散群聊:"+JSONUtil.toJsonStr(result));
        if (result.getCode().intValue() == 200){
            boolean update = imGroupService.update(imGroupLambdaUpdateWrapper);
            //发送群聊消息解散消息
            List<ImGroupUser> imGroupUsers = this.imGroupUserService.list(new LambdaQueryWrapper<ImGroupUser>().select(ImGroupUser::getUserId,ImGroupUser::getGroupId).eq(ImGroupUser::getGroupId,groupId).eq(ImGroupUser::getIsDelete,0));
            if (CollectionUtil.isNotEmpty(imGroupUsers)){
               List<String> userIds = imGroupUsers.stream().map(ImGroupUser::getUserId).map(String::valueOf).collect(Collectors.toList());
                String pushContent = imGroup.getName()+".该群聊被解散";
 
                // IM推送数据json
                ImPushDataDTO pushData = ImPushDataDTO.builder()
                        .type(ImPushTypeEnum.S_TO_C_TIP_GROUP_CANCEL_INFO.getCode())
                        .objId(ObjUtil.toString(imGroup.getId()))
                        .title("通知")
                        .content(pushContent)
//                        .extra("发起聊天")
                        .data1(ObjUtil.toString(LoginContext.me().getLoginUser().getUserId()))
                        .build();
 
                    // 发送
                    imBizService.messageSendSystem(LoginContext.me().getLoginUser().getUserId() + "", userIds.toArray(new String[userIds.size()]), pushData, ImUserTypeEnum.USER,null, true);
 
            }
 
            return new SuccessResponseData(update);
        }
       return new ErrorResponseData<>("解散群聊失败!");
    }
 
 
    @ApiOperation(value = "根据分组id查询业务过期时间")
    @GetResource(name = "根据分组id查询业务过期时间", path = "/getGroupEffectiveInfo")
    public ResponseData<ImEffectiveInfoDTO> getGroupEffectiveInfo(Long groupId) {
        ImGroup imGroup = this.imGroupService.getById(groupId);
        if (imGroup == null){
            return new ErrorResponseData<>("没有对应群聊");
        }
        ImEffectiveInfoDTO imEffectiveInfoDTO = new ImEffectiveInfoDTO();
        imEffectiveInfoDTO.setGroupType(imGroup.getGroupType());
        imEffectiveInfoDTO.setOrderId(imGroup.getOrderId());
        if (imGroup.getGroupType().intValue() ==1){
            CourseOrder courseOrder = this.courseOrderService.getById(imGroup.getOrderId());
            if (courseOrder != null){
               imEffectiveInfoDTO.setUserId(courseOrder.getUserId());
               imEffectiveInfoDTO.setEffectiveEndTime(courseOrder.getEffectiveEndTime());
               imEffectiveInfoDTO.setEffectiveBeginTime(courseOrder.getEffectiveBeginTime());
                Customer customer = this.customerService.getById(imEffectiveInfoDTO.getUserId());
               imEffectiveInfoDTO.setPhone(customer.getTelephone());
            }
        }else{
           ImGroupUser imGroupUser =  this.imGroupUserService.getOne(new LambdaQueryWrapper<ImGroupUser>().eq(ImGroupUser::getGroupId,imGroup.getId())
                    .isNull(ImGroupUser::getUserPost).last(" limit 1 "));
           if (imGroupUser != null){
               CounsellingUser counsellingUser = this.counsellingUserService.getOne(new LambdaQueryWrapper<CounsellingUser>().eq(CounsellingUser::getUserId,imGroupUser.getUserId())
                       .eq(CounsellingUser::getCounsellingInfoId,imGroup.getOrderId()).eq(CounsellingUser::getIsDelete,0));
               if (counsellingUser != null){
                   imEffectiveInfoDTO.setEffectiveEndTime(counsellingUser.getEffectiveEndTime());
                   imEffectiveInfoDTO.setEffectiveBeginTime(counsellingUser.getEffectiveBeginTime());
                   imEffectiveInfoDTO.setPhone(counsellingUser.getPhone());
                   imEffectiveInfoDTO.setClassHours(counsellingUser.getClassHours());
                   imEffectiveInfoDTO.setResidueClassHours(counsellingUser.getResidueClassHours());
               }
               imEffectiveInfoDTO.setUserId(imGroupUser.getUserId());
           }
        }
        if (imEffectiveInfoDTO.getUserId() != null){
            Customer customer = this.customerService.getById(imEffectiveInfoDTO.getUserId());
            if (customer != null){
                imEffectiveInfoDTO.setNickName(customer.getNickName());
            }
        }
 
        return new SuccessResponseData(imEffectiveInfoDTO);
    }
 
}