作者 陈志颖

fix:新增素币兑换清单时增加更新现金池

@@ -373,12 +373,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA @@ -373,12 +373,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA
373 var t2 = time.Date(deadline.Year(), deadline.Month(), deadline.Day(), 0, 0, 0, 0, time.Local) 373 var t2 = time.Date(deadline.Year(), deadline.Month(), deadline.Day(), 0, 0, 0, 0, time.Local)
374 updateExchangeCashActivity := &command.UpdateExchangeCashActivityCommand { 374 updateExchangeCashActivity := &command.UpdateExchangeCashActivityCommand {
375 ExchangeCashActivityId: activity.ActivityId, 375 ExchangeCashActivityId: activity.ActivityId,
376 - //ExchangeActivityName: activity.ExchangeActivityName,  
377 - //Deadline: activity.Deadline,  
378 CountDown: int64(t2.Sub(t1).Hours() / 24), 376 CountDown: int64(t2.Sub(t1).Hours() / 24),
379 - //ExchangedSuMoney: activity.ExchangedSuMoney,  
380 - //ExchangedCash: activity.ExchangedCash,  
381 - //ExchangeRate: activity.Rate,  
382 } 377 }
383 if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivity)); err != nil { 378 if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivity)); err != nil {
384 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 379 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
@@ -485,6 +480,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -485,6 +480,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
485 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPools[0].UnExchangeCash { 480 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPools[0].UnExchangeCash {
486 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金") 481 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
487 } 482 }
  483 + // TODO 批量更新兑换清单中已兑换现金值
  484 +
488 // 计算系统平均兑换汇率并更新现金池 485 // 计算系统平均兑换汇率并更新现金池
489 var employeeDao *dao.EmployeeDao 486 var employeeDao *dao.EmployeeDao
490 if value, err := factory.CreateEmployeeDao(map[string]interface{}{ 487 if value, err := factory.CreateEmployeeDao(map[string]interface{}{
@@ -518,10 +515,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -518,10 +515,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
518 } else { 515 } else {
519 rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64) 516 rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
520 } 517 }
521 - // TODO 更新现金池 518 + // 更新现金池
522 updateCashPoolCommand := &command.UpdateCashPoolCommand{ 519 updateCashPoolCommand := &command.UpdateCashPoolCommand{
523 CashPoolId: cashPools[0].CashPoolId, 520 CashPoolId: cashPools[0].CashPoolId,
524 - ExchangedCash: systemExchangedCash, 521 + ExchangedCash: systemUnExchangeCash,
525 UnExchangeCash: systemUnExchangeCash, 522 UnExchangeCash: systemUnExchangeCash,
526 Rate: rate, 523 Rate: rate,
527 } 524 }
@@ -535,10 +532,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -535,10 +532,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
535 if cashPoolUpdated == nil { 532 if cashPoolUpdated == nil {
536 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 533 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
537 } 534 }
538 - // TODO 批量更新兑换清单中已兑换现金值  
539 -  
540 } 535 }
541 -  
542 if activityUpdated, err := exchangeCashActivityRepository.Save(activity); err != nil { 536 if activityUpdated, err := exchangeCashActivityRepository.Save(activity); err != nil {
543 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 537 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
544 } else { 538 } else {
@@ -672,7 +666,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -672,7 +666,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
672 if activityUpdated == nil { 666 if activityUpdated == nil {
673 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId))) 667 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
674 } 668 }
675 -  
676 // 判断清单中现金总额超过平台未兑换现金 669 // 判断清单中现金总额超过平台未兑换现金
677 var cashPoolDao *dao.CashPoolDao 670 var cashPoolDao *dao.CashPoolDao
678 if value, err := factory.CreateCashPoolDao(map[string]interface{}{ 671 if value, err := factory.CreateCashPoolDao(map[string]interface{}{
@@ -690,6 +683,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -690,6 +683,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
690 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业") 683 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
691 } 684 }
692 activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64) 685 activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
  686 +
693 var employeeDao *dao.EmployeeDao 687 var employeeDao *dao.EmployeeDao
694 if value, err := factory.CreateEmployeeDao(map[string]interface{}{ 688 if value, err := factory.CreateEmployeeDao(map[string]interface{}{
695 "transactionContext": transactionContext, 689 "transactionContext": transactionContext,
@@ -699,12 +693,68 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -699,12 +693,68 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
699 employeeDao = value 693 employeeDao = value
700 } 694 }
701 systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId) 695 systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
  696 + if err != nil {
  697 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  698 + }
  699 + if systemCashStatistics == nil {
  700 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  701 + }
  702 + systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
  703 + systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
702 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) { 704 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
703 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金") 705 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
704 } 706 }
705 -  
706 - // TODO 更新现金池  
707 - 707 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
  708 + if err != nil {
  709 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  710 + }
  711 + if systemSuMoneyStatistics == nil {
  712 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  713 + }
  714 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  715 + // 更新现金池
  716 + var cashPoolRepository domain.CashPoolRepository
  717 + if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
  718 + "transactionContext": transactionContext,
  719 + }); err != nil {
  720 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  721 + } else {
  722 + cashPoolRepository = value
  723 + }
  724 + // 获取现金池
  725 + _, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
  726 + "companyId": activity.CompanyId,
  727 + })
  728 + if err != nil {
  729 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  730 + }
  731 + if cashPools == nil {
  732 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId)))
  733 + }
  734 + // 平均兑换汇率
  735 + var newRate float64
  736 + if systemExchangedSuMoney == 0 {
  737 + newRate = 0
  738 + } else {
  739 + newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
  740 + }
  741 + // 更新现金池
  742 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  743 + CashPoolId: cashPools[0].CashPoolId,
  744 + ExchangedCash: systemUnExchangeCash,
  745 + UnExchangeCash: systemUnExchangeCash,
  746 + Rate: newRate,
  747 + }
  748 + if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  749 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  750 + }
  751 + cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
  752 + if err != nil {
  753 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  754 + }
  755 + if cashPoolUpdated == nil {
  756 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  757 + }
708 if err := transactionContext.CommitTransaction(); err != nil { 758 if err := transactionContext.CommitTransaction(); err != nil {
709 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 759 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
710 } 760 }