...
|
...
|
@@ -102,42 +102,22 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 不存在现金池
|
|
|
if count == 0 { // 新增现金池
|
|
|
newCashPool := &domain.CashPool{
|
|
|
CompanyId: createCashPoolCommand.CompanyId,
|
|
|
Cash: createCashPoolCommand.Cash,
|
|
|
ExchangedCash: 0,
|
|
|
UnExchangeCash: createCashPoolCommand.Cash,
|
|
|
ExchangedSuMoney: 0,
|
|
|
UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
LastRate: 0,
|
|
|
Rate: 0,
|
|
|
CreateTime: time.Now().Local(),
|
|
|
}
|
|
|
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
|
|
|
cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
|
|
|
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
|
|
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
|
|
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
|
|
return cashPool, nil
|
|
|
}
|
|
|
} else { // 更新现金池
|
|
|
if len(cashPools) > 0 { // 更新现金池
|
|
|
cashPoolExchangeCash := cashPools[0].ExchangedCash
|
|
|
cashPoolUnExchangeCash := cashPools[0].UnExchangeCash
|
|
|
cashPoolCash := cashPools[0].Cash
|
|
|
|
|
|
if createCashPoolCommand.Cash < cashPoolExchangeCash {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
|
|
|
}
|
|
|
// 重新计算平均兑换汇率
|
|
|
newRate := cashPoolExchangeCash / systemExchangedSuMoney
|
|
|
|
|
|
// 计算平均兑换汇率
|
|
|
var newRate float64
|
|
|
if systemExchangedSuMoney == 0 {
|
|
|
newRate = 0
|
|
|
} else {
|
|
|
newRate = cashPoolExchangeCash / systemExchangedSuMoney
|
|
|
}
|
|
|
|
|
|
// 获取上次兑换活动兑换汇率查询
|
|
|
var lastActivityRate float64
|
...
|
...
|
@@ -147,10 +127,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
"nearest": true,
|
|
|
"limit": 1,
|
|
|
}
|
|
|
if count, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
|
|
|
if _, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if count > 1 {
|
|
|
if len(activities) > 0 {
|
|
|
lastActivityRate = activities[0].Rate
|
|
|
} else { // 未查询到相关兑换活动
|
|
|
lastActivityRate = 0
|
...
|
...
|
@@ -162,7 +142,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
CashPoolId: cashPools[0].CashPoolId,
|
|
|
Cash: createCashPoolCommand.Cash,
|
|
|
ExchangedCash: cashPoolExchangeCash,
|
|
|
UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash),
|
|
|
UnExchangeCash: cashPoolUnExchangeCash + (createCashPoolCommand.Cash - cashPoolCash),
|
|
|
ExchangedSuMoney: systemExchangedSuMoney,
|
|
|
UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
Rate: newRate,
|
...
|
...
|
@@ -186,6 +166,32 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
|
|
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
|
|
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
|
|
cashPool.LastRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.LastRate), 64)
|
|
|
return cashPool, nil
|
|
|
}
|
|
|
} else { // 新增现金池
|
|
|
newCashPool := &domain.CashPool{
|
|
|
CompanyId: createCashPoolCommand.CompanyId,
|
|
|
Cash: createCashPoolCommand.Cash,
|
|
|
ExchangedCash: 0,
|
|
|
UnExchangeCash: createCashPoolCommand.Cash,
|
|
|
ExchangedSuMoney: systemExchangedSuMoney,
|
|
|
UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
LastRate: 0,
|
|
|
Rate: 0,
|
|
|
CreateTime: time.Now().Local(),
|
|
|
}
|
|
|
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.Rate), 64)
|
|
|
cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeCash), 64)
|
|
|
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedSuMoney), 64)
|
|
|
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.UnExchangeSuMoney), 64)
|
|
|
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", cashPool.ExchangedCash), 64)
|
|
|
return cashPool, nil
|
|
|
}
|
|
|
}
|
...
|
...
|
|