reward_summary_dao.go
11.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)
type RewardSumaryDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewRewardSumaryDao(transactionContext *pgTransaction.TransactionContext) (*RewardSumaryDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RewardSumaryDao{
transactionContext: transactionContext,
}, nil
}
}
// 功过看板-日榜
type RewardSummaryDay struct {
Id int `json:"id"` //id
RecordDateStr string `json:"record_date_str"`
SectionName string `json:"sectionName"` //工段名称
WorkerName string `json:"workerName"` //员工名称
SummaryResult string `json:"summaryResult"` //奖惩结果
YieldResult string `json:"yieldResult"` //产效功过结果
UpToStandardResult string `json:"upToStandardResult"` //合格率功过结果
AccidentResult1 string `json:"accidentResult1"` //质量事故功过结果
AccidentResult2 string `json:"accidentResul2"` //安全事故功过结果
AccidentResult3 string `json:"accidentResul3"` //异物 功过结果
}
// 功过看板-日榜
func (d *RewardSumaryDao) SeachRewardSummaryDay(companyId int, orgId int, recordDate string) ([]RewardSummaryDay, error) {
sqlStr := `SELECT
"id",record_date_str,
work_station->>'sectionName' as section_name,
worker->>'userName' as worker_name,
summary_result,
yield_result,
up_to_standard_result,
accident_result1,
accident_result2,
accident_result3
FROM manufacture."reward_summary"
WHERE company_id=?
and org_id=?
and record_date_str=?`
tx := d.transactionContext.PgTx
var result []RewardSummaryDay
_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
for i := range result {
switch result[i].AccidentResult1 {
case "1":
result[i].AccidentResult1 = "功"
case "0":
result[i].AccidentResult1 = "不奖不惩"
case "-1":
result[i].AccidentResult1 = "过"
}
switch result[i].AccidentResult2 {
case "1":
result[i].AccidentResult2 = "功"
case "0":
result[i].AccidentResult2 = "不奖不惩"
case "-1":
result[i].AccidentResult2 = "过"
}
switch result[i].AccidentResult3 {
case "1":
result[i].AccidentResult3 = "功"
case "0":
result[i].AccidentResult3 = "不奖不惩"
case "-1":
result[i].AccidentResult3 = "过"
}
switch result[i].UpToStandardResult {
case "1":
result[i].UpToStandardResult = "功"
case "0":
result[i].UpToStandardResult = "不奖不惩"
case "-1":
result[i].UpToStandardResult = "过"
}
switch result[i].YieldResult {
case "1":
result[i].YieldResult = "功"
case "0":
result[i].YieldResult = "不奖不惩"
case "-1":
result[i].YieldResult = "过"
}
}
return result, err
}
// 功过看板-产效
type RewardSummaryYield struct {
RecordDateStr string `json:"record_date_str"`
SectionName string `json:"sectionName"` //工段名称
WorkerName string `json:"workerName"` //员工名称
Yield string `json:"yield"` //产效
YieldResult string `json:"yieldResult"` //产效功过结果
YieldMax string `json:"yildMax"` //产效历史最佳
}
// 功过看板-产效
func (d *RewardSumaryDao) SeachRewardSummaryYield(companyId int, orgId int, recordDate string) ([]RewardSummaryYield, error) {
// sqlStr := `SELECT
// record_date_str,
// work_station->>'sectionName' as section_name,
// worker->>'userName' as worker_name,
// yield_result,
// yield,
// '24' as yield_max
// FROM manufacture."reward_summary"
// WHERE company_id=?
// and org_id=?
// and record_date_str=?`
sqlStr := `with
t1 as( SELECT
max(yield) as yield_max,
work_station->>'sectionId' AS section_id,
worker->>'userId' as worker_id
FROM manufacture."reward_summary"
WHERE company_id=? and org_id=?
GROUP BY section_id,worker_id
),
t2 as (SELECT
record_date_str,
work_station->>'sectionId' AS section_id,
worker->>'userId' as worker_id,
work_station->>'sectionName' as section_name,
worker->>'userName' as worker_name,
yield_result,
yield
FROM manufacture."reward_summary"
WHERE company_id=? and org_id=?
and record_date_str=?
)
SELECT t2.record_date_str,t2.section_name,t2.worker_name,
t2.yield_result,t2.yield,t1.yield_max
FROM t2
JOIN t1 on t2.section_id=t1.section_id AND t2.worker_id=t2.worker_id`
tx := d.transactionContext.PgTx
var result []RewardSummaryYield
params := []interface{}{
companyId, orgId,
companyId, orgId, recordDate,
}
_, err := tx.Query(&result, sqlStr, params...)
for i := range result {
switch result[i].YieldResult {
case "1":
result[i].YieldResult = "功"
case "0":
result[i].YieldResult = "不奖不惩"
case "-1":
result[i].YieldResult = "过"
}
}
return result, err
}
// 功过看板-合格率
type RewardSummaryUpToStandard struct {
RecordDateStr string `json:"record_date_str"`
SectionName string `json:"sectionName"` //工段名称
WorkerName string `json:"workerName"` //员工名称
UpToStandard string `json:"upToStandard"` //合格率
UpToStandardResult string `json:"upToStandardResult"` //合格率功过结果
UpToStandardMax string `json:"upToStandardMax"` //合格率历史最佳
}
// 功过看板-合格率
func (d *RewardSumaryDao) SeachRewardUpToStandard(companyId int, orgId int, recordDate string) ([]RewardSummaryUpToStandard, error) {
// sqlStr := `SELECT
// record_date_str,
// work_station->>'sectionName' as section_name,
// worker->>'userName' as worker_name,
// up_to_standard_result,
// up_to_standard,
// '90' as up_to_standard_max
// FROM manufacture."reward_summary"
// WHERE company_id=23
// and org_id=487
// and record_date_str='2022-10-29'`
sqlStr := `with
t1 as( SELECT
max(up_to_standard) as up_to_standard_max,
work_station->>'sectionId' AS section_id,
worker->>'userId' as worker_id
FROM manufacture."reward_summary"
WHERE company_id=? and org_id=?
GROUP BY section_id,worker_id
),
t2 as (SELECT
record_date_str,
work_station->>'sectionId' AS section_id,
worker->>'userId' as worker_id,
work_station->>'sectionName' as section_name,
worker->>'userName' as worker_name,
up_to_standard_result,
up_to_standard
FROM manufacture."reward_summary"
WHERE company_id=? and org_id=?
and record_date_str=?
)
SELECT t2.record_date_str,t2.section_name,t2.worker_name,
t2.up_to_standard_result,t2.up_to_standard,t1.up_to_standard_max
FROM t2
JOIN t1 on t2.section_id=t1.section_id AND t2.worker_id=t2.worker_id`
tx := d.transactionContext.PgTx
var result []RewardSummaryUpToStandard
params := []interface{}{
companyId, orgId,
companyId, orgId, recordDate,
}
_, err := tx.Query(&result, sqlStr, params...)
for i := range result {
switch result[i].UpToStandardResult {
case "1":
result[i].UpToStandardResult = "功"
case "0":
result[i].UpToStandardResult = "不奖不惩"
case "-1":
result[i].UpToStandardResult = "过"
}
}
return result, err
}
// 功过看板-异常
type RewardAccident struct {
Id string `json:"id"`
RecordDateStr string `json:"record_date_str"`
SectionName string `json:"sectionName"` //工段名称
WorkerName string `json:"workerName"` //员工名称
AccidentAmount1 string `json:"accidentAmount1"` //质量事故损失金额
AccidentNum1 string `json:"accidentNum1"` //质量事故次数
AccidentResult1 string `json:"accidentResult1"` //质量事故功过结果
AccidentAmount2 string `json:"accidentAmount2"` //安全事故 损失金额
AccidentResult2 string `json:"accidentResul2"` //安全事故功过结果
AccidentNum2 string `json:"accidentNum2"` //安全事故 次数
AccidentNum3 string `json:"accidentNum3"` //金属异物 次数
AccidentNum4 string `json:"accidentNum4"` //非属异物事故次数
AccidentResult3 string `json:"accidentResul3"` //异物 功过结果
}
// 功过看板-异常
func (d *RewardSumaryDao) SeachRewardAccident(companyId int, orgId int, recordDate string) ([]RewardAccident, error) {
sqlStr := `SELECT
"id",record_date_str,
work_station->>'sectionName' as section_name,
worker->>'userName' as worker_name,
accident_num1,accident_amount1,accident_result1,
accident_num2,accident_amount2,accident_result2,
accident_num3,accident_num4,accident_result3
FROM manufacture."reward_summary"
WHERE company_id=?
and org_id=?
and record_date_str=?`
tx := d.transactionContext.PgTx
var result []RewardAccident
_, err := tx.Query(&result, sqlStr, companyId, orgId, recordDate)
for i := range result {
switch result[i].AccidentResult1 {
case "1":
result[i].AccidentResult1 = "功"
case "0":
result[i].AccidentResult1 = "不奖不惩"
case "-1":
result[i].AccidentResult1 = "过"
}
switch result[i].AccidentResult2 {
case "1":
result[i].AccidentResult2 = "功"
case "0":
result[i].AccidentResult2 = "不奖不惩"
case "-1":
result[i].AccidentResult2 = "过"
}
switch result[i].AccidentResult3 {
case "1":
result[i].AccidentResult3 = "功"
case "0":
result[i].AccidentResult3 = "不奖不惩"
case "-1":
result[i].AccidentResult3 = "过"
}
}
return result, err
}
// 功过看板-月榜
type RewardSummaryMonth struct {
SectionId string `json:"sectionId"` //工段名称
SectionName string `json:"sectionName"` //工段名称
WorkerName string `json:"workerName"` //员工名称
WorkerId string `json:"workerId"` //员工名称
AccidentAmount1 string `json:"accidentAmount1"` //质量事故损失金额
AccidentNum1 string `json:"accidentNum1"` //质量事故次数
AccidentNum3 string `json:"accidentNum3"` //金属异物事故 次数
AccidentNum4 string `json:"accidentNum4"` //非属异物事故 次数
AccidentAmount2 string `json:"accidentAmount2"` //安全事故 损失金额
AccidentNum2 string `json:"accidentNum2"` //安全事故 次数
YieldAvg string `json:"yieldAvg"` //平均产效
YieldMax string `json:"yieldMax"` //产效历史最佳
UpToStandard string `json:"upToStandard"` //合格率
AmountFine string `json:"amountFine"` //惩,的金额
AmountReward string `json:"amountReward"` //奖,的金额
AmountFinal string `json:"amountFinal"` //最终的 奖惩结果
}
// 功过看板-月榜
func (d *RewardSumaryDao) SeachRewardSummaryMonth(companyId int, orgId int, recordMonth string) ([]RewardSummaryMonth, error) {
sqlStr := `
SELECT
work_station->>'sectionId' AS section_id,
work_station->>'sectionName' as section_name,
worker->>'userId' as worker_id,
worker->>'userName' as worker_name,
sum(accident_num1) as accident_num1,
sum(accident_num2) as accident_num2,
sum(accident_num3) as accident_num3,
sum(accident_num4) as accident_num4,
sum(accident_amount1) as accident_amount1,
sum(accident_amount2) as accident_amount2,
CAST(sum(summary_result) as decimal(10,2)) as amount_final,
CAST(
sum(CASE WHEN summary_result<0 THEN summary_result ELSE 0 END) as decimal(10,2)
) as amount_fine,
CAST(
sum(CASE WHEN summary_result>0 THEN summary_result ELSE 0 END) as decimal(10,2)
) as amount_reward,
max(yield) as yield_max,
CAST(avg(yield) as decimal(10,2)) as yield_avg,
CAST(avg(up_to_standard) as decimal(10,2) ) as up_to_standard
FROM manufacture.reward_summary
WHERE company_id=?
and org_id=?
and record_month_str=?
GROUP BY section_id,worker_id,worker_name,section_name
;
`
tx := d.transactionContext.PgTx
var result []RewardSummaryMonth
_, err := tx.Query(&result, sqlStr, companyId, orgId, recordMonth)
return result, err
}