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
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
package models
 
import (
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "silkserver/constvar"
    "silkserver/pkg/mysqlx"
)
 
type (
    //AttendanceManage 考勤管理表
    AttendanceManage struct {
        gorm.Model
        Date             string                    `json:"date" gorm:"type:varchar(255);comment:考勤时间"`
        WorkerId         string                    `json:"workerId" gorm:"type:varchar(255);comment:人员id"`
        WorkerName       string                    `json:"workerName" gorm:"type:varchar(255);comment:人员姓名"`
        StartWorkTime    string                    `json:"startWorkTime" gorm:"type:varchar(255);comment:上班打卡时间"`
        EndWorkTime      string                    `json:"endWorkTime" gorm:"type:varchar(255);comment:下班打卡时间"`
        Classes          string                    `json:"classes" gorm:"type:varchar(255);comment:班次"`
        ClassesStartTime string                    `json:"classesStartTime" gorm:"type:varchar(255);comment:班次开始时间"`
        ClassesEndTime   string                    `json:"classesEndTime" gorm:"type:varchar(255);comment:班次下班时间"`
        CreateTime       string                    `json:"createTime" gorm:"type:varchar(255);comment:添加时间"`
        AddPeople        string                    `json:"addPeople" gorm:"type:varchar(255);comment:添加人"`
        WorkTypeId       uint                      `json:"workTypeId" gorm:"type:int(11);comment:工种id"`
        WorkType         WorkTypeManage            `json:"workType" gorm:"foreignKey:WorkTypeId"`
        Status           constvar.AttendanceStatus `json:"status" gorm:"type:int(11);comment:状态"`
        OverTimeDuration decimal.Decimal           `json:"overTimeDuration" gorm:"type:decimal(20,2);comment:加班时长"`
        PhoneNum         string                    `gorm:"type:varchar(191);comment:手机号" json:"phoneNum"`
    }
    AttendanceManageSearch struct {
        AttendanceManage
        PageNum  int
        PageSize int
        Preload  bool
        Ids      []uint
        Month    string
        Keyword  string
        Order    string
        Orm      *gorm.DB
    }
)
 
func (slf AttendanceManage) TableName() string {
    return "silk_attendance_manage"
}
 
func NewAttendanceManageSearch() *AttendanceManageSearch {
    return &AttendanceManageSearch{Orm: mysqlx.GetDB()}
}
 
func (slf *AttendanceManageSearch) SetOrm(tx *gorm.DB) *AttendanceManageSearch {
    slf.Orm = tx
    return slf
}
 
func (slf *AttendanceManageSearch) SetPage(page, size int) *AttendanceManageSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
 
func (slf *AttendanceManageSearch) SetIds(ids []uint) *AttendanceManageSearch {
    slf.Ids = ids
    return slf
}
 
func (slf *AttendanceManageSearch) SetPreload(preload bool) *AttendanceManageSearch {
    slf.Preload = preload
    return slf
}
 
func (slf *AttendanceManageSearch) SetMonth(month string) *AttendanceManageSearch {
    slf.Month = month
    return slf
}
 
func (slf *AttendanceManageSearch) SetDate(date string) *AttendanceManageSearch {
    slf.Date = date
    return slf
}
 
func (slf *AttendanceManageSearch) SetWorkerId(workerId string) *AttendanceManageSearch {
    slf.WorkerId = workerId
    return slf
}
 
func (slf *AttendanceManageSearch) SetKeyword(keyword string) *AttendanceManageSearch {
    slf.Keyword = keyword
    return slf
}
 
func (slf *AttendanceManageSearch) SetOrder(order string) *AttendanceManageSearch {
    slf.Order = order
    return slf
}
 
func (slf *AttendanceManageSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
 
    if len(slf.Ids) > 0 {
        db = db.Where("id in (?)", slf.Ids)
    }
 
    if slf.Preload {
        db = db.Model(&AttendanceManage{}).Preload("WorkType")
    }
 
    if slf.Month != "" {
        db = db.Where("date like ?", slf.Month+"%")
    }
 
    if slf.Date != "" {
        db = db.Where("date = ?", slf.Date)
    }
 
    if slf.WorkerId != "" {
        db = db.Where("worker_id = ?", slf.WorkerId)
    }
 
    if slf.Keyword != "" {
        db = db.Where("worker_name like ? or worker_id like ?", "%"+slf.Keyword+"%", "%"+slf.Keyword+"%")
    }
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
 
    return db
}
 
// Create 单条插入
func (slf *AttendanceManageSearch) Create(record *AttendanceManage) 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 *AttendanceManageSearch) CreateBatch(record []*AttendanceManage) 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 *AttendanceManageSearch) Save(record *AttendanceManage) 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 *AttendanceManageSearch) 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 *AttendanceManageSearch) Delete() error {
    var db = slf.build()
 
    if err := db.Unscoped().Delete(&AttendanceManage{}).Error; err != nil {
        return err
    }
 
    return nil
}
 
func (slf *AttendanceManageSearch) Find() ([]*AttendanceManage, int64, error) {
    var (
        records = make([]*AttendanceManage, 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 *AttendanceManageSearch) FindNotTotal() ([]*AttendanceManage, error) {
    var (
        records = make([]*AttendanceManage, 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 *AttendanceManageSearch) 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
}