liujiandao
2024-04-15 439b8cf9619e6deb6b42182d0f43cc1252fc9d2a
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "wms/pkg/mysqlx"
)
 
type (
    ReorderRule struct {
        WmsModel
        Id           int             `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        ProductId    string          `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id
        Product      Material        `json:"product" gorm:"foreignKey:ProductId;references:ID"`
        LocationId   int             `json:"locationId" gorm:"type:int;not null;comment:位置id"` //位置id
        Location     Location        `json:"location" gorm:"foreignKey:LocationId;references:id"`
        Route        string          `json:"route" gorm:"type:varchar(255);comment:路线"`            //路线
        MinInventory decimal.Decimal `json:"minInventory" gorm:"type:decimal(35,18);comment:最小库存"` //最小库存
        MaxInventory decimal.Decimal `json:"maxInventory" gorm:"type:decimal(35,18);comment:最大库存"` //最大库存
        OrderNumber  decimal.Decimal `json:"orderNumber" gorm:"type:decimal(35,18);comment:订购数量"`  //订购数量
        Unit         string          `json:"unit" gorm:"type:varchar(100);comment:单位"`             //单位
        Amount       decimal.Decimal `json:"amount" gorm:"-"`                                      //在库数量
        Prediction   decimal.Decimal `json:"prediction" gorm:"-"`                                  //预测数量
        SupplierId   int64           `json:"supplierId" gorm:"-"`                                  //供应商id
    }
    ReorderRuleSearch struct {
        ReorderRule
        PageNum  int
        PageSize int
        Keyword  string
        Orm      *gorm.DB
        Preload  bool
    }
)
 
func (slf *ReorderRule) TableName() string {
    return "wms_reorder_rule"
}
 
func NewReorderRuleSearch() *ReorderRuleSearch {
    return &ReorderRuleSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *ReorderRuleSearch) SetOrm(tx *gorm.DB) *ReorderRuleSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *ReorderRuleSearch) SetPage(page, size int) *ReorderRuleSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *ReorderRuleSearch) SetID(ID int) *ReorderRuleSearch {
    slf.Id = ID
    return slf
}
 
func (slf *ReorderRuleSearch) SetKeyword(keyword string) *ReorderRuleSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *ReorderRuleSearch) SetProductId(productId string) *ReorderRuleSearch {
    slf.ProductId = productId
    return slf
}
 
func (slf *ReorderRuleSearch) SetLocationId(locationId int) *ReorderRuleSearch {
    slf.LocationId = locationId
    return slf
}
 
func (slf *ReorderRuleSearch) SetPreload(preload bool) *ReorderRuleSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *ReorderRuleSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ReorderRule{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    }
    if slf.ProductId != "" {
        db = db.Where("product_id = ?", slf.ProductId)
    }
    if slf.LocationId != 0 {
        db = db.Where("location_id = ?", slf.LocationId)
    }
 
    if slf.Preload {
        db = db.Preload("Product").Preload("Location")
    }
 
    return db
}
 
// Create 单条插入
func (slf *ReorderRuleSearch) Create(record *ReorderRule) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *ReorderRuleSearch) Update(record *ReorderRule) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
        return fmt.Errorf("update err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *ReorderRuleSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
 
    err := db.Count(&total).Error
    return total, err
}
 
func (slf *ReorderRuleSearch) First() (*ReorderRule, error) {
    var (
        record = new(ReorderRule)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *ReorderRuleSearch) Find() ([]*ReorderRule, int64, error) {
    var (
        records = make([]*ReorderRule, 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.Order("created_at desc").Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
 
    return records, total, nil
}
 
func (slf *ReorderRuleSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&ReorderRule{}).Error
}