作者 陈志颖

refactor:优化现金池

... ... @@ -123,6 +123,10 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
if createCashPoolCommand.Cash < cashPools[0].ExchangedCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
}
// 重新计算平均兑换汇率
newRate := cashPools[0].ExchangedCash / systemExchangedSuMoney
// 更新现金池
newCashPool := &domain.CashPool{
CashPoolId: cashPools[0].CashPoolId,
CompanyId: createCashPoolCommand.CompanyId,
... ... @@ -131,10 +135,11 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
UnExchangeCash: cashPools[0].UnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash),
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: cashPools[0].Rate,
Rate: newRate,
LastRate: cashPools[0].LastRate,
CreateTime: time.Now(),
}
// 保存现金池更新
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -500,6 +505,10 @@ func (cashPoolService *CashPoolService) SearchExchangeCashActivity(activityComma
activitiesFormat = append(activitiesFormat, activityFormat)
}
if len(activitiesFormat) == 0 {
activitiesFormat = []interface{}{}
}
return map[string]interface{}{
"count": count,
"activities": activitiesFormat,
... ... @@ -594,6 +603,11 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
activitiesWithTs = append(activitiesWithTs, activityWithTs)
}
}
if len(activitiesWithTs) == 0 {
activitiesWithTs = []interface{}{}
}
return map[string]interface{}{
"count": count,
"activities": activitiesWithTs,
... ... @@ -787,7 +801,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
if updateExchangeCashActivityCommand.ExchangeRate != 0 { // 更新兑换活动兑换汇率
// 获取当前兑换活动汇率
activityFoundRate := activityFound.Rate
if updateExchangeCashActivityCommand.ExchangeRate != activityFoundRate && updateExchangeCashActivityCommand.ExchangeRate != 0 { // 更新兑换活动兑换汇率
// 获取当前公司现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
... ... @@ -886,7 +903,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
// 计算倒计时
var t1 = time.Now()
var t2 = activityFound.Deadline
updateExchangeCashActivityCommand.CountDown = int64(t2.Sub(t1).Hours() / 24)
updateExchangeCashActivityCommand.CountDown = int64(math.Ceil(t2.Sub(t1).Hours() / 24))
// 倒计时结束处理
if t2.Before(t1) {
... ...