|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/im/model"
- md "applet/app/md/im"
- "errors"
- "fmt"
- )
-
- func GroupList(req md.GroupListReq) (err error, resp md.GroupListResp) {
- sess := db.DbIm.Where("1=1")
- var groupIds []int64
- if req.GroupAdmin != "" {
- var user model.User
- has, err1 := db.DbIm.Where("`phone_number`=?", req.GroupAdmin).Get(&user)
- if err1 != nil {
- return
- }
- if !has {
- return errors.New("群主账号不存在"), resp
- }
- var groupUserList []*model.GroupUser
- err = db.DbIm.Where("`user_id`=? and member_type= 1", user.Id).Find(&groupUserList)
- if err != nil {
- return
- }
- for _, v := range groupUserList {
- groupIds = append(groupIds, v.GroupId)
- }
- }
-
- var mm []*model.Group
- if len(groupIds) > 0 {
- sess.In("id", groupIds)
- }
- resp.Total, err = sess.Limit(req.PageSize, (req.Page-1)*req.PageSize).Asc("id").FindAndCount(&mm)
- if err != nil {
- return
- }
- resp.Page = req.Page
- resp.PageSize = req.PageSize
- for _, v := range mm {
- var tmp = struct {
- Id int64 // 群组id
- Name string // 组名
- AvatarUrl string // 头像
- Introduction string // 群简介
- UserNum int32 // 群组人数
- IsAllMemberBanned int32 // 是否全员禁言(1:是 2:否)
- IsAllAddFriend int32 // 是否允许加好友(1:是 2:否)
- Extra string // 附加字段
- CreateTime string // 创建时间
- UpdateTime string // 更新时间
- PhoneNumber string // 群主账号
- Nickname string // 群主昵称
- }{
- Id: v.Id,
- Name: v.Name,
- AvatarUrl: v.AvatarUrl,
- Introduction: v.Introduction,
- UserNum: v.UserNum,
- IsAllMemberBanned: v.IsAllMemberBanned,
- IsAllAddFriend: v.IsAllAddFriend,
- Extra: v.Extra,
- CreateTime: v.CreateTime,
- UpdateTime: v.UpdateTime,
- }
- var groupUser model.GroupUser
- has, err1 := db.DbIm.Where("`group_id`=? and member_type= 1", v.Id).Get(&groupUser)
- if err1 != nil {
- return
- }
- if has {
- var user model.User
- has, err = db.DbIm.ID(groupUser.UserId).Get(&user)
- if err != nil {
- return
- }
- if has {
- tmp.PhoneNumber = user.PhoneNumber
- tmp.Nickname = user.Nickname
- }
- }
- resp.List = append(resp.List, tmp)
- }
- return
- }
-
- type GroupUserJoinUser struct {
- model.GroupUser `xorm:"extends"`
- model.User `xorm:"extends"`
- }
-
- func GroupUserList(req md.GroupUserListReq) (err error, resp md.GroupUserListResp) {
- sess := db.DbIm.Table("group_user").Where("1=1")
- sess.Join("LEFT", "user", "group_user.user_id = user.id")
- if req.Phone != "" {
- sess.And("user.phone LIKE ?", "%"+req.Phone+"%")
- }
- if req.Nickname != "" {
- sess.And("user.nickname LIKE ?", "%"+req.Nickname+"%")
- }
- var mm []*GroupUserJoinUser
- resp.Total, err = sess.Limit(req.PageSize, (req.Page-1)*req.PageSize).Asc("group_user.id").FindAndCount(&mm)
- sql, _ := sess.LastSQL()
- fmt.Println("GroupUserList___SQL:::", sql)
- if err != nil {
- return
- }
- resp.Page = req.Page
- resp.PageSize = req.PageSize
- for _, v := range mm {
- resp.List = append(resp.List, struct {
- Id int64 // 自增主键
- GroupId int64 // 群组id
- UserId int64 // 用户id
- MemberType int // 群组类型
- Remarks string // 备注
- Extra string // 附加属性
- Status int // 状态
- CreateTime string // 创建时间
- UpdateTime string // 更新时间
- PhoneNumber string // 账号
- Nickname string // 昵称
- }{
- Id: v.GroupUser.Id,
- GroupId: int64(v.GroupUser.GroupId),
- UserId: v.GroupUser.UserId,
- MemberType: v.GroupUser.MemberType,
- Remarks: v.GroupUser.Remarks,
- Extra: v.GroupUser.Extra,
- Status: v.GroupUser.Status,
- CreateTime: v.GroupUser.CreateTime,
- UpdateTime: v.GroupUser.UpdateTime,
- PhoneNumber: v.User.PhoneNumber,
- Nickname: v.User.Nickname,
- })
- }
- return
- }
|