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/models"
"crm/response" "crm/response"
"crm/service" "crm/service"
"fmt"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -30,7 +29,6 @@ func (c *ContractApi) Create(context *gin.Context) {
response.Result(response.ErrCodeParamInvalid, nil, context) response.Result(response.ErrCodeParamInvalid, nil, context)
return return
} }
fmt.Println(param)
param.Creator = int64(uid) param.Creator = int64(uid)
errCode := c.contractService.Create(&param) errCode := c.contractService.Create(&param)
response.Result(errCode, nil, context) response.Result(errCode, nil, context)
@@ -85,7 +83,7 @@ func (c *ContractApi) QueryInfo(context *gin.Context) {
// 编辑合同时,查询产品列表 // 编辑合同时,查询产品列表
func (p *ContractApi) QueryPlist(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 { if err := context.ShouldBind(&param); err != nil {
response.Result(response.ErrCodeParamInvalid, nil, context) response.Result(response.ErrCodeParamInvalid, nil, context)
return return
+2 -1
View File
@@ -35,7 +35,7 @@ type ContractCreateParam struct {
type ContractUpdateParam struct { type ContractUpdateParam struct {
Id int64 `json:"id" binding:"required,gt=0"` Id int64 `json:"id" binding:"required,gt=0"`
Name string `json:"name" binding:"required"` 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:"-"` BeginTime string `json:"beginTime" binding:"-"`
OverTime string `json:"overTime" binding:"-"` OverTime string `json:"overTime" binding:"-"`
Remarks string `json:"remarks" binding:"-"` Remarks string `json:"remarks" binding:"-"`
@@ -50,6 +50,7 @@ type ContractDeleteParam struct {
type ContractQueryParam struct { type ContractQueryParam struct {
Id int64 `form:"id" binding:"omitempty,gt=0"` Id int64 `form:"id" binding:"omitempty,gt=0"`
Pids []int64 `form:"pids" json:"pids" binding:"-"`
Name string `form:"name" binding:"-"` Name string `form:"name" binding:"-"`
Creator int64 `form:"creator,omitempty" binding:"-"` Creator int64 `form:"creator,omitempty" binding:"-"`
Page Page Page Page
+43 -6
View File
@@ -4,6 +4,7 @@ import (
"crm/global" "crm/global"
"crm/models" "crm/models"
"crm/response" "crm/response"
"time" "time"
) )
@@ -44,8 +45,8 @@ func (c *ContractService) Update(param *models.ContractUpdateParam) int {
Status: param.Status, Status: param.Status,
Updated: time.Now().Unix(), Updated: time.Now().Unix(),
} }
err := global.Db.Model(&contract).Updates(&contract).Error db := global.Db.Model(&contract).Select("*").Omit("id", "creator", "created")
if err != nil { if err := db.Updates(&contract).Error; err != nil {
return response.ErrCodeFailed return response.ErrCodeFailed
} }
return response.ErrCodeSuccess return response.ErrCodeSuccess
@@ -95,12 +96,48 @@ func (c *ContractService) QueryInfo(param *models.ContractQueryParam) (*models.C
return &contractInfo, response.ErrCodeSuccess return &contractInfo, response.ErrCodeSuccess
} }
// 在编辑合同中,添加产品后,增加已添加的产品列表 // 在编辑合同中,添加产品后,返回已添加的产品列表
func (p *ContractService) QueryPlist(param *models.ProductQueryParam) ([]*models.Products, int) { func (p *ContractService) QueryPlist(param *models.ContractQueryParam) ([]*models.Products, int) {
if param.Id == 0 {
products := make([]*models.Products, 0) products := make([]*models.Products, 0)
err := global.Db.Table(PRODUCT).Find(&products, param.Ids).Error if err := global.Db.Table(PRODUCT).Find(&products, param.Pids).Error; err != nil {
if err != nil {
return nil, response.ErrCodeFailed return nil, response.ErrCodeFailed
} }
return products, response.ErrCodeSuccess 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
}
// 最终已添加的产品列表
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 { func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
customer := models.Customer{ customer := models.Customer{
Id: param.Id,
Name: param.Name, Name: param.Name,
Source: param.Source, Source: param.Source,
Phone: param.Phone, Phone: param.Phone,
@@ -47,8 +48,8 @@ func (c *CustomerService) Update(param *models.CustomerUpdateParam) int {
Status: param.Status, Status: param.Status,
Updated: time.Now().Unix(), Updated: time.Now().Unix(),
} }
err := global.Db.Model(&models.Customer{}).Where("id = ?", param.Id).Updates(&customer).Error db := global.Db.Model(&customer).Select("*").Omit("id", "creator", "created")
if err != nil { if err := db.Updates(&customer).Error; err != nil {
return response.ErrCodeFailed return response.ErrCodeFailed
} }
return response.ErrCodeSuccess return response.ErrCodeSuccess
+2 -2
View File
@@ -42,8 +42,8 @@ func (p *ProductService) Update(param *models.ProductUpdateParam) int {
Status: param.Status, Status: param.Status,
Updated: time.Now().Unix(), Updated: time.Now().Unix(),
} }
err := global.Db.Model(&product).Updates(&product).Error db := global.Db.Model(&product).Select("*").Omit("id", "creator", "created")
if err != nil { if err := db.Updates(&product).Error; err != nil {
return response.ErrCodeFailed return response.ErrCodeFailed
} }
return response.ErrCodeSuccess return response.ErrCodeSuccess