|
@@ -4,14 +4,71 @@ import (
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"go.uber.org/zap"
|
|
|
+ "net/http"
|
|
|
"sfbase/global"
|
|
|
"sfis/db"
|
|
|
"sfis/model"
|
|
|
"sfis/model/response"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-func ListProduct(condMap map[string]interface{},page int,limit int,context *gin.Context) {
|
|
|
+// 创建产品
|
|
|
+func CreateProduct(product model.Product, context *gin.Context) {
|
|
|
+ var tempProduct model.Product
|
|
|
+ // 判断用产品id是否重复
|
|
|
+ db.GetSFISDB().Where("id = ?", product.ID).Find(&tempProduct)
|
|
|
+ fmt.Println(tempProduct)
|
|
|
+ if tempProduct.ID > 0 {
|
|
|
+ response.FailWithMessage("产品id重复", 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.FailWithMessage("产品创建失败", context)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productCreate Success", zap.Any("product", product))
|
|
|
+ response.OkWithMessage("产品创建成功", context)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 删除产品
|
|
|
+func DeleteProduct(product model.Product, context *gin.Context) {
|
|
|
+ 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.FailWithMessage("产品删除失败", context)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productDelete Success", zap.Any("id", product.ID))
|
|
|
+ response.OkWithMessage("产品删除成功", context)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//更新产品
|
|
|
+func UpdateProduct(updateMap map[string]interface{}, id int, context *gin.Context) {
|
|
|
+ //移除map中为空串的key
|
|
|
+ for k, v := range updateMap {
|
|
|
+ if v == "" {
|
|
|
+ delete(updateMap, k)
|
|
|
+ } else if k == "unit_price" || k == "min_unit" || k == "product_type" || k == "test_num" {
|
|
|
+ strV, _ := strconv.Atoi(v.(string))
|
|
|
+ updateMap[k] = strV
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ result := db.GetSFISDB().Table("product").Where("id = ?", id).Updates(updateMap)
|
|
|
+ if result.Error != nil {
|
|
|
+ global.Logger.Error("productUpdate Error", zap.Any("updateMap", updateMap), zap.Any("error", result.Error))
|
|
|
+ response.FailWithMessage("产品更新失败", context)
|
|
|
+ } else {
|
|
|
+ global.Logger.Info("productUpdate Success", zap.Any("updateMap", updateMap))
|
|
|
+ response.OkWithMessage("产品更新成功", context)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 查询产品
|
|
|
+func ListProduct(condMap map[string]interface{}, page int, limit int, context *gin.Context) {
|
|
|
// 拼查询sql
|
|
|
var products []model.Product
|
|
|
var key []string
|
|
@@ -21,25 +78,46 @@ func ListProduct(condMap map[string]interface{},page int,limit int,context *gin.
|
|
|
continue
|
|
|
}
|
|
|
var kStr string
|
|
|
- if k=="name" || k=="url"{
|
|
|
- kStr = fmt.Sprintf("%s like ?",k)
|
|
|
- v = "%"+v.(string)+"%"
|
|
|
- }else {
|
|
|
+ if k == "name" || k == "url" {
|
|
|
+ kStr = fmt.Sprintf("%s like ?", k)
|
|
|
+ v = "%" + v.(string) + "%"
|
|
|
+ } else {
|
|
|
kStr = fmt.Sprintf("%s = ?", k)
|
|
|
}
|
|
|
- key = append(key,kStr)
|
|
|
- param = append(param,v)
|
|
|
+ key = append(key, kStr)
|
|
|
+ param = append(param, v)
|
|
|
}
|
|
|
- sql := strings.Join(key," and ")
|
|
|
+ sql := strings.Join(key, " and ")
|
|
|
var totalCount int64
|
|
|
- result := db.GetSFISDB().Table("product").Where(sql,param...).Limit(limit).Offset((page - 1) * limit).Find(&products)
|
|
|
- db.GetSFISDB().Table("product").Where(sql,param...).Count(&totalCount)
|
|
|
+ errors := db.GetSFISDB().Table("product").Where(sql, param...).Count(&totalCount).Error
|
|
|
+ if errors != nil {
|
|
|
+ global.Logger.Error("productList search count Error", zap.Any("condMap", condMap), zap.Any("error", errors))
|
|
|
+ response.FailWithMessage("产品信息查询失败", context)
|
|
|
+ }
|
|
|
+ if totalCount == 0 {
|
|
|
+ context.JSON(http.StatusOK, map[string]interface{}{
|
|
|
+ "data": products,
|
|
|
+ "msg": "查询成功",
|
|
|
+ "total_count": 0,
|
|
|
+ "code": response.SUCCESS,
|
|
|
+ })
|
|
|
+ //response.OkWithData(products, context)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ result := db.GetSFISDB().Table("product").Where(sql, param...).Limit(limit).Offset((page - 1) * limit).Find(&products)
|
|
|
if result.Error != nil {
|
|
|
global.Logger.Error("productList Error", zap.Any("condMap", condMap), zap.Any("error", result.Error))
|
|
|
- response.Fail(context)
|
|
|
+ response.FailWithMessage("产品信息查询失败", context)
|
|
|
} else {
|
|
|
global.Logger.Info("productList Success", zap.Any("condMap", condMap))
|
|
|
+ //response.OkWithData(products, context)
|
|
|
response.OkWithData(products, context)
|
|
|
+ context.JSON(http.StatusOK, map[string]interface{}{
|
|
|
+ "data": products,
|
|
|
+ "msg": "查询成功",
|
|
|
+ "total_count": totalCount,
|
|
|
+ "code": response.SUCCESS,
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+}
|