pg_employee_dao.go
30.3 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package dao
import (
"fmt"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
"time"
)
type EmployeeDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (dao *EmployeeDao) BatchRemove(uids []int64) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"DELETE FROM employees WHERE uid IN (?)",
pg.In(uids))
return err
}
func (dao *EmployeeDao) BatchSetStatus(uids []int64, status int) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"UPDATE employees SET status=? WHERE uid IN (?)",
status, pg.In(uids))
return err
}
func (dao *EmployeeDao) ChangePrincipal(companyId int64, employeeAccount string) error {
tx := dao.transactionContext.PgTx
if _, err := tx.Query(
pg.Scan(),
"UPDATE employees SET is_principal=? WHERE company_id=?",
false, companyId); err != nil {
return err
}
if _, err := tx.QueryOne(
pg.Scan(),
"UPDATE employees SET is_principal=? WHERE company_id=? AND employee_account=?",
true, companyId, employeeAccount); err != nil {
return err
}
return nil
}
func (dao *EmployeeDao) TransferSuMoney(uid int64, suMoney float64) error {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"UPDATE employees SET su_money=su_money+? WHERE uid=?",
suMoney, uid)
return err
}
// 计算个人未读消息
func (dao *EmployeeDao) CalculatePersonUnReadNotification(uid int64) (map[string]int, error) {
var unReadSystemNotification int
var unReadInteractionNotification int
tx := dao.transactionContext.PgTx
sentNotificationModel := new(models.SentNotification)
if count, err := tx.Model(sentNotificationModel).Relation("Notification").
Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_SYSTEM).
Where("sent_notification.is_read = ?", false).
Count(); err != nil {
return nil, err
} else {
unReadSystemNotification = count
}
if count, err := tx.Model(sentNotificationModel).Relation("Notification").
Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_INTERACTION).
Where("sent_notification.is_read = ?", false).
Count(); err != nil {
return nil, err
} else {
unReadInteractionNotification = count
}
return map[string]int{
"unReadSystemNotification": unReadSystemNotification,
"unReadInteractionNotification": unReadInteractionNotification,
}, nil
}
// 计算个人素币情况
func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{}, error) {
var incomeSuMoney float64
var incomeSuMoneyOfYesterday float64
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
yesterday := time.Now().AddDate(0, 0, -1)
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type = ?`, 2).
Where(`su_money_transaction_record.create_time > ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())).
Where(`su_money_transaction_record.create_time < ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, yesterday.Location())).
Select(&incomeSuMoneyOfYesterday); err != nil {
return nil, err
}
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type = ?`, 2).
Select(&incomeSuMoney); err != nil {
return nil, err
}
return map[string]interface{}{
"incomeSuMoney": incomeSuMoney,
"incomeSuMoneyOfYesterday": incomeSuMoneyOfYesterday,
}, nil
}
// 系统已兑换素币、未兑换素币
func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] interface{}, error) {
var systemUnExchangeSuMoney float64
var systemExchangedSuMoney float64
tx := dao.transactionContext.PgTx
// 系统未兑换素币
employeeModel := new(models.Employee)
if err := tx.Model(employeeModel).
ColumnExpr("sum(employee.su_money) AS system_unExchange_su_money").
Where("employee.company_id = ?", companyId).
Where("employee.status = ?", 1).
Select(&systemUnExchangeSuMoney); err != nil {
return nil, err
}
// 系统已兑换现金素币
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("sum(su_money_transaction_record.su_money) AS system_changed_su_money").
Where("e.company_id = ?", companyId).
Where("e.status = ?", 1).
Where(`su_money_transaction_record.record_type = ?`, 5).
Select(&systemExchangedSuMoney); err != nil {
return nil, err
}
return map[string] interface{} {
"systemUnExchangeSuMoney": systemUnExchangeSuMoney,
"systemExchangedSuMoney": systemExchangedSuMoney,
},nil
}
// 计算系统已兑换现金、未兑换现金
func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interface{}, error) {
tx := dao.transactionContext.PgTx
var (
systemUnExchangeCash float64
systemExchangedCash float64
)
// 系统未兑换现金
cashPool := new(models.CashPool)
if err := tx.Model(cashPool).
Column("exchanged_cash").
Where("cash_pool.company_id = ?", companyId).
Order("id DESC").
Limit(1).
Select(&systemExchangedCash) ; err != nil {
return nil, err
}
if err := tx.Model(cashPool).
Column("un_exchange_cash").
Where("cash_pool.company_id = ?", companyId).
Order("id DESC").
Limit(1).
Select(&systemUnExchangeCash) ; err != nil {
return nil, err
}
return map[string] interface{} {
"systemUnExchangeCash": systemUnExchangeCash,
"systemExchangedCash": systemExchangedCash,
}, nil
}
// 个人素币事务统计
func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
var expendSuMoney float64 // 消耗的素币(1:兑换物资,4:扣除, 5: 兑换现金)
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
Select(&incomeSuMoney); err != nil {
return nil, err
}
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS expend_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{1, 4})).
Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
Select(&expendSuMoney); err != nil {
return nil, err
}
return map[string]interface{}{
"incomeSuMoney": incomeSuMoney,
"expendSuMoney": expendSuMoney,
}, nil
}
// 员工财富值统计
func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var ret []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
Where(`e.company_id = ?`, companyId).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employee_su_money DESC").
Select(&ret); err != nil {
return nil, err
}
return map[string]interface{}{
"employeesSuMoney": ret,
}, nil
}
//func (dao *EmployeeDao) CalculateEmployeesSuMoney(queryOptions map[string]interface{}) (map[string]interface{}, error) {
// var ret []struct {
// Uid int
// EmployeeName string
// EmployeeSuMoney float64
// }
// tx := dao.transactionContext.PgTx
// var (
// startTime time.Time
// endTime time.Time
// companyId int
// )
// if company, ok := queryOptions["companyId"].(int); ok {
// companyId = company
// }
// if start, ok := queryOptions["startTime"].(time.Time); ok {
// startTime = start
// } else {
// startTime = time.Date(1971, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location())
// }
// if end, ok := queryOptions["endTime"].(time.Time); ok {
// endTime = end
// } else {
// endTime = time.Now()
// }
// fmt.Print(startTime, "\n")
// fmt.Print(endTime, "\n")
// fmt.Print(companyId, "\n")
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
// ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
// ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
// ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
// Where(`e.company_id = ?`, companyId).
// Where(`e.status = ?`, 1).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
// Where(`su_money_transaction_record.create_time > ?`, startTime).
// Where(`su_money_transaction_record.create_time < ?`, endTime).
// Group("su_money_transaction_record.employee").
// Order("employee_su_money DESC").
// Select(&ret); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "employeesSuMoney": ret,
// }, nil
//}
// 贡献值、财富值总榜和年榜
func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retEmployeeWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
var retEmployeeContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// 财富值榜单
queryWealth := tx.Model(suMoneyTransactionRecordModel)
queryWealth = queryWealth.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryWealth = queryWealth.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryWealth = queryWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
queryWealth = queryWealth.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
queryWealth = queryWealth.Where(`e.status = ?`, 1)
queryWealth = queryWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryWealth = queryWealth.Where("e.company_id = ?", companyId)
}
if startTime, ok := queryOptions["startTime"]; ok {
queryWealth = queryWealth.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryWealth = queryWealth.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
queryWealth = queryWealth.Group("su_money_transaction_record.employee")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryWealth = queryWealth.Offset(offset)
}
} else {
queryWealth = queryWealth.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryWealth = queryWealth.Limit(limit)
}
} else {
queryWealth = queryWealth.Limit(20)
}
if err := queryWealth.Order("employee_su_money DESC").Select(&retWealth); err != nil {
return nil, err
}
// 当前员工财富值排名
queryEmployeeWealth := tx.Model(suMoneyTransactionRecordModel)
queryEmployeeWealth = queryEmployeeWealth.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
queryEmployeeWealth = queryEmployeeWealth.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
queryEmployeeWealth = queryEmployeeWealth.Where(`e.status = ?`, 1)
queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryEmployeeWealth = queryEmployeeWealth.Where("e.company_id = ?", companyId)
}
if startTime, ok := queryOptions["startTime"]; ok {
queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
if uid, ok := queryOptions["uid"]; ok {
queryEmployeeWealth = queryEmployeeWealth.Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid)
}
queryEmployeeWealth = queryEmployeeWealth.Group("su_money_transaction_record.employee")
if err := queryEmployeeWealth.Order("employee_su_money DESC").Select(&retEmployeeWealth); err != nil {
return nil, err
}
var retCurrentEmployeeWealth interface{}
if len(retEmployeeWealth) == 0 {
retCurrentEmployeeWealth = nil
} else {
retCurrentEmployeeWealth = retEmployeeWealth[0]
}
// 贡献值榜单
queryContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
queryContributionsDecrease = queryContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_contributions_decrease")
queryContributionsDecrease = queryContributionsDecrease.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryContributionsDecrease = queryContributionsDecrease.Where("e.company_id = ?", companyId)
}
queryContributionsDecrease = queryContributionsDecrease.Where(`e.status = ?`, 1)
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.record_type = ?`, 4)
if startTime, ok := queryOptions["startTime"]; ok {
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryContributionsDecrease = queryContributionsDecrease.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
contributionsDecrease := queryContributionsDecrease.Group("su_money_transaction_record.employee")
//if offset, ok := queryOptions["offset"]; ok {
// offset := offset.(int)
// if offset > -1 {
// queryContributionsDecrease = queryContributionsDecrease.Offset(offset)
// }
//} else {
// queryContributionsDecrease = queryContributionsDecrease.Offset(0)
//}
//if limit, ok := queryOptions["limit"]; ok {
// limit := limit.(int)
// if limit > -1 {
// queryContributionsDecrease = queryContributionsDecrease.Limit(limit)
// }
//} else {
// queryContributionsDecrease = queryContributionsDecrease.Limit(20)
//}
//contributionsDecrease := queryWealth.Order("employee_contributions_decrease DESC")
queryContributions := tx.Model()
queryContributions = queryContributions.With("t", contributionsDecrease)
queryContributions = queryContributions.Table("t")
queryContributions = queryContributions.Table("su_money_transaction_records")
queryContributions = queryContributions.Join("JOIN employees AS e ON e.uid = (su_money_transaction_records.employee->>'uid')::bigint")
queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'uid' AS uid")
queryContributions = queryContributions.ColumnExpr("su_money_transaction_records.employee->>'employeeName' AS employee_name")
queryContributions = queryContributions.ColumnExpr(`(sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease) AS employees_contributions`)
queryContributions = queryContributions.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryContributions = queryContributions.Where("e.company_id = ?", companyId)
}
queryContributions = queryContributions.Where(`e.status = ?`, 1)
queryContributions = queryContributions.Where(`su_money_transaction_records.record_type IN (?)`, pg.In([]int{2, 3}))
if startTime, ok := queryOptions["startTime"]; ok {
queryContributions = queryContributions.Where(`su_money_transaction_records.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryContributions = queryContributions.Where(`su_money_transaction_records.create_time < ?`, endTime)
}
queryContributions = queryContributions.Group("su_money_transaction_records.employee")
queryContributions = queryContributions.Group("t.employee_contributions_decrease")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryContributions = queryContributions.Offset(offset)
}
} else {
queryContributions = queryContributions.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryContributions = queryContributions.Limit(limit)
}
} else {
queryContributions = queryContributions.Limit(20)
}
if err := queryContributions.Order("employees_contributions DESC").Select(&retContributions); err != nil {
return nil, err
}
// 当前员工贡献值排名
queryEmployeeContributionsDecrease := tx.Model(suMoneyTransactionRecordModel)
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint")
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid")
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name")
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_contributions_decrease")
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where("e.company_id = ?", companyId)
}
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`e.status = ?`, 1)
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.record_type = ?`, 4)
if startTime, ok := queryOptions["startTime"]; ok {
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryEmployeeContributionsDecrease = queryEmployeeContributionsDecrease.Where(`su_money_transaction_record.create_time < ?`, endTime)
}
employeeContributionsDecrease := queryEmployeeContributionsDecrease.Group("su_money_transaction_record.employee")
queryEmployeeContributions := tx.Model()
queryEmployeeContributions = queryEmployeeContributions.With("t", employeeContributionsDecrease)
queryEmployeeContributions = queryEmployeeContributions.Table("t")
queryEmployeeContributions = queryEmployeeContributions.Table("su_money_transaction_records")
queryEmployeeContributions = queryEmployeeContributions.Join("JOIN employees AS e ON e.uid = (su_money_transaction_records.employee->>'uid')::bigint")
queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("su_money_transaction_records.employee->>'uid' AS uid")
queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("su_money_transaction_records.employee->>'employeeName' AS employee_name")
queryEmployeeContributions = queryEmployeeContributions.ColumnExpr(`(sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease) AS employees_contributions`)
queryEmployeeContributions = queryEmployeeContributions.ColumnExpr("RANK() OVER (ORDER BY sum(su_money_transaction_records.su_money) - t.employee_contributions_decrease DESC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryEmployeeContributions = queryContributions.Where("e.company_id = ?", companyId)
}
queryEmployeeContributions = queryEmployeeContributions.Where(`e.status = ?`, 1)
queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.record_type IN (?)`, pg.In([]int{2, 3}))
if startTime, ok := queryOptions["startTime"]; ok {
queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.create_time > ?`, startTime)
}
if endTime, ok := queryOptions["endTime"]; ok {
queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.create_time < ?`, endTime)
}
if uid, ok := queryOptions["uid"]; ok {
queryEmployeeContributions = queryEmployeeContributions.Where(`su_money_transaction_records.employee @> '{"uid":?}'`, uid)
}
queryEmployeeContributions = queryEmployeeContributions.Group("su_money_transaction_records.employee")
queryEmployeeContributions = queryEmployeeContributions.Group("t.employee_contributions_decrease")
if err := queryEmployeeContributions.Order("employees_contributions DESC").Select(&retEmployeeContributions); err != nil {
return nil, err
}
var retCurrentEmployeeContributions interface{}
if len(retEmployeeContributions) == 0 {
retCurrentEmployeeContributions = nil
} else {
retCurrentEmployeeContributions = retEmployeeContributions[0]
}
return map[string]interface{}{
"employeesWealth": retWealth,
"employeesContributions": retContributions,
"currentEmployeeContributions": retCurrentEmployeeContributions,
"currentEmployeeWealth": retCurrentEmployeeWealth,
}, nil
}
// 排行榜统计,排名(弃用)
func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
var retWealth []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
Ranking int
}
var retContributions []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
var retContributionDecrease []struct { // 员工减少的贡献值
Uid int
EmployeeName string
EmployeesContributions float64
Ranking int
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if limit < -1 {
limit = 20
}
if offset < -1 {
offset = 0
}
// 员工财富值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
Where(`e.company_id = ?`, companyId).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employee_su_money DESC").
Limit(limit).
Offset(offset).
Select(&retWealth); err != nil {
return nil, err
}
// 增加的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Limit(limit).
Offset(offset).
Select(&retContributions); err != nil {
return nil, err
}
// 减少的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type = ?`, 4).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Limit(limit).
Offset(offset).
Select(&retContributionDecrease); err != nil {
return nil, err
}
for i := 0; i < len(retContributions); i++ {
for j := 0; j < len(retContributionDecrease); j++ {
if retContributions[i].Uid == retContributionDecrease[j].Uid {
retContributions[i].EmployeesContributions -= retContributionDecrease[j].EmployeesContributions
}
}
}
return map[string]interface{}{
"employeesContributions": retContributions,
"employeesWealth": retWealth,
}, nil
}
// 员工贡献值统计
func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var ret []struct { // 员工贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
var retDecrease []struct { // 员工减少的贡献值
Uid int
EmployeeName string
EmployeesContributions float64
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// 增加的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Select(&ret); err != nil {
return nil, err
}
// 减少的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
Where(`e.company_id = ?`, companyId).
Where(`su_money_transaction_record.record_type = ?`, 4).
Where(`e.status = ?`, 1).
Where(`su_money_transaction_record.create_time > ?`, startTime).
Where(`su_money_transaction_record.create_time < ?`, endTime).
Group("su_money_transaction_record.employee").
Order("employees_contributions DESC").
Select(&retDecrease); err != nil {
return nil, err
}
for i := 0; i < len(ret); i++ {
for j := 0; j < len(retDecrease); j++ {
if ret[i].Uid == retDecrease[j].Uid {
ret[i].EmployeesContributions -= retDecrease[j].EmployeesContributions
}
}
}
return map[string]interface{}{
"employeesContributions": ret,
}, nil
}
func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &EmployeeDao{
transactionContext: transactionContext,
}, nil
}
}