zhangqian
2024-08-01 fc3313955a083c9480e4ea74398f72f9ba6addcd
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package models
 
import (
    "encoding/json"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "wms/constvar"
    "wms/pkg/mysqlx"
)
 
type (
    // LocationProductAmount 库存产品数量
    LocationProductAmount struct {
        WmsModel
        Id                int             `json:"id"  gorm:"column:id;primary_key;AUTO_INCREMENT"`
        LocationId        int             `json:"locationId" gorm:"type:int;not null;comment:位置id"`            //位置id
        WarehouseId       int             `json:"warehouseId" gorm:"type:int;not null;default:0;comment:仓库id"` // 仓库id
        Location          Location        `json:"location" gorm:"foreignKey:LocationId;references:id"`
        ProductCategoryID int             `json:"productCategoryId" gorm:"type:int;not null;comment:产品种类id"` //产品种类id
        ProductCategory   ProductCategory `json:"productCategory" gorm:"foreignKey:ProductCategoryID;references:Id"`
        ProductId         string          `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id
        Product           Material        `json:"product" gorm:"foreignKey:ProductId;references:ID"`
        Amount            decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:库存数量"` //库存数量
        MoreUnitList      []UnitItems     `json:"amountMoreUnits" gorm:"-"`                               //在库数量多单位
        MoreUnitValue     string          `json:"-" gorm:"type:varchar(255);comment:多单位值"`
        CreateDate        string          `json:"createDate" gorm:"type:varchar(63);comment:日期"` //日期
    }
 
    LocationProductAmountSearch struct {
        LocationProductAmount
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
        //LocationProductIds []int
        LocationIds     []int
        ProductIds      []string
        Ids             []int
        Query           string
        Fields          string
        CategoryIds     []int
        JoinedMaterials bool
    }
 
    LocationProductAmountWithOperation struct {
        //LocationProductAmount LocationProductAmount `json:"locationProductAmount"`
        LocationProductAmountId int                        `json:"locationProductAmountId" gorm:"location_product_amount_id"`
        LocationId              int                        `json:"locationId" gorm:"column:location_id"`
        LocationName            string                     `json:"locationName" gorm:"column:location_name"`
        ProductId               string                     `json:"productId" gorm:"column:product_id"`
        ProductName             string                     `json:"productName" gorm:"column:product_name"`
        Amount                  decimal.Decimal            `json:"amount" gorm:"column:amount"`
        AmountMoreUnits         []UnitItems                `json:"amountMoreUnits" gorm:"-"` //在库数量多单位
        Unit                    string                     `json:"unit" gorm:"column:unit;size:50;comment:物品单位"`
        CreateDate              string                     `json:"createDate" gorm:"column:create_date;size:50;comment:创建时间"`
        AdjustAmount            decimal.Decimal            `json:"adjustAmount" gorm:"column:adjust_amount"` //差值
        DifferenceAmount        decimal.Decimal            `json:"differenceAmount" gorm:"-"`                //计数数量
        OperationId             int                        `json:"operationId" gorm:"column:operation_id;comment:库存操作记录"`
        Status                  constvar.OperationStatus   `json:"status" gorm:";comment:操作状态(3就绪、4完成、5取消)"`                    // 操作状态
        BaseOperationType       constvar.BaseOperationType `json:"baseOperationType" gorm:"base_operation_type;comment:基础作业类型"` // 基础作业类型
        Weight                  decimal.Decimal            `gorm:"type:decimal(20,3);comment:重量" json:"weight"`                 //重量
    }
)
 
func (slf *LocationProductAmount) TableName() string {
    return "wms_location_product_amount"
}
 
func (slf *LocationProductAmount) AfterFind(tx *gorm.DB) (err error) {
    if slf.MoreUnitValue != "" {
        var arr []UnitItems
        err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr)
        if err != nil {
            return err
        }
        slf.MoreUnitList = arr
    }
 
    return
}
 
func (slf *LocationProductAmount) BeforeCreate(tx *gorm.DB) (err error) {
    if len(slf.MoreUnitList) != 0 {
        items := make([]UnitItems, 0)
        for k, item := range slf.MoreUnitList {
            if item.Unit != "" && !item.Amount.IsZero() {
                items = append(items, slf.MoreUnitList[k])
            }
        }
 
        str, err := json.Marshal(items)
        if err != nil {
            return err
        }
        slf.MoreUnitValue = string(str)
    }
    return
}
 
func NewLocationProductAmountSearch() *LocationProductAmountSearch {
    return &LocationProductAmountSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *LocationProductAmountSearch) SetOrm(tx *gorm.DB) *LocationProductAmountSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *LocationProductAmountSearch) SetPage(page, size int) *LocationProductAmountSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *LocationProductAmountSearch) SetOrder(order string) *LocationProductAmountSearch {
    slf.Order = order
    return slf
}
 
func (slf *LocationProductAmountSearch) SetID(id int) *LocationProductAmountSearch {
    slf.Id = id
    return slf
}
 
func (slf *LocationProductAmountSearch) SetIds(ids []int) *LocationProductAmountSearch {
    slf.Ids = ids
    return slf
}
 
func (slf *LocationProductAmountSearch) SetKeyword(keyword string) *LocationProductAmountSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *LocationProductAmountSearch) SetPreload(preload bool) *LocationProductAmountSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *LocationProductAmountSearch) SetProductId(productId string) *LocationProductAmountSearch {
    slf.ProductId = productId
    return slf
}
 
func (slf *LocationProductAmountSearch) SetLocationId(locationId int) *LocationProductAmountSearch {
    slf.LocationId = locationId
    return slf
}
 
func (slf *LocationProductAmountSearch) SetProductIds(ids []string) *LocationProductAmountSearch {
    slf.ProductIds = ids
    return slf
}
 
func (slf *LocationProductAmountSearch) SetLocationIds(ids []int) *LocationProductAmountSearch {
    slf.LocationIds = ids
    return slf
}
 
func (slf *LocationProductAmountSearch) SetQuery(query string) *LocationProductAmountSearch {
    slf.Query = query
    return slf
}
 
func (slf *LocationProductAmountSearch) SetWarehouseId(wid int) *LocationProductAmountSearch {
    slf.WarehouseId = wid
    return slf
}
 
func (slf *LocationProductAmountSearch) SetFields(fields string) *LocationProductAmountSearch {
    slf.Fields = fields
    return slf
}
 
func (slf *LocationProductAmountSearch) SetCategoryIds(ids []int) *LocationProductAmountSearch {
    slf.CategoryIds = ids
    return slf
}
 
func (slf *LocationProductAmountSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&LocationProductAmount{})
 
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    if slf.Keyword != "" {
        if !slf.JoinedMaterials {
            db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
            slf.JoinedMaterials = true
        }
        db = db.Joins("left join wms_location on wms_location_product_amount.location_id = wms_location.id").
            Joins("left join material on wms_location_product_amount.product_id = material.id").
            Joins("left join wms_product_category on wms_location_product_amount.product_category_id = wms_product_category.id").
            Where("wms_location.name like ?", "%"+slf.Keyword+"%").Or("material.name like ?", "%"+slf.Keyword+"%").
            Or("wms_product_category.name like ?", "%"+slf.Keyword+"%")
    }
 
    if slf.Preload {
        db = db.Model(&LocationProductAmount{}).Preload("Location").Preload("Product").Preload("ProductCategory")
    }
 
    if len(slf.ProductIds) > 0 {
        db = db.Where("product_id in (?)", slf.ProductIds)
    }
 
    if len(slf.LocationIds) > 0 {
        db = db.Where("location_id in (?)", slf.LocationIds)
    }
 
    if slf.LocationId != 0 {
        db = db.Where("location_id = ?", slf.LocationId)
    }
 
    if slf.ProductId != "" {
        db = db.Where("product_id=?", slf.ProductId)
    }
 
    if len(slf.Ids) > 0 {
        db = db.Where("id in (?)", slf.Ids)
    }
 
    if slf.Query != "" {
        db = db.Where(slf.Query)
    }
 
    if slf.WarehouseId != 0 {
        db = db.Where("warehouse_id = ?", slf.WarehouseId)
    }
 
    if slf.Fields != "" {
        db = db.Select(slf.Fields)
    }
 
    if len(slf.CategoryIds) > 0 {
        if !slf.JoinedMaterials {
            db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
            slf.JoinedMaterials = true
        }
        db = db.Where("material.category_id in ?", slf.CategoryIds)
    }
 
    return db
}
 
// Create 单条插入
func (slf *LocationProductAmountSearch) Create(record *LocationProductAmount) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *LocationProductAmountSearch) CreateBatch(records []*LocationProductAmount) 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 *LocationProductAmountSearch) Update(record *LocationProductAmount) 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 *LocationProductAmountSearch) Delete() error {
    var db = slf.build()
    return db.Unscoped().Delete(&LocationProductAmount{}).Error
}
 
func (slf *LocationProductAmountSearch) First() (*LocationProductAmount, error) {
    var (
        record = new(LocationProductAmount)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *LocationProductAmountSearch) FindByPage() ([]*LocationProductAmount, int64, error) {
    var (
        records = make([]*LocationProductAmount, 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 *LocationProductAmountSearch) Find() ([]*LocationProductAmount, error) {
    var (
        records = make([]*LocationProductAmount, 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 *LocationProductAmountSearch) FindAll() ([]*LocationProductAmount, error) {
    var (
        records = make([]*LocationProductAmount, 0)
        db      = slf.build()
    )
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}
 
func (slf *LocationProductAmountSearch) FirstRes() (*LocationProductAmount, *gorm.DB) {
    var (
        record = new(LocationProductAmount)
        db     = slf.build()
    )
 
    return record, db.First(&record)
}
 
func (slf *LocationProductAmountSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
    err := db.Count(&total).Error
    return total, err
}
 
func (slf *LocationProductAmountSearch) 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 *LocationProductAmountSearch) GroupCount(field string) ([]*GroupCount, error) {
    var (
        db     = slf.build()
        result = make([]*GroupCount, 0)
    )
    if err := db.Select("count(*) as total, " + field + " as class").Group(field).Scan(&result).Error; err != nil {
        return nil, fmt.Errorf("select group err: %v", err)
    }
    return result, nil
}
 
func (slf *LocationProductAmountSearch) GroupSum(groupField string, sumField string) ([]*GroupSum, error) {
    var (
        db     = slf.build()
        result = make([]*GroupSum, 0)
    )
    if err := db.Select("sum(" + sumField + ") as sum, " + groupField + " as class").Group(groupField).Scan(&result).Error; err != nil {
        return nil, fmt.Errorf("select group err: %v", err)
    }
    return result, nil
}
 
func (slf *LocationProductAmountSearch) Save(record *LocationProductAmount) 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
}