pg_employee_dao.go
7.2 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
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) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
var expendSuMoney float64 // 消耗的素币(1:兑换(物资、现金),4: 扣除)
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) CalculateSystemSuMoney(companyId int64) ( float64, float64, 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).
Select(&systemUnExchangeSuMoney); err != nil {
return 0, 0, err
}
exchangeCashActivityModel := new(models.ExchangeCashActivity)
if err := tx.Model(exchangeCashActivityModel).
ColumnExpr("sum(exchange_cash_activity.exchanged_su_money) AS system_changed_su_money").
Where("exchange_cash_activity.company_id = ?", companyId).
Select(&systemExchangedSuMoney); err != nil {
return 0, 0, err
}
return systemUnExchangeSuMoney, systemExchangedSuMoney, nil
}
// 计算系统已兑换现金
func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (float64, error) {
var systemExchangedCash float64 // 系统已兑换现金
tx := dao.transactionContext.PgTx
exchangeCashActivityModel := new(models.ExchangeCashActivity)
if err := tx.Model(exchangeCashActivityModel).
ColumnExpr("sum(exchange_cash_activity.exchanged_cash) AS system_exchanged_cash").
Where("exchange_cash_activity.company_id = ?", companyId).
Select(&systemExchangedCash) ; err != nil {
return 0, err
}
return systemExchangedCash, nil
}
func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &EmployeeDao{
transactionContext: transactionContext,
}, nil
}
}