作者 陈志颖

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

@@ -666,7 +666,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -666,7 +666,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
666 if activityUpdated == nil { 666 if activityUpdated == nil {
667 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)))
668 } 668 }
669 - // 判断清单中现金总额超过平台未兑换现金 669 + // 获取兑换清单素币状况
670 var cashPoolDao *dao.CashPoolDao 670 var cashPoolDao *dao.CashPoolDao
671 if value, err := factory.CreateCashPoolDao(map[string]interface{}{ 671 if value, err := factory.CreateCashPoolDao(map[string]interface{}{
672 "transactionContext": transactionContext, 672 "transactionContext": transactionContext,
@@ -683,7 +683,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -683,7 +683,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
683 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业") 683 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
684 } 684 }
685 activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64) 685 activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
686 - 686 + // 现金池操作
687 var employeeDao *dao.EmployeeDao 687 var employeeDao *dao.EmployeeDao
688 if value, err := factory.CreateEmployeeDao(map[string]interface{}{ 688 if value, err := factory.CreateEmployeeDao(map[string]interface{}{
689 "transactionContext": transactionContext, 689 "transactionContext": transactionContext,
@@ -692,6 +692,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -692,6 +692,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
692 } else { 692 } else {
693 employeeDao = value 693 employeeDao = value
694 } 694 }
  695 + // 获取平台现金状况
695 systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId) 696 systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
696 if err != nil { 697 if err != nil {
697 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 698 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -701,9 +702,11 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -701,9 +702,11 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
701 } 702 }
702 systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64) 703 systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
703 systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64) 704 systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
  705 + // 判断兑换的现金是否超过现金池未兑换现金
704 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) { 706 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
705 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金") 707 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
706 } 708 }
  709 + // 获取平台素币状况
707 systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId) 710 systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
708 if err != nil { 711 if err != nil {
709 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 712 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -731,7 +734,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -731,7 +734,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
731 if cashPools == nil { 734 if cashPools == nil {
732 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId))) 735 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId)))
733 } 736 }
734 - // 平均兑换汇率 737 + // 计算平均兑换汇率
735 var newRate float64 738 var newRate float64
736 if systemExchangedSuMoney == 0 { 739 if systemExchangedSuMoney == 0 {
737 newRate = 0 740 newRate = 0
@@ -869,7 +872,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC @@ -869,7 +872,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
869 if err != nil { 872 if err != nil {
870 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 873 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
871 } 874 }
872 - // 生成素币流水,还原个人素币值 875 + // 还原个人素币值,生成素币流水
873 var operationSuMoneyService service.OperationSuMoneyService 876 var operationSuMoneyService service.OperationSuMoneyService
874 if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{ 877 if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
875 "transactionContext": transactionContext, 878 "transactionContext": transactionContext,
@@ -923,10 +926,77 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC @@ -923,10 +926,77 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
923 if activityUpdated == nil { 926 if activityUpdated == nil {
924 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId))) 927 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
925 } 928 }
926 -  
927 - // TODO 更新现金池  
928 -  
929 - 929 + // 更新现金池
  930 + var employeeDao *dao.EmployeeDao
  931 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
  932 + "transactionContext": transactionContext,
  933 + }); err != nil {
  934 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  935 + } else {
  936 + employeeDao = value
  937 + }
  938 + // 获取平台现金状况
  939 + systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
  940 + if err != nil {
  941 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  942 + }
  943 + if systemCashStatistics == nil {
  944 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  945 + }
  946 + systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
  947 + systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
  948 + // 获取平台素币状况
  949 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
  950 + if err != nil {
  951 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  952 + }
  953 + if systemSuMoneyStatistics == nil {
  954 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  955 + }
  956 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  957 + // 更新现金池
  958 + var cashPoolRepository domain.CashPoolRepository
  959 + if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
  960 + "transactionContext": transactionContext,
  961 + }); err != nil {
  962 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  963 + } else {
  964 + cashPoolRepository = value
  965 + }
  966 + // 获取现金池
  967 + _, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
  968 + "companyId": activityFound.CompanyId,
  969 + })
  970 + if err != nil {
  971 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  972 + }
  973 + if cashPools == nil {
  974 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
  975 + }
  976 + // 计算平均兑换汇率
  977 + var newRate float64
  978 + if systemExchangedSuMoney == 0 {
  979 + newRate = 0
  980 + } else {
  981 + newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
  982 + }
  983 + // 更新现金池
  984 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  985 + CashPoolId: cashPools[0].CashPoolId,
  986 + ExchangedCash: systemUnExchangeCash,
  987 + UnExchangeCash: systemUnExchangeCash,
  988 + Rate: newRate,
  989 + }
  990 + if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  991 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  992 + }
  993 + cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
  994 + if err != nil {
  995 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  996 + }
  997 + if cashPoolUpdated == nil {
  998 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  999 + }
930 if err := transactionContext.CommitTransaction(); err != nil { 1000 if err := transactionContext.CommitTransaction(); err != nil {
931 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 1001 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
932 } 1002 }
@@ -1035,10 +1105,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC @@ -1035,10 +1105,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
1035 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) { 1105 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
1036 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金") 1106 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
1037 } 1107 }
1038 -  
1039 - // TODO 更新现金池  
1040 -  
1041 - // 生成素币兑换流水记录,更新员工素币 1108 + // 更新员工素币,生成素币兑换流水记录
1042 operationSuMoneyCommand := &command.OperationSuMoneyCommand{ 1109 operationSuMoneyCommand := &command.OperationSuMoneyCommand{
1043 Uid: person.EmployeeInfo.Uid, 1110 Uid: person.EmployeeInfo.Uid,
1044 Operator: updateExchangeCashPersonCommand.Operator, 1111 Operator: updateExchangeCashPersonCommand.Operator,
@@ -1060,14 +1127,77 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC @@ -1060,14 +1127,77 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
1060 if task == nil { 1127 if task == nil {
1061 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid))) 1128 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
1062 } 1129 }
1063 - personUpdated, err := exchangeCashPersonListRepository.Save(person) 1130 + // 更新现金池
  1131 + // 获取平台现金状况
  1132 + newSystemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
1064 if err != nil { 1133 if err != nil {
1065 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 1134 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
1066 } 1135 }
1067 - if err := transactionContext.CommitTransaction(); err != nil {  
1068 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 1136 + if systemCashStatistics == nil {
  1137 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  1138 + }
  1139 + systemExchangedCash := newSystemCashStatistics["systemExchangedCash"].(float64)
  1140 + systemUnExchangeCash := newSystemCashStatistics["systemUnExchangeCash"].(float64)
  1141 + // 获取平台素币状况
  1142 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
  1143 + if err != nil {
  1144 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1145 + }
  1146 + if systemSuMoneyStatistics == nil {
  1147 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  1148 + }
  1149 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  1150 + // 更新现金池
  1151 + var cashPoolRepository domain.CashPoolRepository
  1152 + if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
  1153 + "transactionContext": transactionContext,
  1154 + }); err != nil {
  1155 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1156 + } else {
  1157 + cashPoolRepository = value
  1158 + }
  1159 + // 获取现金池
  1160 + _, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
  1161 + "companyId": activityFound.CompanyId,
  1162 + })
  1163 + if err != nil {
  1164 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1165 + }
  1166 + if cashPools == nil {
  1167 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
  1168 + }
  1169 + // 计算平均兑换汇率
  1170 + var newRate float64
  1171 + if systemExchangedSuMoney == 0 {
  1172 + newRate = 0
  1173 + } else {
  1174 + newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
  1175 + }
  1176 + // 更新现金池
  1177 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  1178 + CashPoolId: cashPools[0].CashPoolId,
  1179 + ExchangedCash: systemUnExchangeCash,
  1180 + UnExchangeCash: systemUnExchangeCash,
  1181 + Rate: newRate,
  1182 + }
  1183 + if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  1184 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1185 + }
  1186 + cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
  1187 + if err != nil {
  1188 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1189 + }
  1190 + if cashPoolUpdated == nil {
  1191 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1192 + }
  1193 + if personUpdated, err := exchangeCashPersonListRepository.Save(person);err != nil {
  1194 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1195 + } else {
  1196 + if err := transactionContext.CommitTransaction(); err != nil {
  1197 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  1198 + }
  1199 + return personUpdated, nil
1069 } 1200 }
1070 - return personUpdated, nil  
1071 } 1201 }
1072 1202
1073 func NewCashPoolService(options map[string]interface{}) *CashPoolService { 1203 func NewCashPoolService(options map[string]interface{}) *CashPoolService {