pg_cash_pool_dao.go
8.8 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
package dao
import (
"fmt"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
)
type CashPoolDao struct {
transactionContext *pgTransaction.TransactionContext
}
// 兑换活动兑换清单已兑换素币、已兑换现金
func (dao *CashPoolDao) CalculateActivityExchangedSuMoney(activityId int64) (map[string]interface{}, error) {
var activityExchangedSuMoney float64
var activityExchangedCash float64
tx := dao.transactionContext.PgTx
exchangeCashPersonListModels := new(models.ExchangeCashPersonList)
if err := tx.Model(exchangeCashPersonListModels).
ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS activity_exchanged_su_money").
Where("exchange_cash_person_list.activity_id = ?", activityId).
Select(&activityExchangedSuMoney); err != nil {
return nil, err
}
if err := tx.Model(exchangeCashPersonListModels).
ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS activity_exchanged_cash").
Where("exchange_cash_person_list.activity_id = ?", activityId).
Select(&activityExchangedCash); err != nil {
return nil, err
}
return map[string]interface{}{
"activityExchangedSuMoney": activityExchangedSuMoney,
"activityExchangedCash": activityExchangedCash,
}, nil
}
// ExchangeCashPersonListExist 判断素币兑换清单是否存在
func (dao *CashPoolDao) ExchangeCashPersonListExist(oldPhone string) (bool, error) {
tx := dao.transactionContext.PgTx
m := models.ExchangeCashPersonList{}
query := tx.Model(&m).Where("employee_account=?", oldPhone)
ok, err := query.Exists()
return ok, err
}
// UpdateExchangeCashPersonListUserInfo 更新素币兑换清单用户信息
func (dao *CashPoolDao) UpdateExchangeCashPersonListUserInfo(oldPhone string, newPhone string) error {
tx := dao.transactionContext.PgTx
if _, err := tx.Query(
pg.Scan(),
"UPDATE exchange_cash_person_lists SET employee_account = ? WHERE employee_account = ?",
newPhone, oldPhone); err != nil {
return err
}
return nil
}
// 返回兑换活动清单榜单
func (dao *CashPoolDao) ExchangeCashListRanking(queryOptions map[string]interface{}) (map[string]interface{}, error) {
var retPeople []struct {
Uid int64
EmployeeName string
SuMoney float64
Cash float64
Ranking int
}
var retEmployee []struct {
Uid int64
EmployeeName string
SuMoney float64
Cash float64
Ranking int
}
tx := dao.transactionContext.PgTx
exchangeCashPersonListModels := new(models.ExchangeCashPersonList)
// 清单人员排名
queryPeople := tx.Model(exchangeCashPersonListModels)
queryPeople = queryPeople.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.uid AS uid")
//queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
queryPeople = queryPeople.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC, e.create_time ASC) AS ranking")
//queryPeople = queryPeople.Where("e.status = ?", 1)
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryPeople = queryPeople.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryPeople = queryPeople.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
queryPeople = queryPeople.Group("exchange_cash_person_list.uid")
queryPeople = queryPeople.Group("e.create_time")
//queryPeople = queryPeople.Group("exchange_cash_person_list.employee_name")
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
queryPeople = queryPeople.Offset(offset)
}
} else {
queryPeople = queryPeople.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
queryPeople = queryPeople.Limit(limit)
}
} else {
queryPeople = queryPeople.Limit(20)
}
if err := queryPeople.Order("ranking ASC").Select(&retPeople); err != nil {
return nil, err
}
// 个人清单排名
queryPeopleAll := tx.Model(exchangeCashPersonListModels)
queryPeopleAll = queryPeopleAll.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryPeopleAll = queryPeopleAll.ColumnExpr("exchange_cash_person_list.uid AS uid")
//queryPeopleAll = queryPeopleAll.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
queryPeopleAll = queryPeopleAll.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
queryPeopleAll = queryPeopleAll.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
queryPeopleAll = queryPeopleAll.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC, e.create_time ASC) AS ranking")
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryPeopleAll = queryPeopleAll.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryPeopleAll = queryPeopleAll.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
queryPeopleAll = queryPeopleAll.Group("exchange_cash_person_list.uid")
queryPeopleAll = queryPeopleAll.Group("e.create_time")
//queryPeopleAll = queryPeopleAll.Group("exchange_cash_person_list.employee_name")
queryPeopleAllWith := queryPeopleAll.Order("su_money DESC")
queryEmployee := tx.Model()
queryEmployee = queryEmployee.With("t", queryPeopleAllWith)
queryEmployee = queryEmployee.Table("t")
queryEmployee = queryEmployee.ColumnExpr("t.uid AS uid")
//queryEmployee = queryEmployee.ColumnExpr("t.employee_name AS employee_name")
queryEmployee = queryEmployee.ColumnExpr("t.cash AS cash")
queryEmployee = queryEmployee.ColumnExpr("t.su_money AS su_money")
queryEmployee = queryEmployee.ColumnExpr("t.ranking AS ranking")
if uid, ok := queryOptions["uid"]; ok {
queryEmployee = queryEmployee.Where("t.uid::bigint = ?", uid)
}
if err := queryEmployee.Select(&retEmployee); err != nil {
return nil, err
}
var currentEmployee interface{}
if len(retEmployee) == 0 {
currentEmployee = nil
} else {
currentEmployee = retEmployee[0]
}
// 清单已兑换素币
var activityExchangedSuMoney float64
queryListSuMoney := tx.Model(exchangeCashPersonListModels)
queryListSuMoney = queryListSuMoney.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryListSuMoney = queryListSuMoney.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS activity_exchanged_su_money")
//queryListSuMoney = queryListSuMoney.Where("e.status = ?", 1)
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryListSuMoney = queryListSuMoney.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryListSuMoney = queryListSuMoney.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if err := queryListSuMoney.Select(&activityExchangedSuMoney); err != nil {
return nil, err
}
// 清单已兑换现金
var activityExchangedCash float64
queryListCash := tx.Model(exchangeCashPersonListModels)
queryListCash = queryListCash.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
queryListCash = queryListCash.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS activity_exchanged_cash")
//queryListCash = queryListCash.Where("e.status = ?", 1)
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryListCash = queryListCash.Where("e.company_id = ?", companyId)
}
if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
queryListCash = queryListCash.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if err := queryListCash.Select(&activityExchangedCash); err != nil {
return nil, err
}
// 清单计数
queryCount := tx.Model(exchangeCashPersonListModels)
count, err := queryCount.Count()
if err != nil {
return nil, err
}
return map[string]interface{}{
"people": retPeople, // 员工排行榜
"count": count, // 计数
"currentEmployee": currentEmployee, // 当前员工排名
"exchangedSuMoney": activityExchangedSuMoney, // 清单已兑换素币
"exchangedCash": activityExchangedCash, // 清单已兑换现金
}, nil
}
func NewCashPoolDao(transactionContext *pgTransaction.TransactionContext) (*CashPoolDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CashPoolDao{
transactionContext: transactionContext,
}, nil
}
}