package agency

import (
	"context"
	"fmt"
	"time"

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

// 新增代理商
func Create(ctx context.Context, in *pb.MarketAgenctyItem) (reply *pb.StatusReply, err error) {
	// _, err = service.ParseMetaCtx(ctx, nil)
	// if err != nil {
	// 	return nil, err
	// }
	if in.GetName() == "" || in.GetAccount() == "" || in.GetPassword() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	salt := utils.RandomString(8)
	mktModel := &models.MarketAgency{
		Std_IICUDS:     types.Std_IICUDS{Identity: utils.ULID(), Status: 1},
		Name:           in.GetName(),
		Avatar:         in.GetAvatar(),
		Account:        in.GetAccount(),
		Phone:          in.GetPhone(),
		Password:       utils.Md5(in.Password + salt),
		Salt:           salt,
		OrgName:        in.GetOrgName(),
		OrgPhoto:       in.GetOrgPhoto(),
		IDName:         in.GetIdName(),
		IDBefore:       in.GetIdBefore(),
		IDAfter:        in.GetIdAfter(),
		Remark:         in.GetRemark(),
		CommissionRate: in.GetCommissionRate(),
		AgencyType:     in.GetAgencyType(),
		Email:          in.GetEmail(),
		Country:        in.GetCountry(),
		Area:           in.GetArea(),
	}
	fmt.Println("mktModel = ", mktModel)
	if err := models.DBService.Create(&mktModel).Error; err != nil {
		fmt.Println("err = ", err)

		return nil, err
	}

	return &pb.StatusReply{
		Code:     0,
		Message:  "OK",
		Identity: mktModel.Identity,
		Timeseq:  time.Now().UnixMilli(),
	}, nil

}
