package service import ( "errors" "strconv" "time" "videoplayer/dao" "videoplayer/proto" "videoplayer/worker" ) func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content string) (error, uint) { // 业务逻辑 var err error var id uint switch msg_type { case proto.MSG_TYPE_SIMPLE: //判断是否是好友 friend := dao.FindFriend(from_id, to_id) if len(friend) == 0 { return errors.New("未添加好友"), 0 } err, id = dao.CreateSimpleMessage(from_id, to_id, content) res := worker.GetRedis("user_" + strconv.Itoa(to_id) + "_status_v2") if res == "1" { //在线,存入redis worker.PushRedisListWithExpire("user_"+strconv.Itoa(to_id)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300) } case proto.MSG_TYPE_GROUP: if from_id == 0 || group_id == 0 || content == "" { return errors.New("参数错误"), 0 } //判断该用户是否在群里 groupUser := dao.FindGroupUser(from_id, group_id) if len(groupUser) == 0 { return errors.New("用户不在群里"), 0 } err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content) //获取群里的用户 users := dao.FindGroupUsers(group_id) for _, user := range users { if user.UserID == from_id { continue } res := worker.GetRedis("user_" + strconv.Itoa(user.UserID) + "_status_v2") if res == "1" { //在线,存入redis worker.PushRedisListWithExpire("user_"+strconv.Itoa(user.UserID)+"_msg_ids", strconv.Itoa(int(id)), time.Second*300) } } case 3: //user := dao.FindUserByID(to_id) // 系统消息,需要管理员权限 err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content) case proto.MSG_TYPE_FRIEND: res1 := dao.FindFriend(from_id, to_id) if len(res1) > 0 { // 已经有会话记录 return errors.New("已是好友关系"), 0 } res, _ := dao.GetMsgUserByIndex(from_id, to_id, 4, 1, 0) if len(res) > 0 { // 已经有会话记录,返回会话id return errors.New("已有请求"), res[0].ID } err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content) case proto.MSG_TYPE_GROUP_ADD: //加入群聊请求 //判断是否在群里 groupUser := dao.FindGroupUser(from_id, group_id) if len(groupUser) > 0 { return errors.New("已在群里"), 0 } //查看是否已经发送过请求 res, _ := dao.GetMsgUserGroupReq(from_id, group_id) if len(res) > 0 { return errors.New("已发送请求"), res[0].ID } err, id = dao.CreateGeneralMessage(from_id, to_id, msg_type, 0, group_id, content) case proto.MSG_TYPE_GROUP_INVI: //邀请加群,直接加入 //判断邀请人是否在群里 groupUser := dao.FindGroupUser(from_id, group_id) if len(groupUser) == 0 { return errors.New("邀请人不在群里"), 0 } //判断被邀请人是否在群里 groupUser = dao.FindGroupUser(to_id, group_id) if len(groupUser) > 0 { return errors.New("已在群里"), 0 } err, id = dao.JoinGroup(group_id, to_id) default: // 未知消息类型 err = errors.New("unknown message type") } return err, id } func GetMsgUserByIndexService(from_id, to_id, index, msq_type, from_user_id, group_id int) ([]dao.Message, error) { // 业务逻辑 var msgs []dao.Message var err error if index <= 0 || index > 100 { return nil, errors.New("index out of range") } if msq_type == proto.MSG_TYPE_FRIEND { from_id = from_user_id } msgs, err = dao.GetMsgUserByIndex(from_id, to_id, msq_type, index, 0) //群聊消息 return msgs, err } // AddFriendService 通过消息id添加好友,和加入群聊 // id 消息id // from_user_id 发送消息的用户id // to_user_id 接收消息的用户id,及接受用户id func AddFriendService(id, from_user_id, to_user_id int) error { // 业务逻辑 res := dao.FindMessageByID(uint(id)) if len(res) == 0 { return errors.New("no such message") } if res[0].FromUserID == to_user_id && res[0].ToUserID == from_user_id { friend := dao.FindFriend(from_user_id, to_user_id) if len(friend) > 0 { return errors.New("already a friend") } dao.UpdateMessageStatus(res[0].ID, 1) return dao.AddFriend(from_user_id, to_user_id) } else if res[0].ToUserID == from_user_id && res[0].GroupID == to_user_id { //加入群聊 //查看是否已经加入 groupUser := dao.FindGroupUser(from_user_id, to_user_id) if len(groupUser) > 0 { return errors.New("already in the group") } err, _ := dao.JoinGroup(groupUser[0].GroupID, to_user_id) if err != nil { return err } err = dao.UpdateMessageStatus(res[0].ID, 1) return err } else { return errors.New("no such message,cannot add friend") } } // CreateGroup 创建群聊 // groupName 群聊名称 // groupInfo 群聊信息 // groupType 群聊类型 // groupIcon 群聊图标 // user_id 创建群的用户id func CreateGroup(groupName, groupInfo, groupType, groupIcon string, user_id int) (error, uint) { // 业务逻辑 err, id := dao.CreateGroup(groupName, groupInfo, groupType, groupIcon, user_id) return err, id } type FGRet struct { Friends []dao.FriendRet `json:"friends"` Groups []dao.Group `json:"groups"` } func GetFriendList(user_id int) FGRet { //获取好友id和群id friends := dao.FindFriends(user_id) groups := dao.FindGroups(user_id) var fg FGRet fg.Friends = friends fg.Groups = groups return fg } func GetFriendRequest(user_id int) []dao.FriendRequest { //获取好友请求 users := dao.GetFriendRequest(user_id) return users } func DelFriendService(user_id, friend_id int) error { //删除好友 err := dao.DeleteFriend(user_id, friend_id) return err } func QuitGroupService(user_id, group_id int) error { //退出群聊 err := dao.QuitGroup(group_id, user_id) return err } func DelGroupService(user_id, group_id int) error { //查找群聊 groups := dao.FindGroup(group_id) if len(groups) == 0 { return errors.New("no such group") } if groups[0].AuthID != user_id { return errors.New("no permission") } //删除群聊 err := dao.DeleteGroup(group_id, user_id) err = dao.DeleteGroupUsers(group_id) return err } func GetGroups(user_id int) []dao.Group { //获取群聊 groups := dao.GetGroups(user_id) return groups } func GetGroupByID(group_id int) []dao.Group { //获取群聊 groups := dao.FindGroupByID(group_id) return groups } func GetGroupByNameLike(name string) []dao.Group { //获取群聊 groups := dao.FindGroupByNameLike(name) return groups } func GetGroupRequestUsers(user_id int) []dao.FriendRequest { //获取群聊请求 users := dao.GetGroupRequestUsers(user_id) return users }