|
@@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
@@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
102
|
if err != nil {
|
102
|
if err != nil {
|
103
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
103
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
104
|
}
|
104
|
}
|
105
|
-
|
|
|
106
|
- // 不存在现金池
|
|
|
107
|
- if count == 0 { // 新增现金池
|
|
|
108
|
- newCashPool := &domain.CashPool{
|
|
|
109
|
- CompanyId: createCashPoolCommand.CompanyId,
|
|
|
110
|
- Cash: createCashPoolCommand.Cash,
|
|
|
111
|
- ExchangedCash: 0,
|
|
|
112
|
- UnExchangeCash: createCashPoolCommand.Cash,
|
|
|
113
|
- ExchangedSuMoney: 0,
|
|
|
114
|
- UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
115
|
- LastRate: 0,
|
|
|
116
|
- Rate: 0,
|
|
|
117
|
- CreateTime: time.Now().Local(),
|
|
|
118
|
- }
|
|
|
119
|
- if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
120
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
121
|
- } else {
|
|
|
122
|
- if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
123
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
124
|
- }
|
|
|
125
|
- cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
|
|
|
126
|
- cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
|
|
|
127
|
- cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
|
|
128
|
- cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
|
|
129
|
- cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
|
|
130
|
- return cashPool, nil
|
|
|
131
|
- }
|
|
|
132
|
- } else { // 更新现金池
|
105
|
+ if len(cashPools) > 0 { // 更新现金池
|
133
|
cashPoolExchangeCash := cashPools[0].ExchangedCash
|
106
|
cashPoolExchangeCash := cashPools[0].ExchangedCash
|
134
|
cashPoolUnExchangeCash := cashPools[0].UnExchangeCash
|
107
|
cashPoolUnExchangeCash := cashPools[0].UnExchangeCash
|
|
|
108
|
+ cashPoolCash := cashPools[0].Cash
|
135
|
|
109
|
|
136
|
if createCashPoolCommand.Cash < cashPoolExchangeCash {
|
110
|
if createCashPoolCommand.Cash < cashPoolExchangeCash {
|
137
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
|
111
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
|
138
|
}
|
112
|
}
|
139
|
- // 重新计算平均兑换汇率
|
|
|
140
|
- newRate := cashPoolExchangeCash / systemExchangedSuMoney
|
113
|
+
|
|
|
114
|
+ // 计算平均兑换汇率
|
|
|
115
|
+ var newRate float64
|
|
|
116
|
+ if systemExchangedSuMoney == 0 {
|
|
|
117
|
+ newRate = 0
|
|
|
118
|
+ } else {
|
|
|
119
|
+ newRate = cashPoolExchangeCash / systemExchangedSuMoney
|
|
|
120
|
+ }
|
141
|
|
121
|
|
142
|
// 获取上次兑换活动兑换汇率查询
|
122
|
// 获取上次兑换活动兑换汇率查询
|
143
|
var lastActivityRate float64
|
123
|
var lastActivityRate float64
|
|
@@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
@@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
147
|
"nearest": true,
|
127
|
"nearest": true,
|
148
|
"limit": 1,
|
128
|
"limit": 1,
|
149
|
}
|
129
|
}
|
150
|
- if count, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
|
130
|
+ if _, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
|
151
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
131
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
152
|
} else {
|
132
|
} else {
|
153
|
- if count > 1 {
|
133
|
+ if len(activities) > 0 {
|
154
|
lastActivityRate = activities[0].Rate
|
134
|
lastActivityRate = activities[0].Rate
|
155
|
} else { // 未查询到相关兑换活动
|
135
|
} else { // 未查询到相关兑换活动
|
156
|
lastActivityRate = 0
|
136
|
lastActivityRate = 0
|
|
@@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
@@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
162
|
CashPoolId: cashPools[0].CashPoolId,
|
142
|
CashPoolId: cashPools[0].CashPoolId,
|
163
|
Cash: createCashPoolCommand.Cash,
|
143
|
Cash: createCashPoolCommand.Cash,
|
164
|
ExchangedCash: cashPoolExchangeCash,
|
144
|
ExchangedCash: cashPoolExchangeCash,
|
165
|
- UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash),
|
145
|
+ UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPoolCash),
|
166
|
ExchangedSuMoney: systemExchangedSuMoney,
|
146
|
ExchangedSuMoney: systemExchangedSuMoney,
|
167
|
UnExchangeSuMoney: systemUnExchangeSuMoney,
|
147
|
UnExchangeSuMoney: systemUnExchangeSuMoney,
|
168
|
Rate: newRate,
|
148
|
Rate: newRate,
|
|
@@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
@@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
186
|
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
166
|
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
187
|
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
167
|
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
188
|
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
168
|
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
|
|
169
|
+ cashPool.LastRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.LastRate), 64)
|
|
|
170
|
+ return cashPool, nil
|
|
|
171
|
+ }
|
|
|
172
|
+ } else { // 新增现金池
|
|
|
173
|
+ newCashPool := &domain.CashPool{
|
|
|
174
|
+ CompanyId: createCashPoolCommand.CompanyId,
|
|
|
175
|
+ Cash: createCashPoolCommand.Cash,
|
|
|
176
|
+ ExchangedCash: 0,
|
|
|
177
|
+ UnExchangeCash: createCashPoolCommand.Cash,
|
|
|
178
|
+ ExchangedSuMoney: systemExchangedSuMoney,
|
|
|
179
|
+ UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
180
|
+ LastRate: 0,
|
|
|
181
|
+ Rate: 0,
|
|
|
182
|
+ CreateTime: time.Now().Local(),
|
|
|
183
|
+ }
|
|
|
184
|
+ if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
185
|
+ return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
186
|
+ } else {
|
|
|
187
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
188
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
189
|
+ }
|
|
|
190
|
+ cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
|
|
|
191
|
+ cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
|
|
|
192
|
+ cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
|
|
193
|
+ cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
|
|
194
|
+ cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
189
|
return cashPool, nil
|
195
|
return cashPool, nil
|
190
|
}
|
196
|
}
|
191
|
}
|
197
|
}
|