作者 陈志颖

fix:修改现金池逻辑

... ... @@ -21,7 +21,7 @@ import (
type CashPoolService struct {
}
// 新增现金池
// 新增现金池,投入现金池总额
func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *command.CreateCashPoolCommand) (interface{}, error) {
if err := createCashPoolCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -37,6 +37,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
transactionContext.RollbackTransaction()
}()
// 员工仓储初始化
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -46,17 +47,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
employeeRepository = value
}
// 增加公司id判断
count, _, err := employeeRepository.Find(map[string]interface{}{
"companyId": createCashPoolCommand.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司id")
}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -65,19 +56,8 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
} else {
employeeDao = value
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(createCashPoolCommand.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 cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -87,48 +67,64 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
cashPoolRepository = value
}
countCashPools, _, err := cashPoolRepository.Find(tool_funs.SimpleStructToMap(createCashPoolCommand))
//公司id判断
count, _, err := employeeRepository.Find(map[string]interface{}{
"companyId": createCashPoolCommand.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司id")
}
// 获取平台素币兑换情况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(createCashPoolCommand.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) // 平台已兑换素币
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64) // 平台未兑换素币
count, cashPools, err := cashPoolRepository.Find(tool_funs.SimpleStructToMap(createCashPoolCommand))
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fmt.Print(count, "\n")
if count == 0 { // 新增现金池
newCashPool := &domain.CashPool{
CompanyId: createCashPoolCommand.CompanyId,
Cash: createCashPoolCommand.Cash,
ExchangedCash: 0,
UnExchangeCash: 0,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeCash: createCashPoolCommand.Cash,
ExchangedSuMoney: 0,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: 0,
CreateTime: time.Now(),
}
if countCashPools == 0 { // 现金池为空时处理
newCashPool.ExchangedCash = 0.0
newCashPool.UnExchangeCash = createCashPoolCommand.Cash
} else {
systemCashStatistics, err := employeeDao.CalculateSystemCash(createCashPoolCommand.CompanyId)
if err != nil {
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
return cashPool, nil
}
newCashPool.ExchangedCash = systemCashStatistics["systemExchangedCash"].(float64)
newCashPool.UnExchangeCash = systemCashStatistics["systemUnExchangeCash"].(float64) + createCashPoolCommand.Cash
// 计算系统平均兑换汇率
var rate float64
if systemExchangedSuMoney == 0 {
rate = 0
} else {
rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", newCashPool.ExchangedCash / systemExchangedSuMoney), 64) // 平均兑换汇率
} else { // 更新现金池
if createCashPoolCommand.Cash < cashPools[0].ExchangedCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "投入的现金值必须大于当前已兑换现金值")
}
newCashPool.Rate = rate
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].ExchangedCash + cashPools[0].UnExchangeCash)),
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: cashPools[0].Rate,
CreateTime: time.Now(),
}
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -137,6 +133,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
}
return cashPool, nil
}
}
}
// 返回现金池
... ... @@ -155,6 +152,7 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
transactionContext.RollbackTransaction()
}()
// 员工仓储初始化
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -164,6 +162,36 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
employeeRepository = value
}
// 兑换现金活动仓储初始化
var exchangeActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeActivityRepository = value
}
// 现金池仓储初始化
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 员工DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
// 增加公司id判断
count, _, err := employeeRepository.Find(map[string]interface{}{
"companyId": getCashPoolQuery.CompanyId,
... ... @@ -175,21 +203,11 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司id")
}
// 获取上次兑换活动兑换汇率
var exchangeActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
exchangeActivityRepository = value
}
// 获取上次兑换活动兑换汇率查询
var lastActivityRate float64
listExchangeCashActivityQuery := map[string]interface{}{
"companyId": getCashPoolQuery.CompanyId,
}
var lastActivityRate float64
if count, activities, err := exchangeActivityRepository.Find(listExchangeCashActivityQuery); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -200,15 +218,6 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
}
}
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
// 计算系统素币
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(getCashPoolQuery.CompanyId)
if err != nil {
... ... @@ -217,19 +226,10 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) // 平台已兑换素币
//systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) // 平台已兑换素币
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64) // 平台未兑换素币
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 查找当前公司现金池
if count, cashPools, err := cashPoolRepository.Find(tool_funs.SimpleStructToMap(getCashPoolQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -244,7 +244,7 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
"companyId": getCashPoolQuery.CompanyId,
"exchangedCash": 0,
"unExchangeCash": 0,
"exchangedSuMoney": systemExchangedSuMoney,
"exchangedSuMoney": 0,
"unExchangeSuMoney": systemUnExchangeSuMoney,
"rate": 0, // 平均兑换汇率
"lastRate": 0, // 上期活动兑换汇率
... ... @@ -1266,6 +1266,46 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
exchangeCashActivityRepository = value
}
// 现金池仓储初始化
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
}
// 操作素币服务初始化
var operationSuMoneyService service.OperationSuMoneyService
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
operationSuMoneyService = value
}
// 现金池DAO初始化
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
}
// 用户DAO初始化
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
// 获取兑换清单
personFound, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"listId": updateExchangeCashPersonCommand.ListId})
if err != nil {
... ... @@ -1275,6 +1315,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ListId)))
}
fmt.Printf("Person Found: %+v\n", personFound)
// 获取相关兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": personFound.ExchangeCashActivityId})
if err != nil {
... ... @@ -1292,12 +1334,14 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换清单更新
// 保存兑换素币清单更新
personUpdated, err := exchangeCashPersonListRepository.Save(personFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fmt.Printf("Person Updated: %+v\n", personUpdated)
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personFound.ExchangeCashActivityId,
... ... @@ -1308,16 +1352,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
ExchangeRate: activityFound.Rate,
}
// 更新员工素币,生成素币兑换流水记录
var operationSuMoneyService service.OperationSuMoneyService
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
operationSuMoneyService = value
}
// 操作素币服务以及生成素币兑换流水记录命令
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: personFound.EmployeeInfo.Uid,
Operator: updateExchangeCashPersonCommand.Operator,
... ... @@ -1326,6 +1361,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
OperationDescription: activityFound.ExchangeActivityName + "素币调整",
}
// 判断操作素币类型
if updateExchangeCashActivityCommand.ExchangedSuMoney - personUpdated.ExchangedSuMoney > 0 {
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - personUpdated.ExchangedSuMoney)
operationSuMoneyCommand.OperationType = 1
... ... @@ -1334,6 +1370,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
operationSuMoneyCommand.OperationType = 3
}
// 操作素币并生成素币流水
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())
... ... @@ -1342,10 +1379,14 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
fmt.Printf("Activity Found: %+v\n", activityFound)
// 更新兑换活动
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())
... ... @@ -1354,86 +1395,59 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
// 更新现金池
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
}
fmt.Printf("Activity Updated: %+v\n", activityUpdated)
// 统计活动已兑换素币
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.ActivityId)
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityUpdated.ActivityId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if activitySuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 统计平台现金兑换情况
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityUpdated.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 判断是否超过平台未兑换现金
if activitySuMoney * activityFound.Rate > systemCashStatistics["systemUnExchangeCash"].(float64) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 重新获取系统现金兑换情况
newSystemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := newSystemCashStatistics["systemExchangedCash"].(float64)
//systemUnExchangeCash := newSystemCashStatistics["systemUnExchangeCash"].(float64)
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
// 统计平台素币兑换状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityUpdated.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 cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cashPoolRepository = value
// 判断是否超过平台未兑换现金
if activitySuMoney * activityUpdated.Rate > systemUnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 获取现金池
// 重新获取系统现金兑换情况
//newSystemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//}
//if newSystemCashStatistics == nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
//}
//newSystemExchangedCash := newSystemCashStatistics["systemExchangedCash"].(float64)
//newSystemUnExchangeCash := newSystemCashStatistics["systemUnExchangeCash"].(float64)
// 获取当前现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
"companyId": activityUpdated.CompanyId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1442,7 +1456,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 计算平均兑换汇率
// 计算现金池平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
... ... @@ -1450,22 +1464,37 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
fmt.Printf("CashPool Found: %+v\n", cashPools[0])
fmt.Printf("UpdateExchangeCashPersonCommand: %+v\n", updateExchangeCashPersonCommand)
updateExchangedCash := cashPools[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney) * activityUpdated.Rate
fmt.Print(updateExchangedCash,"\n")
updateUnExchangeCash := cashPools[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney) * activityUpdated.Rate
fmt.Print(updateUnExchangeCash, "\n")
updateExchangedSuMoney := cashPools[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney)
fmt.Print(updateExchangedSuMoney, "\n")
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: cashPools[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney) * activityFound.Rate,
UnExchangeCash: cashPools[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney) * activityFound.Rate,
ExchangedCash: updateExchangedCash,
UnExchangeCash: updateUnExchangeCash,
Rate: newRate,
ExchangedSuMoney: cashPools[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFound.ExchangedSuMoney),
ExchangedSuMoney: updateExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
}
fmt.Print(updateCashPoolCommand.ExchangedCash, "\n")
fmt.Printf("UpdateCashPoolCommand=%+v\n", updateCashPoolCommand)
// 更新现金池
if err := cashPools[0].Update(tool_funs.SimpleStructToMap(updateCashPoolCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存现金池更新
cashPoolUpdated, err := cashPoolRepository.Save(cashPools[0])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1474,9 +1503,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
fmt.Printf("Updated CashPool: %+v\n", cashPoolUpdated)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personUpdated, nil
}
... ...
... ... @@ -1102,8 +1102,10 @@ func (taskService *TaskService) UpdateTask(updateTaskCommand *command.UpdateTask
updateData["assignedPerson"] = &domain.EmployeeInfo{}
}
}
// TODO 任务返回错误类型
if err := task.Update(updateData); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
//return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
return nil, err
}
if task, err := taskRepository.Save(task); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ...
... ... @@ -23,7 +23,9 @@ type CashPoolRepository interface {
}
func (cashPool *CashPool) Update(data map[string]interface{}) error {
if cash, ok := data["cash"]; ok {
cashPool.Cash = cash.(float64)
}
if exchangedCash, ok := data["exchangedCash"]; ok {
cashPool.ExchangedCash = exchangedCash.(float64)
}
... ... @@ -47,5 +49,9 @@ func (cashPool *CashPool) Identity() interface{} {
return nil
}
return cashPool.CashPoolId
//if cashPool.CompanyId == 0 {
// return nil
//}
//return cashPool.CompanyId
}
... ...
... ... @@ -160,7 +160,7 @@ func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] int
ColumnExpr("sum(su_money_transaction_record.su_money) AS system_changed_su_money").
Where("e.company_id = ?", companyId).
Where("e.status = ?", 1).
Where(`su_money_transaction_record.record_type = ?`, 1).
Where(`su_money_transaction_record.record_type = ?`, 5).
Select(&systemExchangedSuMoney); err != nil {
return nil, err
}
... ...
... ... @@ -39,7 +39,7 @@ func (dao *TaskDao) UpdateExpiredPlannedCompletionTimeBidTask() error {
tx := dao.transactionContext.PgTx
_, err := tx.Query(
pg.Scan(),
"UPDATE tasks SET task_status = ? WHERE planned_completion_time > ? AND task_type = ? AND task_status = ?",
"UPDATE tasks SET task_status = ? WHERE planned_completion_time < ? AND task_type = ? AND task_status = ?",
domain.TASK_STATUS_EXPIRED, currentDay, domain.TASK_TYPE_BID, domain.TASK_STATUS_UNCLAIMED)
return err
}
... ...
... ... @@ -14,6 +14,7 @@ type CashPoolRepository struct {
func (repository *CashPoolRepository) Save(cashPool *domain.CashPool) (*domain.CashPool, error) {
tx := repository.transactionContext.PgTx
fmt.Print(cashPool.Identity(), "\n")
if cashPool.Identity() == nil {
if _, err := tx.QueryOne(
pg.Scan(&cashPool.CashPoolId, &cashPool.Cash, &cashPool.CompanyId, &cashPool.ExchangedCash, &cashPool.UnExchangeCash, &cashPool.ExchangedSuMoney, &cashPool.UnExchangeSuMoney, &cashPool.Rate, &cashPool.CreateTime),
... ... @@ -75,9 +76,12 @@ func (repository *CashPoolRepository) FindOne(queryOptions map[string]interface{
tx := repository.transactionContext.PgTx
cashPoolModel := new(models.CashPool)
query := tx.Model(cashPoolModel)
if cashPoolId, ok := queryOptions["cashPoolId"]; ok {
query = query.Where("cash_pool.id = ?", cashPoolId)
if companyId, ok := queryOptions["companyId"]; ok {
query = query.Where("cash_pool.company_id = ?", companyId)
}
//if cashPoolId, ok := queryOptions["cashPoolId"]; ok {
// query = query.Where("cash_pool.id = ?", cashPoolId)
//}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -8,8 +8,11 @@ import (
var Logger log.Logger
// TODO 日志输出
func init() {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
}
... ...