From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 01 十一月 2023 19:20:21 +0800
Subject: [PATCH] 公海member_id使用0

---
 service/quotation.go |   85 ++++++++++++++++++++++++++++++++----------
 1 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/service/quotation.go b/service/quotation.go
index 247f4e8..3f32563 100644
--- a/service/quotation.go
+++ b/service/quotation.go
@@ -3,12 +3,22 @@
 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)
+	//鏍规嵁閿�鍞満浼歩d鍘绘煡璇㈠鎴穒d
+	if quotation.ClientId == 0 {
+		record, err := model.NewSaleChanceSearch().SetId(quotation.SaleChanceId).Find()
+		if err != nil {
+			return ecode.SaleChanceNotExist
+		}
+		quotation.ClientId = record.ClientId
+		quotation.ContactId = record.ContactId
+	}
+	err := model.NewQuotationSearch(nil).Create(quotation)
 	if err != nil {
 		return ecode.QuotationExist
 	}
@@ -16,39 +26,72 @@
 	return ecode.OK
 }
 
-func (QuotationService) DeleteQuotation(id int) int {
-	_, err := model.NewQuotationSearch().SetId(id).Find()
+func (QuotationService) UpdateQuotation(quotation *model.Quotation) int {
+	// check quotation exist
+	tmp, err := model.NewQuotationSearch(nil).SetId(quotation.Id).Find()
 	if err != nil {
 		return ecode.QuotationNotExist
 	}
 
-	err = model.NewQuotationSearch().SetId(id).Delete()
-	if err != nil {
-		return ecode.QuotationNotExist
+	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
 }
 
-func (QuotationService) GetQuotationList() ([]*model.Quotation, int) {
-	list, err := model.NewQuotationSearch().FindAll()
+func (QuotationService) GetQuotationList(page, pageSize int, data map[string]interface{}) ([]*model.Quotation, int64, int) {
+	// get contact list
+	contacts, total, err := model.NewQuotationSearch(nil).SetPage(page, pageSize).SetSearchMap(data).FindAll()
 	if err != nil {
-		return nil, ecode.QuotationListErr
+		return nil, 0, ecode.QuotationListErr
 	}
-
-	return list, ecode.OK
+	return contacts, total, ecode.OK
 }
 
-func (QuotationService) UpdateQuotation(quotation *model.Quotation) int {
-	// check quotation exist
-	_, err := model.NewQuotationSearch().SetId(quotation.Id).Find()
+func (QuotationService) DeleteQuotation(ids []int) int {
+	// delete client
+	err := model.NewQuotationSearch(nil).SetIds(ids).Delete()
 	if err != nil {
-		return ecode.QuotationNotExist
+		return ecode.QuotationDeleteErr
 	}
-
-	err = model.NewQuotationSearch().SetId(quotation.Id).Update(quotation)
-	if err != nil {
-		return ecode.QuotationSetErr
-	}
-
 	return ecode.OK
 }

--
Gitblit v1.8.0