package exchange

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"sundynix-micro-go/app/plant/api/internal/svc"
	"sundynix-micro-go/app/plant/api/internal/types"
	plantPb "sundynix-micro-go/app/plant/rpc/plant"
)

type UpdateExchangeItemLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateExchangeItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateExchangeItemLogic {
	return &UpdateExchangeItemLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateExchangeItemLogic) UpdateExchangeItem(req *types.UpdateExchangeItemReq) error {
	desc := req.Desc
	if desc == "" {
		desc = req.Description
	}
	imgID := req.ImgId
	if imgID == "" {
		imgID = req.ImageId
	}
	cost := req.Cost
	if cost == 0 {
		cost = req.CostSunlight
	}
	_, err := l.svcCtx.PlantRpc.UpdateExchangeItem(l.ctx, &plantPb.UpdateExchangeItemReq{
		Id: req.Id, Name: req.Name, Desc: desc, ImgId: imgID,
		Cost: cost, Stock: int32(req.Stock), Status: int32(req.Status),
		Type: req.Type, CostSunlight: req.CostSunlight, LimitPerUser: int32(req.LimitPerUser),
		Sort: int32(req.Sort), StartTime: req.StartTime, EndTime: req.EndTime, ImageId: req.ImageId,
	})
	return err
}
