zhangqian
2024-03-22 9c03486e5a7f0b0298436c4b6227f21cd3c10649
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "outsourcing/pkg/mysqlx"
)
 
type (
    OutsourcingOrderDeliveryDetails struct {
        BaseModelInt
        OutsourcingOrderID         uint                     `json:"outsourcingOrderID" gorm:"comment:委外订单ID"`       //委外订单ID
        OutsourcingOrderDeliveryID uint                     `json:"outsourcingOrderDeliveryID" gorm:"comment:发货ID"` //委外订单发货表ID
        OutsourcingOrderDelivery   OutsourcingOrderDelivery `json:"outsourcingOrderDelivery" gorm:"foreignkey:OutsourcingOrderDeliveryID"`
        OutsourcingOrderProductID  uint                     `json:"outsourcingOrderProductID"  gorm:"comment:委外订单产品表ID"` //委外订单产品表ID
        OutsourcingOrderProduct    OutsourcingOrderProduct  `json:"outsourcingOrderProduct"  gorm:"foreignkey:OutsourcingOrderProductID"`
        SendAmount                 decimal.Decimal          `gorm:"type:decimal(18,2);comment:数量" json:"sendAmount"` //发货数量
    }
    OutsourcingOrderDeliveryDetailsSearch struct {
        OutsourcingOrderDeliveryDetails
        PageNum  int
        PageSize int
        Orm      *gorm.DB
        Preload  bool
    }
)
 
func (slf OutsourcingOrderDeliveryDetails) TableName() string {
    return "outsourcing_order_delivery_details"
}
 
func NewOutsourcingOrderDeliveryDetailsSearch() *OutsourcingOrderDeliveryDetailsSearch {
    return &OutsourcingOrderDeliveryDetailsSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) SetOrm(tx *gorm.DB) *OutsourcingOrderDeliveryDetailsSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) SetPage(page, size int) *OutsourcingOrderDeliveryDetailsSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) SetOutsourcingOrderDeliveryID(id uint) *OutsourcingOrderDeliveryDetailsSearch {
    slf.OutsourcingOrderDeliveryID = id
    return slf
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) SetOutsourcingOrderID(id uint) *OutsourcingOrderDeliveryDetailsSearch {
    slf.OutsourcingOrderID = id
    return slf
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) SetPreload(preload bool) *OutsourcingOrderDeliveryDetailsSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *OutsourcingOrderDeliveryDetailsSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.OutsourcingOrderDeliveryID != 0 {
        db = db.Where("outsourcing_order_delivery_id = ?", slf.OutsourcingOrderDeliveryID)
    }
 
    if slf.OutsourcingOrderID != 0 {
        db = db.Where("outsourcing_order_id = ?", slf.OutsourcingOrderID)
    }
 
    if slf.Preload {
        db = db.Preload("OutsourcingOrderDelivery").Preload("OutsourcingOrderProduct")
    }
 
    return db
}
 
// CreateBatch 批量插入
func (slf *OutsourcingOrderDeliveryDetailsSearch) CreateBatch(records []*OutsourcingOrderDeliveryDetails) 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 *OutsourcingOrderDeliveryDetailsSearch) Find() ([]*OutsourcingOrderDeliveryDetails, int64, error) {
    var (
        records = make([]*OutsourcingOrderDeliveryDetails, 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 *OutsourcingOrderDeliveryDetailsSearch) FindNotTotal() ([]*OutsourcingOrderDeliveryDetails, error) {
    var (
        records = make([]*OutsourcingOrderDeliveryDetails, 0)
        db      = slf.build()
    )
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}