first commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/dto"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqldao"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqlmodel"
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/pkg/errcode"
|
||||
)
|
||||
|
||||
// GoodsService 商品业务服务。
|
||||
type GoodsService struct {
|
||||
goodsDao *mysqldao.GoodsDao
|
||||
}
|
||||
|
||||
// NewGoodsService 创建商品服务。
|
||||
func NewGoodsService() *GoodsService {
|
||||
return &GoodsService{
|
||||
goodsDao: mysqldao.NewGoodsDao(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetByCode 按商品编码查询商品(下架返回错误)。
|
||||
func (s *GoodsService) GetByCode(ctx context.Context, request *dto.GoodsGetByCodeRequest) (*dto.GoodsGetByCodeResponse, *errcode.Error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "svc.GoodsGetByCode")
|
||||
defer span.End()
|
||||
|
||||
goods, err := s.goodsDao.GoodsGetByCode(ctx, request.GoodsCode)
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return nil, errcode.GoodsNotFound
|
||||
}
|
||||
return nil, errcode.DatabaseError
|
||||
}
|
||||
if goods.Status != GoodsStatusOnShelf {
|
||||
return nil, errcode.GoodsOffShelf
|
||||
}
|
||||
response := buildGoodsResponse(goods)
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
// List 分页查询上架商品(按类型过滤)。
|
||||
func (s *GoodsService) List(ctx context.Context, request *dto.GoodsListRequest) (*dto.GoodsListResponse, *errcode.Error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "svc.GoodsList")
|
||||
defer span.End()
|
||||
|
||||
page, pageSize := normalizePage(request.Page, request.PageSize)
|
||||
|
||||
list, total, err := s.goodsDao.GoodsList(ctx, request.GoodsType, page, pageSize)
|
||||
if err != nil {
|
||||
return nil, errcode.DatabaseError
|
||||
}
|
||||
|
||||
items := make([]dto.GoodsGetByCodeResponse, 0, len(list))
|
||||
for i := range list {
|
||||
items = append(items, buildGoodsResponse(&list[i]))
|
||||
}
|
||||
|
||||
return &dto.GoodsListResponse{
|
||||
PageResponse: dto.PageResponse{Total: total, Page: page, PageSize: pageSize},
|
||||
List: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// buildGoodsResponse 将商品模型映射为响应。注意 dto.UploadQuota 对应 model.CosQuota。
|
||||
func buildGoodsResponse(g *mysqlmodel.Goods) dto.GoodsGetByCodeResponse {
|
||||
return dto.GoodsGetByCodeResponse{
|
||||
ID: g.ID,
|
||||
GoodsCode: g.GoodsCode,
|
||||
GoodsName: g.GoodsName,
|
||||
GoodsType: g.GoodsType,
|
||||
Price: g.Price,
|
||||
DiscountPrice: g.DiscountPrice,
|
||||
MemberDays: g.MemberDays,
|
||||
UploadQuota: g.CosQuota,
|
||||
AIRecommendQuota: g.AIRecommendQuota,
|
||||
ClosetShareQuota: g.ClosetShareQuota,
|
||||
OutfitShareQuota: g.OutfitShareQuota,
|
||||
ValidDays: g.ValidDays,
|
||||
Description: g.Description,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user