zhangqian
2024-06-06 6b2c4936814854f658b501e87cdcca454937a786
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package models
 
import (
    "fmt"
    "gorm.io/gorm"
    "strings"
    "wms/pkg/mysqlx"
)
 
type (
    // Warehouse 仓库
    Warehouse struct {
        WmsModel
        Id                     int          `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        Name                   string       `json:"name" gorm:"index;type:varchar(255);not null;comment:仓库名称"`                   //仓库名称
        Active                 bool         `json:"active" gorm:"type:tinyint(1);not null;comment:是否激活"`                         //是否启用,传true就行
        Code                   string       `json:"code" gorm:"index;type:varchar(255);not null;comment:仓库编码"`                   //仓库编码
        PartnerID              int          `json:"partnerId"  gorm:"type:int;not null;comment:合作伙伴id"`                          //合作伙伴id
        BuyToResupply          bool         `json:"buyToResupply" gorm:"type:tinyint(1);not null;comment:是否购买补给"`                //是否购买补给,已购买产品能够发送到此仓库
        ResupplyWhIdsStr       string       `json:"-" gorm:"column:resupply_wh_ids;type:varchar(255);not null;comment:补给来源仓库ID"` //补给来源仓库ID
        ResupplyWhIds          []string     `json:"resupplyWhIds" gorm:"-"`                                                      //补给来源仓库ID
        ResupplyWh             []*Warehouse `json:"resupplyWh" gorm:"-"`                                                         //补给来源仓库
        CompanyId              int          `json:"companyId" gorm:"type:int;not null;comment:公司id"`
        Company                Company      `json:"company" gorm:"foreignKey:CompanyId"`
        Address                string       `json:"address" gorm:"type:varchar(512);comment:地址"`         //地址
        InboundTransportation  int          `json:"inboundTransportation" gorm:"type:int;comment:入向运输"`  //入向运输
        OutboundTransportation int          `json:"outboundTransportation" gorm:"type:int;comment:出库运输"` //出库运输
        LocationId             int          `json:"locationId" gorm:"type:int;comment:位置id"`             //默认位置id
        WarehouseLocation      string       `json:"warehouseLocation" gorm:"-"`                          //库存位置
        Contacts               string       `json:"contacts" gorm:"type:varchar(255);comment:联系人"`       //联系人
    }
 
    WarehouseSearch struct {
        Warehouse
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
        Codes    []string
    }
)
 
func (slf *Warehouse) TableName() string {
    return "wms_warehouse"
}
 
func (slf *Warehouse) BeforeCreate(tx *gorm.DB) (err error) {
    slf.ResupplyWhIdsStr = strings.Join(slf.ResupplyWhIds, ",")
    return nil
}
 
func NewWarehouseSearch() *WarehouseSearch {
    return &WarehouseSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *WarehouseSearch) SetOrm(tx *gorm.DB) *WarehouseSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *WarehouseSearch) SetPage(page, size int) *WarehouseSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *WarehouseSearch) SetOrder(order string) *WarehouseSearch {
    slf.Order = order
    return slf
}
 
func (slf *WarehouseSearch) SetID(id int) *WarehouseSearch {
    slf.Id = id
    return slf
}
 
func (slf *WarehouseSearch) SetCode(code string) *WarehouseSearch {
    slf.Code = code
    return slf
}
 
func (slf *WarehouseSearch) SetCodes(codes []string) *WarehouseSearch {
    slf.Codes = codes
    return slf
}
 
func (slf *WarehouseSearch) SetName(name string) *WarehouseSearch {
    slf.Name = name
    return slf
}
 
func (slf *WarehouseSearch) SetKeyword(keyword string) *WarehouseSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *WarehouseSearch) SetPreload(preload bool) *WarehouseSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *WarehouseSearch) 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 slf.Keyword != "" {
        db = db.Where("name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
    }
 
    if slf.Name != "" {
        db = db.Where("name = ?", slf.Name)
    }
 
    if slf.Preload {
        db = db.Preload("Company")
    }
 
    if slf.Code != "" {
        db = db.Where("code = ?", slf.Code)
    }
 
    if len(slf.Codes) > 0 {
        db = db.Where("code in ?", slf.Codes)
    }
 
    return db
}
 
// Create 单条插入
func (slf *WarehouseSearch) Create(record *Warehouse) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *WarehouseSearch) CreateBatch(records []*Warehouse) 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 *WarehouseSearch) Update(record *Warehouse) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *WarehouseSearch) 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 *WarehouseSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
 
    return nil
}
 
func (slf *WarehouseSearch) Delete() error {
    var db = slf.build()
    return db.Unscoped().Delete(&Warehouse{}).Error
}
 
func (slf *WarehouseSearch) First() (*Warehouse, error) {
    var (
        record = new(Warehouse)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *WarehouseSearch) Find() ([]*Warehouse, int64, error) {
    var (
        records = make([]*Warehouse, 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 *WarehouseSearch) FindNotTotal() ([]*Warehouse, error) {
    var (
        records = make([]*Warehouse, 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
}
 
// FindByQuery 指定条件查询.
func (slf *WarehouseSearch) FindByQuery(query string, args []interface{}) ([]*Warehouse, int64, error) {
    var (
        records = make([]*Warehouse, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query 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 by query records err: %v, query: %s, args: %+v", err, query, args)
    }
 
    return records, total, nil
}
 
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *WarehouseSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Warehouse, error) {
    var (
        records = make([]*Warehouse, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
 
    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 by query records err: %v, query: %s, args: %+v", err, query, args)
    }
 
    return records, nil
}