fix: zero value failed to update

This commit is contained in:
zchengo
2022-12-23 20:21:36 +08:00
parent 8de3ccdebd
commit 7fecd6ba7f
5 changed files with 55 additions and 18 deletions
+1 -3
View File
@@ -4,7 +4,6 @@ import (
"crm/models"
"crm/response"
"crm/service"
"fmt"
"strconv"
"github.com/gin-gonic/gin"
@@ -30,7 +29,6 @@ func (c *ContractApi) Create(context *gin.Context) {
response.Result(response.ErrCodeParamInvalid, nil, context)
return
}
fmt.Println(param)
param.Creator = int64(uid)
errCode := c.contractService.Create(&param)
response.Result(errCode, nil, context)
@@ -85,7 +83,7 @@ func (c *ContractApi) QueryInfo(context *gin.Context) {
// 编辑合同时,查询产品列表
func (p *ContractApi) QueryPlist(context *gin.Context) {
var param models.ProductQueryParam
var param models.ContractQueryParam
if err := context.ShouldBind(&param); err != nil {
response.Result(response.ErrCodeParamInvalid, nil, context)
return
+5 -4
View File
@@ -35,7 +35,7 @@ type ContractCreateParam struct {
type ContractUpdateParam struct {
Id int64 `json:"id" binding:"required,gt=0"`
Name string `json:"name" binding:"required"`
Amount float64 `json:"amount" binding:"required,gt=0"`
Amount float64 `json:"amount" binding:"omitempty,gt=0"`
BeginTime string `json:"beginTime" binding:"-"`
OverTime string `json:"overTime" binding:"-"`
Remarks string `json:"remarks" binding:"-"`
@@ -49,9 +49,10 @@ type ContractDeleteParam struct {
}
type ContractQueryParam struct {
Id int64 `form:"id" binding:"omitempty,gt=0"`
Name string `form:"name" binding:"-"`
Creator int64 `form:"creator,omitempty" binding:"-"`
Id int64 `form:"id" binding:"omitempty,gt=0"`
Pids []int64 `form:"pids" json:"pids" binding:"-"`
Name string `form:"name" binding:"-"`
Creator int64 `form:"creator,omitempty" binding:"-"`
Page Page
}
+44 -7
View File
@@ -4,6 +4,7 @@ import (
"crm/global"
"crm/models"
"crm/response"
"time"
)
@@ -44,8 +45,8 @@ func (c *ContractService) Update(param *models.ContractUpdateParam) int {
Status: param.Status,
Updated: time.Now().Unix(),
}
err := global.Db.Model(&contract).Updates(&contract).Error
if err != nil {
db := global.Db.Model(&contract).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&contract).Error; err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
@@ -95,12 +96,48 @@ func (c *ContractService) QueryInfo(param *models.ContractQueryParam) (*models.C
return &contractInfo, response.ErrCodeSuccess
}
// 在编辑合同中,添加产品后,增加已添加的产品列表
func (p *ContractService) QueryPlist(param *models.ProductQueryParam) ([]*models.Products, int) {
products := make([]*models.Products, 0)
err := global.Db.Table(PRODUCT).Find(&products, param.Ids).Error
// 在编辑合同中,添加产品后,返回已添加的产品列表
func (p *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
if param.Id == 0 {
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, param.Pids).Error; err != nil {
return nil, response.ErrCodeFailed
}
return products, response.ErrCodeSuccess
}
// 默认已添加的产品列表
var contract models.Contract
err := global.Db.Table(CONTRACT).Select("productlist").First(&contract, param.Id).Error
if err != nil {
return nil, response.ErrCodeFailed
}
return products, response.ErrCodeSuccess
// 最终已添加的产品列表
addedProductList := make([]*models.Products, 0)
if len(param.Pids) == 0 {
return addedProductList, response.ErrCodeSuccess
}
addedPids := make([]int64, 0)
for _, pid := range param.Pids {
if len(*contract.Productlist) == 0 {
addedPids = param.Pids
break
}
for _, product := range *contract.Productlist {
if pid == product.Id {
addedProductList = append(addedProductList, product)
continue
}
addedPids = append(addedPids, pid)
}
}
products := make([]*models.Products, 0)
if err := global.Db.Table(PRODUCT).Find(&products, addedPids).Error; err != nil {
return nil, response.ErrCodeFailed
}
addedProductList = append(addedProductList, products...)
return addedProductList, response.ErrCodeSuccess
}
+3 -2
View File
@@ -35,6 +35,7 @@ func (c *CustomerService) Create(param *models.CustomerCreateParam) int {
// 更新客户
func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
customer := models.Customer{
Id: param.Id,
Name: param.Name,
Source: param.Source,
Phone: param.Phone,
@@ -47,8 +48,8 @@ func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
Status: param.Status,
Updated: time.Now().Unix(),
}
err := global.Db.Model(&models.Customer{}).Where("id = ?", param.Id).Updates(&customer).Error
if err != nil {
db := global.Db.Model(&customer).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&customer).Error; err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess
+2 -2
View File
@@ -42,8 +42,8 @@ func (p *ProductService) Update(param *models.ProductUpdateParam) int {
Status: param.Status,
Updated: time.Now().Unix(),
}
err := global.Db.Model(&product).Updates(&product).Error
if err != nil {
db := global.Db.Model(&product).Select("*").Omit("id", "creator", "created")
if err := db.Updates(&product).Error; err != nil {
return response.ErrCodeFailed
}
return response.ErrCodeSuccess