2025-04-09 14:46:53 +00:00
|
|
|
package productitemservicelogic
|
2025-04-09 09:29:56 +00:00
|
|
|
|
|
|
|
import (
|
2025-04-10 07:59:30 +00:00
|
|
|
"context"
|
|
|
|
|
2025-04-09 09:29:56 +00:00
|
|
|
"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"
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2025-04-09 14:46:53 +00:00
|
|
|
type CreateLogic struct {
|
2025-04-09 09:29:56 +00:00
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
logx.Logger
|
|
|
|
}
|
|
|
|
|
2025-04-09 14:46:53 +00:00
|
|
|
func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLogic {
|
|
|
|
return &CreateLogic{
|
2025-04-09 09:29:56 +00:00
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-09 14:46:53 +00:00
|
|
|
// Create 建立 ProductItem
|
|
|
|
func (l *CreateLogic) Create(in *PB.CreateProductItemRequest) (*PB.OKResp, error) {
|
2025-04-09 09:29:56 +00:00
|
|
|
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))
|
2025-04-09 14:46:53 +00:00
|
|
|
for i, item := range in.GetItem().Media {
|
2025-04-09 09:29:56 +00:00
|
|
|
m = append(m, usecase.Media{
|
2025-04-09 14:46:53 +00:00
|
|
|
Sort: uint64(i),
|
2025-04-09 09:29:56 +00:00
|
|
|
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
|
|
|
|
}
|