zhangqian
2024-03-30 66c2da8eb27c065a409165da5a597a2d60df1698
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "outsourcing/pkg/mysqlx"
)
 
type (
    // OutsourcingOrderProduct 委外订单产品表
    OutsourcingOrderProduct struct {
        BaseModelInt
        OutsourcingOrderID uint            `gorm:"index;not null;comment:委外订单ID" json:"outsourcingOrderID"`
        EnterpriseID       uint            `gorm:"type:int;not null;default:0;comment:供应商ID" json:"enterpriseID" ` //供应商ID
        ProductId          string          `gorm:"type:varchar(191);comment:产品id" json:"productId"`
        ProductName        string          `gorm:"type:varchar(191);comment:产品名称" json:"productName"`
        Specs              string          `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
        Type               string          `gorm:"type:varchar(191);comment:物料型号" json:"type"`
        Amount             int64           `gorm:"type:int(11);comment:订单数量" json:"amount"`
        Unit               string          `gorm:"type:varchar(100);comment:单位" json:"unit"`
        BomID              string          `gorm:"type:varchar(255);default '';comment:bomID" json:"bomID"`
        SendAmount         decimal.Decimal `gorm:"-" json:"sendAmount"`
    }
 
    OutsourcingOrderProductSearch struct {
        OutsourcingOrderProduct
        Order               string
        PageNum             int
        PageSize            int
        Orm                 *gorm.DB
        IDs                 []uint
        OutsourcingOrderIDs []uint
    }
)
 
func (slf OutsourcingOrderProduct) TableName() string {
    return "outsourcing_order_product"
}
 
func NewOutsourcingOrderProductSearch() *OutsourcingOrderProductSearch {
    return &OutsourcingOrderProductSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *OutsourcingOrderProductSearch) SetOrm(tx *gorm.DB) *OutsourcingOrderProductSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetPage(page, size int) *OutsourcingOrderProductSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetOrder(order string) *OutsourcingOrderProductSearch {
    slf.Order = order
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetId(id uint) *OutsourcingOrderProductSearch {
    slf.ID = id
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetOutsourcingOrderID(id uint) *OutsourcingOrderProductSearch {
    slf.OutsourcingOrderID = id
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetOutsourcingOrderIDs(ids []uint) *OutsourcingOrderProductSearch {
    slf.OutsourcingOrderIDs = ids
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetEnterpriseID(id uint) *OutsourcingOrderProductSearch {
    slf.EnterpriseID = id
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) SetIds(ids []uint) *OutsourcingOrderProductSearch {
    slf.IDs = ids
    return slf
}
 
func (slf *OutsourcingOrderProductSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if len(slf.IDs) > 0 {
        db = db.Where("id in ?", slf.IDs)
    }
 
    if slf.OutsourcingOrderID != 0 {
        db = db.Where("outsourcing_order_id = ?", slf.OutsourcingOrderID)
    }
 
    if len(slf.OutsourcingOrderIDs) > 0 {
        db = db.Where("outsourcing_order_id in (?)", slf.OutsourcingOrderIDs)
    }
 
    if slf.EnterpriseID != 0 {
        db = db.Where("enterprise_id = ?", slf.EnterpriseID)
    }
 
    return db
}
 
// Create 单条插入
func (slf *OutsourcingOrderProductSearch) Create(record *OutsourcingOrderProduct) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *OutsourcingOrderProductSearch) CreateBatch(records []*OutsourcingOrderProduct) error {
    var db = slf.build()
 
    if err := db.Create(&records).Error; err != nil {
        return fmt.Errorf("create batch err: %v, records: %+v", err, records)
    }
 
    return nil
}
 
func (slf *OutsourcingOrderProductSearch) Save(record *OutsourcingOrderProduct) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *OutsourcingOrderProductSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
 
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
 
    return nil
}
 
func (slf *OutsourcingOrderProductSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&OutsourcingOrderProduct{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *OutsourcingOrderProductSearch) First() (*OutsourcingOrderProduct, error) {
    var (
        record = new(OutsourcingOrderProduct)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *OutsourcingOrderProductSearch) Find() ([]*OutsourcingOrderProduct, int64, error) {
    var (
        records = make([]*OutsourcingOrderProduct, 0)
        total   int64
        db      = slf.build()
    )
 
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
 
    return records, total, nil
}
 
func (slf *OutsourcingOrderProductSearch) FindNotTotal() ([]*OutsourcingOrderProduct, error) {
    var (
        records = make([]*OutsourcingOrderProduct, 0)
        db      = slf.build()
    )
 
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}
 
func (slf *OutsourcingOrderProductSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
 
    err := db.Count(&total).Error
    return total, err
}