/// <summary> /// 创建聊天的房间 /// </summary> /// <param name="OPDRegisterID"></param> /// <returns></returns> public bool CreateIMRoom(string OPDRegisterID) { try { using (DAL.EF.DBEntities db = new DBEntities()) { var model = (from opd in db.UserOpdRegisters.Where(a => a.OPDRegisterID == OPDRegisterID) join user in db.Users on opd.UserID equals user.UserID join member in db.UserMembers on opd.MemberID equals member.MemberID select new { OPDType = opd.OPDType, DoctorID = opd.DoctorID, //opd.DoctorID, UserID = opd.UserID, MemberID = opd.MemberID, member.MemberName, user.PhotoUrl, user.UserCNName, user.UserENName }).FirstOrDefault(); if (model != null) { #region 创建IM群组 XuHos.Integration.QQCloudy.IMHelper imservice = new XuHos.Integration.QQCloudy.IMHelper(); ConversationIMUidService imUidService = new ConversationIMUidService(CurrentOperatorUserID); var roomService = new BLL.Sys.Implements.ConversationRoomService(); BLL.Doctor.Implements.DoctorService doctorService = new Doctor.Implements.DoctorService(); //房间信息 var room = roomService.GetChannelInfo(OPDRegisterID); if (room != null) { var GroupName = model.OPDType.GetEnumDescript(); var Introduction = ""; var Notification = ""; var groupMembers = new List <int>(); var channelMembers = new List <RequestChannelMemberDTO>(); //患者信息 var userIdentifier = imUidService.GetUserIMUid(model.UserID); if (userIdentifier > 0) { groupMembers.Add(userIdentifier); channelMembers.Add(new RequestChannelMemberDTO() { Identifier = userIdentifier, UserType = EnumUserType.User, UserID = model.UserID, UserMemberID = model.MemberID, PhotoUrl = model.PhotoUrl, UserCNName = model.MemberName, UserENName = model.MemberName }); } //获取医生信息(如果走导诊系统,此时还未分配医生,分诊后才会把分诊医生加入到IM组) if (!string.IsNullOrEmpty(model.DoctorID)) { var doctorInfo = doctorService.GetDoctorInfo(model.DoctorID); var doctorIdentifier = imUidService.GetDoctorIMUid(model.DoctorID); if (doctorIdentifier > 0) { groupMembers.Add(doctorIdentifier); channelMembers.Add(new RequestChannelMemberDTO() { Identifier = doctorIdentifier, UserID = doctorInfo.UserID, UserType = EnumUserType.Doctor, UserMemberID = doctorInfo.User.MemberID, PhotoUrl = doctorInfo.User._PhotoUrl,//DTO已经进行了路径转换,这里需要使用没有转换之前的数据 UserENName = doctorInfo.DoctorEnName, UserCNName = doctorInfo.DoctorName }); } } if (room.Enable) { if (imservice.AddGroupMember(room.ChannelID, groupMembers)) { return(roomService.InsertChannelMembers(room.ChannelID, channelMembers)); } } else { //创建裙子成功 if (imservice.CreateGroup(room.ChannelID, GroupName, model.OPDType, groupMembers, Introduction, Notification)) { using (XuHos.EventBus.MQChannel mqChannel = new EventBus.MQChannel()) { if (mqChannel.Publish <EventBus.Events.ChannelCreatedEvent>(new EventBus.Events.ChannelCreatedEvent() { ChannelID = room.ChannelID, ServiceID = room.ServiceID, ServiceType = room.ServiceType })) { room.Enable = true; if (roomService.CompareAndSetChannelInfo(room)) { return(roomService.InsertChannelMembers(room.ChannelID, channelMembers)); } } else { return(false); } } } } } else { return(true); } #endregion } else { return(true); } } } //出现数据库并发更新异常需要重试 catch (DbUpdateConcurrencyException ex) { return(CreateIMRoom(OPDRegisterID)); } return(false); }
public bool Handle(EventBus.Events.ChannelC2CCreateEvent evt) { try { if (evt == null) { return(true); } if (string.IsNullOrEmpty(evt.FromUserID)) { return(true); } if (evt.AddFriendItem == null || evt.AddFriendItem.Count <= 0) { return(true); } //获取当前用户信息 var FromUserInfo = userService.GetUserInfoByUserId(evt.FromUserID); //用户存在 if (FromUserInfo != null) { var ToUserInfos = new Dictionary <string, ResponseUserDTO>(); //创建房间请求参数 var createC2CChannelRequest = new BLL.Sys.DTOs.Request.RequestConversactionApplyAddFriendDTO() { FromUserIdentifier = FromUserInfo.identifier, FromUserMemberID = FromUserInfo.MemberID, FromUserType = FromUserInfo.UserType, FromUserID = FromUserInfo.UserID, FromUserName = FromUserInfo.UserCNName, AddFriendItem = new List <BLL.Sys.DTOs.Request.RequestConversactionApplyAddFriendDTO.AddFriendAccount>() }; //添加群组请求参数 var requestParamsCreateGroup = new List <int>() { FromUserInfo.identifier }; //添加好友请求参数 var requestParamsApplyAddFriend = new List <XuHos.Integration.QQCloudy.Models.AddFriendAccount>(); //循环好友项 evt.AddFriendItem.ForEach(a => { //好友好友的信息 var ToUserInfo = userService.GetUserInfoByUserId(a.ToUserID); //好友存在 if (ToUserInfo != null) { createC2CChannelRequest.AddFriendItem.Add(new BLL.Sys.DTOs.Request.RequestConversactionApplyAddFriendDTO.AddFriendAccount() { AddType = "Add_Type_Both", AddWording = a.AddWording, ForceAddFlags = 1, GroupName = a.GroupName, Remark = a.Remark, ToUserID = ToUserInfo.UserID, ToUserIdentifier = ToUserInfo.identifier, ToUserMemberID = ToUserInfo.MemberID, ToUserType = ToUserInfo.UserType, ToUserName = ToUserInfo.UserCNName }); requestParamsApplyAddFriend.Add(new XuHos.Integration.QQCloudy.Models.AddFriendAccount { AddWording = a.AddWording, AddSource = "AddSource_Type_WEB",//需要前缀AddSource_Type_ GroupName = a.GroupName, Remark = a.Remark, To_Account = ToUserInfo.identifier.ToString() }); requestParamsCreateGroup.Add(ToUserInfo.identifier); } }); //写入数据库 var ChannelInfoList = roomService.ApplyAddFriend(createC2CChannelRequest); using (XuHos.EventBus.MQChannel mqChannel = new EventBus.MQChannel()) { mqChannel.BeginTransaction(); foreach (var item in ChannelInfoList) { var room = item.Value; //根据Uid 获取好友信息 var friend = requestParamsApplyAddFriend.Find(a => a.To_Account == item.Key.ToString()); //如果房间还未启用才调用请求否则忽略 if (!room.Enable) { //发送好友附言消息 if (!mqChannel.Publish <EventBus.Events.ChannelSendGroupMsgEvent <string> >(new EventBus.Events.ChannelSendGroupMsgEvent <string>() { ChannelID = room.ChannelID, FromAccount = FromUserInfo.identifier, Msg = friend.AddWording })) { return(false); } //发布房间创建完成的领域消息 if (!mqChannel.Publish <EventBus.Events.ChannelCreatedEvent>(new EventBus.Events.ChannelCreatedEvent() { ChannelID = room.ChannelID, ServiceID = room.ServiceID, ServiceType = room.ServiceType })) { return(false); } //新增好友 if (!imHelper.ApplyAddFriend(FromUserInfo.identifier.ToString(), requestParamsApplyAddFriend)) { return(false); } //创建群组 if (!imHelper.CreateGroup(room.ChannelID, room.ChannelID.ToString(), room.ServiceType, requestParamsCreateGroup, "", "")) { return(false); } //设置房间已经启用 room.Enable = true; //更新房间状态 if (!roomService.CompareAndSetChannelInfo(room)) { return(false); } } } mqChannel.Commit(); } return(true); } } catch (Exception E) { LogHelper.WriteError(E); } return(false); }