89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package productitemservicelogic
|
|
|
|
import (
|
|
"code.30cm.net/digimon/app-cloudep-product-service/gen_result/pb/product"
|
|
"code.30cm.net/digimon/app-cloudep-product-service/internal/svc"
|
|
domain "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/product"
|
|
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase"
|
|
"code.30cm.net/digimon/library-go/errs"
|
|
"context"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
|
|
return &UpdateLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// Update 更新 Item
|
|
func (l *UpdateLogic) Update(in *product.UpdateProductItemRequest) (*product.OKResp, error) {
|
|
update := &usecase.UpdateProductItems{
|
|
Name: in.Name,
|
|
Description: in.Description,
|
|
ShortDescription: in.ShortDescription,
|
|
IsUnLimit: in.IsUnLimit,
|
|
IsFree: in.IsFree,
|
|
Stock: in.Stock,
|
|
Price: in.Price,
|
|
SKU: in.Sku,
|
|
}
|
|
if in.TimeSeries != nil {
|
|
ts, status := domain.StringToTimeSeries(in.GetTimeSeries())
|
|
if !status {
|
|
return nil, errs.InvalidFormat("timeSeries")
|
|
}
|
|
update.TimeSeries = &ts
|
|
}
|
|
if len(in.GetMedia()) > 0 {
|
|
// 專案動態內容(圖片或者影片)
|
|
m := make([]usecase.Media, 0, len(in.GetMedia()))
|
|
for _, item := range in.GetMedia() {
|
|
m = append(m, usecase.Media{
|
|
Sort: *item.Sort,
|
|
URL: item.Url,
|
|
Type: item.Type,
|
|
})
|
|
}
|
|
update.Media = m
|
|
}
|
|
if len(in.GetCustomFields()) > 0 {
|
|
// 自定義屬性
|
|
c := make([]usecase.CustomFields, 0, len(in.GetCustomFields()))
|
|
for _, cItem := range in.GetCustomFields() {
|
|
c = append(c, usecase.CustomFields{
|
|
Key: cItem.Key,
|
|
Value: cItem.Value,
|
|
})
|
|
}
|
|
update.CustomFields = c
|
|
}
|
|
if len(in.GetFreight()) > 0 {
|
|
// 運費
|
|
f := make([]usecase.CustomFields, 0, len(in.GetFreight()))
|
|
for _, fItem := range in.GetFreight() {
|
|
f = append(f, usecase.CustomFields{
|
|
Key: fItem.Key,
|
|
Value: fItem.Value,
|
|
})
|
|
}
|
|
update.Freight = f
|
|
}
|
|
|
|
err := l.svcCtx.ProductItemUseCase.Update(l.ctx, in.GetId(), update)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &product.OKResp{}, nil
|
|
}
|