在这个接口里/api/product_book/list 将self_id返回

This commit is contained in:
Administrator 2026-06-18 16:25:32 +08:00
parent 1d622ed889
commit 46aa5831cb
7 changed files with 73 additions and 16 deletions

View File

@ -18,6 +18,7 @@ type Product struct {
Price int64 `json:"price" gorm:"not null;default:0;comment:价格"` Price int64 `json:"price" gorm:"not null;default:0;comment:价格"`
SalePrice int64 `json:"sale_price" gorm:"not null;default:0;comment:书价"` SalePrice int64 `json:"sale_price" gorm:"not null;default:0;comment:书价"`
Cost int64 `json:"cost" gorm:"not null;default:0;comment:最低运费"` Cost int64 `json:"cost" gorm:"not null;default:0;comment:最低运费"`
Stock int64 `json:"stock" gorm:"not null;default:0;comment:库存"`
LiveImage datatypes.JSON `json:"live_image" gorm:"type:json;not null;comment:实拍图json"` LiveImage datatypes.JSON `json:"live_image" gorm:"type:json;not null;comment:实拍图json"`
IsBatchManaged int8 `json:"is_batch_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否批次管理(0:否,1:是)"` IsBatchManaged int8 `json:"is_batch_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否批次管理(0:否,1:是)"`
IsShelfLifeManaged int8 `json:"is_shelf_life_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否效期管理(0:否,1:是)"` IsShelfLifeManaged int8 `json:"is_shelf_life_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否效期管理(0:否,1:是)"`

View File

@ -36,6 +36,8 @@ type ProductBook struct {
Price int64 `json:"price" gorm:"not null;default:0;comment:价格"` Price int64 `json:"price" gorm:"not null;default:0;comment:价格"`
SalePrice int64 `json:"sale_price" gorm:"not null;default:0;comment:书价"` SalePrice int64 `json:"sale_price" gorm:"not null;default:0;comment:书价"`
Cost int64 `json:"cost" gorm:"not null;default:0;comment:最低运费"` Cost int64 `json:"cost" gorm:"not null;default:0;comment:最低运费"`
Stock int64 `json:"stock" gorm:"not null;default:0;comment:库存"`
SaleID int64 `json:"sale_id" gorm:"not null;default:0;comment:销售ID"`
LiveImage datatypes.JSON `json:"live_image" gorm:"type:json;not null;comment:实拍图json"` LiveImage datatypes.JSON `json:"live_image" gorm:"type:json;not null;comment:实拍图json"`
IsBatchManaged int8 `json:"is_batch_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否批次管理(0:否,1:是)"` IsBatchManaged int8 `json:"is_batch_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否批次管理(0:否,1:是)"`
IsShelfLifeManaged int8 `json:"is_shelf_life_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否效期管理(0:否,1:是)"` IsShelfLifeManaged int8 `json:"is_shelf_life_managed" gorm:"type:tinyint(1);not null;default:0;comment:是否效期管理(0:否,1:是)"`

View File

@ -30,12 +30,16 @@ type ProductBookRequest struct {
Price int64 `form:"price"` // 价格 Price int64 `form:"price"` // 价格
SalePrice int64 `form:"sale_price"` // 书价 SalePrice int64 `form:"sale_price"` // 书价
Cost int64 `form:"cost"` // 最低运费 Cost int64 `form:"cost"` // 最低运费
Stock int64 `form:"stock"` // 库存
SelfID int64 `form:"self_id"` // 分库商品表中的商品ID
LiveImage []string `form:"live_image[]"` // 实拍图数组 LiveImage []string `form:"live_image[]"` // 实拍图数组
IsBatchManaged int8 `form:"is_batch_managed"` // 是否批次管理 IsBatchManaged int8 `form:"is_batch_managed"` // 是否批次管理
IsShelfLifeManaged int8 `form:"is_shelf_life_managed"` // 是否效期管理 IsShelfLifeManaged int8 `form:"is_shelf_life_managed"` // 是否效期管理
Status int8 `form:"status"` // 状态 Status int8 `form:"status"` // 状态
} }
// ... existing code ...
// GetProductBookListRequest 获取商品反射列表请求 // GetProductBookListRequest 获取商品反射列表请求
type GetProductBookListRequest struct { type GetProductBookListRequest struct {
Page int `form:"page"` // 页码 Page int `form:"page"` // 页码
@ -67,12 +71,15 @@ type GetProductBookListRequest struct {
Price int64 `form:"price"` // 价格 Price int64 `form:"price"` // 价格
SalePrice int64 `form:"sale_price"` // 书价 SalePrice int64 `form:"sale_price"` // 书价
Cost int64 `form:"cost"` // 最低运费 Cost int64 `form:"cost"` // 最低运费
Stock int64 `form:"stock"` // 库存
SelfID int64 `form:"self_id"` // 分库商品表中的商品ID
IsBatchManaged *int8 `form:"is_batch_managed"` // 批次管理 IsBatchManaged *int8 `form:"is_batch_managed"` // 批次管理
IsShelfLifeManaged *int8 `form:"is_shelf_life_managed"` // 效期管理 IsShelfLifeManaged *int8 `form:"is_shelf_life_managed"` // 效期管理
Status *int8 `form:"status"` // 状态 Status *int8 `form:"status"` // 状态
StartCreatedAt int64 `form:"start_created_at"` // 创建时间开始 StartCreatedAt int64 `form:"start_created_at"` // 创建时间开始
EndCreatedAt int64 `form:"end_created_at"` // 创建时间结束 EndCreatedAt int64 `form:"end_created_at"` // 创建时间结束
Keyword string `form:"keyword"` // 关键词(搜索书名/作者/ISBN) Keyword string `form:"keyword"` // 关键词(搜索书名/作者/ISBN)
SortByTotal string `form:"sort_by_total"` // 按售价+运费总和排序: asc/desc
} }
// DeleteProductBookRequest 删除商品反射请求 // DeleteProductBookRequest 删除商品反射请求

View File

@ -223,6 +223,8 @@ type ProductFullInfoResponse struct {
Barcode string `json:"barcode"` // 条码 Barcode string `json:"barcode"` // 条码
Price int64 `json:"price"` // 价格(分) Price int64 `json:"price"` // 价格(分)
SalePrice int64 `json:"sale_price"` // 售价(分) SalePrice int64 `json:"sale_price"` // 售价(分)
Cost int64 `json:"cost"` // 运费(分)
Stock int64 `json:"stock"` // 库存数量
LiveImage []string `json:"live_image"` // 实拍图 LiveImage []string `json:"live_image"` // 实拍图
IsBatchManaged int8 `json:"is_batch_managed"` // 是否批次管理 IsBatchManaged int8 `json:"is_batch_managed"` // 是否批次管理
IsShelfLifeManaged int8 `json:"is_shelf_life_managed"` // 是否保质期管理 IsShelfLifeManaged int8 `json:"is_shelf_life_managed"` // 是否保质期管理

View File

@ -43,6 +43,9 @@ type ProductBookItem struct {
Price int64 `json:"price"` Price int64 `json:"price"`
SalePrice int64 `json:"sale_price"` SalePrice int64 `json:"sale_price"`
Cost int64 `json:"cost"` Cost int64 `json:"cost"`
Stock int64 `json:"stock"`
SelfID int64 `json:"self_id"`
TotalPrice int64 `json:"total_price"` // 售价+运费总和
LiveImage []string `json:"live_image"` LiveImage []string `json:"live_image"`
IsBatchManaged int8 `json:"is_batch_managed"` IsBatchManaged int8 `json:"is_batch_managed"`
IsShelfLifeManaged int8 `json:"is_shelf_life_managed"` IsShelfLifeManaged int8 `json:"is_shelf_life_managed"`
@ -92,6 +95,9 @@ func ConvertProductBookToItem(book models.ProductBook) ProductBookItem {
Price: book.Price, Price: book.Price,
SalePrice: book.SalePrice, SalePrice: book.SalePrice,
Cost: book.Cost, Cost: book.Cost,
Stock: book.Stock,
SelfID: book.SelfID,
TotalPrice: book.SalePrice + book.Cost,
LiveImage: liveImage, LiveImage: liveImage,
IsBatchManaged: book.IsBatchManaged, IsBatchManaged: book.IsBatchManaged,
IsShelfLifeManaged: book.IsShelfLifeManaged, IsShelfLifeManaged: book.IsShelfLifeManaged,

View File

@ -456,7 +456,8 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
return nil, utils.NewError("查询商品失败") return nil, utils.NewError("查询商品失败")
} }
fmt.Printf("【断点6】商品基本信息查询成功 - Name: %s, Barcode: %s\n", product.Name, product.Barcode) fmt.Printf("【断点6】商品基本信息查询成功 - Name: %s, Barcode: %s, Cost: %d, Stock: %d\n",
product.Name, product.Barcode, product.Cost, product.Stock)
var liveImage []string var liveImage []string
if len(product.LiveImage) > 0 { if len(product.LiveImage) > 0 {
@ -477,7 +478,7 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
} }
} }
fmt.Printf("【断点7】开始查询库存信息\n") fmt.Printf("【断点7】开始查询库存明细信息\n")
var inventories []systemRes.ProductInventoryDetail var inventories []systemRes.ProductInventoryDetail
if err := databaseConn.Table("inventory_detail inv"). if err := databaseConn.Table("inventory_detail inv").
@ -493,14 +494,15 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
inventories = []systemRes.ProductInventoryDetail{} inventories = []systemRes.ProductInventoryDetail{}
} }
fmt.Printf("【断点8】库存信息查询完成 - 数量: %d\n", len(inventories)) fmt.Printf("【断点8】库存明细查询完成 - 数量: %d\n", len(inventories))
totalStock := int64(0) totalStock := int64(0)
for _, inv := range inventories { for _, inv := range inventories {
totalStock += inv.Quantity totalStock += inv.Quantity
} }
fmt.Printf("【断点9】计算总库存: %d\n", totalStock) fmt.Printf("【断点9】库存统计 - 模型层Stock: %d, 计算总库存(total_stock): %d, 运费(Cost): %d\n",
product.Stock, totalStock, product.Cost)
outTaskInfoMap, err := s.getProductOutTaskInfo(databaseConn, []int64{req.ProductID}) outTaskInfoMap, err := s.getProductOutTaskInfo(databaseConn, []int64{req.ProductID})
if err != nil { if err != nil {
@ -527,6 +529,8 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
Barcode: product.Barcode, Barcode: product.Barcode,
Price: product.Price, Price: product.Price,
SalePrice: product.SalePrice, SalePrice: product.SalePrice,
Cost: product.Cost,
Stock: product.Stock,
LiveImage: liveImage, LiveImage: liveImage,
IsBatchManaged: product.IsBatchManaged, IsBatchManaged: product.IsBatchManaged,
IsShelfLifeManaged: product.IsShelfLifeManaged, IsShelfLifeManaged: product.IsShelfLifeManaged,
@ -546,6 +550,9 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
"user_id": req.UserID, "user_id": req.UserID,
"product_name": product.Name, "product_name": product.Name,
"barcode": product.Barcode, "barcode": product.Barcode,
"sale_price": product.SalePrice,
"cost": product.Cost,
"stock": product.Stock,
"total_stock": totalStock, "total_stock": totalStock,
"inventory_count": len(inventories), "inventory_count": len(inventories),
"shop_count": len(shopList), "shop_count": len(shopList),

View File

@ -15,9 +15,14 @@ import (
type ProductBookService struct{} type ProductBookService struct{}
// List 获取商品反射列表 // List 获取商品反射列表
// List 获取商品反射列表 - 支持按售价+运费统计排序
// List 获取商品反射列表 - 支持按售价+运费统计排序
// List 获取商品反射列表 - 支持按售价+运费统计排序
func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*systemRes.ProductBookListResponse, error) { func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*systemRes.ProductBookListResponse, error) {
db := database.DB db := database.DB
fmt.Printf("【断点1】开始查询商品反射列表\n")
if req.Page < 1 { if req.Page < 1 {
req.Page = 1 req.Page = 1
} }
@ -25,7 +30,6 @@ func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*sys
req.PageSize = 20 req.PageSize = 20
} }
// 根据ISBN确定分表
tableName := "product_book_00" tableName := "product_book_00"
if req.ISBN != "" { if req.ISBN != "" {
tableName = models.ProductBookTableName(req.ISBN) tableName = models.ProductBookTableName(req.ISBN)
@ -33,9 +37,10 @@ func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*sys
tableName = models.ProductBookTableName(req.Barcode) tableName = models.ProductBookTableName(req.Barcode)
} }
fmt.Printf("【断点2】使用表: %s\n", tableName)
query := db.Table(tableName).Where("is_del = ?", 0) query := db.Table(tableName).Where("is_del = ?", 0)
// 添加查询条件
if req.ID > 0 { if req.ID > 0 {
query = query.Where("id = ?", req.ID) query = query.Where("id = ?", req.ID)
} }
@ -117,6 +122,12 @@ func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*sys
if req.Cost > 0 { if req.Cost > 0 {
query = query.Where("cost = ?", req.Cost) query = query.Where("cost = ?", req.Cost)
} }
if req.Stock > 0 {
query = query.Where("stock = ?", req.Stock)
}
if req.SelfID > 0 {
query = query.Where("self_id = ?", req.SelfID)
}
if req.IsBatchManaged != nil { if req.IsBatchManaged != nil {
query = query.Where("is_batch_managed = ?", *req.IsBatchManaged) query = query.Where("is_batch_managed = ?", *req.IsBatchManaged)
} }
@ -137,22 +148,47 @@ func (s *ProductBookService) List(req systemReq.GetProductBookListRequest) (*sys
"%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%") "%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
} }
fmt.Printf("【断点3】查询条件构建完成\n")
var total int64 var total int64
if err := query.Count(&total).Error; err != nil { if err := query.Count(&total).Error; err != nil {
fmt.Printf("【断点4】查询总数失败: %v\n", err)
return nil, utils.NewError("查询总数失败") return nil, utils.NewError("查询总数失败")
} }
fmt.Printf("【断点5】查询总数: %d\n", total)
var books []models.ProductBook var books []models.ProductBook
offset := (req.Page - 1) * req.PageSize offset := (req.Page - 1) * req.PageSize
if err := query.Order("created_at DESC").Offset(offset).Limit(req.PageSize).Find(&books).Error; err != nil {
orderClause := "created_at DESC"
if req.SortByTotal == "asc" {
orderClause = "(sale_price + cost) ASC"
fmt.Printf("【断点6】按总价升序排序\n")
} else if req.SortByTotal == "desc" {
orderClause = "(sale_price + cost) DESC"
fmt.Printf("【断点6】按总价降序排序\n")
} else {
fmt.Printf("【断点6】按创建时间降序排序(默认)\n")
}
if err := query.Order(orderClause).Offset(offset).Limit(req.PageSize).Find(&books).Error; err != nil {
fmt.Printf("【断点7】查询列表失败: %v\n", err)
return nil, utils.NewError("查询列表失败") return nil, utils.NewError("查询列表失败")
} }
fmt.Printf("【断点8】查询到 %d 条记录\n", len(books))
items := make([]systemRes.ProductBookItem, 0, len(books)) items := make([]systemRes.ProductBookItem, 0, len(books))
for _, book := range books { for _, book := range books {
items = append(items, systemRes.ConvertProductBookToItem(book)) item := systemRes.ConvertProductBookToItem(book)
fmt.Printf(" - ID: %d, 书名: %s, 售价: %d, 运费: %d, 库存: %d, SaleID: %d, 总价: %d\n",
item.ID, item.BookName, item.SalePrice, item.Cost, item.Stock, item.SelfID, item.TotalPrice)
items = append(items, item)
} }
fmt.Printf("【断点9】数据转换完成,准备返回\n")
return &systemRes.ProductBookListResponse{ return &systemRes.ProductBookListResponse{
List: items, List: items,
Total: total, Total: total,
@ -183,7 +219,6 @@ func (s *ProductBookService) Create(req systemReq.ProductBookRequest) (int64, er
now := time.Now().Unix() now := time.Now().Unix()
// 解析LiveImage
var liveImage datatypes.JSON var liveImage datatypes.JSON
if len(req.LiveImage) > 0 { if len(req.LiveImage) > 0 {
jsonBytes, _ := json.Marshal(req.LiveImage) jsonBytes, _ := json.Marshal(req.LiveImage)
@ -192,7 +227,6 @@ func (s *ProductBookService) Create(req systemReq.ProductBookRequest) (int64, er
liveImage = datatypes.JSON("[]") liveImage = datatypes.JSON("[]")
} }
// 解析CatID
var catID datatypes.JSON var catID datatypes.JSON
if req.CatID != "" { if req.CatID != "" {
catID = datatypes.JSON(req.CatID) catID = datatypes.JSON(req.CatID)
@ -200,7 +234,6 @@ func (s *ProductBookService) Create(req systemReq.ProductBookRequest) (int64, er
catID = datatypes.JSON("{}") catID = datatypes.JSON("{}")
} }
// 根据ISBN确定分表
tableName := models.ProductBookTableName(req.ISBN) tableName := models.ProductBookTableName(req.ISBN)
book := models.ProductBook{ book := models.ProductBook{
@ -231,6 +264,8 @@ func (s *ProductBookService) Create(req systemReq.ProductBookRequest) (int64, er
Price: req.Price, Price: req.Price,
SalePrice: req.SalePrice, SalePrice: req.SalePrice,
Cost: req.Cost, Cost: req.Cost,
Stock: req.Stock,
SelfID: req.SelfID,
LiveImage: liveImage, LiveImage: liveImage,
IsBatchManaged: req.IsBatchManaged, IsBatchManaged: req.IsBatchManaged,
IsShelfLifeManaged: req.IsShelfLifeManaged, IsShelfLifeManaged: req.IsShelfLifeManaged,
@ -257,10 +292,8 @@ func (s *ProductBookService) Update(req systemReq.ProductBookRequest) error {
now := time.Now().Unix() now := time.Now().Unix()
// 先查询获取ISBN以确定分表
var existingBook models.ProductBook var existingBook models.ProductBook
// 如果提供了ISBN,直接使用;否则需要遍历所有分表查找
tableName := "" tableName := ""
if req.ISBN != "" { if req.ISBN != "" {
tableName = models.ProductBookTableName(req.ISBN) tableName = models.ProductBookTableName(req.ISBN)
@ -268,7 +301,6 @@ func (s *ProductBookService) Update(req systemReq.ProductBookRequest) error {
return utils.NewError("商品不存在") return utils.NewError("商品不存在")
} }
} else { } else {
// 遍历所有分表查找
allTables := models.ProductBookAllTableNames() allTables := models.ProductBookAllTableNames()
found := false found := false
for _, tName := range allTables { for _, tName := range allTables {
@ -283,7 +315,6 @@ func (s *ProductBookService) Update(req systemReq.ProductBookRequest) error {
} }
} }
// 解析LiveImage
var liveImage datatypes.JSON var liveImage datatypes.JSON
if len(req.LiveImage) > 0 { if len(req.LiveImage) > 0 {
jsonBytes, _ := json.Marshal(req.LiveImage) jsonBytes, _ := json.Marshal(req.LiveImage)
@ -292,7 +323,6 @@ func (s *ProductBookService) Update(req systemReq.ProductBookRequest) error {
liveImage = existingBook.LiveImage liveImage = existingBook.LiveImage
} }
// 解析CatID
var catID datatypes.JSON var catID datatypes.JSON
if req.CatID != "" { if req.CatID != "" {
catID = datatypes.JSON(req.CatID) catID = datatypes.JSON(req.CatID)
@ -327,6 +357,8 @@ func (s *ProductBookService) Update(req systemReq.ProductBookRequest) error {
"price": req.Price, "price": req.Price,
"sale_price": req.SalePrice, "sale_price": req.SalePrice,
"cost": req.Cost, "cost": req.Cost,
"stock": req.Stock,
"self_id": req.SelfID,
"live_image": liveImage, "live_image": liveImage,
"is_batch_managed": req.IsBatchManaged, "is_batch_managed": req.IsBatchManaged,
"is_shelf_life_managed": req.IsShelfLifeManaged, "is_shelf_life_managed": req.IsShelfLifeManaged,