From fbb3b8c3eb3b48772dc1123561e68741d05dfffa Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 30 八月 2023 13:31:35 +0800
Subject: [PATCH] 统一记录编号
---
service/quotation.go | 52 +++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/service/quotation.go b/service/quotation.go
index f0bbdbe..ede0edc 100644
--- a/service/quotation.go
+++ b/service/quotation.go
@@ -3,12 +3,13 @@
import (
"aps_crm/model"
"aps_crm/pkg/ecode"
+ "aps_crm/pkg/mysqlx"
)
type QuotationService struct{}
func (QuotationService) AddQuotation(quotation *model.Quotation) int {
- err := model.NewQuotationSearch().Create(quotation)
+ err := model.NewQuotationSearch(nil).Create(quotation)
if err != nil {
return ecode.QuotationExist
}
@@ -18,14 +19,51 @@
func (QuotationService) UpdateQuotation(quotation *model.Quotation) int {
// check quotation exist
- _, err := model.NewQuotationSearch().SetId(quotation.Id).Find()
+ tmp, err := model.NewQuotationSearch(nil).SetId(quotation.Id).Find()
if err != nil {
return ecode.QuotationNotExist
}
- err = model.NewQuotationSearch().SetId(quotation.Id).Update(quotation)
- if err != nil {
- return ecode.QuotationSetErr
+ if len(quotation.Products) == 0 {
+ err = model.NewQuotationSearch(nil).SetId(quotation.Id).Update(quotation)
+ if err != nil {
+ return ecode.QuotationSetErr
+ }
+ } else {
+ tx := mysqlx.GetDB().Begin()
+ err = tx.Model(tmp).Association("Products").Clear()
+ if err != nil {
+ tx.Rollback()
+ return ecode.QuotationSetErr
+ }
+
+ err = model.NewQuotationSearch(tx).SetId(quotation.Id).Update(quotation)
+ if err != nil {
+ tx.Rollback()
+ return ecode.QuotationSetErr
+ }
+
+ for _, product := range quotation.Products {
+ if product.Id == 0 {
+ err = model.NewProductSearch(tx).Create(&product)
+ if err != nil {
+ tx.Rollback()
+ return ecode.QuotationSetErr
+ }
+ } else {
+ err = model.NewProductSearch(tx).SetId(product.Id).Update(&product)
+ if err != nil {
+ tx.Rollback()
+ return ecode.QuotationSetErr
+ }
+ }
+ err = tx.Model(&tmp).Association("Products").Append(&product)
+ if err != nil {
+ tx.Rollback()
+ return ecode.QuotationSetErr
+ }
+ }
+ tx.Commit()
}
return ecode.OK
@@ -33,7 +71,7 @@
func (QuotationService) GetQuotationList(page, pageSize int, data map[string]interface{}) ([]*model.Quotation, int64, int) {
// get contact list
- contacts, total, err := model.NewQuotationSearch().SetPage(page, pageSize).SetSearchMap(data).FindAll()
+ contacts, total, err := model.NewQuotationSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll()
if err != nil {
return nil, 0, ecode.QuotationListErr
}
@@ -42,7 +80,7 @@
func (QuotationService) DeleteQuotation(ids []int) int {
// delete client
- err := model.NewQuotationSearch().SetIds(ids).Delete()
+ err := model.NewQuotationSearch(nil).SetIds(ids).Delete()
if err != nil {
return ecode.QuotationDeleteErr
}
--
Gitblit v1.8.0