package supply

import (
	"context"
	"log"
	"time"

	"bsm/full/module/ec/market/internal/impl"
	"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/service"
)

// 更新供应商数据
func Modify(ctx context.Context, in *pb.MarketSupplyItem) (reply *pb.IdentityStatusReply, err error) {
	_, err = service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	if in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}
	mktModel := &models.MarketSupply{
		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(),
	}
	if err := impl.DBService.Where("identity=?", in.GetIdentity()).Updates(&mktModel).Error; err != nil {
		log.Println("更新供应商数据失败：", err)
		return nil, err
	}

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

}
