package member

import (
	"context"
	"time"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"git.apinb.com/bsm-sdk/core/vars"
	"bsm/full/module/social/group/internal/impl"
	"bsm/full/module/social/group/internal/models"
	pb "bsm/full/module/social/group/pb"
)

// 踢人
func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	// 判断是不是群管理或是创建者
	var tc int64
	if err = impl.DBService.Model(&models.GroupMember{}).Where("identity=? and role !=0 and passport_id=?", in.GroupIdentity, auth.Identity).Count(&tc).Error; err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	if tc == 0 {
		return nil, errcode.ErrPermissionDenied
	}

	err = impl.DBService.Model(&models.GroupMember{}).Delete("group_identity=? and identity=?", in.GroupIdentity, in.Identity).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	} else {
		//更新群组表数据统计
		models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
	}
	return &pb.StatusReply{
		Data:    vars.OK,
		Timeseq: time.Now().UnixMilli(),
	}, nil
}
