147 lines
4.1 KiB
Go
147 lines
4.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
"psi/constant"
|
|
"psi/models/request"
|
|
systemRes "psi/models/response"
|
|
"psi/service"
|
|
"psi/utils"
|
|
)
|
|
|
|
var productBookService = &service.ProductBookService{}
|
|
|
|
type ProductBookApi struct{}
|
|
|
|
// List 获取商品反射列表
|
|
func (r *ProductBookApi) List(c *gin.Context) {
|
|
var req request.GetProductBookListRequest
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "获取商品反射列表请求参数异常",
|
|
"err_msg": err.Error(),
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
result, err := productBookService.List(req)
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "获取商品反射列表异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
systemRes.OkWithDetailed(result, "获取成功", c)
|
|
}
|
|
|
|
// Detail 获取商品反射详情
|
|
func (r *ProductBookApi) Detail(c *gin.Context) {
|
|
id := c.Query("id")
|
|
isbn := c.Query("isbn")
|
|
|
|
if id == "" || isbn == "" {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "获取商品反射详情请求参数异常",
|
|
"err_msg": "id和isbn不能为空",
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: id和isbn不能为空", c)
|
|
return
|
|
}
|
|
|
|
var idInt int64
|
|
if _, err := fmt.Sscanf(id, "%d", &idInt); err != nil || idInt <= 0 {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "获取商品反射详情请求参数异常",
|
|
"err_msg": "ID格式错误",
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: ID格式不正确", c)
|
|
return
|
|
}
|
|
|
|
result, err := productBookService.Detail(idInt, isbn)
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "获取商品反射详情异常", err, c, gin.H{"id": idInt, "isbn": isbn})
|
|
return
|
|
}
|
|
|
|
systemRes.OkWithDetailed(result, "获取成功", c)
|
|
}
|
|
|
|
// Create 创建商品反射
|
|
func (r *ProductBookApi) Create(c *gin.Context) {
|
|
var req request.ProductBookRequest
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "创建商品反射请求参数异常",
|
|
"err_msg": err.Error(),
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
id, err := productBookService.Create(req)
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "创建商品反射异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
systemRes.OkWithDetailed(gin.H{"id": id}, "创建成功", c)
|
|
}
|
|
|
|
// Update 更新商品反射
|
|
func (r *ProductBookApi) Update(c *gin.Context) {
|
|
var req request.ProductBookRequest
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "更新商品反射请求参数异常",
|
|
"err_msg": err.Error(),
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
if err := productBookService.Update(req); err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "更新商品反射异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
systemRes.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// Del 删除商品反射
|
|
func (r *ProductBookApi) Del(c *gin.Context) {
|
|
id := c.Query("id")
|
|
isbn := c.Query("isbn")
|
|
|
|
if id == "" || isbn == "" {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "删除商品反射请求参数异常",
|
|
"err_msg": "id和isbn不能为空",
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: id和isbn不能为空", c)
|
|
return
|
|
}
|
|
|
|
var idInt int64
|
|
if _, err := fmt.Sscanf(id, "%d", &idInt); err != nil || idInt <= 0 {
|
|
utils.ErrorLog(constant.LoggerChannelRequest, logrus.Fields(gin.H{
|
|
"source": "删除商品反射请求参数异常",
|
|
"err_msg": "ID格式错误",
|
|
}))
|
|
systemRes.FailWithValidateMessage("参数错误: ID格式不正确", c)
|
|
return
|
|
}
|
|
|
|
if err := productBookService.Del(idInt, isbn); err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "删除商品反射异常", err, c, gin.H{"id": idInt, "isbn": isbn})
|
|
return
|
|
}
|
|
|
|
systemRes.OkWithMessage("删除成功", c)
|
|
}
|