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/subOrder.go |  128 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 104 insertions(+), 24 deletions(-)

diff --git a/model/subOrder.go b/model/subOrder.go
index 42bb6eb..765b112 100644
--- a/model/subOrder.go
+++ b/model/subOrder.go
@@ -2,29 +2,33 @@
 
 import (
 	"aps_crm/pkg/mysqlx"
+	"fmt"
 	"gorm.io/gorm"
 )
 
 type (
 	SubOrder struct {
-		Id            int       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		ClientId      int       `json:"clientId" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
-		Client        Client    `json:"client" gorm:"foreignKey:ClientId"`
-		MemberId      int       `json:"memberId" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
-		MasterOrderId int       `json:"masterOrderId" gorm:"column:master_order_id;type:int;comment:閿�鍞�诲崟id"`
-		Number        string    `json:"number" gorm:"column:number;type:varchar(255);comment:閿�鍞瓙鍗曞彿"`
-		Products      []Product `json:"products" gorm:"many2many:subOrder_product;"`
+		Id            int         `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		ClientId      int         `json:"clientId" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
+		Client        Client      `json:"client" gorm:"foreignKey:ClientId"`
+		MemberId      int         `json:"memberId" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
+		Member        User        `json:"member" gorm:"foreignKey:MemberId"`
+		MasterOrderId int         `json:"masterOrderId" gorm:"column:master_order_id;type:int;comment:閿�鍞�诲崟id"`
+		MasterOrder   MasterOrder `json:"masterOrder" gorm:"foreignKey:MasterOrderId"`
+		Number        string      `json:"number" gorm:"column:number;type:varchar(255);comment:閿�鍞瓙鍗曞彿"`
+		Products      []Product   `json:"products" gorm:"many2many:subOrder_product;"`
+		CodeStandID   string      `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:缂栫爜id"`
 		gorm.Model    `json:"-"`
 	}
 
 	SubOrderSearch struct {
 		SubOrder
 
-		Orm      *gorm.DB
-		Keyword  string
-		OrderBy  string
-		PageNum  int
-		PageSize int
+		Orm       *gorm.DB
+		SearchMap map[string]interface{}
+		OrderBy   string
+		PageNum   int
+		PageSize  int
 	}
 )
 
@@ -32,19 +36,57 @@
 	return "sub_order"
 }
 
-func NewSubOrderSearch() *SubOrderSearch {
+func NewSubOrderSearch(db *gorm.DB) *SubOrderSearch {
+	if db == nil {
+		db = mysqlx.GetDB()
+	}
 	return &SubOrderSearch{
-		Orm: mysqlx.GetDB(),
+		Orm: db,
 	}
 }
 
 func (slf *SubOrderSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&SubOrder{})
-	if slf.Keyword != "" {
-		db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
-	}
+
 	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" {
+					db = db.Where(key+" LIKE ?", "%"+v+"%")
+				}
+
+				if key == "client_name" {
+					db = db.Joins("Client").Where("Client.name LIKE ?", "%"+v+"%")
+				}
+
+				if key == "master_order_number" {
+					db = db.Joins("MasterOrder").Where("MasterOrder.number LIKE ?", "%"+v+"%")
+				}
+
+				if key == "member_name" {
+					db = db.Joins("Member").Where("User.username LIKE ?", "%"+v+"%")
+				}
+			case int, float64:
+				if key == "client_id" || key == "master_order_id" {
+					db = db.Where(key+" = ?", v)
+				}
+				if key == "member_id" {
+					db = db.Where("sub_order.member_id = ?", v)
+				}
+			case []int:
+				if key == "member_ids" {
+					db = db.Where("sub_order.member_id in ?", v)
+				}
+			}
+		}
 	}
 
 	return db
@@ -82,17 +124,36 @@
 		db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
 	}
 
-	err := db.Preload("Products").Preload("Client").Find(&records).Error
+	err := db.Preload("MasterOrder").Preload("Member").Preload("Products").Preload("Client").Order("id desc").Find(&records).Error
 	return records, total, err
+}
+
+func (slf *SubOrderSearch) Count() (int64, error) {
+	var db = slf.build()
+	var total int64
+	err := db.Count(&total).Error
+	return total, err
+}
+
+func (slf *SubOrderSearch) 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 *SubOrderSearch) SetId(id int) *SubOrderSearch {
 	slf.Id = id
-	return slf
-}
-
-func (slf *SubOrderSearch) SetKeyword(keyword string) *SubOrderSearch {
-	slf.Keyword = keyword
 	return slf
 }
 
@@ -104,4 +165,23 @@
 func (slf *SubOrderSearch) SetOrder(order string) *SubOrderSearch {
 	slf.OrderBy = order
 	return slf
-}
\ No newline at end of file
+}
+
+func (slf *SubOrderSearch) SetSearchMap(data map[string]interface{}) *SubOrderSearch {
+	slf.SearchMap = data
+	return slf
+}
+func (slf *SubOrderSearch) SetIds(ids []int) *SubOrderSearch {
+	slf.Orm = slf.Orm.Where("id in (?)", ids)
+	return slf
+}
+
+func (slf *SubOrderSearch) SetNumber(number string) *SubOrderSearch {
+	slf.Number = number
+	return slf
+}
+
+func (slf *SubOrderSearch) UpdateByMap(data map[string]interface{}) error {
+	var db = slf.build()
+	return db.Updates(data).Error
+}

--
Gitblit v1.8.0