package models

import (
	"bsm/full/module/social/group/internal/impl"
	pb "bsm/full/module/social/group/pb"
	"gorm.io/gorm"
)

func InitData() error {

	return nil
}

func GetMyGroup(passport_id uint) (groups []*pb.GroupItem, err error) {

	sql := `Select group.*,gm.nickname,gm.remark_name,gm.role 
			From  group_member as gm 
			Left join group  on group.id=gm.group_id
			Where passport_id=? and deleted_at is null`
	err = impl.DBService.Raw(sql, passport_id).Find(&groups).Error
	return
}

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
}

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  group_member as gm 
			Left join passport_extend as pe  on pe.passport_id=gm.passport_id
			Where group_identity=? and deleted_at is null`
	err = impl.DBService.Raw(sql, group_identity).Find(&members).Error
	return
}

func GetGroupEnableAnyJoin(passport_id uint, group_identity string) bool {
	var is bool = false
	impl.DBService.Model(&GroupBasic{}).Select("enable_any_join").Where("createor_id=? and identity=?", passport_id, group_identity).Scan(&is)
	return is
}

func UpsetGroupMemberTotal(group_identity string, op string) {
	impl.DBService.Model(&GroupBasic{}).Where("identity=?", group_identity).UpdateColumn("member_total", gorm.Expr("member_total ? ?", op, 1))
}
