zhangqian
2024-04-17 e63c9d7f5f5c0819ee949697aec753895cc88ab5
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "srm/global"
)
 
type (
    OutsourcingMaterialApply struct {
        gorm.Model
        OutsourcingOrderNumber string          `json:"outsourcingOrderNumber" gorm:"type:varchar(255);comment:委外订单编码"`
        MaterialNumber         string          `json:"materialNumber" gorm:"type:varchar(191);comment:物料编码"`
        MaterialName           string          `json:"materialName" gorm:"type:varchar(191);comment:物料名称"`
        Unit                   string          `json:"unit" gorm:"type:varchar(100);comment:单位"`
        Specs                  string          `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
        Type                   string          `gorm:"type:varchar(191);comment:物料型号" json:"type"`
        Amount                 decimal.Decimal `gorm:"type:decimal(35,18);comment:数量" json:"amount"`
    }
    OutsourcingMaterialApplySearch struct {
        OutsourcingMaterialApply
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
 
func (slf OutsourcingMaterialApply) TableName() string {
    return "outsourcing_material_apply"
}
 
func NewOutsourcingMaterialApplySearch() *OutsourcingMaterialApplySearch {
    return &OutsourcingMaterialApplySearch{Orm: global.GVA_DB}
}
 
func (slf *OutsourcingMaterialApplySearch) SetOrm(tx *gorm.DB) *OutsourcingMaterialApplySearch {
    slf.Orm = tx
    return slf
}
 
func (slf *OutsourcingMaterialApplySearch) SetPage(page, size int) *OutsourcingMaterialApplySearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *OutsourcingMaterialApplySearch) SetOutsourcingOrderNumber(number string) *OutsourcingMaterialApplySearch {
    slf.OutsourcingOrderNumber = number
    return slf
}
 
func (slf *OutsourcingMaterialApplySearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.OutsourcingOrderNumber != "" {
        db = db.Where("outsourcing_order_number = ?", slf.OutsourcingOrderNumber)
    }
 
    return db
}
 
// CreateBatch 批量插入
func (slf *OutsourcingMaterialApplySearch) CreateBatch(records []*OutsourcingMaterialApply) 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 *OutsourcingMaterialApplySearch) Find() ([]*OutsourcingMaterialApply, int64, error) {
    var (
        records = make([]*OutsourcingMaterialApply, 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 *OutsourcingMaterialApplySearch) FindNotTotal() ([]*OutsourcingMaterialApply, error) {
    var (
        records = make([]*OutsourcingMaterialApply, 0)
        db      = slf.build()
    )
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}