package staff

import (
	"context"
	"time"

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

// GetProfile 获取个人信息
func GetProfile(ctx context.Context, in *pb.IdentRequest) (reply *pb.StaffItem, err error) {
	// parse authorization meta.
	_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
	if err != nil {
		return nil, err
	}

	// valildate request id,identity.
	if in.Id == 0 && in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	var data models.MallStaff
	err = impl.DBService.Where("id=? or identity=?", in.GetId(), in.GetIdentity()).First(&data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	return &pb.StaffItem{
		Id:        int64(data.ID),
		Identity:  data.Identity,
		Name:      data.Name,
		Account:   data.Account,
		Profile:   data.Profile,
		Phone:     data.Phone,
		Email:     data.Email,
		Avatar:    data.Avatar,
		Role:      data.Role,
		Status:    int32(data.Status),
		CreatedAt: data.CreatedAt.Format(time.DateTime),
	}, nil
}
