liujiandao
2024-04-18 4fc6b9ac7e0a7fb87464e6650370763a1a0978bc
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
package models
 
import (
    "fmt"
    "gorm.io/gorm"
    "wms/constvar"
    "wms/pkg/mysqlx"
)
 
type (
    // OperationType 作业类型
    OperationType 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:仓库名称"`     //仓库名称
        BaseOperationType constvar.BaseOperationType `json:"baseOperationType" gorm:"type:tinyint;not null;comment:基础作业类型"` //基础作业类型
        CompanyId         int                        `json:"companyId" gorm:"type:int;not null;comment:公司id"`               //公司id
        Company           Company                    `json:"company" gorm:"foreignKey:CompanyId"`                           //公司
        WarehouseId       int                        `json:"warehouseId" gorm:"type:int;not null;comment:仓库id"`             //仓库id
        Warehouse         Warehouse                  `json:"warehouse" gorm:"foreignKey:WarehouseId"`                       //仓库
        Prefix            string                     `json:"prefix" gorm:"type:varchar(255);comment:前缀"`                    //前缀
 
        DefaultLocationSrcId  int      `json:"defaultLocationSrcId"   gorm:"type:int;not null;comment:默认源位置id"`  //默认源位置id
        DefaultLocationSrc    Location `json:"defaultLocationSrc"     gorm:"foreignKey:DefaultLocationSrcId"`    //默认源位置
        DefaultLocationDestId int      `json:"defaultLocationDestId"  gorm:"type:int;not null;comment:默认目标位置id"` //默认目标位置id
        DefaultLocationDest   Location `json:"defaultLocationDest"    gorm:"foreignKey:DefaultLocationDestId"`   //默认目标位置
 
        PrintLabel                    bool                       `json:"printLabel" gorm:"column:print_label;type:tinyint;comment:打印标签"` //是否打印标签
        ReservationMethod             constvar.ReservationMethod `json:"reservationMethod" gorm:"column:reservation_method"`             //保留方式
        ReservationDaysBefore         int                        `json:"reservationDaysBefore" gorm:"type:int;"`                         //收货前几天
        ReservationDaysBeforePriority int                        `json:"ReservationDaysBeforePriority" gorm:"type:int;"`                 //在优先级的前几天
        ShowOperations                bool                       `json:"showOperations" gorm:"column:show_operations;type:int"`          //显示作业详情
        EarlyOperations               bool                       `json:"earlyOperations" gorm:"type:int;comment:预填写作业详情"`                //预填写作业详情
        CreateBackorder               constvar.WhetherType       `json:"createBackorder" gorm:"column:create_backorder"`                 //创建欠单
        ReturnOperationTypeID         int                        `json:"returnOperationTypeID" gorm:"column:return_job_type_id"`         //退货类型ID
        ReturnOperationType           string                     `json:"returnOperationType" gorm:"-"`                                   //退货类型名称
        ReadyCount                    int                        `json:"readyCount" gorm:"-"`                                            //就绪数量
        FinishCount                   int                        `json:"finishCount" gorm:"-"`                                           //完成数量
        CancelCount                   int                        `json:"cancelCount" gorm:"-"`                                           //取消数量
    }
 
    OperationTypeSearch struct {
        OperationType
        Order        string
        PageNum      int
        PageSize     int
        Keyword      string
        Orm          *gorm.DB
        Preload      bool
        WarehouseIds []int
    }
 
    OperationTypeByStatus struct {
        Id     int                      `gorm:"column:id"`
        Status constvar.OperationStatus `gorm:"column:status"`
        Count  int                      `gorm:"column:count"`
    }
)
 
func (slf *OperationType) TableName() string {
    return "wms_job_type"
}
 
func NewOperationTypeSearch() *OperationTypeSearch {
    return &OperationTypeSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *OperationTypeSearch) SetOrm(tx *gorm.DB) *OperationTypeSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *OperationTypeSearch) SetPage(page, size int) *OperationTypeSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *OperationTypeSearch) SetOrder(order string) *OperationTypeSearch {
    slf.Order = order
    return slf
}
 
func (slf *OperationTypeSearch) SetID(id uint) *OperationTypeSearch {
    slf.ID = id
    return slf
}
 
func (slf *OperationTypeSearch) SetName(name string) *OperationTypeSearch {
    slf.Name = name
    return slf
}
 
func (slf *OperationTypeSearch) SetKeyword(keyword string) *OperationTypeSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *OperationTypeSearch) SetPreload(preload bool) *OperationTypeSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *OperationTypeSearch) SetBaseOperationType(baseOperationType constvar.BaseOperationType) *OperationTypeSearch {
    slf.BaseOperationType = baseOperationType
    return slf
}
 
func (slf *OperationTypeSearch) SetWarehouseId(warehouseId int) *OperationTypeSearch {
    slf.WarehouseId = warehouseId
    return slf
}
 
func (slf *OperationTypeSearch) SetWarehouseIds(warehouseIds []int) *OperationTypeSearch {
    slf.WarehouseIds = warehouseIds
    return slf
}
 
func (slf *OperationTypeSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&OperationType{})
 
    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").Preload("Warehouse").Preload("DefaultLocationSrc").Preload("DefaultLocationDest")
    }
 
    if slf.WarehouseId > 0 {
        db = db.Where("warehouse_id = ?", slf.WarehouseId)
    }
 
    if len(slf.WarehouseIds) > 0 {
        db = db.Where("warehouse_id in ?", slf.WarehouseIds)
    }
 
    if int(slf.BaseOperationType) != 0 {
        db = db.Where("base_operation_type = ?", slf.BaseOperationType)
    }
 
    return db
}
 
// Create 单条插入
func (slf *OperationTypeSearch) Create(record *OperationType) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return err
    }
 
    return nil
}
 
// CreateBatch 批量插入
func (slf *OperationTypeSearch) CreateBatch(records []*OperationType) 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 *OperationTypeSearch) Update(record *OperationType) 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 *OperationTypeSearch) 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 *OperationTypeSearch) 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 *OperationTypeSearch) Delete() error {
    var db = slf.build()
    return db.Delete(&OperationType{}).Error
}
 
func (slf *OperationTypeSearch) First() (*OperationType, error) {
    var (
        record = new(OperationType)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *OperationTypeSearch) Find() ([]*OperationType, int64, error) {
    var (
        records = make([]*OperationType, 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 *OperationTypeSearch) FindNotTotal() ([]*OperationType, error) {
    var (
        records = make([]*OperationType, 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 *OperationTypeSearch) FindByQuery(query string, args []interface{}) ([]*OperationType, int64, error) {
    var (
        records = make([]*OperationType, 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 *OperationTypeSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*OperationType, error) {
    var (
        records = make([]*OperationType, 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 (slf *OperationTypeSearch) ListByStatusAndCount(idList []int) ([]*OperationTypeByStatus, error) {
    var (
        records = make([]*OperationTypeByStatus, 0)
        db      = slf.Orm
    )
    db = db.Table("wms_job_type").Select("wms_job_type.id,wms_operation.status,count(wms_operation.id) as count").InnerJoins("inner join wms_operation on wms_operation.operation_type_id=wms_job_type.id").Where("wms_operation.deleted_at is null").
        Group("wms_job_type.id,wms_operation.status")
    if len(idList) > 0 {
        db = db.Where("wms_job_type.id IN ?", idList)
    }
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("func ListByStatusAndCount err: %v, ", err)
    }
 
    return records, nil
}