97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
|
package productlogic
|
||
|
|
||
|
import (
|
||
|
"code.30cm.net/digimon/app-cloudep-product-service/internal/utils"
|
||
|
"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"
|
||
|
|
||
|
PB "code.30cm.net/digimon/app-cloudep-product-service/gen_result/pb/product"
|
||
|
"code.30cm.net/digimon/app-cloudep-product-service/internal/svc"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
)
|
||
|
|
||
|
type CreateItemLogic struct {
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
logx.Logger
|
||
|
}
|
||
|
|
||
|
func NewCreateItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateItemLogic {
|
||
|
return &CreateItemLogic{
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *CreateItemLogic) CreateItem(in *PB.CreateProductItemRequest) (*PB.OKResp, error) {
|
||
|
insert := &usecase.ProductItems{
|
||
|
ReferenceID: in.GetItem().ReferenceId, // 對應的專案 ID
|
||
|
Name: in.GetItem().Name, // 名稱
|
||
|
Description: utils.EncodeToBase64Snappy(in.GetItem().Description), // 描述
|
||
|
ShortDescription: utils.EncodeToBase64Snappy(in.GetItem().ShortDescription), // 封面簡短描述
|
||
|
IsUnLimit: in.GetItem().IsUnLimit, // 是否沒有數量上限
|
||
|
IsFree: in.GetItem().IsFree, // 是否為免費品項(贈品) -> 開啟就是自訂金額
|
||
|
Stock: in.GetItem().Stock, // 庫存總數
|
||
|
Price: in.GetItem().Price, // 價格
|
||
|
SKU: in.GetItem().Sku, // 型號:對應顯示 Item 的 FK
|
||
|
SalesCount: 0, // 已賣出數量(相反,減到零就不能在賣)
|
||
|
}
|
||
|
if len(in.GetItem().Media) > 0 {
|
||
|
// 專案動態內容(圖片或者影片)
|
||
|
m := make([]usecase.Media, 0, len(in.GetItem().Media))
|
||
|
for _, item := range in.GetItem().Media {
|
||
|
m = append(m, usecase.Media{
|
||
|
URL: item.Url,
|
||
|
Type: item.Type,
|
||
|
})
|
||
|
}
|
||
|
insert.Media = m
|
||
|
}
|
||
|
if len(in.GetItem().CustomFields) > 0 {
|
||
|
// 自定義屬性
|
||
|
c := make([]usecase.CustomFields, 0, len(in.GetItem().CustomFields))
|
||
|
for _, cItem := range in.GetItem().CustomFields {
|
||
|
c = append(c, usecase.CustomFields{
|
||
|
Key: cItem.Key,
|
||
|
Value: cItem.Value,
|
||
|
})
|
||
|
}
|
||
|
insert.CustomFields = c
|
||
|
}
|
||
|
if len(in.GetItem().Freight) > 0 {
|
||
|
// 運費
|
||
|
f := make([]usecase.CustomFields, 0, len(in.GetItem().Freight))
|
||
|
for _, fItem := range in.GetItem().Freight {
|
||
|
f = append(f, usecase.CustomFields{
|
||
|
Key: fItem.Key,
|
||
|
Value: fItem.Value,
|
||
|
})
|
||
|
}
|
||
|
insert.Freight = f
|
||
|
}
|
||
|
|
||
|
status, e := product.StringToItemStatus(in.GetItem().Status)
|
||
|
if !e {
|
||
|
return nil, errs.InvalidFormat("failed to convert string to item status")
|
||
|
}
|
||
|
insert.Status = status
|
||
|
|
||
|
timeSeries, te := product.StringToTimeSeries(in.GetItem().TimeSeries)
|
||
|
if !te {
|
||
|
return nil, errs.InvalidFormat("failed to convert string to time series")
|
||
|
}
|
||
|
insert.TimeSeries = timeSeries
|
||
|
|
||
|
err := l.svcCtx.ProductItemUseCase.Create(l.ctx, insert)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &PB.OKResp{}, nil
|
||
|
}
|