yinbentan
2024-07-25 f874e81b77282079398c22c67af754a23d260c91
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "silkserver/constvar"
    "silkserver/pkg/mysqlx"
)
 
type (
    //WorkTypeManage 工种管理
    WorkTypeManage struct {
        gorm.Model
        Code            constvar.JobType `json:"code" gorm:"type:varchar(255);comment:工种编码"`
        WorkName        string           `json:"workName" gorm:"type:varchar(255);comment:工种名称"`
        IsGuaranteed    bool             `json:"isGuaranteed" gorm:"type:int(1);comment:不达标保底"`
        GuaranteedWages decimal.Decimal  `json:"guaranteedWages" gorm:"type:decimal(20,3);comment:保底工资"`
        CreateTime      string           `json:"createTime" gorm:"type:varchar(255);comment:添加时间"`
        AddPeople       string           `json:"addPeople" gorm:"type:varchar(255);comment:添加人"`
        SalaryPlans     []*SalaryPlan    `json:"salaryPlans" gorm:"many2many:silk_salaryPlan_workType"`
    }
    WorkTypeManageSearch struct {
        WorkTypeManage
        PageNum  int
        PageSize int
        Preload  bool
        Ids      []uint
        Orm      *gorm.DB
    }
)
 
func (slf WorkTypeManage) TableName() string {
    return "silk_work_type_manage"
}
 
func NewWorkTypeManageSearch() *WorkTypeManageSearch {
    return &WorkTypeManageSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *WorkTypeManageSearch) SetOrm(tx *gorm.DB) *WorkTypeManageSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *WorkTypeManageSearch) SetPage(page, size int) *WorkTypeManageSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *WorkTypeManageSearch) SetId(id int) *WorkTypeManageSearch {
    slf.ID = uint(id)
    return slf
}
 
func (slf *WorkTypeManageSearch) SetPreload(preload bool) *WorkTypeManageSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *WorkTypeManageSearch) SetIds(ids []uint) *WorkTypeManageSearch {
    slf.Ids = ids
    return slf
}
 
func (slf *WorkTypeManageSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.ID > 0 {
        db = db.Where("id = ?", slf.ID)
    }
 
    if len(slf.Ids) > 0 {
        db = db.Where("id in (?)", slf.Ids)
    }
 
    if slf.Preload {
        db = db.Model(WorkTypeManage{}).Preload("SalaryPlans")
    }
 
    return db
}
 
// Create 单条插入
func (slf *WorkTypeManageSearch) Create(record *WorkTypeManage) error {
    var db = slf.build()
 
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *WorkTypeManageSearch) Save(record *WorkTypeManage) error {
    var db = slf.build()
 
    if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
 
    return nil
}
 
func (slf *WorkTypeManageSearch) 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 *WorkTypeManageSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Delete(&WorkTypeManage{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *WorkTypeManageSearch) Find() ([]*WorkTypeManage, int64, error) {
    var (
        records = make([]*WorkTypeManage, 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 *WorkTypeManageSearch) FindNotTotal() ([]*WorkTypeManage, error) {
    var (
        records = make([]*WorkTypeManage, 0)
        db      = slf.build()
    )
 
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
 
    return records, nil
}
 
func (slf *WorkTypeManageSearch) Count() (int64, error) {
    var (
        total int64
        db    = slf.build()
    )
 
    if err := db.Count(&total).Error; err != nil {
        return total, fmt.Errorf("find count err: %v", err)
    }
    return total, nil
}