作者 陈志颖

refactor:导入素币兑换清单

... ... @@ -1319,7 +1319,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
// 导入新增兑换素币清单
func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExchangeCashPersonCommands []*command.CreateExchangeCashPersonCommand, failureDataList []interface{}) ([]interface{}, error) {
// 批量校验
// 新增素币兑换清单命令校验
for _, createExchangeCashPersonCommand := range createExchangeCashPersonCommands {
if err := createExchangeCashPersonCommand.ValidateCommand(); err != nil {
row := []interface{}{
... ... @@ -1332,7 +1332,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
continue
}
}
if len(failureDataList) > 0 {
return []interface{}{}, application.ThrowError(application.TRANSACTION_ERROR, "校验失败")
}
... ... @@ -1432,12 +1431,11 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
activityFoundExchangedSuMoney := activityFound.ExchangedSuMoney
activityFoundExchangedCash := activityFound.ExchangedCash
activityFoundExchangedSuMoney := activityFound.ExchangedSuMoney // 当前兑换活动未兑换素币
activityFoundExchangedCash := activityFound.ExchangedCash // 当前兑换活动已兑换素币
// 获取当前公司现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
cashPoolFound, err := cashPoolRepository.FindOne(map[string]interface{}{
"companyId": activityFound.CompanyId,
})
if err != nil {
... ... @@ -1450,7 +1448,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
if len(cashPoolsFound) == 0 {
if cashPoolFound == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
... ... @@ -1460,17 +1458,14 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
cashPoolFoundUnExchangeCash := cashPoolFound.UnExchangeCash // 当前公司现金池未兑换现金
cashPoolFoundExchangedCash := cashPoolFound.ExchangedCash // 当前公司现金池已兑换现金
cashPoolFoundUnExchangeCash := cashPoolsFound[0].UnExchangeCash
cashPoolFoundExchangedCash := cashPoolsFound[0].ExchangedCash
// 获取员工查询条件
// 判断当前员工是否有效
getEmployee := map[string]interface{}{
"account": createExchangeCashPersonCommand.PersonAccount,
"companyId": activityFound.CompanyId,
}
// 判断当前员工是否有效
employeeFound, err := employeeRepository.FindOne(getEmployee)
if err != nil {
row := []interface{}{
... ... @@ -1491,29 +1486,46 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
}
failureDataList = append(failureDataList, row)
continue
} else {
employeeFoundSuMoney := employeeFound.SuMoney
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - employeeFoundSuMoney) {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前兑换素币超过本人持有的素币值",
}
failureDataList = append(failureDataList, row)
continue
}
}
employeeFoundSuMoney := employeeFound.SuMoney // 当前导入员工持有的素币值
//else {
// employeeFoundSuMoney := employeeFound.SuMoney
// // 判断该员工兑换的素币是否超过本人持有的素币
// if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - employeeFoundSuMoney) {
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "当前兑换素币超过本人持有的素币值",
// }
// failureDataList = append(failureDataList, row)
// continue
// }
//}
employeeFoundSuMoney := employeeFound.SuMoney
//// 判断当前员工是否已经在素币兑换清单中
//_, 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
//}
// 判断当前员工是否已经在素币兑换清单中
_, peopleFound, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
personFound, _ := exchangeCashPersonListRepository.FindOne(map[string]interface{}{
"employeeAccount": employeeFound.EmployeeInfo.EmployeeAccount,
"exchangeCashActivityId": activityFound.ActivityId,
"offset": 0,
"limit": 1,
"activityId": activityFound.ActivityId,
})
if err != nil {
row := []interface{}{
... ... @@ -1525,29 +1537,213 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
//if len(peopleFound) != 0 {
// peopleFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
// // 判断该员工兑换的素币是否超过本人持有的素币
// if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFoundExchangedSuMoney {
// if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - peopleFoundExchangedSuMoney) {
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "当前兑换素币超过本人持有的素币值",
// }
// failureDataList = append(failureDataList, row)
// continue
// }
// }
//}
if len(peopleFound) > 0 { // 当前导入员工在素币兑换清单中,判断追加素币兑换或撤回素币兑换
// 获取当前员工已兑换素币
personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
if personFound == nil { // 当前人员不在兑换素币清单中,新增兑换素币清单
// 判断该员工兑换的素币是否超过本人持有的素币
if (createExchangeCashPersonCommand.ExchangedSuMoney - employeeFoundSuMoney) > employeeFoundSuMoney {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前兑换素币超过本人持有的素币值",
}
failureDataList = append(failureDataList, row)
continue
}
// 新增兑换素币清单
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 {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if personSaved == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"保存到兑换素币清单失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 获取当前兑换员工已兑换现金
//personFoundExchangedCash := peopleFound[0].ExchangedCash
// 操作素币,生成素币流水
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
OperationType: 4, // 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
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
}
// 更新兑换活动
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFoundExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFoundExchangedCash + 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,
err.Error(),
}
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 createExchangeCashPersonCommand.ExchangedSuMoney * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
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 = (cashPoolFoundExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolFound.CashPoolId,
Cash: cashPoolFound.Cash,
ExchangedCash: cashPoolFoundExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolFoundUnExchangeCash - personSaved.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolFound.LastRate,
}
if err := cashPoolFound.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(cashPoolFound)
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 { // 当前人员存在兑换素币清单中
personFoundExchangedSuMoney := personFound.ExchangedSuMoney // 当前素币清单已兑换素币
personFoundExchangedCash := personFound.ExchangedCash // 当前素币清单已兑换现金
if createExchangeCashPersonCommand.ExchangedSuMoney < personFoundExchangedSuMoney { // 当前兑换的素币小于本人已兑换素币,撤回兑换素币
// 素币减量
suMoneyDecrement := personFoundExchangedSuMoney - createExchangeCashPersonCommand.ExchangedSuMoney
... ... @@ -1555,16 +1751,14 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
// 现金减量
cashDecrement := suMoneyDecrement * activityFound.Rate
// 更新兑换素币清单命令
// 更新兑换素币清单
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ListId: personFound.ListId,
ExchangedSuMoney: personFoundExchangedSuMoney - suMoneyDecrement,
ExchangedCash: (personFoundExchangedSuMoney - suMoneyDecrement) * activityFound.Rate,
ExchangedCash: personFoundExchangedCash - cashDecrement,
Operator: createExchangeCashPersonCommand.Operator,
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
if err := personFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
... ... @@ -1574,9 +1768,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换素币清单更新
_, err := exchangeCashPersonListRepository.Save(peopleFound[0])
exchangeCashPersonListUpdated, err := exchangeCashPersonListRepository.Save(personFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
... ... @@ -1587,18 +1779,26 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
if exchangeCashPersonListUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"保存到兑换素币清单失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 更新素币兑换活动命令
// 更新素币兑换活动
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangeCashActivityId: personFound.ExchangeCashActivityId,
ExchangedSuMoney: activityFoundExchangedSuMoney - suMoneyDecrement,
ExchangedCash: activityFoundExchangedCash - cashDecrement,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
... ... @@ -1609,8 +1809,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
row := []interface{}{
... ... @@ -1633,25 +1831,21 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
continue
}
// 操作类型->记录类型对应: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,
OperationType: 41, // 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
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
... ... @@ -1667,20 +1861,6 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
continue
}
// 判断是否超过平台未兑换现金
if activityUpdated.ExchangedSuMoney > activityFoundExchangedSuMoney {
if (activityUpdated.ExchangedSuMoney - activityFoundExchangedSuMoney) * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
}
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
... ... @@ -1698,36 +1878,32 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"无效的公司",
"获取公司素币兑换情况失败",
}
failureDataList = append(failureDataList, row)
continue
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) // 公司已兑换素币
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64) // 公司未兑换素币
// 计算平均兑换汇率
// 更新现金池
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolFoundExchangedCash - cashDecrement) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
CashPoolId: cashPoolFound.CashPoolId,
Cash: cashPoolFound.Cash,
ExchangedCash: cashPoolFoundExchangedCash - cashDecrement,
UnExchangeCash: cashPoolFoundUnExchangeCash + cashDecrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
LastRate: cashPoolFound.LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
if err := cashPoolFound.Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
... ... @@ -1737,15 +1913,13 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
failureDataList = append(failureDataList, row)
continue
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1755,7 +1929,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
"更新现金池失败",
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1768,7 +1942,7 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
cashIncrement := suMoneyIncrement * activityFound.Rate
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - employeeFoundSuMoney) {
if suMoneyIncrement > employeeFoundSuMoney {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
... ... @@ -1779,69 +1953,71 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
continue
}
// 更新兑换素币清单命令
// 更新兑换素币清单
updateExchangeCashPersonCommand := &command.UpdateExchangeCashPersonCommand{
ListId: peopleFound[0].ListId,
ListId: personFound.ListId,
ExchangedSuMoney: personFoundExchangedSuMoney + suMoneyIncrement,
ExchangedCash: (personFoundExchangedSuMoney + suMoneyIncrement) * activityFound.Rate,
ExchangedCash: personFoundExchangedCash + cashIncrement,
Operator: createExchangeCashPersonCommand.Operator,
}
// 更新兑换素币清单
if err := peopleFound[0].Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
if err := personFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换素币清单更新
_, err := exchangeCashPersonListRepository.Save(peopleFound[0])
exchangeCashPersonListUpdated, err := exchangeCashPersonListRepository.Save(personFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
if exchangeCashPersonListUpdated == nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"保存到兑换现金活动失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 更新素币兑换活动命令
// 更新兑换活动
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: peopleFound[0].ExchangeCashActivityId,
ExchangeCashActivityId: personFound.ExchangeCashActivityId,
ExchangedSuMoney: activityFoundExchangedSuMoney + suMoneyIncrement,
ExchangedCash: activityFoundExchangedCash + cashIncrement,
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,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存兑换现金活动更新
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1851,31 +2027,27 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
"保存到兑换现金活动失败",
}
failureDataList = append(failureDataList, row)
continue
}
// 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
// 操作素币命令
// 操作素币,生成素币流水
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: employeeFound.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
SuMoney: suMoneyIncrement,
OperationType: 4,
OperationType: 4, // 操作类型->记录类型对应:1->3: 增加 2->4: 扣除 3->1: 兑换物资 4->5: 兑换现金活动 41->6: 撤回兑换现金素币 5->2: 任务奖励
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(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1885,33 +2057,19 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误",
"操作素币错误",
}
failureDataList = append(failureDataList, row)
continue
}
//// 判断是否超过平台未兑换现金
//if activityUpdated.ExchangedSuMoney > activityFoundExchangedSuMoney {
// if (activityUpdated.ExchangedSuMoney - activityFoundExchangedSuMoney) * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "已超过现金池未兑换现金",
// }
// failureDataList = append(failureDataList, row)
// continue
// }
//}
// 判断是否超过平台未兑换现金
if createExchangeCashPersonCommand.ExchangedSuMoney * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
if personFoundExchangedCash > cashPoolFoundUnExchangeCash {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
"已超过投入现金池的未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1942,46 +2100,40 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 计算平均兑换汇率
// 更新现金池命令
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate = (cashPoolFoundExchangedCash + cashIncrement) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
CashPoolId: cashPoolFound.CashPoolId,
Cash: cashPoolFound.Cash,
ExchangedCash: cashPoolFoundExchangedCash + cashIncrement,
UnExchangeCash: cashPoolFoundUnExchangeCash - cashIncrement,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
LastRate: cashPoolsFound[0].LastRate,
LastRate: cashPoolFound.LastRate,
}
// 更新现金池
if err := cashPoolsFound[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
if err := cashPoolFound.Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolsFound[0])
cashPoolUpdated, err := cashPoolRepository.Save(cashPoolFound)
if err != nil {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内务业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
... ... @@ -1991,247 +2143,49 @@ func (cashPoolService *CashPoolService) ImportCreateExchangeCashPerson(createExc
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"内部业务错误" + err.Error(),
err.Error(),
}
failureDataList = append(failureDataList, row)
continue
}
}
} else if len(peopleFound) == 0 { // 导入兑换素币清单员工不存在兑换素币清单中,新增兑换素币清单
// 判断该员工兑换的素币是否超过本人持有的素币
if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - employeeFoundSuMoney) {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"当前兑换素币超过本人持有的素币值",
}
failureDataList = append(failureDataList, row)
continue
}
// 新增兑换素币清单命令
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 {
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{
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
}
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFoundExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: activityFoundExchangedCash + 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,
"内部业务错误" + err.Error(),
}
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 > activityFoundExchangedSuMoney {
// if (activityUpdated.ExchangedSuMoney - activityFoundExchangedSuMoney) * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "已超过现金池未兑换现金",
// }
// failureDataList = append(failureDataList, row)
// continue
// }
//}
// 判断是否超过平台未兑换现金
if createExchangeCashPersonCommand.ExchangedSuMoney * activityUpdated.Rate > cashPoolFoundUnExchangeCash {
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"已超过现金池未兑换现金",
}
failureDataList = append(failureDataList, row)
continue
//return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 获取平台素币兑换情况
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 = (cashPoolFoundExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: cashPoolFoundExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolFoundUnExchangeCash - personSaved.ExchangedCash,
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 { // 未知情况
row := []interface{}{
createExchangeCashPersonCommand.PersonName,
createExchangeCashPersonCommand.PersonAccount,
createExchangeCashPersonCommand.ExchangedSuMoney,
"未知错误",
}
failureDataList = append(failureDataList, row)
continue
}
//if len(peopleFound) != 0 {
// peopleFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
// // 判断该员工兑换的素币是否超过本人持有的素币
// if createExchangeCashPersonCommand.ExchangedSuMoney > peopleFoundExchangedSuMoney {
// if employeeFoundSuMoney < (createExchangeCashPersonCommand.ExchangedSuMoney - peopleFoundExchangedSuMoney) {
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "当前兑换素币超过本人持有的素币值",
// }
// failureDataList = append(failureDataList, row)
// continue
// }
// }
//}
//if len(peopleFound) > 0 { // 当前导入员工在素币兑换清单中,判断追加素币兑换或撤回素币兑换
// // 获取当前员工已兑换素币
// //personFoundExchangedSuMoney := peopleFound[0].ExchangedSuMoney
//
// // 获取当前兑换员工已兑换现金
// //personFoundExchangedCash := peopleFound[0].ExchangedCash
//
//
//} else if len(peopleFound) == 0 { // 导入兑换素币清单员工不存在兑换素币清单中,新增兑换素币清单
//
//} else { // 未知情况
// row := []interface{}{
// createExchangeCashPersonCommand.PersonName,
// createExchangeCashPersonCommand.PersonAccount,
// createExchangeCashPersonCommand.ExchangedSuMoney,
// "未知错误",
// }
// failureDataList = append(failureDataList, row)
// continue
//}
}
if len(failureDataList) == 0 {
... ...
... ... @@ -47,6 +47,9 @@ func (repository *ExchangeCashPersonListRepository) FindOne(queryOptions map[str
if activityId, ok := queryOptions["activityId"]; ok {
query = query.Where("exchange_cash_person_list.activity_id = ?", activityId)
}
if employeeAccount, ok := queryOptions["employeeAccount"]; ok {
query = query.Where("exchange_cash_person_list.employee_account = ?", employeeAccount)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...