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 Modify(ctx context.Context, in *pb.MarketAgenctyItem) (reply *pb.StatusReply, 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{
		Name:           in.GetName(),
		Avatar:         in.GetAvatar(),
		Account:        in.GetAccount(),
		Phone:          in.GetPhone(),
		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(),
	}
	if err := models.DBService.Where("identity=?", identity).Updates(&mktModel).Error; err != nil {
		log.Println("更新代理商数据失败：", err)
		return nil, err
	}

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

}
