liujiandao
2024-03-26 e079b27d686b045639c7e0d470c2cbc812c401b1
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
package model
 
import (
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
)
 
type (
    // InvoiceProduct 合同产品
    InvoiceProduct struct {
        InvoiceId int `gorm:"invoice_id" json:"invoiceId"`
        ProductId int `gorm:"product_id" json:"productId"`
    }
 
    // InvoiceProductSearch 合同产品搜索条件
    InvoiceProductSearch struct {
        InvoiceProduct
        Orm        *gorm.DB
        Keyword    string
        PageNum    int
        PageSize   int
        ProductIds []uint
    }
)
 
func (InvoiceProduct) TableName() string {
    return "invoice_product"
}
 
func NewInvoiceProductSearch() *InvoiceProductSearch {
    return &InvoiceProductSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *InvoiceProductSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&InvoiceProduct{})
    if len(slf.ProductIds) != 0 {
        db = db.Where("product_id in ?", slf.ProductIds)
    }
    if slf.InvoiceId != 0 {
        db = db.Where("invoice_id = ?", slf.InvoiceId)
    }
 
    return db
}
 
func (slf *InvoiceProductSearch) Create(record *InvoiceProduct) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *InvoiceProductSearch) CreateBatch(records []*InvoiceProduct) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
func (slf *InvoiceProductSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&InvoiceProduct{}).Error
}
 
func (slf *InvoiceProductSearch) Update(record *InvoiceProduct) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *InvoiceProductSearch) FindAll() ([]*InvoiceProduct, error) {
    var db = slf.build()
    var record = make([]*InvoiceProduct, 0)
    err := db.Find(&record).Error
    return record, err
}
 
func (slf *InvoiceProductSearch) SetProductIds(ids []uint) *InvoiceProductSearch {
    slf.ProductIds = ids
    return slf
}
 
func (slf *InvoiceProductSearch) SetInvoiceId(id int) *InvoiceProductSearch {
    slf.InvoiceId = id
    return slf
}
 
func (slf *InvoiceProductSearch) SetOrm(tx *gorm.DB) *InvoiceProductSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *InvoiceProductSearch) First() (*InvoiceProduct, error) {
    var db = slf.build()
    var record = new(InvoiceProduct)
    err := db.First(record).Error
    return record, err
}
 
func (slf *InvoiceProductSearch) Updates(values interface{}) error {
    var db = slf.build()
    return db.Updates(values).Error
}
 
func (slf *InvoiceProductSearch) Find() ([]*InvoiceProduct, int64, error) {
    var db = slf.build()
    var records = make([]*InvoiceProduct, 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.Find(&records).Error
    return records, total, err
}
 
// InitDefaultData 初始化数据
func (slf *InvoiceProductSearch) InitDefaultData() error {
    var (
        db          = slf.Orm.Table(slf.TableName())
        total int64 = 0
    )
    if err := db.Count(&total).Error; err != nil {
        return err
    }
    if total != 0 {
        return nil
    }
    records := []*InvoiceProduct{}
    return slf.CreateBatch(records)
}