yinbentan
2024-07-31 b94bef381946e22fd1038f24e6d9de911d194640
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "silkserver/constvar"
    "silkserver/pkg/mysqlx"
)
 
type (
    // PayrollConstitute 其它补贴、奖惩
    PayrollConstitute struct {
        BaseModelInt
        Cycle         string           `json:"cycle" gorm:"index;size:20;not null;comment:统计周期(yyyy-MM)"` //月份
        WorkerID      string           `json:"workerId" gorm:"size:200;not null;comment:员工ID"`            //员工ID
        Worker        Worker           `json:"worker" gorm:"foreignKey:WorkerID;references:ID"`
        WorkTypeID    uint             `json:"workTypeID" gorm:"type:bigint(20);not null;comment:工种ID"`      //工种ID
        WorkType      WorkTypeManage   `json:"workType" gorm:"foreignKey:WorkTypeID;references:ID"`          //工种ID
        WorkTypeCode  constvar.JobType `json:"workTypeCode" gorm:"size:255;not null;comment:工种代码"`           //工种代码
        WorkTypeName  string           `json:"workTypeName" gorm:"size:255;not null;comment:工种名称"`           //工种名称
        SalaryPlanId  uint             `json:"salaryPlanId" gorm:"type:bigint(20);not null;comment:薪资方案ID"`  //薪资方案ID
        SalaryPlan    SalaryPlan       `json:"subsidyTypeName" gorm:"foreignKey:SalaryPlanId;references:ID"` //薪资方案
        SalaryFormula string           `json:"salaryFormula" gorm:"size:255;not null;comment:薪资方案(翻译)"`      //薪资方案
        Amount        decimal.Decimal  `json:"amount" gorm:"type:decimal(12,4);comment:金额"`                  // 金额
        CreatedBy     string           `json:"createdBy" gorm:"size:255;not null;comment:添加者"`               // 添加者(auto,用户id)
    }
 
    PayrollConstituteSearch struct {
        PayrollConstitute
        Monthly  string
        Order    string
        PageNum  int
        PageSize int
        Preload  bool
        Orm      *gorm.DB
    }
)
 
func (slf PayrollConstitute) TableName() string {
    return "silk_payroll_constitute"
}
 
// NewPayrollConstituteSearch 其它补贴
func NewPayrollConstituteSearch() *PayrollConstituteSearch {
    return &PayrollConstituteSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *PayrollConstituteSearch) SetOrm(tx *gorm.DB) *PayrollConstituteSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *PayrollConstituteSearch) SetPage(page, size int) *PayrollConstituteSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *PayrollConstituteSearch) SetOrder(order string) *PayrollConstituteSearch {
    slf.Order = order
    return slf
}
 
func (slf *PayrollConstituteSearch) SetID(id uint) *PayrollConstituteSearch {
    slf.ID = id
    return slf
}
 
func (slf *PayrollConstituteSearch) SetCycle(cycle string) *PayrollConstituteSearch {
    slf.Cycle = cycle
    return slf
}
 
func (slf *PayrollConstituteSearch) SetMonthly(monthly string) *PayrollConstituteSearch {
    slf.Monthly = monthly
    return slf
}
 
func (slf *PayrollConstituteSearch) SetWorkTypeID(workTypeID uint) *PayrollConstituteSearch {
    slf.WorkTypeID = workTypeID
    return slf
}
 
func (slf *PayrollConstituteSearch) SetWorkerID(workerID string) *PayrollConstituteSearch {
    slf.WorkerID = workerID
    return slf
}
 
func (slf *PayrollConstituteSearch) SetCreatedBy(createdBy string) *PayrollConstituteSearch {
    slf.CreatedBy = createdBy
    return slf
}
 
func (slf *PayrollConstituteSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.Preload {
        db = db.Model(&PayrollConstitute{}).Preload("Worker").Preload("WorkType").Preload("WorkType.SalaryPlan")
    }
 
    if slf.ID > 0 {
        db = db.Where("id = ?", slf.ID)
    }
 
    if slf.Cycle != "" {
        db = db.Where("cycle = ?", slf.Cycle)
    }
 
    if slf.Monthly != "" {
        db = db.Where("cycle like ?", slf.Monthly+"%")
    }
 
    if slf.WorkTypeID > 0 {
        db = db.Where("work_type_id = ?", slf.WorkTypeID)
    }
 
    if slf.WorkerID != "" {
        db = db.Where("worker_id = ?", slf.WorkerID)
    }
 
    if slf.WorkerID != "" {
        db = db.Where("worker_id = ?", slf.WorkerID)
    }
 
    if slf.CreatedBy != "" {
        db = db.Where("created_by = ?", slf.SalaryPlanId)
    }
 
    db.Where("1 = 1")
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    return db
}
 
// Create 单条插入
func (slf *PayrollConstituteSearch) Create(record *PayrollConstitute) 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
}
 
// CreateBatch 批量插入
func (slf *PayrollConstituteSearch) CreateBatch(records []*PayrollConstitute) 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
}
 
// Save 单条更新
func (slf *PayrollConstituteSearch) Save(record *PayrollConstitute) 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
}
 
// SaveBatch 批量更新
func (slf *PayrollConstituteSearch) SaveBatch(record []*PayrollConstitute) 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
}
 
// UpdateByMap 单条更新
func (slf *PayrollConstituteSearch) 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
}
 
// UpdateByQuery 批量更新
func (slf *PayrollConstituteSearch) 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
}
 
// Delete 删除
func (slf *PayrollConstituteSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&PayrollConstitute{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
// First 根据条件查询一条记录
func (slf *PayrollConstituteSearch) First() (*PayrollConstitute, error) {
    var (
        record = new(PayrollConstitute)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
// Find 指定条件查询(包含总条数)
func (slf *PayrollConstituteSearch) Find() ([]*PayrollConstitute, int64, error) {
    var (
        records = make([]*PayrollConstitute, 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
}
 
// FindNotTotal 指定条件查询
func (slf *PayrollConstituteSearch) FindNotTotal() ([]*PayrollConstitute, error) {
    var (
        records = make([]*PayrollConstitute, 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 *PayrollConstituteSearch) FindByQuery(query string, args []interface{}) ([]*PayrollConstitute, int64, error) {
    var (
        records = make([]*PayrollConstitute, 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 *PayrollConstituteSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*PayrollConstitute, error) {
    var (
        records = make([]*PayrollConstitute, 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
}