zhangqian
2024-07-05 d91f181819984ed68d928bec6e926da6566e7a3f
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
)
 
type (
    // WarehouseMonthStats 按仓库进行月度统计
    WarehouseMonthStats struct {
        WmsModel
        Id          int             `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        WarehouseId int             `json:"warehouseId" gorm:"type:int;not null;default:0"`             //仓库ID
        ProductId   string          `json:"productId" gorm:"type:varchar(255);not null;comment:产品id"`   //产品id
        ProductName string          `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
        Unit        string          `json:"unit" gorm:"type:char(10);not null;comment:单位"`              //单位
        SalePrice   decimal.Decimal `gorm:"type:decimal(35,18);comment:销售单价" json:"salePrice"`          //销售单价
 
        BeginAmount decimal.Decimal `json:"beginAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //期初数量
        EndAmount   decimal.Decimal `json:"amount" gorm:"type:decimal(30,10);not null;comment:数量"`      //期末结余数量
 
        InputAmount  decimal.Decimal        `json:"inputAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //入库数量
        InputItems   []*WarehouseStatsItems `json:"inputItems" gorm:"-"`                                        //入库明细
        Items        []*WarehouseStatsItems `json:"-"`
        OutputAmount decimal.Decimal        `json:"outputAmount" gorm:"type:decimal(30,10);not null;comment:数量"` //出库数量
        OutputItems  []*WarehouseStatsItems `json:"outputItems"  gorm:"-"`                                       //出库明细
 
        Date string `json:"date" gorm:"index;type:varchar(255); not null;default ''"` //日期 2024-04
    }
 
    WarehouseStatsItems struct {
        WarehouseMonthStatsId int                 `json:"warehouseMonthStatsId"`
        Type                  MonthStatsItemsType `json:"type" gorm:"type:tinyint;not null;default:1"`
        Name                  string              `json:"name" gorm:"type:varchar(255);not null;default:''"` //入库来源,出库去处
        Amount                decimal.Decimal     `json:"amount" gorm:"type:decimal(30,10);not null;"`       //数量
    }
 
    WarehouseMonthStatsSearch struct {
        WarehouseMonthStats
        Order    string
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
        Fields   string
    }
)
 
type MonthStatsItemsType int
 
const (
    MonthStatsItemsTypeInput  MonthStatsItemsType = 1 //入库
    MonthStatsItemsTypeOutput MonthStatsItemsType = 2 //出库
)
 
func (slf *WarehouseStatsItems) TableName() string {
    return "wms_warehouse_month_stats_items"
}
 
func (slf *WarehouseMonthStats) TableName() string {
    return "wms_warehouse_month_stats"
}
 
func (slf *WarehouseMonthStats) BeforeCreate(tx *gorm.DB) error {
    if len(slf.InputItems) != 0 || len(slf.OutputItems) != 0 {
        items := make([]*WarehouseStatsItems, 0, len(slf.InputItems)+len(slf.OutputItems))
        for _, item := range slf.InputItems {
            items = append(items, &WarehouseStatsItems{
                Type:   MonthStatsItemsTypeInput,
                Name:   item.Name,
                Amount: item.Amount,
            })
        }
 
        for _, item := range slf.OutputItems {
            items = append(items, &WarehouseStatsItems{
                Type:   MonthStatsItemsTypeOutput,
                Name:   item.Name,
                Amount: item.Amount,
            })
        }
 
        slf.Items = items
    }
 
    return nil
}
 
func (slf *WarehouseMonthStats) AfterFind(tx *gorm.DB) error {
    if len(slf.Items) != 0 {
        inputItems := make([]*WarehouseStatsItems, 0)
        outputItems := make([]*WarehouseStatsItems, 0)
        for _, v := range slf.Items {
            item := WarehouseStatsItems{
                Type:   v.Type,
                Name:   v.Name,
                Amount: v.Amount,
            }
            if v.Type == MonthStatsItemsTypeInput {
                inputItems = append(inputItems, &item)
            } else {
                outputItems = append(outputItems, &item)
            }
        }
    }
    return nil
}
 
func NewWarehouseMonthStatsSearch() *WarehouseMonthStatsSearch {
    return &WarehouseMonthStatsSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *WarehouseMonthStatsSearch) SetOrm(tx *gorm.DB) *WarehouseMonthStatsSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetPage(page, size int) *WarehouseMonthStatsSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetOrder(order string) *WarehouseMonthStatsSearch {
    slf.Order = order
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetID(id int) *WarehouseMonthStatsSearch {
    slf.Id = id
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetKeyword(keyword string) *WarehouseMonthStatsSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetPreload(preload bool) *WarehouseMonthStatsSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetDate(date string) *WarehouseMonthStatsSearch {
    slf.Date = date
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetFields(fields string) *WarehouseMonthStatsSearch {
    slf.Fields = fields
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) SetWarehouseId(id int) *WarehouseMonthStatsSearch {
    slf.WarehouseId = id
    return slf
}
 
func (slf *WarehouseMonthStatsSearch) Save(record *WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&WarehouseMonthStats{})
 
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
 
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    if slf.Keyword != "" {
        kw := fmt.Sprintf("%%%v%%", slf.Keyword)
        db = db.Where("product_id like ? or product_name like ?", kw, kw)
    }
 
    if slf.Date != "" {
        db = db.Where("date = ?", slf.Date)
    }
 
    if slf.Fields != "" {
        db = db.Select(slf.Fields)
    }
 
    if slf.WarehouseId != 0 {
        db = db.Where("warehouse_id = ?", slf.WarehouseId)
    }
 
    if slf.Preload {
        db = db.Preload("Items")
    }
 
    return db
}
 
// Create 单条插入
func (slf *WarehouseMonthStatsSearch) Create(record *WarehouseMonthStats) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *WarehouseMonthStatsSearch) CreateBatch(records []*WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) Update(record *WarehouseMonthStats) 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 *WarehouseMonthStatsSearch) 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 *WarehouseMonthStatsSearch) 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 *WarehouseMonthStatsSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&WarehouseMonthStats{}).Error
}
 
func (slf *WarehouseMonthStatsSearch) First() (*WarehouseMonthStats, error) {
    var (
        record = new(WarehouseMonthStats)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *WarehouseMonthStatsSearch) Find() ([]*WarehouseMonthStats, int64, error) {
    var (
        records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindNotTotal() ([]*WarehouseMonthStats, error) {
    var (
        records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindByQuery(query string, args []interface{}) ([]*WarehouseMonthStats, int64, error) {
    var (
        records = make([]*WarehouseMonthStats, 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 *WarehouseMonthStatsSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*WarehouseMonthStats, error) {
    var (
        records = make([]*WarehouseMonthStats, 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
}
 
func WarehouseMonthStatsMap(records []*WarehouseMonthStats) (m map[string]*WarehouseMonthStats) {
    m = make(map[string]*WarehouseMonthStats, len(records))
    for _, record := range records {
        m[record.ProductId] = record
    }
    return m
}
 
func WarehouseStatsItemMap(records []*WarehouseStatsItems) (m map[string]*WarehouseStatsItems) {
    m = make(map[string]*WarehouseStatsItems, len(records))
    for _, record := range records {
        m[record.Name] = record
    }
    return m
}
 
func (slf *WarehouseMonthStatsSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
    err := db.Count(&total).Error
    return total, err
}