package agency

import (
	"context"
	"log"
	"time"

	"bsm/full/module/ec/market/internal/models"
	pb "bsm/full/module/ec/market/pb"
	"git.apinb.com/bsm-sdk/core/service"
)

// 获取一个代理商
func Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.MarketAgenctyItem, err error) {
	var (
		identity string
	)
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	identity = auth.Identity
	if in.GetIdentity() != "" {
		identity = in.GetIdentity()
	}
	mktModel := models.MarketAgency{}
	if err := models.DBService.Where("identity=?", identity).First(&mktModel).Error; err != nil {
		log.Println("获取代理商数据失败：", err)
		return nil, err
	}
	reply = &pb.MarketAgenctyItem{
		Identity:   mktModel.Identity,
		Name:       mktModel.Name,
		Avatar:     mktModel.Avatar,
		Account:    mktModel.Account,
		Phone:      mktModel.Phone,
		OrgName:    mktModel.OrgName,
		OrgPhoto:   mktModel.OrgPhoto,
		IdName:     mktModel.IDName,
		IdBefore:   mktModel.IDBefore,
		IdAfter:    mktModel.IDAfter,
		Remark:     mktModel.Remark,
		Status:     int32(mktModel.Status),
		AgencyType: int32(mktModel.AgencyType),
		CreatedAt:  mktModel.CreatedAt.Format(time.DateTime),
		Email:      mktModel.Email,
		Country:    mktModel.Country,
		Area:       mktModel.Area,
		Approve:    int32(mktModel.Approve),
	}

	return reply, nil
}
