liujiandao
2024-03-01 6db46482ae4c8dac7161a0e9ded92cff5db7c1a4
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
package models
 
import (
    "fmt"
    "gorm.io/gorm"
    "silkserver/constvar"
    "silkserver/pkg/mysqlx"
)
 
// WorkshopManage 车间管理
type (
    WorkshopManage struct {
        gorm.Model
        WorkshopNumber string           `json:"workshopNumber" gorm:"type:varchar(255);comment:车间编号"`
        GroupNumber    int              `json:"groupNumber" gorm:"type:int(11);comment:组别"`
        StartCarNumber int              `json:"startCarNumber"  gorm:"type:int(11);comment:组别"`
        EndCarNumber   int              `json:"endCarNumber"  gorm:"type:int(11);comment:组别"`
        CarFlag        constvar.CarFlag `json:"carFlag" gorm:"type:int(1);comment:半车标志"`
        Notes          string           `json:"notes" gorm:"type:varchar(255);comment:备注"`
    }
    WorkshopManageSearch struct {
        WorkshopManage
        PageNum  int
        PageSize int
        Orm      *gorm.DB
    }
)
 
func (slf WorkshopManage) TableName() string {
    return "workshop_manage"
}
 
func NewWorkshopManageSearch() *WorkshopManageSearch {
    return &WorkshopManageSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *WorkshopManageSearch) SetOrm(tx *gorm.DB) *WorkshopManageSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *WorkshopManageSearch) SetPage(page, size int) *WorkshopManageSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *WorkshopManageSearch) SetId(id int) *WorkshopManageSearch {
    slf.ID = uint(id)
    return slf
}
 
func (slf *WorkshopManageSearch) build() *gorm.DB {
    db := slf.Orm.Table(slf.TableName())
 
    if slf.ID > 0 {
        db = db.Where("id = ?", slf.ID)
    }
 
    return db
}
 
// Create 单条插入
func (slf *WorkshopManageSearch) Create(record *WorkshopManage) error {
    db := slf.build()
    err := db.Create(record).Error
    if err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
 
func (slf *WorkshopManageSearch) Find() ([]*WorkshopManage, int64, error) {
    var (
        records = make([]*WorkshopManage, 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 *WorkshopManageSearch) Save(record *WorkshopManage) error {
    var db = slf.build()
 
    if err := db.Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *WorkshopManageSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&WorkshopManage{}).Error; err != nil {
        return err
    }
 
    return nil
}