package member

import (
	"context"
	"strings"
	"time"

	"bsm/full/module/social/group/internal/impl"
	"bsm/full/module/social/group/internal/models"
	pb "bsm/full/module/social/group/pb"
	"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"
)

// 设置或取消管理员
func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	// 判断是不是创建者
	var creator_id uint
	if err = impl.DBService.Model(&models.GroupBasic{}).Select("creator_id").Where("identity=?", in.GroupIdentity).Scan(&creator_id).Error; err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	if auth.ID != creator_id {
		return nil, errcode.ErrPermissionDenied
	}

	switch strings.ToUpper(in.Direction) {
	case "SET":
		//设置管理员
		impl.DBService.Model(&models.GroupMember{}).Where("identity=?", in.Identity).UpdateColumn("role", ROLE_MASTER)
	case "CANCEL":
		//取消管理员
		impl.DBService.Model(&models.GroupMember{}).Where("identity=?", in.Identity).UpdateColumn("role", ROLE_MEMBER)
	}
	return &pb.DataStatusReply{
		Data:    vars.OK,
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
