作者 陈志颖

refactor:重构素币兑换清单导入

@@ -867,6 +867,230 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -867,6 +867,230 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
867 } 867 }
868 } 868 }
869 869
  870 +// 更新兑换素币清单
  871 +func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeCashPersonCommand *command.UpdateExchangeCashPersonCommand) (interface{}, error) {
  872 + if err := updateExchangeCashPersonCommand.ValidateCommand(); err != nil {
  873 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  874 + }
  875 + transactionContext, err := factory.CreateTransactionContext(nil)
  876 + if err != nil {
  877 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  878 + }
  879 + if err := transactionContext.StartTransaction(); err != nil {
  880 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  881 + }
  882 + defer func() {
  883 + transactionContext.RollbackTransaction()
  884 + }()
  885 +
  886 + // 兑换素币清单仓储初始化
  887 + var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
  888 + if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
  889 + "transactionContext": transactionContext,
  890 + }); err != nil {
  891 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  892 + } else {
  893 + exchangeCashPersonListRepository = value
  894 + }
  895 +
  896 + // 兑换现金活动仓储初始化
  897 + var exchangeCashActivityRepository domain.ExchangeActivityRepository
  898 + if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
  899 + "transactionContext": transactionContext,
  900 + }); err != nil {
  901 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  902 + } else {
  903 + exchangeCashActivityRepository = value
  904 + }
  905 +
  906 + // 现金池仓储初始化
  907 + var cashPoolRepository domain.CashPoolRepository
  908 + if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
  909 + "transactionContext": transactionContext,
  910 + }); err != nil {
  911 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  912 + } else {
  913 + cashPoolRepository = value
  914 + }
  915 +
  916 + // 操作素币服务初始化
  917 + var operationSuMoneyService service.OperationSuMoneyService
  918 + if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
  919 + "transactionContext": transactionContext,
  920 + }); err != nil {
  921 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  922 + } else {
  923 + operationSuMoneyService = value
  924 + }
  925 +
  926 + // 用户DAO初始化
  927 + var employeeDao *dao.EmployeeDao
  928 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
  929 + "transactionContext": transactionContext,
  930 + }); err != nil {
  931 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  932 + } else {
  933 + employeeDao = value
  934 + }
  935 +
  936 + // 获取兑换清单员工
  937 + personFound, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"listId": updateExchangeCashPersonCommand.ListId})
  938 + if err != nil {
  939 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  940 + }
  941 + if personFound == nil {
  942 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ListId)))
  943 + }
  944 +
  945 + // 获取兑换清单员工已兑换素币
  946 + personFoundExchangedSuMoney := personFound.ExchangedSuMoney
  947 +
  948 + // 获取相关兑换活动
  949 + activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": personFound.ExchangeCashActivityId})
  950 + if err != nil {
  951 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  952 + }
  953 + if activityFound == nil {
  954 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(personFound.ExchangeCashActivityId)))
  955 + }
  956 +
  957 + // 更新兑换清单,个人已兑换现金计算
  958 + updateExchangeCashPersonCommand.ExchangedCash = updateExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate
  959 +
  960 + // 更新兑换素币清单
  961 + if err := personFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
  962 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  963 + }
  964 +
  965 + // 保存兑换素币清单更新
  966 + personUpdated, err := exchangeCashPersonListRepository.Save(personFound)
  967 + if err != nil {
  968 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  969 + }
  970 +
  971 + // 更新素币兑换活动命令
  972 + updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
  973 + ExchangeCashActivityId: personFound.ExchangeCashActivityId,
  974 + ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
  975 + ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
  976 + Deadline: activityFound.Deadline,
  977 + CountDown: activityFound.CountDown,
  978 + ExchangeRate: activityFound.Rate,
  979 + }
  980 +
  981 + // 操作素币服务以及生成素币兑换流水记录命令
  982 + operationSuMoneyCommand := &command.OperationSuMoneyCommand{
  983 + Uid: personFound.EmployeeInfo.Uid,
  984 + Operator: updateExchangeCashPersonCommand.Operator,
  985 + SuMoney: 0,
  986 + OperationType: 0,
  987 + OperationDescription: activityFound.ExchangeActivityName + "调整",
  988 + }
  989 +
  990 + // 判断操作素币类型
  991 + if updateExchangeCashPersonCommand.ExchangedSuMoney > personFoundExchangedSuMoney { // 追加素币兑换
  992 + operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
  993 + operationSuMoneyCommand.OperationType = 4
  994 + } else { // 撤回素币兑换
  995 + operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
  996 + operationSuMoneyCommand.OperationType = 41
  997 + }
  998 +
  999 + // 操作素币并生成素币流水
  1000 + task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
  1001 + if err != nil {
  1002 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1003 + }
  1004 + if task == nil {
  1005 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
  1006 + }
  1007 +
  1008 + // 更新兑换活动
  1009 + if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
  1010 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1011 + }
  1012 +
  1013 + // 保存兑换活动更新
  1014 + activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
  1015 + if err != nil {
  1016 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1017 + }
  1018 + if activityUpdated == nil {
  1019 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
  1020 + }
  1021 +
  1022 + // 统计平台素币兑换状况
  1023 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityUpdated.CompanyId)
  1024 + if err != nil {
  1025 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1026 + }
  1027 + if systemSuMoneyStatistics == nil {
  1028 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  1029 + }
  1030 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  1031 + systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
  1032 +
  1033 + // 获取当前现金池
  1034 + _, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
  1035 + "companyId": activityUpdated.CompanyId,
  1036 + })
  1037 + if err != nil {
  1038 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1039 + }
  1040 + if cashPoolsFound == nil {
  1041 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
  1042 + }
  1043 +
  1044 + // 判断是否超过平台未兑换现金
  1045 + if activityUpdated.ExchangedSuMoney * activityUpdated.Rate > cashPoolsFound[0].UnExchangeCash {
  1046 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
  1047 + }
  1048 +
  1049 + systemExchangedCash := cashPoolsFound[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate
  1050 + //systemExchangedSuMoney := cashPoolsFound[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
  1051 +
  1052 + // 计算现金池平均兑换汇率
  1053 + var newRate float64
  1054 + if systemExchangedSuMoney == 0 {
  1055 + newRate = 0
  1056 + } else {
  1057 + newRate = systemExchangedCash / systemExchangedSuMoney
  1058 + }
  1059 +
  1060 + // 更新现金池命令
  1061 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  1062 + CashPoolId: cashPoolsFound[0].CashPoolId,
  1063 + Cash: cashPoolsFound[0].Cash,
  1064 + ExchangedCash: systemExchangedCash,
  1065 + UnExchangeCash: cashPoolsFound[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate,
  1066 + Rate: newRate,
  1067 + ExchangedSuMoney: systemExchangedSuMoney,
  1068 + UnExchangeSuMoney: systemUnExchangeSuMoney,
  1069 + LastRate: cashPoolsFound[0].LastRate,
  1070 + }
  1071 +
  1072 + // 更新现金池
  1073 + if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  1074 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1075 + }
  1076 +
  1077 + // 保存现金池更新
  1078 + cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
  1079 + if err != nil {
  1080 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1081 + }
  1082 + if cashPoolUpdated == nil {
  1083 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1084 + }
  1085 +
  1086 + if err := transactionContext.CommitTransaction(); err != nil {
  1087 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  1088 + }
  1089 +
  1090 + return personUpdated, nil
  1091 +}
  1092 +
  1093 +
870 // 新增导入兑换素币清单 1094 // 新增导入兑换素币清单
871 func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) { 1095 func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
872 if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil { 1096 if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
@@ -979,155 +1203,274 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc @@ -979,155 +1203,274 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
979 if err != nil { 1203 if err != nil {
980 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 1204 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
981 } 1205 }
982 -  
983 - // 更新兑换素币清单命令  
984 - updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{  
985 - ListId: peopleFound[0].ListId,  
986 - ExchangedSuMoney: peopleFound[0].ExchangedSuMoney,  
987 - ExchangedCash: peopleFound[0].ExchangedCash,  
988 - }  
989 -  
990 if count > 0 { // 追加素币兑换或撤回素币兑换 1206 if count > 0 { // 追加素币兑换或撤回素币兑换
991 - operationSuMoneyCommand := &command.OperationSuMoneyCommand{  
992 - Uid: employeeFound.EmployeeInfo.Uid,  
993 - Operator: createExchangeCashPersonCommand.Operator,  
994 - SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,  
995 - OperationType: 4,  
996 - OperationDescription: "素币兑换现金",  
997 - } 1207 + // 获取兑换清单员工已兑换素币
  1208 + personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
  1209 +
998 if createExchangeCashPersonCommand.ExchangedSuMoney < peopleFound[0].ExchangedSuMoney { // 当前兑换的素币小于已兑换素币,撤回兑换素币 1210 if createExchangeCashPersonCommand.ExchangedSuMoney < peopleFound[0].ExchangedSuMoney { // 当前兑换的素币小于已兑换素币,撤回兑换素币
999 - //suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney // 减量 1211 + // 减量
  1212 + suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney
  1213 +
1000 // 更新兑换素币清单命令 1214 // 更新兑换素币清单命令
  1215 + updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{
  1216 + ListId: peopleFound[0].ListId,
  1217 + ExchangedSuMoney: personFoundExchangedSuMoney - suMoneyDecrement,
  1218 + ExchangedCash: (personFoundExchangedSuMoney - suMoneyDecrement) * activityFound.Rate,
  1219 + }
1001 1220
1002 - // 操作素币命令  
1003 - operationSuMoneyCommand.OperationType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE  
1004 - operationSuMoneyCommand.OperationDescription = "" 1221 + // 更新兑换素币清单
  1222 + if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
  1223 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1224 + }
1005 1225
1006 - } else if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFound[0].ExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币  
1007 - //suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - peopleFound[0].ExchangedSuMoney // 增量  
1008 - // 更新兑换素币清单命令 1226 + // 保存兑换素币清单更新
  1227 + personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
  1228 + if err != nil {
  1229 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1230 + }
  1231 +
  1232 + // 更新素币兑换活动命令
  1233 + updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
  1234 + ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
  1235 + ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
  1236 + ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
  1237 + Deadline: activityFound.Deadline,
  1238 + CountDown: activityFound.CountDown,
  1239 + ExchangeRate: activityFound.Rate,
  1240 + }
  1241 +
  1242 + // 更新兑换活动
  1243 + if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
  1244 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1245 + }
  1246 +
  1247 + // 保存兑换现金活动更新
  1248 + activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
  1249 + if err != nil {
  1250 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1251 + }
  1252 + if activityUpdated == nil {
  1253 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
  1254 + }
1009 1255
1010 // 操作素币命令 1256 // 操作素币命令
1011 - operationSuMoneyCommand.OperationType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH  
1012 - operationSuMoneyCommand.OperationDescription = "" 1257 + operationSuMoneyCommand := &command.OperationSuMoneyCommand{
  1258 + Uid: employeeFound.EmployeeInfo.Uid,
  1259 + Operator: createExchangeCashPersonCommand.Operator,
  1260 + SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
  1261 + OperationType: domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE,
  1262 + OperationDescription: activityFound.ExchangeActivityName + "调整",
  1263 + }
1013 1264
  1265 + // 操作素币,生成素币流水
  1266 + task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
  1267 + if err != nil {
  1268 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1269 + }
  1270 + if task == nil {
  1271 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
  1272 + }
1014 1273
1015 - }  
1016 - // 更新兑换素币清单  
1017 - if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {  
1018 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1019 - } 1274 + // 获取当前现金池
  1275 + _, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
  1276 + "companyId": activityFound.CompanyId,
  1277 + })
  1278 + if err != nil {
  1279 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1280 + }
  1281 + if len(cashPoolsFound) == 0 {
  1282 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")
  1283 + }
1020 1284
1021 - // 保存兑换素币清单更新  
1022 - personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])  
1023 - if err != nil {  
1024 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1025 - } 1285 + // 判断兑换活动的现金是否超过现金池未兑换现金
  1286 + if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
  1287 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
  1288 + }
1026 1289
1027 - // 操作素币,生成素币流水  
1028 - task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)  
1029 - if err != nil {  
1030 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1031 - }  
1032 - if task == nil {  
1033 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))  
1034 - } 1290 + // 获取平台素币兑换情况
  1291 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
  1292 + if err != nil {
  1293 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1294 + }
  1295 + if systemSuMoneyStatistics == nil {
  1296 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  1297 + }
  1298 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  1299 + systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
1035 1300
1036 - // 更新兑换活动命令  
1037 - updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{  
1038 - ExchangeCashActivityId: personUpdated.ExchangeCashActivityId,  
1039 - ExchangeActivityName: activityFound.ExchangeActivityName,  
1040 - ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,  
1041 - ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,  
1042 - Deadline: activityFound.Deadline,  
1043 - CountDown: activityFound.CountDown,  
1044 - ExchangeRate: activityFound.Rate,  
1045 - } 1301 + // 计算平均兑换汇率
  1302 + var newRate float64
  1303 + if systemExchangedSuMoney == 0 {
  1304 + newRate = 0
  1305 + } else {
  1306 + newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
  1307 + }
1046 1308
1047 - // 更新兑换活动  
1048 - if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {  
1049 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1050 - } 1309 + // 更新现金池命令
  1310 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  1311 + CashPoolId: cashPoolsFound[0].CashPoolId,
  1312 + Cash: cashPoolsFound[0].Cash,
  1313 + ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,
  1314 + UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,
  1315 + ExchangedSuMoney: systemExchangedSuMoney,
  1316 + UnExchangeSuMoney: systemUnExchangeSuMoney,
  1317 + Rate: newRate,
  1318 + LastRate: cashPoolsFound[0].LastRate,
  1319 + }
1051 1320
1052 - // 保存兑换现金活动更新  
1053 - activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)  
1054 - if err != nil {  
1055 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1056 - }  
1057 - if activityUpdated == nil {  
1058 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))  
1059 - } 1321 + // 更新现金池
  1322 + if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  1323 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1324 + }
1060 1325
1061 - // 获取当前现金池  
1062 - _, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{  
1063 - "companyId": activityFound.CompanyId,  
1064 - })  
1065 - if err != nil {  
1066 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1067 - }  
1068 - if len(cashPoolsFound) == 0 {  
1069 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")  
1070 - } 1326 + // 保存现金池更新
  1327 + cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
  1328 + if err != nil {
  1329 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1330 + }
  1331 + if cashPoolUpdated == nil {
  1332 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1333 + }
1071 1334
1072 - // 判断兑换活动的现金是否超过现金池未兑换现金  
1073 - if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {  
1074 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")  
1075 - } 1335 + if err := transactionContext.CommitTransaction(); err != nil {
  1336 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  1337 + }
1076 1338
1077 - // 获取平台素币兑换情况  
1078 - systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)  
1079 - if err != nil {  
1080 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1081 - }  
1082 - if systemSuMoneyStatistics == nil {  
1083 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")  
1084 - }  
1085 - systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)  
1086 - systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64) 1339 + return personUpdated, nil
  1340 + } else if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFound[0].ExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币
  1341 + // 增量
  1342 + suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - peopleFound[0].ExchangedSuMoney
1087 1343
1088 - // 计算平均兑换汇率  
1089 - var newRate float64  
1090 - if systemExchangedSuMoney == 0 {  
1091 - newRate = 0  
1092 - } else {  
1093 - newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney  
1094 - } 1344 + // 更新兑换素币清单命令
  1345 + updateExchangeCashPersonCommand := command.UpdateExchangeCashPersonCommand{
  1346 + ListId: peopleFound[0].ListId,
  1347 + ExchangedSuMoney: personFoundExchangedSuMoney + suMoneyIncrement,
  1348 + ExchangedCash: (personFoundExchangedSuMoney + suMoneyIncrement) * activityFound.Rate,
  1349 + }
1095 1350
1096 - // 更新现金池命令  
1097 - updateCashPoolCommand := &command.UpdateCashPoolCommand{  
1098 - CashPoolId: cashPoolsFound[0].CashPoolId,  
1099 - Cash: cashPoolsFound[0].Cash,  
1100 - ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,  
1101 - UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,  
1102 - ExchangedSuMoney: systemExchangedSuMoney,  
1103 - UnExchangeSuMoney: systemUnExchangeSuMoney,  
1104 - Rate: newRate,  
1105 - LastRate: cashPoolsFound[0].LastRate,  
1106 - } 1351 + // 更新兑换素币清单
  1352 + if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
  1353 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1354 + }
  1355 +
  1356 + // 保存兑换素币清单更新
  1357 + personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
  1358 + if err != nil {
  1359 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1360 + }
  1361 +
  1362 + // 更新素币兑换活动命令
  1363 + updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
  1364 + ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
  1365 + ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
  1366 + ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
  1367 + Deadline: activityFound.Deadline,
  1368 + CountDown: activityFound.CountDown,
  1369 + ExchangeRate: activityFound.Rate,
  1370 + }
  1371 +
  1372 + // 更新兑换活动
  1373 + if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
  1374 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1375 + }
  1376 +
  1377 + // 保存兑换现金活动更新
  1378 + activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
  1379 + if err != nil {
  1380 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1381 + }
  1382 + if activityUpdated == nil {
  1383 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
  1384 + }
  1385 +
  1386 + // 操作素币命令
  1387 + operationSuMoneyCommand := &command.OperationSuMoneyCommand{
  1388 + Uid: employeeFound.EmployeeInfo.Uid,
  1389 + Operator: createExchangeCashPersonCommand.Operator,
  1390 + SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
  1391 + OperationType: domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE,
  1392 + OperationDescription: activityFound.ExchangeActivityName + "调整",
  1393 + }
  1394 +
  1395 + // 操作素币,生成素币流水
  1396 + task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
  1397 + if err != nil {
  1398 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1399 + }
  1400 + if task == nil {
  1401 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
  1402 + }
  1403 +
  1404 + // 获取当前现金池
  1405 + _, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
  1406 + "companyId": activityFound.CompanyId,
  1407 + })
  1408 + if err != nil {
  1409 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1410 + }
  1411 + if len(cashPoolsFound) == 0 {
  1412 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")
  1413 + }
  1414 +
  1415 + // 判断兑换活动的现金是否超过现金池未兑换现金
  1416 + if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
  1417 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
  1418 + }
  1419 +
  1420 + // 获取平台素币兑换情况
  1421 + systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
  1422 + if err != nil {
  1423 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1424 + }
  1425 + if systemSuMoneyStatistics == nil {
  1426 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
  1427 + }
  1428 + systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  1429 + systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
1107 1430
1108 - // 更新现金池  
1109 - if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {  
1110 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1111 - } 1431 + // 计算平均兑换汇率
  1432 + var newRate float64
  1433 + if systemExchangedSuMoney == 0 {
  1434 + newRate = 0
  1435 + } else {
  1436 + newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
  1437 + }
1112 1438
1113 - // 保存现金池更新  
1114 - cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])  
1115 - if err != nil {  
1116 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1117 - }  
1118 - if cashPoolUpdated == nil {  
1119 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1120 - } 1439 + // 更新现金池命令
  1440 + updateCashPoolCommand := &command.UpdateCashPoolCommand{
  1441 + CashPoolId: cashPoolsFound[0].CashPoolId,
  1442 + Cash: cashPoolsFound[0].Cash,
  1443 + ExchangedCash: cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash,
  1444 + UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personUpdated.ExchangedCash,
  1445 + ExchangedSuMoney: systemExchangedSuMoney,
  1446 + UnExchangeSuMoney: systemUnExchangeSuMoney,
  1447 + Rate: newRate,
  1448 + LastRate: cashPoolsFound[0].LastRate,
  1449 + }
1121 1450
1122 - if err := transactionContext.CommitTransaction(); err != nil {  
1123 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 1451 + // 更新现金池
  1452 + if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
  1453 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  1454 + }
  1455 +
  1456 + // 保存现金池更新
  1457 + cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
  1458 + if err != nil {
  1459 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1460 + }
  1461 + if cashPoolUpdated == nil {
  1462 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  1463 + }
  1464 +
  1465 + if err := transactionContext.CommitTransaction(); err != nil {
  1466 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  1467 + }
  1468 +
  1469 + return personUpdated, nil
1124 } 1470 }
1125 - return nil, nil  
1126 } else if count == 0 { // 新增 1471 } else if count == 0 { // 新增
1127 - // 新增兑换清单命令  
1128 - newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)  
1129 -  
1130 // 新增兑换素币清单命令 1472 // 新增兑换素币清单命令
  1473 + newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
1131 newPerson := &domain.ExchangeCashPersonList{ 1474 newPerson := &domain.ExchangeCashPersonList{
1132 EmployeeInfo: &domain.EmployeeInfo{ 1475 EmployeeInfo: &domain.EmployeeInfo{
1133 Uid: employeeFound.EmployeeInfo.Uid, 1476 Uid: employeeFound.EmployeeInfo.Uid,
@@ -1612,248 +1955,6 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP @@ -1612,248 +1955,6 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP
1612 } 1955 }
1613 1956
1614 // 更新兑换素币清单 1957 // 更新兑换素币清单
1615 -func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeCashPersonCommand *command.UpdateExchangeCashPersonCommand) (interface{}, error) {  
1616 - if err := updateExchangeCashPersonCommand.ValidateCommand(); err != nil {  
1617 - return nil, application.ThrowError(application.ARG_ERROR, err.Error())  
1618 - }  
1619 - transactionContext, err := factory.CreateTransactionContext(nil)  
1620 - if err != nil {  
1621 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
1622 - }  
1623 - if err := transactionContext.StartTransaction(); err != nil {  
1624 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
1625 - }  
1626 - defer func() {  
1627 - transactionContext.RollbackTransaction()  
1628 - }()  
1629 -  
1630 - // 兑换素币清单仓储初始化  
1631 - var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository  
1632 - if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{  
1633 - "transactionContext": transactionContext,  
1634 - }); err != nil {  
1635 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1636 - } else {  
1637 - exchangeCashPersonListRepository = value  
1638 - }  
1639 -  
1640 - // 兑换现金活动仓储初始化  
1641 - var exchangeCashActivityRepository domain.ExchangeActivityRepository  
1642 - if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{  
1643 - "transactionContext": transactionContext,  
1644 - }); err != nil {  
1645 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1646 - } else {  
1647 - exchangeCashActivityRepository = value  
1648 - }  
1649 -  
1650 - // 现金池仓储初始化  
1651 - var cashPoolRepository domain.CashPoolRepository  
1652 - if value, err := factory.CreateCashPoolRepository(map[string] interface{} {  
1653 - "transactionContext": transactionContext,  
1654 - }); err != nil {  
1655 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1656 - } else {  
1657 - cashPoolRepository = value  
1658 - }  
1659 -  
1660 - // 操作素币服务初始化  
1661 - var operationSuMoneyService service.OperationSuMoneyService  
1662 - if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{  
1663 - "transactionContext": transactionContext,  
1664 - }); err != nil {  
1665 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1666 - } else {  
1667 - operationSuMoneyService = value  
1668 - }  
1669 -  
1670 - // 用户DAO初始化  
1671 - var employeeDao *dao.EmployeeDao  
1672 - if value, err := factory.CreateEmployeeDao(map[string]interface{}{  
1673 - "transactionContext": transactionContext,  
1674 - }); err != nil {  
1675 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
1676 - } else {  
1677 - employeeDao = value  
1678 - }  
1679 -  
1680 - // 获取兑换清单员工  
1681 - personFound, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"listId": updateExchangeCashPersonCommand.ListId})  
1682 - if err != nil {  
1683 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1684 - }  
1685 - if personFound == nil {  
1686 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ListId)))  
1687 - }  
1688 -  
1689 - // 获取兑换清单员工已兑换素币  
1690 - personFoundExchangedSuMoney := personFound.ExchangedSuMoney  
1691 -  
1692 - // 获取相关兑换活动  
1693 - activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": personFound.ExchangeCashActivityId})  
1694 - if err != nil {  
1695 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1696 - }  
1697 - if activityFound == nil {  
1698 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(personFound.ExchangeCashActivityId)))  
1699 - }  
1700 -  
1701 - // 更新兑换清单,个人已兑换现金计算  
1702 - updateExchangeCashPersonCommand.ExchangedCash = updateExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate  
1703 -  
1704 - // 更新兑换素币清单  
1705 - if err := personFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {  
1706 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1707 - }  
1708 -  
1709 - // 保存兑换素币清单更新  
1710 - personUpdated, err := exchangeCashPersonListRepository.Save(personFound)  
1711 - if err != nil {  
1712 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1713 - }  
1714 -  
1715 - // 更新素币兑换活动命令  
1716 - updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{  
1717 - ExchangeCashActivityId: personFound.ExchangeCashActivityId,  
1718 - ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),  
1719 - ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,  
1720 - Deadline: activityFound.Deadline,  
1721 - CountDown: activityFound.CountDown,  
1722 - ExchangeRate: activityFound.Rate,  
1723 - }  
1724 -  
1725 - // 操作素币服务以及生成素币兑换流水记录命令  
1726 - operationSuMoneyCommand := &command.OperationSuMoneyCommand{  
1727 - Uid: personFound.EmployeeInfo.Uid,  
1728 - Operator: updateExchangeCashPersonCommand.Operator,  
1729 - SuMoney: 0,  
1730 - OperationType: 0,  
1731 - OperationDescription: activityFound.ExchangeActivityName + "调整",  
1732 - }  
1733 -  
1734 - // 判断操作素币类型  
1735 - if updateExchangeCashPersonCommand.ExchangedSuMoney > personFoundExchangedSuMoney { // 追加素币兑换  
1736 - operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)  
1737 - operationSuMoneyCommand.OperationType = 4  
1738 - } else { // 撤回素币兑换  
1739 - operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)  
1740 - operationSuMoneyCommand.OperationType = 41  
1741 - }  
1742 -  
1743 - // 操作素币并生成素币流水  
1744 - task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)  
1745 - if err != nil {  
1746 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1747 - }  
1748 - if task == nil {  
1749 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))  
1750 - }  
1751 -  
1752 - // 更新兑换活动  
1753 - if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {  
1754 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1755 - }  
1756 -  
1757 - // 保存兑换活动更新  
1758 - activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)  
1759 - if err != nil {  
1760 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1761 - }  
1762 - if activityUpdated == nil {  
1763 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))  
1764 - }  
1765 -  
1766 - //// 统计活动已兑换素币  
1767 - //activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityUpdated.ActivityId)  
1768 - //if err != nil {  
1769 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1770 - //}  
1771 - //if activitySuMoneyStatistics == nil {  
1772 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")  
1773 - //}  
1774 - //activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)  
1775 -  
1776 - //// 统计平台现金兑换情况  
1777 - //systemCashStatistics, err := employeeDao.CalculateSystemCash(activityUpdated.CompanyId)  
1778 - //if err != nil {  
1779 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1780 - //}  
1781 - //if systemCashStatistics == nil {  
1782 - // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")  
1783 - //}  
1784 - ////systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)  
1785 - //systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)  
1786 -  
1787 - // 统计平台素币兑换状况  
1788 - systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityUpdated.CompanyId)  
1789 - if err != nil {  
1790 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1791 - }  
1792 - if systemSuMoneyStatistics == nil {  
1793 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")  
1794 - }  
1795 - systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)  
1796 - systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)  
1797 -  
1798 - // 获取当前现金池  
1799 - _, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{  
1800 - "companyId": activityUpdated.CompanyId,  
1801 - })  
1802 - if err != nil {  
1803 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1804 - }  
1805 - if cashPoolsFound == nil {  
1806 - return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))  
1807 - }  
1808 -  
1809 - // 判断是否超过平台未兑换现金  
1810 - if activityUpdated.ExchangedSuMoney * activityUpdated.Rate > cashPoolsFound[0].UnExchangeCash {  
1811 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")  
1812 - }  
1813 -  
1814 - systemExchangedCash := cashPoolsFound[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate  
1815 - //systemExchangedSuMoney := cashPoolsFound[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)  
1816 -  
1817 - // 计算现金池平均兑换汇率  
1818 - var newRate float64  
1819 - if systemExchangedSuMoney == 0 {  
1820 - newRate = 0  
1821 - } else {  
1822 - newRate = systemExchangedCash / systemExchangedSuMoney  
1823 - }  
1824 -  
1825 - // 更新现金池命令  
1826 - updateCashPoolCommand := &command.UpdateCashPoolCommand{  
1827 - CashPoolId: cashPoolsFound[0].CashPoolId,  
1828 - Cash: cashPoolsFound[0].Cash,  
1829 - ExchangedCash: systemExchangedCash,  
1830 - UnExchangeCash: cashPoolsFound[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate,  
1831 - Rate: newRate,  
1832 - ExchangedSuMoney: systemExchangedSuMoney,  
1833 - UnExchangeSuMoney: systemUnExchangeSuMoney,  
1834 - LastRate: cashPoolsFound[0].LastRate,  
1835 - }  
1836 -  
1837 - // 更新现金池  
1838 - if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {  
1839 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
1840 - }  
1841 -  
1842 - // 保存现金池更新  
1843 - cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])  
1844 - if err != nil {  
1845 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1846 - }  
1847 - if cashPoolUpdated == nil {  
1848 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())  
1849 - }  
1850 -  
1851 - if err := transactionContext.CommitTransaction(); err != nil {  
1852 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
1853 - }  
1854 -  
1855 - return personUpdated, nil  
1856 -}  
1857 1958
1858 // 移除兑换素币清单 1959 // 移除兑换素币清单
1859 func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeCashPersonCommand *command.RemoveExchangeCashPersonCommand) (interface{}, error) { 1960 func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeCashPersonCommand *command.RemoveExchangeCashPersonCommand) (interface{}, error) {