Skip to content

Commit

Permalink
feat(product): 添加类别到SPU,增加sql语句等 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
longyue0521 authored May 4, 2024
1 parent 4ffd84b commit c7dbed4
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 24 deletions.
26 changes: 20 additions & 6 deletions .script/mysql/product_insert.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
CREATE TABLE IF NOT EXISTS `product_spus` (
CREATE TABLE IF NOT EXISTS `categories` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品类别自增ID',
`name` varchar(255) NOT NULL COMMENT '商品类别名称',
`description` longtext NOT NULL COMMENT '商品类别描述',
`ctime` bigint DEFAULT NULL,
`utime` bigint DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `categories` (id, name, description, ctime, utime)
VALUES (1, '会员', '周期性商品', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, '项目', '永久性商品', UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

CREATE TABLE IF NOT EXISTS `spus` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品SPU自增ID',
`category_id` bigint NOT NULL COMMENT '商品类别自增ID',
`sn` varchar(255) NOT NULL COMMENT '商品SPU序列号',
`name` varchar(255) NOT NULL COMMENT '商品名称',
`description` longtext NOT NULL COMMENT '商品描述',
Expand All @@ -10,11 +24,11 @@ CREATE TABLE IF NOT EXISTS `product_spus` (
UNIQUE KEY `uniq_product_spu_sn` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `product_spus` (sn, name, description, status, ctime, utime)
VALUES ('SPU001', '会员服务', '提供不同期限的会员服务', 2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('SPU002', '面试项目', '提供不同规模的面试项目', 2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
INSERT INTO `spus` (category_id, sn, name, description, status, ctime, utime)
VALUES (1, 'SPU001', '会员服务', '提供不同期限的会员服务', 2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, 'SPU002', '面试项目', '提供不同规模的面试项目', 2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());

CREATE TABLE IF NOT EXISTS `product_skus` (
CREATE TABLE IF NOT EXISTS `skus` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品SKU自增ID',
`sn` varchar(255) NOT NULL COMMENT '商品SKU序列号',
`product_spu_id` bigint NOT NULL COMMENT '商品SPU自增ID',
Expand All @@ -35,7 +49,7 @@ CREATE TABLE IF NOT EXISTS `product_skus` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


INSERT INTO `product_skus` (sn, product_spu_id, name, description, price, stock, stock_limit, sale_type, attrs, image, status, ctime, utime)
INSERT INTO `skus` (sn, product_spu_id, name, description, price, stock, stock_limit, sale_type, attrs, image, status, ctime, utime)
VALUES
('SKU001', 1, '星期会员', '提供一周的会员服务', 799, 1000, 100000000, 1, '{"days":7}', 'image-SKU001', 2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
('SKU002', 1, '月会员', '提供一个月的会员服务', 990, 1000, 100000000, 1, '{"days":31}', 'image-SKU002',2, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
Expand Down
18 changes: 12 additions & 6 deletions internal/product/internal/domain/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ const (
)

type SPU struct {
ID int64
SN string
Name string
Desc string
SKUs []SKU
Status Status
ID int64
SN string
Name string
Desc string
Category Category
SKUs []SKU
Status Status
}

type SKU struct {
Expand All @@ -64,3 +65,8 @@ type SKU struct {
Image string
Status Status
}

type Category struct {
Name string
Desc string
}
51 changes: 51 additions & 0 deletions internal/product/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ func (s *ProductModuleTestSuite) TearDownSuite() {
require.NoError(s.T(), err)
err = s.db.Exec("DROP TABLE `skus`").Error
require.NoError(s.T(), err)
err = s.db.Exec("DROP TABLE `categories`").Error
require.NoError(s.T(), err)
}

func (s *ProductModuleTestSuite) TearDownTest() {
err := s.db.Exec("TRUNCATE TABLE `spus`").Error
require.NoError(s.T(), err)
err = s.db.Exec("TRUNCATE TABLE `skus`").Error
require.NoError(s.T(), err)
err = s.db.Exec("TRUNCATE TABLE `categories`").Error
require.NoError(s.T(), err)
}

func (s *ProductModuleTestSuite) TestHandler_RetrieveSKUDetail() {
Expand All @@ -105,7 +109,15 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSKUDetail() {
{
name: "查找成功",
before: func(t *testing.T) {

c := dao.Category{
Name: "会员1",
Description: "周期性商品1",
}
cid, err := s.dao.CreateCategory(context.Background(), c)
require.NoError(t, err)
spu := dao.SPU{
CategoryId: cid,
SN: "SPU001",
Name: "会员服务",
Description: "提供不同期限的会员服务",
Expand Down Expand Up @@ -295,7 +307,15 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetail() {
{
name: "查找成功",
before: func(t *testing.T) {
c := dao.Category{
Name: "会员2",
Description: "周期性商品2",
}
cid, err := s.dao.CreateCategory(context.Background(), c)
require.NoError(t, err)

spu := dao.SPU{
CategoryId: cid,
SN: "SPU102",
Name: "会员服务-2",
Description: "提供不同期限的会员服务-2",
Expand Down Expand Up @@ -342,6 +362,10 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetail() {
SN: "SPU102",
Name: "会员服务-2",
Desc: "提供不同期限的会员服务-2",
Category: web.Category{
Name: "会员2",
Desc: "周期性商品2",
},
SKUs: []web.SKU{
{
SN: "SKU102",
Expand Down Expand Up @@ -387,6 +411,7 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetail() {

func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetailFailed() {
t := s.T()

testCases := []struct {
name string
before func(t *testing.T)
Expand All @@ -409,7 +434,14 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetailFailed() {
name: "SPU下架_SKU上架",
before: func(t *testing.T) {
t.Helper()
c := dao.Category{
Name: "会员3",
Description: "周期性商品3",
}
cid, err := s.dao.CreateCategory(context.Background(), c)
require.NoError(t, err)
spu := dao.SPU{
CategoryId: cid,
SN: "SPU103",
Name: "会员服务-3",
Description: "提供不同期限的会员服务-3",
Expand Down Expand Up @@ -448,7 +480,14 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetailFailed() {
name: "SPU下架_SKU下架",
before: func(t *testing.T) {
t.Helper()
c := dao.Category{
Name: "会员4",
Description: "周期性商品4",
}
cid, err := s.dao.CreateCategory(context.Background(), c)
require.NoError(t, err)
spu := dao.SPU{
CategoryId: cid,
SN: "SPU104",
Name: "会员服务-4",
Description: "提供不同期限的会员服务-4",
Expand Down Expand Up @@ -502,6 +541,7 @@ func (s *ProductModuleTestSuite) TestHandler_RetrieveSPUDetailFailed() {

func (s *ProductModuleTestSuite) TestService_FindSPUByID() {
t := s.T()

testCases := []struct {
name string
getSPUID func(t *testing.T) int64
Expand All @@ -512,7 +552,14 @@ func (s *ProductModuleTestSuite) TestService_FindSPUByID() {
{
name: "查找成功",
getSPUID: func(t *testing.T) int64 {
c := dao.Category{
Name: "会员5",
Description: "周期性商品5",
}
cid, err := s.dao.CreateCategory(context.Background(), c)
require.NoError(t, err)
spu := dao.SPU{
CategoryId: cid,
SN: "SPU1102",
Name: "会员服务-2",
Description: "提供不同期限的会员服务-2",
Expand Down Expand Up @@ -558,6 +605,10 @@ func (s *ProductModuleTestSuite) TestService_FindSPUByID() {
Name: "会员服务-2",
Desc: "提供不同期限的会员服务-2",
Status: domain.StatusOnShelf,
Category: domain.Category{
Name: "会员5",
Desc: "周期性商品5",
},
SKUs: []domain.SKU{
{
SN: "SKU1102",
Expand Down
2 changes: 1 addition & 1 deletion internal/product/internal/repository/dao/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ package dao
import "github.com/ego-component/egorm"

func InitTables(db *egorm.Component) error {
return db.AutoMigrate(&SPU{}, &SKU{})
return db.AutoMigrate(&SPU{}, &SKU{}, &Category{})
}
23 changes: 23 additions & 0 deletions internal/product/internal/repository/dao/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type ProductDAO interface {
FindSPUBySN(ctx context.Context, sn string) (SPU, error)
FindSKUBySN(ctx context.Context, sn string) (SKU, error)
FindSKUsBySPUID(ctx context.Context, spuId int64) ([]SKU, error)
FindCategoryByID(ctx context.Context, id int64) (Category, error)
CreateCategory(ctx context.Context, c Category) (int64, error)
CreateSPU(ctx context.Context, spu SPU) (int64, error)
CreateSKU(ctx context.Context, sku SKU) (int64, error)
}
Expand Down Expand Up @@ -66,6 +68,18 @@ func (d *ProductGORMDAO) FindSKUsBySPUID(ctx context.Context, spuId int64) ([]SK
return res, err
}

func (d *ProductGORMDAO) FindCategoryByID(ctx context.Context, id int64) (Category, error) {
var c Category
err := d.db.WithContext(ctx).Where("id = ?", id).First(&c).Error
return c, err
}

func (d *ProductGORMDAO) CreateCategory(ctx context.Context, c Category) (int64, error) {
now := time.Now()
c.Ctime, c.Utime = now.UnixMilli(), now.UnixMilli()
return c.Id, d.db.WithContext(ctx).Create(&c).Error
}

func (d *ProductGORMDAO) CreateSPU(ctx context.Context, spu SPU) (int64, error) {
now := time.Now()
spu.Utime, spu.Ctime = now.UnixMilli(), now.UnixMilli()
Expand All @@ -78,8 +92,17 @@ func (d *ProductGORMDAO) CreateSKU(ctx context.Context, sku SKU) (int64, error)
return sku.Id, d.db.WithContext(ctx).Create(&sku).Error
}

type Category struct {
Id int64 `gorm:"primaryKey;autoIncrement;comment:商品类别自增ID"`
Name string `gorm:"type:varchar(255);not null;comment:商品类别名称"`
Description string `gorm:"not null;comment:商品类别描述"`
Ctime int64
Utime int64
}

type SPU struct {
Id int64 `gorm:"primaryKey;autoIncrement;comment:商品SPU自增ID"`
CategoryId int64 `gorm:"not null;index:idx_category_id;comment:商品类别自增ID"`
SN string `gorm:"type:varchar(255);not null;uniqueIndex:uniq_product_spu_sn;comment:商品SPU序列号"`
Name string `gorm:"type:varchar(255);not null;comment:商品名称"`
Description string `gorm:"not null; comment:商品描述"`
Expand Down
26 changes: 19 additions & 7 deletions internal/product/internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,27 @@ func (p *productRepository) FindSPUBySN(ctx context.Context, sn string) (domain.
if err != nil {
return domain.SPU{}, err
}
c, err := p.dao.FindCategoryByID(ctx, spu.CategoryId)
if err != nil {
return domain.SPU{}, err
}
skus, err := p.dao.FindSKUsBySPUID(ctx, spu.Id)
if err != nil {
return domain.SPU{}, err
}
return p.toDomainSPU(spu, skus), err
return p.toDomainSPU(c, spu, skus), err
}

func (p *productRepository) toDomainSPU(spu dao.SPU, skus []dao.SKU) domain.SPU {
func (p *productRepository) toDomainSPU(c dao.Category, spu dao.SPU, skus []dao.SKU) domain.SPU {
return domain.SPU{
ID: spu.Id,
SN: spu.SN,
Name: spu.Name,
Desc: spu.Description,
ID: spu.Id,
SN: spu.SN,
Name: spu.Name,
Desc: spu.Description,
Category: domain.Category{
Name: c.Name,
Desc: c.Description,
},
Status: domain.Status(spu.Status),
SKUs: slice.Map(skus, func(idx int, src dao.SKU) domain.SKU {
return p.toDomainSKU(src)
Expand Down Expand Up @@ -87,11 +95,15 @@ func (p *productRepository) FindSPUByID(ctx context.Context, id int64) (domain.S
if err != nil {
return domain.SPU{}, err
}
c, err := p.dao.FindCategoryByID(ctx, spu.CategoryId)
if err != nil {
return domain.SPU{}, err
}
skus, err := p.dao.FindSKUsBySPUID(ctx, spu.Id)
if err != nil {
return domain.SPU{}, err
}
return p.toDomainSPU(spu, skus), err
return p.toDomainSPU(c, spu, skus), err
}

func (p *productRepository) FindSKUBySN(ctx context.Context, sn string) (domain.SKU, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/product/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (h *Handler) RetrieveSPUDetail(ctx *ginx.Context, req SNReq, _ session.Sess
SN: spu.SN,
Name: spu.Name,
Desc: spu.Desc,
Category: Category{
Name: spu.Category.Name,
Desc: spu.Category.Desc,
},
SKUs: slice.Map(spu.SKUs, func(idx int, src domain.SKU) SKU {
return h.toSKU(src)
}),
Expand Down
14 changes: 10 additions & 4 deletions internal/product/internal/web/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ type SNReq struct {
}

type SPU struct {
SN string `json:"sn"`
Name string `json:"name"`
Desc string `json:"desc"`
SKUs []SKU `json:"skus"`
SN string `json:"sn"`
Name string `json:"name"`
Desc string `json:"desc"`
Category Category `json:"category"`
SKUs []SKU `json:"skus"`
}

type SKU struct {
Expand All @@ -38,3 +39,8 @@ type SKU struct {
// SaleStart int64 `json:"saleStart"`
// SaleEnd int64 `json:"saleEnd"`
}

type Category struct {
Name string `json:"name"`
Desc string `json:"desc"`
}

0 comments on commit c7dbed4

Please sign in to comment.