|
@@ -1 +1,103 @@
|
|
|
package product
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "go.uber.org/zap"
|
|
|
+ "sfbase/global"
|
|
|
+ "sfis/db"
|
|
|
+ "sfis/model"
|
|
|
+ "sfis/model/response"
|
|
|
+ "sfis/service"
|
|
|
+ "strconv"
|
|
|
+)
|
|
|
+
|
|
|
+func ProductManageRegister(router *gin.Engine) {
|
|
|
+ productGroup := router.Group("/manage/product/")
|
|
|
+ productGroup.Use()
|
|
|
+ {
|
|
|
+ productGroup.POST("/create", productCreate)
|
|
|
+ productGroup.POST("/delete", productDelete)
|
|
|
+ productGroup.POST("/update", productUpdate)
|
|
|
+ productGroup.POST("/list", productList)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 创建产品
|
|
|
+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)
|
|
|
+ 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)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productCreate Success", zap.Any("product", product))
|
|
|
+ response.OkWithData(product, context)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 删除产品
|
|
|
+func productDelete(context *gin.Context) {
|
|
|
+ var product model.Product
|
|
|
+ if err := context.ShouldBind(&product); err != nil {
|
|
|
+ response.Fail(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)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productDelete Success", zap.Any("id", product.ID))
|
|
|
+ response.OkWithData(product, 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)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productUpdate Success", zap.Any("product", product))
|
|
|
+ response.OkWithData(product, context)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 产品信息列表
|
|
|
+func productList(context *gin.Context) {
|
|
|
+
|
|
|
+ page, _ := strconv.Atoi(context.Query("page"))
|
|
|
+ limit, _ := strconv.Atoi(context.Query("limit"))
|
|
|
+ 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")
|
|
|
+ condMap := map[string]interface{}{
|
|
|
+ "id": id,
|
|
|
+ "name": name,
|
|
|
+ "url": url,
|
|
|
+ "unit_price": unitPrice,
|
|
|
+ "min_unit": minUnit,
|
|
|
+ "product_type": productType,
|
|
|
+ "test_num": testNum,
|
|
|
+ }
|
|
|
+ service.ListProduct(condMap,page,limit,context)
|
|
|
+}
|