创建一个接口,不需要签名认证,通过传入的商品id与user_id 去分库查询并返回商品的所有信息

This commit is contained in:
Administrator 2026-06-18 14:00:20 +08:00
parent a15d1d93c8
commit 8e4cce2109
3 changed files with 150 additions and 13 deletions

View File

@ -109,14 +109,14 @@ func (r *ProductApi) GetProductDetail(c *gin.Context) {
}) })
} }
// GetProductFullInfo 获取商品完整信息(无需签名认证) // GetProductFullInfo 获取商品完整信息(无需签名认证) - 从租户分库查询
func (r *ProductApi) GetProductFullInfo(c *gin.Context) { func (r *ProductApi) GetProductFullInfo(c *gin.Context) {
var req systemReq.GetProductFullInfoRequest var req systemReq.GetProductFullInfoRequest
fmt.Printf("【断点1】接收到获取商品完整信息请求\n") fmt.Printf("========== 【接口入口】获取商品完整信息 ==========\n")
if err := c.ShouldBindQuery(&req); err != nil { if err := c.ShouldBindQuery(&req); err != nil {
fmt.Printf("【断点2】参数绑定失败: %v\n", err) fmt.Printf("【参数验证失败】%v\n", err)
utils.InfoLog(constant.LoggerChannelRequest, map[string]interface{}{ utils.InfoLog(constant.LoggerChannelRequest, map[string]interface{}{
"action": "获取商品完整信息参数验证失败", "action": "获取商品完整信息参数验证失败",
"error": err.Error(), "error": err.Error(),
@ -125,16 +125,17 @@ func (r *ProductApi) GetProductFullInfo(c *gin.Context) {
return return
} }
fmt.Printf("【断点3】参数验证通过 - ProductID: %d, UserID: %d\n", req.ProductID, req.UserID) fmt.Printf("【参数验证通过】UserID(租户): %d, ProductID: %d\n", req.UserID, req.ProductID)
result, err := productService.GetProductFullInfo(req, database.GetDB(c)) result, err := productService.GetProductFullInfo(req, database.GetDB(c))
if err != nil { if err != nil {
fmt.Printf("【断点4】查询商品完整信息失败: %v\n", err) fmt.Printf("【查询失败】%v\n", err)
utils.FailWithRequestLog(constant.LoggerChannelWork, "获取商品完整信息异常", err, c, req) utils.FailWithRequestLog(constant.LoggerChannelWork, "获取商品完整信息异常", err, c, req)
return return
} }
fmt.Printf("【断点5】查询成功,返回商品信息\n") fmt.Printf("【查询成功】返回商品完整信息\n")
fmt.Printf("========== 【接口完成】 ==========\n")
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": 200, "code": 200,

View File

@ -85,8 +85,8 @@ func initRouter() (r *gin.Engine) {
public.GET("/product_book/detail", productBookApi.Detail) // 获取商品反射详情 public.GET("/product_book/detail", productBookApi.Detail) // 获取商品反射详情
public.POST("/product_book/create", productBookApi.Create) // 创建商品反射 public.POST("/product_book/create", productBookApi.Create) // 创建商品反射
public.POST("/product_book/update", productBookApi.Update) // 更新商品反射 public.POST("/product_book/update", productBookApi.Update) // 更新商品反射
public.GET("/product_book/del", productBookApi.Del) public.GET("/product_book/del", productBookApi.Del) // 删除商品反射
public.GET("/product/full_info", productApi.GetProductFullInfo) // 删除商品反射 public.GET("/product/full_info", productApi.GetProductFullInfo) // 没有签名认证获取商品完整信息
} }

View File

@ -289,7 +289,7 @@ func (s *ProductService) GetProductDetail(req systemReq.GetProductDetailRequest,
return item, nil return item, nil
} }
// GetProductFullInfo 获取商品完整信息(包含库存和店铺信息) /*// GetProductFullInfo 获取商品完整信息(包含库存和店铺信息)
func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequest, db ...*gorm.DB) (*systemRes.ProductFullInfoResponse, error) { func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequest, db ...*gorm.DB) (*systemRes.ProductFullInfoResponse, error) {
databaseConn := database.OptionalDB(db...) databaseConn := database.OptionalDB(db...)
@ -407,6 +407,142 @@ func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequ
"shop_count": len(shopList), "shop_count": len(shopList),
}) })
return response, nil
}*/
// GetProductFullInfo 获取商品完整信息(包含库存和店铺信息) - 从租户分库查询
func (s *ProductService) GetProductFullInfo(req systemReq.GetProductFullInfoRequest, db ...*gorm.DB) (*systemRes.ProductFullInfoResponse, error) {
fmt.Printf("【断点1】开始查询商品完整信息 - ProductID: %d, UserID: %d\n", req.ProductID, req.UserID)
if req.ProductID <= 0 {
return nil, utils.NewError("商品ID不能为空")
}
if req.UserID <= 0 {
return nil, utils.NewError("用户ID不能为空")
}
databaseConn, err := database.GetTenantDB(req.UserID)
if err != nil {
fmt.Printf("【断点2】获取租户数据库连接失败: %v\n", err)
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "获取租户数据库连接失败",
"user_id": req.UserID,
"error": err.Error(),
})
return nil, utils.NewError("获取数据库连接失败")
}
fmt.Printf("【断点3】租户数据库连接成功, 开始查询商品信息\n")
var product models.Product
if err := databaseConn.Where("id = ? AND is_del = ?", req.ProductID, 0).First(&product).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
fmt.Printf("【断点4】商品不存在 - ProductID: %d\n", req.ProductID)
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "商品不存在",
"product_id": req.ProductID,
"user_id": req.UserID,
})
return nil, utils.NewError("商品不存在")
}
fmt.Printf("【断点5】查询商品失败: %v\n", err)
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "查询商品失败",
"product_id": req.ProductID,
"user_id": req.UserID,
"error": err.Error(),
})
return nil, utils.NewError("查询商品失败")
}
fmt.Printf("【断点6】商品基本信息查询成功 - Name: %s, Barcode: %s\n", product.Name, product.Barcode)
var liveImage []string
if len(product.LiveImage) > 0 {
if err := json.Unmarshal(product.LiveImage, &liveImage); err != nil {
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "解析商品图片失败",
"error": err.Error(),
})
liveImage = []string{}
}
}
var categoryName string
if product.CategoryID > 0 {
var category models.ProductCategory
if err := databaseConn.Where("id = ?", product.CategoryID).First(&category).Error; err == nil {
categoryName = category.Name
}
}
fmt.Printf("【断点7】开始查询库存信息\n")
var inventories []systemRes.ProductInventoryDetail
if err := databaseConn.Table("inventory_detail inv").
Select("inv.warehouse_id, w.name as warehouse_name, w.code as warehouse_code, inv.location_id, l.code as location_code, l.name as location_name, inv.quantity, FROM_UNIXTIME(inv.created_at) as inbound_time").
Joins("LEFT JOIN warehouse w ON w.id = inv.warehouse_id AND w.is_del = 0").
Joins("LEFT JOIN location l ON l.id = inv.location_id AND l.is_del = 0").
Where("inv.product_id = ? AND inv.is_del = ?", req.ProductID, 0).
Scan(&inventories).Error; err != nil {
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "查询库存信息失败",
"error": err.Error(),
})
inventories = []systemRes.ProductInventoryDetail{}
}
fmt.Printf("【断点8】库存信息查询完成 - 数量: %d\n", len(inventories))
outTaskInfoMap, err := s.getProductOutTaskInfo(databaseConn, []int64{req.ProductID})
if err != nil {
utils.ErrorLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "查询任务信息失败",
"error": err.Error(),
})
}
var shopList []systemRes.ShopInfo
if outTaskInfo, exists := outTaskInfoMap[req.ProductID]; exists {
shopList = outTaskInfo.ShopList
}
fmt.Printf("【断点9】店铺信息查询完成 - 数量: %d\n", len(shopList))
response := &systemRes.ProductFullInfoResponse{
ID: product.ID,
CategoryID: product.CategoryID,
CategoryName: categoryName,
StandardProductID: product.StandardProductID,
Name: product.Name,
Appearance: product.Appearance,
Barcode: product.Barcode,
Price: product.Price,
SalePrice: product.SalePrice,
LiveImage: liveImage,
IsBatchManaged: product.IsBatchManaged,
IsShelfLifeManaged: product.IsShelfLifeManaged,
Status: product.Status,
CreatedAt: product.CreatedAt,
UpdatedAt: product.UpdatedAt,
Inventories: inventories,
ShopList: shopList,
}
fmt.Printf("【断点10】商品完整信息查询成功,准备返回\n")
utils.InfoLog(constant.LoggerChannelWork, map[string]interface{}{
"action": "商品完整信息查询成功",
"product_id": req.ProductID,
"user_id": req.UserID,
"product_name": product.Name,
"barcode": product.Barcode,
"inventory_count": len(inventories),
"shop_count": len(shopList),
"success": true,
})
return response, nil return response, nil
} }