yinbentan
2024-08-30 cc7c2094568ea8f9d1697da5ed0a2c759ca81abd
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "silkserver/pkg/mysqlx"
)
 
type (
    // SalaryReportForm 薪资报表
    SalaryReportForm struct {
        gorm.Model
        WorkerId    string           `json:"workerId" gorm:"type:varchar(255);comment:人员id"`
        WorkerName  string           `json:"workerName"  gorm:"type:varchar(255);comment:人员姓名"`
        Phone       string           `json:"phone" gorm:"type:varchar(255);comment:电话"`
        WorkTypeId  uint             `json:"workTypeId" gorm:"type:int(11);comment:工种类型id"`
        WorkType    WorkTypeManage   `json:"workType"  gorm:"foreignKey:WorkTypeId;references:ID"`
        Month       string           `json:"month" gorm:"type:varchar(255);comment:月份"`
        IssueSalary decimal.Decimal  `json:"issueSalary" gorm:"type:decimal(20,3);comment:应发工资"`
        Remark      string           `json:"remark" gorm:"type:varchar(255);comment:备注"`
        Details     []*SalaryDetails `json:"details" gorm:"foreignKey:SalaryReportFormId"`
    }
    SalaryReportFormSearch struct {
        SalaryReportForm
        PageNum  int
        PageSize int
        Preload  bool
        Keyword  string
        Orm      *gorm.DB
    }
)
 
func (slf SalaryReportForm) TableName() string {
    return "silk_salary_report_form"
}
 
func NewSalaryReportFormSearch() *SalaryReportFormSearch {
    return &SalaryReportFormSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *SalaryReportFormSearch) SetOrm(tx *gorm.DB) *SalaryReportFormSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *SalaryReportFormSearch) SetPage(page, size int) *SalaryReportFormSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *SalaryReportFormSearch) SetPreload(preload bool) *SalaryReportFormSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *SalaryReportFormSearch) SetKeyword(keyword string) *SalaryReportFormSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *SalaryReportFormSearch) SetMonth(month string) *SalaryReportFormSearch {
    slf.Month = month
    return slf
}
 
func (slf *SalaryReportFormSearch) SetWorkerId(workerId string) *SalaryReportFormSearch {
    slf.WorkerId = workerId
    return slf
}
 
func (slf *SalaryReportFormSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if slf.Preload {
        db = db.Model(SalaryReportForm{}).Preload("Details").Preload("WorkType")
    }
    if slf.Keyword != "" {
        db = db.Where("worker_name like ? or worker_id like ?", "%"+slf.Keyword+"%", "%"+slf.Keyword+"%")
    }
    if slf.Month != "" {
        db = db.Where("month = ?", slf.Month)
    }
    if slf.WorkerId != "" {
        db = db.Where("worker_id = ?", slf.WorkerId)
    }
 
    return db
}
 
// Create 单条插入
func (slf *SalaryReportFormSearch) Create(record *SalaryReportForm) 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 *SalaryReportFormSearch) CreateBatch(record []*SalaryReportForm) 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 *SalaryReportFormSearch) Save(record *SalaryReportForm) 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 *SalaryReportFormSearch) 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 *SalaryReportFormSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&SalaryReportForm{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *SalaryReportFormSearch) Find() ([]*SalaryReportForm, int64, error) {
    var (
        records = make([]*SalaryReportForm, 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 *SalaryReportFormSearch) FindNotTotal() ([]*SalaryReportForm, error) {
    var (
        records = make([]*SalaryReportForm, 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 *SalaryReportFormSearch) First() (*SalaryReportForm, error) {
    var (
        record = new(SalaryReportForm)
        db     = slf.build()
    )
 
    if err := db.First(record).Error; err != nil {
        return record, err
    }
 
    return record, nil
}
 
func (slf *SalaryReportFormSearch) 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
}