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

---
 model/quotation.go |  149 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 127 insertions(+), 22 deletions(-)

diff --git a/model/quotation.go b/model/quotation.go
index d5fdec8..1c45c24 100644
--- a/model/quotation.go
+++ b/model/quotation.go
@@ -2,32 +2,43 @@
 
 import (
 	"aps_crm/pkg/mysqlx"
+	"fmt"
 	"gorm.io/gorm"
-	"time"
 )
 
 type (
 	// Quotation 鎶ヤ环鍗�
 	Quotation struct {
-		Id                int        `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		ClientId          int        `json:"client_id" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
-		Number            string     `json:"number" gorm:"column:number;type:varchar(255);comment:鎶ヤ环鍗曞彿"`
-		QuotationStatusId int        `json:"quotation_status_id" gorm:"column:quotation_status_id;type:int;comment:鎶ヤ环鍗曠姸鎬乮d"`
-		ValidityDate      time.Time  `json:"validity_date" gorm:"column:validity_date;type:datetime;comment:鏈夋晥鏈�"`
-		ContactId         int        `json:"contact_id" gorm:"column:contact_id;type:int;comment:鑱旂郴浜篿d"`
-		MemberId          int        `json:"member_id" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
-		SaleChanceId      int        `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int;comment:鍟嗘満id"`
-		Conditions        string     `json:"conditions" gorm:"column:conditions;type:text;comment:鎶ヤ环鏉′欢"`
-		File              string     `json:"file" gorm:"column:file;type:varchar(255);comment:闄勪欢"`
-		Client            Client     `json:"client" gorm:"foreignKey:ClientId"`
-		Contact           Contact    `json:"contact" gorm:"foreignKey:ContactId"`
-		SaleChance        SaleChance `json:"sale_chance" gorm:"foreignKey:SaleChanceId"`
+		Id                int             `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		QuotationName     string          `json:"quotationName" gorm:"column:quotation_name;type:varchar(255);comment:鎶ヤ环鍗曞悕绉�"`
+		ClientId          int             `json:"client_id" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
+		Number            string          `json:"number" gorm:"column:number;type:varchar(255);comment:鎶ヤ环鍗曞彿"`
+		QuotationStatusId int             `json:"quotation_status_id" gorm:"column:quotation_status_id;type:int;comment:鎶ヤ环鍗曠姸鎬乮d"`
+		QuotationStatus   QuotationStatus `json:"quotation_status" gorm:"foreignKey:QuotationStatusId"`
+		ValidityDate      *CustomTime     `json:"validity_date" gorm:"column:validity_date;type:datetime;comment:鏈夋晥鏈�"`
+		ContactId         int             `json:"contact_id" gorm:"column:contact_id;type:int;comment:鑱旂郴浜篿d"`
+		MemberId          int             `json:"member_id" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
+		Member            User            `json:"member" gorm:"foreignKey:MemberId"`
+		SaleChanceId      int             `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int;comment:閿�鍞満浼歩d"`
+		Conditions        string          `json:"conditions" gorm:"column:conditions;type:text;comment:鎶ヤ环鏉′欢"`
+		File              string          `json:"file" gorm:"column:file;type:varchar(255);comment:闄勪欢"`
+		Client            Client          `json:"client" gorm:"foreignKey:ClientId"`
+		Contact           Contact         `json:"contact" gorm:"foreignKey:ContactId"`
+		SaleChance        SaleChance      `json:"sale_chance" gorm:"foreignKey:SaleChanceId"`
+		Products          []Product       `json:"products" gorm:"many2many:quotation_product"`
+		CodeStandID       string          `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:缂栫爜id"`
+		gorm.Model        `json:"-"`
 	}
 
 	// QuotationSearch 鎶ヤ环鍗曟悳绱㈡潯浠�
 	QuotationSearch struct {
 		Quotation
-		Orm *gorm.DB
+
+		Orm       *gorm.DB
+		SearchMap map[string]interface{}
+		OrderBy   string
+		PageNum   int
+		PageSize  int
 	}
 )
 
@@ -35,9 +46,12 @@
 	return "quotation"
 }
 
-func NewQuotationSearch() *QuotationSearch {
+func NewQuotationSearch(db *gorm.DB) *QuotationSearch {
+	if db == nil {
+		db = mysqlx.GetDB()
+	}
 	return &QuotationSearch{
-		Orm: mysqlx.GetDB(),
+		Orm: db,
 	}
 }
 
@@ -45,6 +59,41 @@
 	var db = slf.Orm.Model(&Quotation{})
 	if slf.Id != 0 {
 		db = db.Where("id = ?", slf.Id)
+	}
+	if slf.Number != "" {
+		db = db.Where("number = ?", slf.Number)
+	}
+
+	if len(slf.SearchMap) > 0 {
+		for key, value := range slf.SearchMap {
+			switch v := value.(type) {
+			case string:
+				if key == "number" || key == "validity_date" {
+					db = db.Where(key+" LIKE ?", "%"+v+"%")
+				}
+
+				if key == "client_name" {
+					db = db.Joins("Client").Where("Client.name LIKE ?", "%"+v+"%")
+				}
+
+				if key == "contact_name" {
+					db = db.Joins("Contact").Where("Contact.name LIKE ?", "%"+v+"%")
+				}
+
+				if key == "member_name" {
+					db = db.Joins("Member").Where("Member.username LIKE ?", "%"+v+"%")
+				}
+
+			case int, float64:
+				if key == "client_id" || key == "sale_chance_id" || key == "member_id" {
+					db = db.Where(key+" = ?", v)
+				}
+			case []int:
+				if key == "member_ids" {
+					db = db.Where("quotation.member_id in ?", v)
+				}
+			}
+		}
 	}
 
 	return db
@@ -68,15 +117,47 @@
 func (slf *QuotationSearch) Find() (*Quotation, error) {
 	var db = slf.build()
 	var record Quotation
-	err := db.Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
+	err := db.Preload("Products").Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
 	return &record, err
 }
 
-func (slf *QuotationSearch) FindAll() ([]*Quotation, error) {
+func (slf *QuotationSearch) FindAll() ([]*Quotation, int64, error) {
 	var db = slf.build()
-	var records []*Quotation
-	err := db.Find(&records).Error
-	return records, err
+	var records = make([]*Quotation, 0)
+	var total int64
+	if err := db.Count(&total).Error; err != nil {
+		return records, total, err
+	}
+	if slf.PageNum > 0 && slf.PageSize > 0 {
+		db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
+	}
+
+	err := db.Preload("Products").Preload("Member").Preload("QuotationStatus").Preload("Client").Preload("Contact").Order("id desc").Find(&records).Error
+	return records, total, err
+}
+
+func (slf *QuotationSearch) Count() (int64, error) {
+	var db = slf.build()
+	var total int64
+	err := db.Count(&total).Error
+	return total, err
+}
+
+func (slf *QuotationSearch) MaxAutoIncr() (int, error) {
+	type Result struct {
+		Max int
+	}
+
+	var (
+		result Result
+		db     = slf.build()
+	)
+
+	err := db.Select("MAX(id) as max").Scan(&result).Error
+	if err != nil {
+		return result.Max, fmt.Errorf("max err: %v", err)
+	}
+	return result.Max, nil
 }
 
 func (slf *QuotationSearch) SetId(id int) *QuotationSearch {
@@ -88,3 +169,27 @@
 	var db = slf.build()
 	return db.Updates(data).Error
 }
+
+func (slf *QuotationSearch) SetPage(page, size int) *QuotationSearch {
+	slf.PageNum, slf.PageSize = page, size
+	return slf
+}
+
+func (slf *QuotationSearch) SetOrder(order string) *QuotationSearch {
+	slf.OrderBy = order
+	return slf
+}
+
+func (slf *QuotationSearch) SetSearchMap(searchMap map[string]interface{}) *QuotationSearch {
+	slf.SearchMap = searchMap
+	return slf
+}
+
+func (slf *QuotationSearch) SetNumber(number string) *QuotationSearch {
+	slf.Number = number
+	return slf
+}
+func (slf *QuotationSearch) SetIds(ids []int) *QuotationSearch {
+	slf.Orm = slf.Orm.Where("id in (?)", ids)
+	return slf
+}

--
Gitblit v1.8.0