diff --git a/api/admin/crm/product/productstatistics.api b/api/admin/crm/product/productstatistics.api index fcdfcb9b..cae764e2 100644 --- a/api/admin/crm/product/productstatistics.api +++ b/api/admin/crm/product/productstatistics.api @@ -15,48 +15,38 @@ info( ) service PowerX { - @doc "查询产品规格列表" + @doc "查询产品统计列表" @handler ListProductStatisticsPage get /product-statistics/page-list (ListProductStatisticsPageRequest) returns (ListProductStatisticsPageReply) - @doc "查询产品规格详情" + @doc "查询产品统计详情" @handler GetProductStatistics get /product-statistics/:id (GetProductStatisticsRequest) returns (GetProductStatisticsReply) - - @doc "创建产品规格" - @handler CreateProductStatistics - post /product-statistics (CreateProductStatisticsRequest) returns (CreateProductStatisticsReply) - - @doc "配置产品规格" + @doc "配置产品统计" @handler ConfigProductStatistics post /product-statistics/config (ConfigProductStatisticsRequest) returns (ConfigProductStatisticsReply) - - @doc "全量产品规格" + @doc "全量产品统计" @handler PutProductStatistics put /product-statistics/:id (PutProductStatisticsRequest) returns (PutProductStatisticsReply) - @doc "增量产品规格" + @doc "增量产品统计" @handler PatchProductStatistics patch /product-statistics/:id (PatchProductStatisticsRequest) returns (PatchProductStatisticsReply) - - @doc "删除产品规格" - @handler DeleteProductStatistics - delete /product-statistics/:id (DeleteProductStatisticsRequest) returns (DeleteProductStatisticsReply) } type ( ProductStatistics { Id int64 `json:"id,optional"` ProductId int64 `json:"productId"` - SoldAmount int64 `json:"soldAmount,optional"` - InventoryQuantity int64 `json:"inventoryQuantity,optional"` - ViewCount int64 `json:"viewCount,optional"` - BaseSoldAmount int64 `json:"baseSoldAmount,optional"` - BaseInventoryQuantity int64 `json:"baseInventoryQuantity,optional"` - BaseViewCount int64 `json:"baseViewCount,optional"` + SoldAmount int64 `json:"soldAmount,optional"` + InventoryQuantity int64 `json:"inventoryQuantity,optional"` + ViewCount int64 `json:"viewCount,optional"` + BaseSoldAmount int64 `json:"baseSoldAmount,optional"` + BaseInventoryQuantity int64 `json:"baseInventoryQuantity,optional"` + BaseViewCount int64 `json:"baseViewCount,optional"` } ) @@ -78,19 +68,10 @@ type ( } ) -type ( - CreateProductStatisticsRequest struct { - ProductStatistics - } - - CreateProductStatisticsReply struct { - ProductStatisticsId int64 `json:"id"` - } -) type ( ConfigProductStatisticsRequest struct { - ProductStatisticss []ProductStatistics `json:"productStatisticss"` + *ProductStatistics } ConfigProductStatisticsReply struct { @@ -100,7 +81,7 @@ type ( type ( GetProductStatisticsRequest struct { - ProductStatisticsId int64 `path:"id"` + ProductId int64 `path:"id"` } GetProductStatisticsReply struct { diff --git a/internal/handler/admin/crm/product/productstatistics/createproductstatisticshandler.go b/internal/handler/admin/crm/product/productstatistics/createproductstatisticshandler.go deleted file mode 100644 index 0960c424..00000000 --- a/internal/handler/admin/crm/product/productstatistics/createproductstatisticshandler.go +++ /dev/null @@ -1,28 +0,0 @@ -package productstatistics - -import ( - "net/http" - - "PowerX/internal/logic/admin/crm/product/productstatistics" - "PowerX/internal/svc" - "PowerX/internal/types" - "github.com/zeromicro/go-zero/rest/httpx" -) - -func CreateProductStatisticsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - var req types.CreateProductStatisticsRequest - if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) - return - } - - l := productstatistics.NewCreateProductStatisticsLogic(r.Context(), svcCtx) - resp, err := l.CreateProductStatistics(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } - } -} diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 82755b57..38e2303b 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1666,11 +1666,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/product-statistics/:id", Handler: admincrmproductproductstatistics.GetProductStatisticsHandler(serverCtx), }, - { - Method: http.MethodPost, - Path: "/product-statistics", - Handler: admincrmproductproductstatistics.CreateProductStatisticsHandler(serverCtx), - }, { Method: http.MethodPost, Path: "/product-statistics/config", @@ -1686,11 +1681,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/product-statistics/:id", Handler: admincrmproductproductstatistics.PatchProductStatisticsHandler(serverCtx), }, - { - Method: http.MethodDelete, - Path: "/product-statistics/:id", - Handler: admincrmproductproductstatistics.DeleteProductStatisticsHandler(serverCtx), - }, }..., ), rest.WithPrefix("/api/v1/admin/product"), diff --git a/internal/logic/admin/crm/product/productspecific/configproductspecificlogic.go b/internal/logic/admin/crm/product/productspecific/configproductspecificlogic.go index e3de19f1..f5f51e63 100644 --- a/internal/logic/admin/crm/product/productspecific/configproductspecificlogic.go +++ b/internal/logic/admin/crm/product/productspecific/configproductspecificlogic.go @@ -27,7 +27,7 @@ func NewConfigProductSpecificLogic(ctx context.Context, svcCtx *svc.ServiceConte func (l *ConfigProductSpecificLogic) ConfigProductSpecific(req *types.ConfigProductSpecificRequest) (resp *types.ConfigProductSpecificReply, err error) { - specifics := TransformProductSpecificsRequestToProductSpecifics(req.ProductSpecifics) + specifics := TransformRequestToProductSpecifics(req.ProductSpecifics) options := GetOptionsFromProductSpecificsRequest(specifics) //fmt.Dump(specifics) err = l.svcCtx.PowerX.ProductSpecific.ConfigProductSpecific(l.ctx, specifics, options) @@ -51,7 +51,7 @@ func (l *ConfigProductSpecificLogic) ConfigProductSpecific(req *types.ConfigProd }, nil } -func TransformProductSpecificsRequestToProductSpecifics(specificsRequest []types.ProductSpecific) []*product2.ProductSpecific { +func TransformRequestToProductSpecifics(specificsRequest []types.ProductSpecific) []*product2.ProductSpecific { specifics := []*product2.ProductSpecific{} for _, specificRequest := range specificsRequest { diff --git a/internal/logic/admin/crm/product/productspecific/getproductspecificlogic.go b/internal/logic/admin/crm/product/productspecific/getproductspecificlogic.go index dcade3ff..f234e9f8 100644 --- a/internal/logic/admin/crm/product/productspecific/getproductspecificlogic.go +++ b/internal/logic/admin/crm/product/productspecific/getproductspecificlogic.go @@ -30,7 +30,7 @@ func (l *GetProductSpecificLogic) GetProductSpecific(req *types.GetProductSpecif return } -func TransformProductSpecificToProductSpecificReply(specific *product2.ProductSpecific) (specificReply *types.ProductSpecific) { +func TransformProductSpecificToReply(specific *product2.ProductSpecific) (specificReply *types.ProductSpecific) { if specific == nil { return nil } diff --git a/internal/logic/admin/crm/product/productspecific/listproductspecificpagelogic.go b/internal/logic/admin/crm/product/productspecific/listproductspecificpagelogic.go index efb1bf2d..ec68d447 100644 --- a/internal/logic/admin/crm/product/productspecific/listproductspecificpagelogic.go +++ b/internal/logic/admin/crm/product/productspecific/listproductspecificpagelogic.go @@ -54,7 +54,7 @@ func TransformProductSpecificsToReply(specifics []*product2.ProductSpecific) (sp specificsReply = []*types.ProductSpecific{} for _, entry := range specifics { - specificsReply = append(specificsReply, TransformProductSpecificToProductSpecificReply(entry)) + specificsReply = append(specificsReply, TransformProductSpecificToReply(entry)) } diff --git a/internal/logic/admin/crm/product/productstatistics/configproductstatisticslogic.go b/internal/logic/admin/crm/product/productstatistics/configproductstatisticslogic.go index b3845bfd..7badc495 100644 --- a/internal/logic/admin/crm/product/productstatistics/configproductstatisticslogic.go +++ b/internal/logic/admin/crm/product/productstatistics/configproductstatisticslogic.go @@ -1,6 +1,8 @@ package productstatistics import ( + "PowerX/internal/model/crm/product" + "PowerX/internal/types/errorx" "context" "PowerX/internal/svc" @@ -24,7 +26,25 @@ func NewConfigProductStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceCon } func (l *ConfigProductStatisticsLogic) ConfigProductStatistics(req *types.ConfigProductStatisticsRequest) (resp *types.ConfigProductStatisticsReply, err error) { - // todo: add your logic here and delete this line - return + staticstics := TransformRequestToProductStaticstics(req.ProductStatistics) + + staticstics, err = l.svcCtx.PowerX.ProductStatistics.UpsertProductStatistics(l.ctx, staticstics) + if err != nil { + return nil, errorx.WithCause(errorx.ErrBadRequest, err.Error()) + } + + return &types.ConfigProductStatisticsReply{ + Result: true, + }, nil +} + +func TransformRequestToProductStaticstics(statistics *types.ProductStatistics) *product.ProductStatistics { + + return &product.ProductStatistics{ + ProductId: statistics.ProductId, + BaseSoldAmount: statistics.BaseSoldAmount, + BaseInventoryQuantity: statistics.BaseInventoryQuantity, + BaseViewCount: statistics.BaseViewCount, + } } diff --git a/internal/logic/admin/crm/product/productstatistics/createproductstatisticslogic.go b/internal/logic/admin/crm/product/productstatistics/createproductstatisticslogic.go deleted file mode 100644 index 7f83e51c..00000000 --- a/internal/logic/admin/crm/product/productstatistics/createproductstatisticslogic.go +++ /dev/null @@ -1,30 +0,0 @@ -package productstatistics - -import ( - "context" - - "PowerX/internal/svc" - "PowerX/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type CreateProductStatisticsLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewCreateProductStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateProductStatisticsLogic { - return &CreateProductStatisticsLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *CreateProductStatisticsLogic) CreateProductStatistics(req *types.CreateProductStatisticsRequest) (resp *types.CreateProductStatisticsReply, err error) { - // todo: add your logic here and delete this line - - return -} diff --git a/internal/logic/admin/crm/product/productstatistics/getproductstatisticslogic.go b/internal/logic/admin/crm/product/productstatistics/getproductstatisticslogic.go index a588ebc6..867c58bb 100644 --- a/internal/logic/admin/crm/product/productstatistics/getproductstatisticslogic.go +++ b/internal/logic/admin/crm/product/productstatistics/getproductstatisticslogic.go @@ -1,6 +1,7 @@ package productstatistics import ( + product2 "PowerX/internal/model/crm/product" "context" "PowerX/internal/svc" @@ -24,7 +25,26 @@ func NewGetProductStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContex } func (l *GetProductStatisticsLogic) GetProductStatistics(req *types.GetProductStatisticsRequest) (resp *types.GetProductStatisticsReply, err error) { - // todo: add your logic here and delete this line - return + statistics, err := l.svcCtx.PowerX.ProductStatistics.GetProductStatisticsByProductId(l.ctx, req.ProductId) + return &types.GetProductStatisticsReply{ + ProductStatistics: TransformProductStatisticsToReply(statistics), + }, nil +} + +func TransformProductStatisticsToReply(specific *product2.ProductStatistics) (specificReply *types.ProductStatistics) { + if specific == nil { + return nil + } + + return &types.ProductStatistics{ + Id: specific.Id, + ProductId: specific.ProductId, + SoldAmount: specific.SoldAmount, + InventoryQuantity: specific.InventoryQuantity, + ViewCount: specific.ViewCount, + BaseSoldAmount: specific.BaseSoldAmount, + BaseInventoryQuantity: specific.BaseInventoryQuantity, + BaseViewCount: specific.BaseViewCount, + } } diff --git a/internal/model/crm/product/productstatistics.go b/internal/model/crm/product/productstatistics.go index ceec40b3..5e618e93 100644 --- a/internal/model/crm/product/productstatistics.go +++ b/internal/model/crm/product/productstatistics.go @@ -5,7 +5,7 @@ import "PowerX/internal/model/powermodel" type ProductStatistics struct { powermodel.PowerModel - ProductId int64 `gorm:"comment:产品Id" json:"productId"` + ProductId int64 `gorm:"comment:产品Id; unique" json:"productId"` SoldAmount int64 `gorm:"comment:销量" json:"soldAmount"` InventoryQuantity int64 `gorm:"comment:库存;" json:"inventoryQuantity"` ViewCount int64 `gorm:"comment:浏览量;" json:"viewCount"` @@ -14,4 +14,4 @@ type ProductStatistics struct { BaseViewCount int64 `gorm:"comment:浏览量;" json:"baseViewCount"` } -const ProductStatisticsUniqueId = powermodel.UniqueId +const ProductStatisticsUniqueId = "product_id" diff --git a/internal/types/types.go b/internal/types/types.go index 7590d8b4..3c49ef9e 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -2934,12 +2934,12 @@ type ListProductCategoriesReply struct { type ProductStatistics struct { Id int64 `json:"id,optional"` ProductId int64 `json:"productId"` - SoldAmount int64 `json:"soldAmount,optional"` - InventoryQuantity int64 `json:"inventoryQuantity,optional"` - ViewCount int64 `json:"viewCount,optional"` - BaseSoldAmount int64 `json:"baseSoldAmount,optional"` - BaseInventoryQuantity int64 `json:"baseInventoryQuantity,optional"` - BaseViewCount int64 `json:"baseViewCount,optional"` + SoldAmount int64 `json:"soldAmount,optional"` + InventoryQuantity int64 `json:"inventoryQuantity,optional"` + ViewCount int64 `json:"viewCount,optional"` + BaseSoldAmount int64 `json:"baseSoldAmount,optional"` + BaseInventoryQuantity int64 `json:"baseInventoryQuantity,optional"` + BaseViewCount int64 `json:"baseViewCount,optional"` } type ListProductStatisticsPageRequest struct { @@ -2957,16 +2957,8 @@ type ListProductStatisticsPageReply struct { Total int64 `json:"total"` } -type CreateProductStatisticsRequest struct { - ProductStatistics -} - -type CreateProductStatisticsReply struct { - ProductStatisticsId int64 `json:"id"` -} - type ConfigProductStatisticsRequest struct { - ProductStatisticss []ProductStatistics `json:"productStatisticss"` + *ProductStatistics } type ConfigProductStatisticsReply struct { @@ -2974,7 +2966,7 @@ type ConfigProductStatisticsReply struct { } type GetProductStatisticsRequest struct { - ProductStatisticsId int64 `path:"id"` + ProductId int64 `path:"id"` } type GetProductStatisticsReply struct { diff --git a/internal/uc/powerx.go b/internal/uc/powerx.go index d1653cc7..4009e3b0 100644 --- a/internal/uc/powerx.go +++ b/internal/uc/powerx.go @@ -34,6 +34,7 @@ type PowerXUseCase struct { Customer *customerDomainUC.CustomerUseCase Lead *customerDomainUC.LeadUseCase Product *productUC.ProductUseCase + ProductStatistics *productUC.ProductStatisticsUseCase ProductSpecific *productUC.ProductSpecificUseCase SKU *productUC.SKUUseCase ProductCategory *productUC.ProductCategoryUseCase @@ -107,6 +108,7 @@ func NewPowerXUseCase(conf *config.Config) (uc *PowerXUseCase, clean func()) { // 加载产品服务UseCase uc.ProductSpecific = productUC.NewProductSpecificUseCase(db) + uc.ProductStatistics = productUC.NewProductStatisticsUseCase(db) uc.SKU = productUC.NewSKUUseCase(db) uc.Product = productUC.NewProductUseCase(db) uc.ProductCategory = productUC.NewProductCategoryUseCase(db) diff --git a/internal/uc/powerx/crm/product/productstatistics.go b/internal/uc/powerx/crm/product/productstatistics.go index 9b5fa30b..40dcb0ee 100644 --- a/internal/uc/powerx/crm/product/productstatistics.go +++ b/internal/uc/powerx/crm/product/productstatistics.go @@ -144,6 +144,20 @@ func (uc *ProductStatisticsUseCase) GetProductStatistics(ctx context.Context, id return &ProductStatistics, nil } +func (uc *ProductStatisticsUseCase) GetProductStatisticsByProductId(ctx context.Context, productId int64) (*product.ProductStatistics, error) { + var ProductStatistics product.ProductStatistics + if err := uc.db.WithContext(ctx). + First(&ProductStatistics). + //Debug(). + Where("product_id", productId).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到价格手册") + } + panic(err) + } + return &ProductStatistics, nil +} + func (uc *ProductStatisticsUseCase) DeleteProductStatistics(ctx context.Context, id int64) error { result := uc.db.WithContext(ctx).Delete(&product.ProductStatistics{}, id) if err := result.Error; err != nil {