|
@@ -3,8 +3,8 @@ package product
|
|
|
import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"go.uber.org/zap"
|
|
|
+ "net/http"
|
|
|
"sfbase/global"
|
|
|
- "sfis/db"
|
|
|
"sfis/model"
|
|
|
"sfis/model/response"
|
|
|
"sfis/service"
|
|
@@ -27,16 +27,14 @@ func productCreate(context *gin.Context) {
|
|
|
var product model.Product
|
|
|
if err := context.ShouldBind(&product); err != nil {
|
|
|
global.Logger.Error("productCreate Bind Error", zap.Any("error", err))
|
|
|
- response.Fail(context)
|
|
|
+ response.FailWithDetailed(response.ParamError, nil, "参数错误", context)
|
|
|
return
|
|
|
}
|
|
|
- result := db.GetSFISDB().Create(&product)
|
|
|
- if result.Error != nil {
|
|
|
- global.Logger.Error("productCreate Error", zap.Any("product", product), zap.Any("error", result.Error))
|
|
|
- response.Fail(context)
|
|
|
+ err := service.CreateProduct(product)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), context)
|
|
|
} else {
|
|
|
- global.Logger.Info("productCreate Success", zap.Any("product", product))
|
|
|
- response.OkWithData(product, context)
|
|
|
+ response.OkWithMessage("创建成功", context)
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -45,42 +43,51 @@ func productCreate(context *gin.Context) {
|
|
|
func productDelete(context *gin.Context) {
|
|
|
var product model.Product
|
|
|
if err := context.ShouldBind(&product); err != nil {
|
|
|
- response.Fail(context)
|
|
|
+ response.FailWithDetailed(response.ParamError, nil, "参数错误", context)
|
|
|
global.Logger.Error("productDelete Bind Error", zap.Any("error", err))
|
|
|
return
|
|
|
}
|
|
|
- result := db.GetSFISDB().Delete(&product)
|
|
|
- if result.Error != nil {
|
|
|
- global.Logger.Error("productDelete Error", zap.Any("id", product.ID), zap.Any("error", result.Error))
|
|
|
- response.Fail(context)
|
|
|
+ err := service.DeleteProduct(product)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), context)
|
|
|
} else {
|
|
|
- global.Logger.Info("productDelete Success", zap.Any("id", product.ID))
|
|
|
- response.OkWithData(product, context)
|
|
|
+ response.OkWithMessage("删除成功", context)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//更新产品信息
|
|
|
func productUpdate(context *gin.Context) {
|
|
|
- var product model.Product
|
|
|
- if err := context.ShouldBind(&product); err != nil {
|
|
|
- global.Logger.Error("productUpdate Bind Error", zap.Any("error", err))
|
|
|
- response.Fail(context)
|
|
|
+ id_ := context.PostForm("id")
|
|
|
+ name := context.PostForm("name")
|
|
|
+ url := context.PostForm("url")
|
|
|
+ unitPrice := context.PostForm("unit_price")
|
|
|
+ minUnit := context.PostForm("min_unit")
|
|
|
+ productType := context.PostForm("product_type")
|
|
|
+ testNum := context.PostForm("test_num")
|
|
|
+ if id_ == "" {
|
|
|
+ response.FailWithDetailed(response.ParamError, nil, "参数错误", context)
|
|
|
return
|
|
|
}
|
|
|
+ id, _ := strconv.Atoi(id_)
|
|
|
+ p := gin.H{
|
|
|
+ "name": name,
|
|
|
+ "url": url,
|
|
|
+ "unit_price": unitPrice,
|
|
|
+ "min_unit": minUnit,
|
|
|
+ "product_type": productType,
|
|
|
+ "test_num": testNum}
|
|
|
|
|
|
- result := db.GetSFISDB().Table("product").Where("id = ?", product.ID).Updates(map[string]interface{}{"name": product.Name, "url": product.Path, "unit_price": product.UnitPrice, "min_unit": product.MinUnit, "product_type": product.ProductType, "test_num": product.TestNum})
|
|
|
- if result.Error != nil {
|
|
|
- global.Logger.Error("productUpdate Error", zap.Any("product", product), zap.Any("error", result.Error))
|
|
|
- response.Fail(context)
|
|
|
+ global.Logger.Info("api userProjectChoose参数:", zap.Any("param", p), zap.Any("id", id))
|
|
|
+ err := service.UpdateProduct(p, id)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), context)
|
|
|
} else {
|
|
|
- global.Logger.Info("productUpdate Success", zap.Any("product", product))
|
|
|
- response.OkWithData(product, context)
|
|
|
+ response.OkWithMessage("更新成功", context)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 产品信息列表
|
|
|
func productList(context *gin.Context) {
|
|
|
-
|
|
|
page, _ := strconv.Atoi(context.Query("page"))
|
|
|
limit, _ := strconv.Atoi(context.Query("limit"))
|
|
|
id := context.PostForm("id")
|
|
@@ -99,5 +106,15 @@ func productList(context *gin.Context) {
|
|
|
"product_type": productType,
|
|
|
"test_num": testNum,
|
|
|
}
|
|
|
- service.ListProduct(condMap,page,limit,context)
|
|
|
+ productList, totalCount, err := service.ListProduct(condMap, page, limit)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), context)
|
|
|
+ } else {
|
|
|
+ context.JSON(http.StatusOK, gin.H{
|
|
|
+ "code": response.SUCCESS,
|
|
|
+ "data": productList,
|
|
|
+ "msg": "查询成功",
|
|
|
+ "totalCount": totalCount,
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|