liujiandao
2023-10-24 6aa75c2a266a2522ae713b13dc702b5ad0a08f87
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "fmt"
    "gorm.io/gorm"
)
 
type (
    // Quotation 报价单
    Quotation struct {
        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:报价单状态id"`
        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:联系人id"`
        MemberId          int             `json:"member_id" gorm:"column:member_id;type:int;comment:负责人id"`
        Member            User            `json:"member" gorm:"foreignKey:MemberId"`
        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"`
        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
        SearchMap map[string]interface{}
        OrderBy   string
        PageNum   int
        PageSize  int
    }
)
 
func (Quotation) TableName() string {
    return "quotation"
}
 
func NewQuotationSearch(db *gorm.DB) *QuotationSearch {
    if db == nil {
        db = mysqlx.GetDB()
    }
    return &QuotationSearch{
        Orm: db,
    }
}
 
func (slf *QuotationSearch) build() *gorm.DB {
    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)
                }
            }
        }
    }
 
    return db
}
 
func (slf *QuotationSearch) Create(record *Quotation) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *QuotationSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&Quotation{}).Error
}
 
func (slf *QuotationSearch) Update(record *Quotation) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *QuotationSearch) Find() (*Quotation, error) {
    var db = slf.build()
    var record Quotation
    err := db.Preload("Products").Preload("Client").Preload("Contact").Preload("SaleChance").First(&record).Error
    return &record, err
}
 
func (slf *QuotationSearch) FindAll() ([]*Quotation, int64, error) {
    var db = slf.build()
    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 {
    slf.Id = id
    return slf
}
 
func (slf *QuotationSearch) Updates(data map[string]interface{}) error {
    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
}