88 lines
2.8 KiB
Go
88 lines
2.8 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"bsm/full/module/social/group/internal/impl"
|
|
pb "bsm/full/module/social/group/pb"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func InitData() error {
|
|
|
|
return nil
|
|
}
|
|
|
|
// ToGroupItem 将群组模型转换为对外的 GroupItem
|
|
func ToGroupItem(group *GroupBasic) *pb.GroupItem {
|
|
if group == nil {
|
|
return nil
|
|
}
|
|
return &pb.GroupItem{
|
|
Id: int64(group.ID),
|
|
Identity: group.Identity,
|
|
Number: group.Number,
|
|
Avatar: group.Avatar,
|
|
Name: group.Name,
|
|
Introduce: group.Introduce,
|
|
CreatorId: int64(group.PassportID),
|
|
CretorIdentity: group.PassportIdentity,
|
|
MemberLimit: group.MemberLimit,
|
|
Notice: group.Notice,
|
|
Background: group.Background,
|
|
MemberTotal: group.MemberTotal,
|
|
EnableSearchByNumber: group.EnableSearchByNumber,
|
|
EnableSearchByName: group.EnableSearchByName,
|
|
CreatedAt: group.CreatedAt.Format(time.DateTime),
|
|
}
|
|
}
|
|
|
|
// GetMyGroup 查询用户已加入的群组列表
|
|
func GetMyGroup(passport_id uint) (groups []*pb.GroupItem, err error) {
|
|
var ids []uint
|
|
if err = impl.DBService.Model(&GroupMember{}).Where("passport_id = ?", passport_id).Pluck("group_id", &ids).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if len(ids) == 0 {
|
|
return groups, nil
|
|
}
|
|
var basics []GroupBasic
|
|
if err = impl.DBService.Where("id in ?", ids).Find(&basics).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
groups = make([]*pb.GroupItem, 0, len(basics))
|
|
for i := range basics {
|
|
groups = append(groups, ToGroupItem(&basics[i]))
|
|
}
|
|
return groups, nil
|
|
}
|
|
|
|
func GetMasterGroup(passport_id uint) []uint {
|
|
var ids []uint
|
|
impl.DBService.Model(GroupMember{}).Select("group_id").Where("passport_id=? and role!=0", passport_id).Pluck("group_id", &ids)
|
|
return ids
|
|
}
|
|
|
|
// GetGroupMember 查询群成员列表,成员资料来自外部表 passport_extend
|
|
func GetGroupMember(group_identity string) (members []*pb.PassportInfoSimpleCard, err error) {
|
|
sql := `Select pe.identity,pe.nickname,pe.avatar,pe.sex,gm.remark_name,gm.role
|
|
From relation_group_member as gm
|
|
Left join passport_extend as pe on pe.passport_id=gm.passport_id
|
|
Where gm.group_identity=? and gm.deleted_at is null`
|
|
err = impl.DBService.Raw(sql, group_identity).Find(&members).Error
|
|
return
|
|
}
|
|
|
|
// GetGroupEnableAnyJoin 查询群组是否开启免验证入群
|
|
func GetGroupEnableAnyJoin(group_identity string) (bool, error) {
|
|
var group GroupBasic
|
|
if err := impl.DBService.Select("enable_any_join").Where("identity=?", group_identity).Take(&group).Error; err != nil {
|
|
return false, err
|
|
}
|
|
return group.EnableAnyJoin, nil
|
|
}
|
|
|
|
func UpsetGroupMemberTotal(group_identity string, op string) {
|
|
impl.DBService.Model(&GroupBasic{}).Where("identity=?", group_identity).UpdateColumn("member_total", gorm.Expr("member_total ? ?", op, 1))
|
|
}
|