作者 陈志颖

fix:更新兑换清单时增加更新现金池操作

... ... @@ -666,7 +666,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 判断清单中现金总额超过平台未兑换现金
// 获取兑换清单素币状况
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -683,7 +683,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 现金池操作
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -692,6 +692,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
employeeDao = value
}
// 获取平台现金状况
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -701,9 +702,11 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 判断兑换的现金是否超过现金池未兑换现金
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -731,7 +734,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if cashPools == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId)))
}
// 平均兑换汇率
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
... ... @@ -869,7 +872,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 生成素币流水,还原个人素币值
// 还原个人素币值,生成素币流水
var operationSuMoneyService service.OperationSuMoneyService
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -923,10 +926,77 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
// TODO 更新现金池
// 更新现金池
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
// 获取平台现金状况
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 更新现金池
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 获取现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPools == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: newRate,
}
if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ... @@ -1035,10 +1105,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// TODO 更新现金池
// 生成素币兑换流水记录,更新员工素币
// 更新员工素币,生成素币兑换流水记录
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: person.EmployeeInfo.Uid,
Operator: updateExchangeCashPersonCommand.Operator,
... ... @@ -1060,14 +1127,77 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
personUpdated, err := exchangeCashPersonListRepository.Save(person)
// 更新现金池
// 获取平台现金状况
newSystemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := newSystemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := newSystemCashStatistics["systemUnExchangeCash"].(float64)
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 更新现金池
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 获取现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPools == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: newRate,
}
if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if personUpdated, err := exchangeCashPersonListRepository.Save(person);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())
}
return personUpdated, nil
}
return personUpdated, nil
}
func NewCashPoolService(options map[string]interface{}) *CashPoolService {
... ...