|
@@ -1,52 +1,49 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "errors"
|
|
"fmt"
|
|
"fmt"
|
|
- "github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
- "net/http"
|
|
|
|
"sfbase/global"
|
|
"sfbase/global"
|
|
"sfis/db"
|
|
"sfis/db"
|
|
"sfis/model"
|
|
"sfis/model"
|
|
- "sfis/model/response"
|
|
|
|
"strconv"
|
|
"strconv"
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
|
|
|
|
// 创建产品
|
|
// 创建产品
|
|
-func CreateProduct(product model.Product, context *gin.Context) {
|
|
|
|
- var tempProduct model.Product
|
|
|
|
|
|
+func CreateProduct(product model.Product) error {
|
|
|
|
+ var count int64
|
|
// 判断用产品id是否重复
|
|
// 判断用产品id是否重复
|
|
- db.GetSFISDB().Where("id = ?", product.ID).Find(&tempProduct)
|
|
|
|
- fmt.Println(tempProduct)
|
|
|
|
- if tempProduct.ID > 0 {
|
|
|
|
- response.FailWithMessage("产品id重复", context)
|
|
|
|
- return
|
|
|
|
|
|
+ db.GetSFISDB().Table("product").Where("id = ?", product.ID).Count(&count)
|
|
|
|
+ fmt.Println(count, product.ID)
|
|
|
|
+ if count > 0 {
|
|
|
|
+ return errors.New("产品id重复")
|
|
}
|
|
}
|
|
result := db.GetSFISDB().Create(&product)
|
|
result := db.GetSFISDB().Create(&product)
|
|
if result.Error != nil {
|
|
if result.Error != nil {
|
|
global.Logger.Error("productCreate Error", zap.Any("product", product), zap.Any("error", result.Error))
|
|
global.Logger.Error("productCreate Error", zap.Any("product", product), zap.Any("error", result.Error))
|
|
- response.FailWithMessage("产品创建失败", context)
|
|
|
|
|
|
+ return errors.New("产品创建失败")
|
|
} else {
|
|
} else {
|
|
global.Logger.Info("productCreate Success", zap.Any("product", product))
|
|
global.Logger.Info("productCreate Success", zap.Any("product", product))
|
|
- response.OkWithMessage("产品创建成功", context)
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 删除产品
|
|
// 删除产品
|
|
-func DeleteProduct(product model.Product, context *gin.Context) {
|
|
|
|
|
|
+func DeleteProduct(product model.Product) error {
|
|
result := db.GetSFISDB().Delete(&product)
|
|
result := db.GetSFISDB().Delete(&product)
|
|
if result.Error != nil {
|
|
if result.Error != nil {
|
|
global.Logger.Error("productDelete Error", zap.Any("id", product.ID), zap.Any("error", result.Error))
|
|
global.Logger.Error("productDelete Error", zap.Any("id", product.ID), zap.Any("error", result.Error))
|
|
- response.FailWithMessage("产品删除失败", context)
|
|
|
|
|
|
+ return errors.New("产品删除失败")
|
|
} else {
|
|
} else {
|
|
global.Logger.Info("productDelete Success", zap.Any("id", product.ID))
|
|
global.Logger.Info("productDelete Success", zap.Any("id", product.ID))
|
|
- response.OkWithMessage("产品删除成功", context)
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//更新产品
|
|
//更新产品
|
|
-func UpdateProduct(updateMap map[string]interface{}, id int, context *gin.Context) {
|
|
|
|
|
|
+func UpdateProduct(updateMap map[string]interface{}, id int) error {
|
|
//移除map中为空串的key
|
|
//移除map中为空串的key
|
|
for k, v := range updateMap {
|
|
for k, v := range updateMap {
|
|
if v == "" {
|
|
if v == "" {
|
|
@@ -56,19 +53,18 @@ func UpdateProduct(updateMap map[string]interface{}, id int, context *gin.Contex
|
|
updateMap[k] = strV
|
|
updateMap[k] = strV
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
result := db.GetSFISDB().Table("product").Where("id = ?", id).Updates(updateMap)
|
|
result := db.GetSFISDB().Table("product").Where("id = ?", id).Updates(updateMap)
|
|
if result.Error != nil {
|
|
if result.Error != nil {
|
|
global.Logger.Error("productUpdate Error", zap.Any("updateMap", updateMap), zap.Any("error", result.Error))
|
|
global.Logger.Error("productUpdate Error", zap.Any("updateMap", updateMap), zap.Any("error", result.Error))
|
|
- response.FailWithMessage("产品更新失败", context)
|
|
|
|
|
|
+ return errors.New("更新失败")
|
|
} else {
|
|
} else {
|
|
global.Logger.Info("productUpdate Success", zap.Any("updateMap", updateMap))
|
|
global.Logger.Info("productUpdate Success", zap.Any("updateMap", updateMap))
|
|
- response.OkWithMessage("产品更新成功", context)
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 查询产品
|
|
// 查询产品
|
|
-func ListProduct(condMap map[string]interface{}, page int, limit int, context *gin.Context) {
|
|
|
|
|
|
+func ListProduct(condMap map[string]interface{}, page int, limit int) ([]model.Product, int64, error) {
|
|
// 拼查询sql
|
|
// 拼查询sql
|
|
var products []model.Product
|
|
var products []model.Product
|
|
var key []string
|
|
var key []string
|
|
@@ -89,35 +85,21 @@ func ListProduct(condMap map[string]interface{}, page int, limit int, context *g
|
|
}
|
|
}
|
|
sql := strings.Join(key, " and ")
|
|
sql := strings.Join(key, " and ")
|
|
var totalCount int64
|
|
var totalCount int64
|
|
- 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)
|
|
|
|
|
|
+ // 查询
|
|
|
|
+ err := db.GetSFISDB().Table("product").Where(sql, param...).Find(&products).Count(&totalCount).Error
|
|
|
|
+ if err != nil {
|
|
|
|
+ global.Logger.Error("productList search count Error", zap.Any("condMap", condMap), zap.Any("error", err))
|
|
|
|
+ return products, 0, errors.New("产品信息查询失败")
|
|
}
|
|
}
|
|
if totalCount == 0 {
|
|
if totalCount == 0 {
|
|
- context.JSON(http.StatusOK, map[string]interface{}{
|
|
|
|
- "data": products,
|
|
|
|
- "msg": "查询成功",
|
|
|
|
- "total_count": 0,
|
|
|
|
- "code": response.SUCCESS,
|
|
|
|
- })
|
|
|
|
- //response.OkWithData(products, context)
|
|
|
|
- return
|
|
|
|
|
|
+ return products, 0, nil
|
|
}
|
|
}
|
|
result := db.GetSFISDB().Table("product").Where(sql, param...).Limit(limit).Offset((page - 1) * limit).Find(&products)
|
|
result := db.GetSFISDB().Table("product").Where(sql, param...).Limit(limit).Offset((page - 1) * limit).Find(&products)
|
|
if result.Error != nil {
|
|
if result.Error != nil {
|
|
global.Logger.Error("productList Error", zap.Any("condMap", condMap), zap.Any("error", result.Error))
|
|
global.Logger.Error("productList Error", zap.Any("condMap", condMap), zap.Any("error", result.Error))
|
|
- response.FailWithMessage("产品信息查询失败", context)
|
|
|
|
|
|
+ return products, 0, errors.New("产品信息查询失败")
|
|
} else {
|
|
} else {
|
|
global.Logger.Info("productList Success", zap.Any("condMap", condMap))
|
|
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,
|
|
|
|
- })
|
|
|
|
|
|
+ return products, totalCount, nil
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|