liujiandao
2024-01-11 40e540f8ca398fee68f4520dbebd6db6fe2e164c
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
package model
 
import (
    "aps_crm/constvar"
    "aps_crm/pkg/mysqlx"
    "fmt"
    "gorm.io/gorm"
)
 
type (
    // SalesDetailsProduct 销售明细和产品关联
    SalesDetailsProduct struct {
        SalesDetailsId int  `json:"id" gorm:"column:sales_details_id;type:int;primary_key;not null;default:0"`
        ProductId      uint `json:"name" gorm:"primary_key;column:product_id;type:int;not null;default:0;comment:产品id"`
    }
 
    // SalesDetailsProductSearch 销售明细和产品关联搜索条件
    SalesDetailsProductSearch struct {
        SalesDetailsProduct
        Orm         *gorm.DB
        QueryClass  constvar.SalesDetailsProductQueryClass
        KeywordType constvar.SalesDetailsProductKeywordType
        Keyword     string
        PageNum     int
        PageSize    int
        ProductIds  []uint
    }
)
 
func (SalesDetailsProduct) TableName() string {
    return "sales_details_product"
}
 
func NewSalesDetailsProductSearch() *SalesDetailsProductSearch {
    return &SalesDetailsProductSearch{
        Orm: mysqlx.GetDB(),
    }
}
 
func (slf *SalesDetailsProductSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&SalesDetailsProduct{})
    if len(slf.ProductIds) > 0 {
        db = db.Where("product_id in (?)", slf.ProductIds)
    }
 
    return db
}
 
func (slf *SalesDetailsProductSearch) SetProductIds(ids []uint) *SalesDetailsProductSearch {
    slf.ProductIds = ids
    return slf
}
 
func (slf *SalesDetailsProductSearch) Create(record *SalesDetailsProduct) error {
    var db = slf.build()
    return db.Create(record).Error
}
 
func (slf *SalesDetailsProductSearch) CreateBatch(records []*SalesDetailsProduct) error {
    var db = slf.build()
    return db.Create(records).Error
}
 
func (slf *SalesDetailsProductSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&SalesDetailsProduct{}).Error
}
 
func (slf *SalesDetailsProductSearch) Update(record *SalesDetailsProduct) error {
    var db = slf.build()
    return db.Updates(record).Error
}
 
func (slf *SalesDetailsProductSearch) FindAll() ([]*SalesDetailsProduct, error) {
    var db = slf.build()
    var record = make([]*SalesDetailsProduct, 0)
    err := db.Find(&record).Error
    return record, err
}
 
func (slf *SalesDetailsProductSearch) SetPage(page, size int) *SalesDetailsProductSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *SalesDetailsProductSearch) SetOrm(tx *gorm.DB) *SalesDetailsProductSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *SalesDetailsProductSearch) First() (*SalesDetailsProduct, error) {
    var db = slf.build()
    var record = new(SalesDetailsProduct)
    err := db.First(record).Error
    return record, err
}
 
func (slf *SalesDetailsProductSearch) Updates(values interface{}) error {
    var db = slf.build()
    return db.Updates(values).Error
}
 
func (slf *SalesDetailsProductSearch) Save(record *SalesDetailsProduct) error {
    var db = slf.build()
 
    if err := db.Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *SalesDetailsProductSearch) Find() ([]*SalesDetailsProduct, int64, error) {
    var db = slf.build()
    var records = make([]*SalesDetailsProduct, 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 *SalesDetailsProductSearch) 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 := []*SalesDetailsProduct{}
    return slf.CreateBatch(records)
}