Skip to content

Commit

Permalink
添加小程序显示产品统计的数据接口
Browse files Browse the repository at this point in the history
添加了静态的库存量,销量,浏览量
  • Loading branch information
Matrix-X committed Oct 10, 2023
1 parent 43d876f commit 370165c
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 105 deletions.
1 change: 1 addition & 0 deletions api/mp.api
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "mp/market/media.api"
import "mp/product/artisan.api"
import "mp/product/product.api"
import "mp/product/productcategory.api"
import "mp/product/productstatistics.api"
import "mp/trade/cart.api"
import "mp/trade/order.api"
import "mp/trade/shippingaddress.api"
Expand Down
99 changes: 0 additions & 99 deletions api/mp/product/productstatistics.api
Original file line number Diff line number Diff line change
Expand Up @@ -23,104 +23,5 @@ service PowerX {
@handler GetProductStatistics
get /product-statistics/:id (GetProductStatisticsRequest) returns (GetProductStatisticsReply)

@doc "配置产品统计"
@handler ConfigProductStatistics
post /product-statistics/config (ConfigProductStatisticsRequest) returns (ConfigProductStatisticsReply)

@doc "全量产品统计"
@handler PutProductStatistics
put /product-statistics/:id (PutProductStatisticsRequest) returns (PutProductStatisticsReply)

@doc "增量产品统计"
@handler PatchProductStatistics
patch /product-statistics/:id (PatchProductStatisticsRequest) returns (PatchProductStatisticsReply)

}

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"`
}
)

type (
ListProductStatisticsPageRequest struct {
LikeName string `form:"likeName,optional"`
ProductId int64 `form:"productId"`
OrderBy string `form:"orderBy,optional"`
PageIndex int `form:"pageIndex,optional"`
PageSize int `form:"pageSize,optional"`
}


ListProductStatisticsPageReply struct {
List []*ProductStatistics `json:"list"`
PageIndex int `json:"pageIndex"`
PageSize int `json:"pageSize"`
Total int64 `json:"total"`
}
)


type (
ConfigProductStatisticsRequest struct {
*ProductStatistics
}

ConfigProductStatisticsReply struct {
Result bool `json:"result"`
}
)

type (
GetProductStatisticsRequest struct {
ProductId int64 `path:"id"`
}

GetProductStatisticsReply struct {
*ProductStatistics
}
)


type (
PutProductStatisticsRequest struct {
ProductStatisticsId int64 `path:"id"`
ProductStatistics
}

PutProductStatisticsReply struct {
*ProductStatistics
}
)

type (
PatchProductStatisticsRequest struct {
ProductStatisticsId int64 `path:"id"`
ProductStatistics
}

PatchProductStatisticsReply struct {
*ProductStatistics
}
)


type (
DeleteProductStatisticsRequest struct {
ProductStatisticsId int64 `path:"id"`
}

DeleteProductStatisticsReply struct {
ProductStatisticsId int64 `json:"id"`
}
)


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package productstatistics

import (
"net/http"

"PowerX/internal/logic/mp/crm/product/productstatistics"
"PowerX/internal/svc"
"PowerX/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

func GetProductStatisticsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductStatisticsRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := productstatistics.NewGetProductStatisticsLogic(r.Context(), svcCtx)
resp, err := l.GetProductStatistics(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package productstatistics

import (
"net/http"

"PowerX/internal/logic/mp/crm/product/productstatistics"
"PowerX/internal/svc"
"PowerX/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

func ListProductStatisticsPageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ListProductStatisticsPageRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := productstatistics.NewListProductStatisticsPageLogic(r.Context(), svcCtx)
resp, err := l.ListProductStatisticsPage(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
20 changes: 20 additions & 0 deletions internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package productstatistics

import (
product2 "PowerX/internal/model/crm/product"
"PowerX/internal/types/errorx"
"context"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *GetProductStatisticsLogic) GetProductStatistics(req *types.GetProductStatisticsRequest) (resp *types.GetProductStatisticsReply, err error) {

statistics, err := l.svcCtx.PowerX.ProductStatistics.GetProductStatisticsByProductId(l.ctx, req.ProductId)
if err != nil {
return nil, errorx.WithCause(err, err.Error())
}

return &types.GetProductStatisticsReply{
ProductStatistics: TransformProductStatisticsToReplyForMP(statistics),
}, nil
}

func TransformProductStatisticsToReplyForMP(specific *product2.ProductStatistics) (specificReply *types.ProductStatistics) {
if specific == nil {
return nil
}

return &types.ProductStatistics{
Id: specific.Id,
ProductId: specific.ProductId,
SoldAmount: specific.BaseSoldAmount + specific.SoldAmount,
InventoryQuantity: specific.BaseInventoryQuantity + specific.InventoryQuantity,
ViewCount: specific.BaseViewCount + specific.ViewCount,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package productstatistics

import (
"context"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *ListProductStatisticsPageLogic) ListProductStatisticsPage(req *types.ListProductStatisticsPageRequest) (resp *types.ListProductStatisticsPageReply, err error) {
// todo: add your logic here and delete this line

return
}
13 changes: 7 additions & 6 deletions internal/uc/powerx/crm/product/productstatistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (uc *ProductStatisticsUseCase) GetProductStatistics(ctx context.Context, id
var ProductStatistics product.ProductStatistics
if err := uc.db.WithContext(ctx).First(&ProductStatistics, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到价格手册")
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到产品统计记录")
}
panic(err)
}
Expand All @@ -147,11 +147,12 @@ func (uc *ProductStatisticsUseCase) GetProductStatistics(ctx context.Context, id
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 {
Where("product_id", productId).
Debug().
FirstOrCreate(&ProductStatistics).
Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到价格手册")
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到产品统计记录")
}
panic(err)
}
Expand All @@ -164,7 +165,7 @@ func (uc *ProductStatisticsUseCase) DeleteProductStatistics(ctx context.Context,
panic(err)
}
if result.RowsAffected == 0 {
return errorx.WithCause(errorx.ErrDeleteObjectNotFound, "未找到价格手册")
return errorx.WithCause(errorx.ErrDeleteObjectNotFound, "未找到产品统计记录")
}
return nil
}

0 comments on commit 370165c

Please sign in to comment.