作者 陈志颖

refactor:优化导入功能

... ... @@ -548,3 +548,32 @@
BodyData:
2020/11/24 13:45:45.059 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606196744625039000 BodyData:{"code":0,"data":{"fail":null,"successCount":1},"msg":"ok"}
2020/11/24 13:45:45.059 [D] [server.go:1925] | 127.0.0.1| 200 | 437.704111ms| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 15:51:18.546 [I] [???:0] http server Running on http://:8082
2020/11/24 15:51:26.674 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606204286674056000
Auth=
BodyData:
2020/11/24 15:51:27.664 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606204286674056000 BodyData:{"code":0,"data":{"fail":null,"successCount":1},"msg":"ok"}
2020/11/24 15:51:27.664 [D] [server.go:1925] | 127.0.0.1| 200 | 990.726762ms| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 15:52:10.472 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606204330472439000
Auth=
BodyData:
2020/11/24 15:52:10.488 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606204330472439000 BodyData:{"code":0,"data":{"fail":{"tableData":[["卞信华","18900000000","单元格包含空字符"]],"tableHeader":["员工姓名","手机号","已兑换素币","错误详情"]},"successCount":0},"msg":"ok"}
2020/11/24 15:52:10.488 [D] [server.go:1925] | 127.0.0.1| 200 | 16.812751ms| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 15:56:05.844 [I] [???:0] http server Running on http://:8082
2020/11/24 15:57:07.287 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606204627287511000
Auth=
BodyData:
2020/11/24 15:57:07.586 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606204627287511000 BodyData:{"code":0,"data":{"fail":null,"successCount":2},"msg":"ok"}
2020/11/24 15:57:07.586 [D] [server.go:1925] | 127.0.0.1| 200 | 301.568812ms| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 16:00:00.327 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606204800327448000
Auth=
BodyData:
2020/11/24 16:04:40.206 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606204800327448000 BodyData:{"code":0,"data":{"fail":null,"successCount":2},"msg":"ok"}
2020/11/24 16:04:40.206 [D] [server.go:1925] | 127.0.0.1| 200 |4m39.880470496s| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 16:04:53.206 [I] [???:0] http server Running on http://:8082
2020/11/24 16:04:55.624 [D] [server.go:2843] ====>Recv User:<nil> RequestId:POST./cash-pool/activity/exchange-list/import.1606205095624342000
Auth=
BodyData:
2020/11/24 16:04:59.355 [D] [server.go:2843] <====Send RequestId:POST./cash-pool/activity/exchange-list/import.1606205095624342000 BodyData:{"code":0,"data":{"fail":{"tableData":[["hello","13162672794",2,{"Code":501,"Message":"内部服务出错:没有此员工"}]],"tableHeader":["员工姓名","手机号","已兑换素币","错误详情"]},"successCount":1},"msg":"ok"}
2020/11/24 16:04:59.355 [D] [server.go:1925] | 127.0.0.1| 200 | 3.731193052s| match| POST  /cash-pool/activity/exchange-list/import r:/cash-pool/activity/exchange-list/import
2020/11/24 16:21:47.937 [I] [???:0] http server Running on http://:8082
... ...
... ... @@ -154,20 +154,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
}
}
// 更新现金池
//newCashPool := &domain.CashPool{
// CashPoolId: cashPools[0].CashPoolId,
// CompanyId: createCashPoolCommand.CompanyId,
// Cash: createCashPoolCommand.Cash,
// ExchangedCash: cashPools[0].ExchangedCash,
// UnExchangeCash: cashPools[0].UnExchangeCash + (createCashPoolCommand.Cash - cashPools[0].Cash),
// ExchangedSuMoney: systemExchangedSuMoney,
// UnExchangeSuMoney: systemUnExchangeSuMoney,
// Rate: newRate,
// LastRate: lastActivityRate,
// CreateTime: time.Now().Local(),
//}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
... ... @@ -1288,17 +1274,31 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
}
// 导入新增兑换素币清单
func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommand *command.CreateExchangeCashPersonCommand) (interface{}, error) {
if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommands []*command.CreateExchangeCashPersonCommand, failureDataList []interface{}) ([]interface{}, error) {
// 批量校验
for _, createExchangeCashPersonCommand := range createExchangeCashPersonCommands {
if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
}
if len(failureDataList) > 0 {
return []interface{}{}, application.ThrowError(application.TRANSACTION_ERROR, "校验失败")
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
... ... @@ -1309,7 +1309,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeCashActivityRepository = value
}
... ... @@ -1319,7 +1319,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeCashPersonListRepository = value
}
... ... @@ -1329,7 +1329,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
... ... @@ -1339,7 +1339,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
... ... @@ -1349,7 +1349,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
... ... @@ -1359,248 +1359,579 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return []interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
operationSuMoneyService = value
}
// 获取当前兑换清单兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": createExchangeCashPersonCommand.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR,"当前未创建任务兑换活动")
}
// 循环添加
for _, createExchangeCashPersonCommand := range createExchangeCashPersonCommands {
// 获取当前兑换清单兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": createExchangeCashPersonCommand.ExchangeCashActivityId})
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if activityFound == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前未创建任务兑换活动",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取当前公司现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(cashPoolsFound) == 0 {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, "当前现金池为空,请先投入现金")
}
// 获取当前公司现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if len(cashPoolsFound) == 0 {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前现金池为空,请先投入现金",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取员工查询条件
getEmployee := map[string]interface{}{
"account": createExchangeCashPersonCommand.PersonAccount,
"companyId": activityFound.CompanyId,
"status": 1,
}
// 获取员工查询条件
getEmployee := map[string]interface{}{
"account": createExchangeCashPersonCommand.PersonAccount,
"companyId": activityFound.CompanyId,
}
// 判断当前员工是否有效
employeeFound, err := employeeRepository.FindOne(getEmployee)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employeeFound == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的员工")
}
// 判断当前员工是否有效
employeeFound, err := employeeRepository.FindOne(getEmployee)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if employeeFound == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"无效的导入员工",
}
failureDataList = append(failureDataList, row)
continue
}
// 判断当前员工是否已经在素币兑换清单中
_, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
"exchangeCashActivityId": activityFound.ActivityId,
"offset": 0,
"limit": 1,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 判断当前员工是否已经在素币兑换清单中
_, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
"exchangeCashActivityId": activityFound.ActivityId,
"offset": 0,
"limit": 1,
})
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFound.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFound.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前兑换素币超过本人持有的素币值",
}
failureDataList = append(failureDataList, row)
continue
}
if len(peopleFound) > 0 { // 当前导入员工在素币兑换清单中,判断追加素币兑换或撤回素币兑换
// 获取当前员工已兑换素币
personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
if len(peopleFound) > 0 { // 当前导入员工在素币兑换清单中,判断追加素币兑换或撤回素币兑换
// 获取当前员工已兑换素币
personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
// 获取当前兑换员工已兑换现金
//personFoundExchangedCash := peopleFound[0].ExchangedCash
// 获取当前兑换员工已兑换现金
//personFoundExchangedCash := peopleFound[0].ExchangedCash
if createExchangeCashPersonCommand.ExchangedSuMoney < personFoundExchangedSuMoney { // 当前兑换的素币小于本人已兑换素币,撤回兑换素币
// 素币减量
suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney
if createExchangeCashPersonCommand.ExchangedSuMoney < personFoundExchangedSuMoney { // 当前兑换的素币小于本人已兑换素币,撤回兑换素币
// 素币减量
suMoneyDecrement := peopleFound[0].ExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney
// 现金减量
cashDecrement := suMoneyDecrement * activityFound.Rate
// 现金减量
cashDecrement := suMoneyDecrement * activityFound.Rate
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ExchangedSuMoney: personFoundExchangedSuMoney - suMoneyDecrement,
ExchangedCash: (personFoundExchangedSuMoney - suMoneyDecrement) * activityFound.Rate,
Operator: createExchangeCashPersonCommand.Operator,
}
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ExchangedSuMoney: personFoundExchangedSuMoney - suMoneyDecrement,
ExchangedCash: (personFoundExchangedSuMoney - suMoneyDecrement) * activityFound.Rate,
Operator: createExchangeCashPersonCommand.Operator,
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney - suMoneyDecrement,
ExchangedCash: activityFound.ExchangedCash - cashDecrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney - suMoneyDecrement,
ExchangedCash: activityFound.ExchangedCash - cashDecrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if activityUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"兑换现金活动保存失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 个人素币操作命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: suMoneyDecrement,
OperationType: 41,
OperationDescription: activityFound.ExchangeActivityName + "调整",
}
// 个人素币操作命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: suMoneyDecrement,
OperationType: 41,
OperationDescription: activityFound.ExchangeActivityName + "调整",
}
// 操作个人素币,生成素币流水记录
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 操作个人素币,生成素币流水记录
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"操作个人素币失败",
}
failureDataList = append(failureDataList, row)
continue
}
if task == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"操作个人素币失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取平台素币兑换情况
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)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if systemSuMoneyStatistics == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"无效的公司",
}
failureDataList = append(failureDataList, row)
continue
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash - cashDecrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash + cashDecrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash - cashDecrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash + cashDecrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if cashPoolUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
} else { // 当前兑换素币大于等于已兑换素币,追加兑换素币
// 素币增量
suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney
// 现金增量
cashIncrement := suMoneyIncrement * activityFound.Rate
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ExchangedSuMoney: personFoundExchangedSuMoney + suMoneyIncrement,
ExchangedCash: (personFoundExchangedSuMoney + suMoneyIncrement) * activityFound.Rate,
Operator: createExchangeCashPersonCommand.Operator,
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[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 := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personUpdated, nil
} else if createExchangeCashPersonCommand.ExchangedSuMoney > personFoundExchangedSuMoney { // 当前兑换素币大于已兑换素币,追加兑换素币
// 素币增量
suMoneyIncrement := createExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney
// 现金增量
cashIncrement := suMoneyIncrement * activityFound.Rate
// 更新兑换素币清单命令
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ExchangedSuMoney: personFoundExchangedSuMoney + suMoneyIncrement,
ExchangedCash: (personFoundExchangedSuMoney + suMoneyIncrement) * activityFound.Rate,
Operator: createExchangeCashPersonCommand.Operator,
}
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + suMoneyIncrement,
ExchangedCash: activityFound.ExchangedCash + cashIncrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(peopleFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + suMoneyIncrement,
ExchangedCash: activityFound.ExchangedCash + cashIncrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if activityUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
}
failureDataList = append(failureDataList, row)
continue
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: 4,
OperationDescription: "参与" + activityFound.ExchangeActivityName,
}
// 操作素币,生成素币流水
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if task == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
}
failureDataList = append(failureDataList, row)
continue
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if systemSuMoneyStatistics == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"无效的公司员工",
}
failureDataList = append(failureDataList, row)
continue
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + cashIncrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - cashIncrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内务业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if cashPoolUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
}
} else if len(peopleFound) == 0 { // 导入兑换素币清单员工不存在兑换素币清单中,新增兑换素币清单
// 新增兑换素币清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
EmployeeName: employeeFound.EmployeeInfo.EmployeeName,
EmployeeAccount: employeeFound.EmployeeInfo.EmployeeAccount,
},
ExchangeCashActivityId: createExchangeCashPersonCommand.ExchangeCashActivityId,
ExchangedSuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: newPersonExchangedCash,
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
// 保存兑换素币清单
personSaved, err := exchangeCashPersonListRepository.Save(newPerson)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内务业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
... ... @@ -1611,42 +1942,123 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
// 操作素币,生成素币流水
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
}
failureDataList = append(failureDataList, row)
continue
}
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前兑换素币超过本人持有的素币值",
}
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if activityUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
}
failureDataList = append(failureDataList, row)
continue
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"无效的公司员工",
}
failureDataList = append(failureDataList, row)
continue
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
// 重新计算现金池平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personUpdated.ExchangedCash) / systemExchangedSuMoney
newRate = (cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + cashIncrement,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - cashIncrement,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personSaved.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
... ... @@ -1655,153 +2067,57 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
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())
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
return personUpdated, nil
} else { // 当前导入兑换素币清单素币等于本人已兑换素币
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else { // 未知情况
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"未知错误",
}
return nil, nil
}
} else if len(peopleFound) == 0 { // 导入兑换素币清单员工不存在兑换素币清单中,新增兑换素币清单
// 新增兑换素币清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
EmployeeName: employeeFound.EmployeeInfo.EmployeeName,
EmployeeAccount: employeeFound.EmployeeInfo.EmployeeAccount,
},
ExchangeCashActivityId: createExchangeCashPersonCommand.ExchangeCashActivityId,
ExchangedSuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: newPersonExchangedCash,
}
// 保存兑换素币清单
personSaved, err := exchangeCashPersonListRepository.Save(newPerson)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: 4,
OperationDescription: "参与" + activityFound.ExchangeActivityName,
}
// 操作素币,生成素币流水
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 获取平台素币兑换情况
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)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 重新计算现金池平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personSaved.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[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())
failureDataList = append(failureDataList, row)
continue
}
}
if len(failureDataList) == 0 {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personSaved, nil
} else { // 未知情况
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
return failureDataList, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return failureDataList, nil
}
return failureDataList, nil
}
// 页面新增兑换素币清单
... ...
... ... @@ -460,7 +460,7 @@ func (controller *SuMoneyController) ImportExchangeList () {
if i > 2 {
if len(row) != 3 {
for j, cell := range row {
if cell == "" {
if cell == " " {
utils_tool.InsertSlice(j, row, newstr)
}
}
... ... @@ -490,6 +490,8 @@ func (controller *SuMoneyController) ImportExchangeList () {
// 新增成功计数
var successDataCount int64
var createExchangeCashPersonCommands []*command.CreateExchangeCashPersonCommand
for i, row := range rows {
if i > 2 && len(row) > 1 {
// 创建兑换清单命令
... ... @@ -500,18 +502,22 @@ func (controller *SuMoneyController) ImportExchangeList () {
createExchangeCashPersonCommand.PersonAccount = row[1]
r2, _ := strconv.ParseFloat(row[2], 64)
createExchangeCashPersonCommand.ExchangedSuMoney = r2
// 创建兑换素币清单
_, err := cashPoolService.ImportCreateExchangeCashPerson(createExchangeCashPersonCommand)
if err != nil { // 导入失败处理
rows[i] = append(rows[i], err.Error())
failureDataList = append(failureDataList, rows[i]) // 导入失败的记录
} else { // 导入成功处理
successDataCount += 1
}
createExchangeCashPersonCommands = append(createExchangeCashPersonCommands, createExchangeCashPersonCommand)
}
}
// 创建兑换素币清单
failureDataList, err = cashPoolService.ImportCreateExchangeCashPerson(createExchangeCashPersonCommands, failureDataList)
if err != nil { // 导入失败处理
response = utils.ResponseError(controller.Ctx, err)
controller.Data["json"] = response
controller.ServeJSON()
return
} else {
successDataCount = int64(len(rows) - 3 - len(failureDataList))
}
// 导入失败返回
ret = map[string]interface{}{
"successCount": successDataCount,
"fail": map[string]interface{}{
... ... @@ -520,6 +526,7 @@ func (controller *SuMoneyController) ImportExchangeList () {
},
}
// 导入成功返回
if successDataCount == int64(len(rows) - 3) {
ret = map[string]interface{}{
"successCount": successDataCount,
... ...