From c5a0eb549cba2cd358a2d0496c44f3a289f15d9c Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期一, 28 八月 2023 14:22:06 +0800
Subject: [PATCH] fix

---
 model/masterOrder.go |   78 ++++++++++++++++++++++++--------------
 1 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/model/masterOrder.go b/model/masterOrder.go
index 71f053e..f33fd6c 100644
--- a/model/masterOrder.go
+++ b/model/masterOrder.go
@@ -3,20 +3,20 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
-	"time"
 )
 
 type (
 	// MasterOrder 閿�鍞�诲崟
 	MasterOrder struct {
-		Id         int       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		Number     string    `json:"number" gorm:"column:number;type:varchar(255);comment:閿�鍞�诲崟鍙�"`
-		ClientId   int       `json:"client_id" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
-		Client     Client    `json:"client" gorm:"foreignKey:ClientId"`
-		MemberId   int       `json:"member_id" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
-		StartTime  time.Time `json:"start_time" gorm:"column:start_time;type:datetime;comment:寮�濮嬫椂闂�"`
-		EndTime    time.Time `json:"end_time" gorm:"column:end_time;type:datetime;comment:缁撴潫鏃堕棿"`
-		Money      float64   `json:"money" gorm:"column:money;type:decimal(10,2);comment:鎬婚噾棰�"`
+		Id         int         `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		Number     string      `json:"number" gorm:"column:number;type:varchar(255);comment:閿�鍞�诲崟鍙�"`
+		ClientId   int         `json:"client_id" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
+		Client     Client      `json:"client" gorm:"foreignKey:ClientId"`
+		MemberId   int         `json:"member_id" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
+		Member     User        `json:"member" gorm:"foreignKey:MemberId"`
+		StartTime  *CustomTime `json:"start_time" gorm:"column:start_time;type:datetime;comment:寮�濮嬫椂闂�"`
+		EndTime    *CustomTime `json:"end_time" gorm:"column:end_time;type:datetime;comment:缁撴潫鏃堕棿"`
+		Money      float64     `json:"money" gorm:"column:money;type:decimal(10,2);comment:鎬婚噾棰�"`
 		gorm.Model `json:"-"`
 	}
 
@@ -24,12 +24,11 @@
 	MasterOrderSearch struct {
 		MasterOrder
 
-				Orm      *gorm.DB
-		Keyword  string
-		OrderBy  string
-		PageNum  int
-		PageSize int
-
+		Orm       *gorm.DB
+		SearchMap map[string]interface{}
+		OrderBy   string
+		PageNum   int
+		PageSize  int
 	}
 )
 
@@ -45,11 +44,32 @@
 
 func (slf *MasterOrderSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&MasterOrder{})
-	if slf.Keyword != "" {
-		db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
-	}
 	if slf.Id != 0 {
 		db = db.Where("id = ?", slf.Id)
+	}
+
+	if len(slf.SearchMap) > 0 {
+		for key, value := range slf.SearchMap {
+			switch v := value.(type) {
+			case string:
+				if key == "number" || key == "start_time" || key == "end_time" {
+					db = db.Where(key+" = ?", v)
+				}
+
+				if key == "client_name" {
+					db = db.Joins("Client").Where("Client.name = ?", v)
+				}
+
+				if key == "member_name" {
+					db = db.Joins("Member").Where("Member.username = ?", v)
+				}
+			case int:
+			case float64:
+				if key == "client_id" || key == "member_id" || key == "money" {
+					db = db.Where(key+" = ?", v)
+				}
+			}
+		}
 	}
 
 	return db
@@ -88,21 +108,12 @@
 		db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
 	}
 
-	if slf.PageNum > 0 && slf.PageSize > 0 {
-		db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
-	}
-
-	err := db.Preload("Client").Find(&records).Error
+	err := db.Preload("Member").Preload("Client").Order("id desc").Find(&records).Error
 	return records, total, err
 }
 
 func (slf *MasterOrderSearch) SetId(id int) *MasterOrderSearch {
 	slf.Id = id
-	return slf
-}
-
-func (slf *MasterOrderSearch) SetKeyword(keyword string) *MasterOrderSearch {
-	slf.Keyword = keyword
 	return slf
 }
 
@@ -114,4 +125,13 @@
 func (slf *MasterOrderSearch) SetOrder(order string) *MasterOrderSearch {
 	slf.OrderBy = order
 	return slf
-}
\ No newline at end of file
+}
+
+func (slf *MasterOrderSearch) SetSearchMap(searchMap map[string]interface{}) *MasterOrderSearch {
+	slf.SearchMap = searchMap
+	return slf
+}
+func (slf *MasterOrderSearch) SetIds(ids []int) *MasterOrderSearch {
+	slf.Orm = slf.Orm.Where("id in (?)", ids)
+	return slf
+}

--
Gitblit v1.8.0