作者 陈志颖

refactor:优化现金池

... ... @@ -358,18 +358,16 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivityDeadline(listExc
for _, activity := range activities {
deadlines = append(deadlines, activity.Deadline.Format("2006-01-02"))
}
if len(deadlines) == 0 {
deadlines = []interface{}{}
}
return map[string]interface{}{
"deadlines": deadlines,
}, nil
}
}
// 搜索兑换现金活动
// 搜索兑换现金活动 for PC
func (cashPoolService *CashPoolService) SearchExchangeCashActivity(activityCommand *command.SearchExchangeCashActivityCommand) (interface{}, error) {
if err := activityCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -434,7 +432,6 @@ func (cashPoolService *CashPoolService) SearchExchangeCashActivity(activityComma
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"activities": activities,
... ... @@ -442,7 +439,7 @@ func (cashPoolService *CashPoolService) SearchExchangeCashActivity(activityComma
}
}
// 返回兑换现金活动列表
// 返回兑换现金活动列表 for app
func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCashActivityQuery *query.ListExchangeCashActivityQuery) (interface{}, error) {
if err := listExchangeCashActivityQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -476,7 +473,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
var t1 = time.Now()
var t2 = activity.Deadline
// 更新兑换现金活动命令
// 更新兑换现金活动倒计时命令
updateExchangeCashActivity := &command.UpdateExchangeCashActivityCommand {
ExchangeCashActivityId: activity.ActivityId,
CountDown: int64(t2.Sub(t1).Hours() / 24), // 重新计算活动倒计时
... ... @@ -489,15 +486,14 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
// 倒计时结束处理
if t2.Before(t1) {
updateExchangeCashActivity.CountDown = 0
updateExchangeCashActivity.Deadline = activity.Deadline
}
// 更新兑换现金活动
// 更新兑换现金活动倒计时
if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivity)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 保存兑换现金活动更新
// 保存兑换现金活动倒计时更新
_, err := exchangeActivityRepository.Save(activity)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -524,7 +520,6 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
}
activitiesWithTs = append(activitiesWithTs, activityWithTs)
}
return map[string]interface{}{
"count": count,
"activities": activitiesWithTs,
... ... @@ -699,14 +694,14 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
}
// 现金池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
}
//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
... ... @@ -728,15 +723,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
}
if updateExchangeCashActivityCommand.ExchangeRate != 0 { // 更新兑换活动兑换汇率
//// 增大汇率
//if updateExchangeCashActivityCommand.ExchangeRate > activityFound.Rate {
//
//}
//
//// 减小汇率
//if updateExchangeCashActivityCommand.ExchangeRate < activityFound.Rate {
//
//}
// 获取当前公司现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
... ... @@ -749,18 +735,18 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
}
// 获取活动素币统计
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.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, "无效的企业")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
//activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.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, "无效的企业")
//}
//activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 判断兑换活动清单中现金总金额是否超过平台未兑换现金值
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
if activityFound.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 获取该活动所有兑换清单
... ... @@ -776,9 +762,9 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
personFoundExchangedSuMoney := personFound.ExchangedSuMoney
// 更新兑换清单命令
updateExchangeCashActivityExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", updateExchangeCashActivityCommand.ExchangeRate * personFoundExchangedSuMoney), 64)
//updateExchangeCashActivityExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", updateExchangeCashActivityCommand.ExchangeRate * personFoundExchangedSuMoney), 64)
updateExchangeCashPerson := &command.UpdateExchangeCashPersonCommand {
ExchangedCash: updateExchangeCashActivityExchangedCash,
ExchangedCash: updateExchangeCashActivityCommand.ExchangeRate * personFoundExchangedSuMoney,
ExchangedSuMoney: personFoundExchangedSuMoney,
}
... ... @@ -805,32 +791,32 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 获取平台现金状况
systemCashStatistics, 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 := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
//systemCashStatistics, 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 := systemCashStatistics["systemExchangedCash"].(float64)
//systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 更新兑换活动命令已兑换现金
updateExchangeCashActivityCommand.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", updateExchangeCashActivityCommand.ExchangeRate * activityFound.ExchangedSuMoney), 64)
updateExchangeCashActivityCommand.ExchangedCash = updateExchangeCashActivityCommand.ExchangeRate * activityFound.ExchangedSuMoney
// 计算平均兑换汇率
var rate float64
if systemExchangedSuMoney == 0 {
rate = 0
} else {
rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (systemExchangedCash + (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash)) / systemExchangedSuMoney), 64)
rate = (cashPoolsFound[0].ExchangedCash + (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash)) / systemExchangedSuMoney
}
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
ExchangedCash: systemExchangedCash + (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash),
UnExchangeCash: systemUnExchangeCash - (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash) ,
ExchangedCash: cashPoolsFound[0].ExchangedCash + (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash),
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - (updateExchangeCashActivityCommand.ExchangedCash - activityFound.ExchangedCash) ,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: rate,
... ... @@ -865,7 +851,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
}
// 更新兑换活动命令已兑换素币
updateExchangeCashActivityCommand.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", activityFound.ExchangedSuMoney), 64)
updateExchangeCashActivityCommand.ExchangedSuMoney = activityFound.ExchangedSuMoney
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
... ... @@ -939,15 +925,15 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
employeeRepository = 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 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
... ... @@ -1003,9 +989,19 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
// 判断当前员工是否已经在素币兑换清单中
employeeExist, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{
"uid": employeeFound.EmployeeInfo.Uid,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employeeExist != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前员工已经在素币兑换清单中")
}
// 新增兑换清单命令
newPersonExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employeeFound.EmployeeInfo.Uid,
... ... @@ -1041,14 +1037,12 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
updateExchangeCashActivityExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
// 更新兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personSaved.ExchangeCashActivityId,
ExchangeActivityName: activityFound.ExchangeActivityName,
ExchangedSuMoney: activityFound.ExchangedSuMoney + createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: updateExchangeCashActivityExchangedCash,
ExchangedCash: activityFound.ExchangedCash + createExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
... ... @@ -1069,14 +1063,14 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
}
// 获取兑换活动素币兑换情况
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.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, "无效的企业")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
//activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.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, "无效的企业")
//}
//activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 获取当前现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
... ... @@ -1090,7 +1084,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
}
// 判断兑换活动的现金是否超过现金池未兑换现金
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
if activityUpdated.ExchangedSuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
... ... @@ -1121,18 +1115,15 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney), 64)
newRate = (cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash) / systemExchangedSuMoney
}
updateCashPoolExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash), 64)
updateCashPoolUnExchangeCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cashPoolsFound[0].UnExchangeCash - personSaved.ExchangedCash), 64)
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: updateCashPoolExchangedCash,
UnExchangeCash: updateCashPoolUnExchangeCash,
ExchangedCash: cashPoolsFound[0].ExchangedCash + personSaved.ExchangedCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - personSaved.ExchangedCash,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
Rate: newRate,
... ... @@ -1338,15 +1329,15 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
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 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
... ... @@ -1380,7 +1371,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
}
// 更新兑换清单,个人已兑换现金计算
updateExchangeCashPersonCommand.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", updateExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate), 64)
updateExchangeCashPersonCommand.ExchangedCash = updateExchangeCashPersonCommand.ExchangedSuMoney * activityFound.Rate
// 更新兑换素币清单
if err := personFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
... ... @@ -1393,13 +1384,11 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
updateActivityExchangeCash ,_ := strconv.ParseFloat(fmt.Sprintf("%.2f", activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate), 64)
// 更新素币兑换活动命令
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: personFound.ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney),
ExchangedCash: updateActivityExchangeCash,
ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate,
Deadline: activityFound.Deadline,
CountDown: activityFound.CountDown,
ExchangeRate: activityFound.Rate,
... ... @@ -1415,12 +1404,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
}
// 判断操作素币类型
if updateExchangeCashActivityCommand.ExchangedSuMoney - personUpdated.ExchangedSuMoney > 0 {
if updateExchangeCashActivityCommand.ExchangedSuMoney - personFound.ExchangedSuMoney > 0 { // 追加素币兑换
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
operationSuMoneyCommand.OperationType = 4
} else {
} else { // 撤回素币兑换
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
operationSuMoneyCommand.OperationType = 3
operationSuMoneyCommand.OperationType = 41
}
// 操作素币并生成素币流水
... ... @@ -1446,26 +1435,26 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
// 统计活动已兑换素币
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, "无效的公司")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
//// 统计活动已兑换素币
//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, "无效的公司")
//}
//activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 统计平台现金兑换情况
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)
//// 统计平台现金兑换情况
//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)
// 统计平台素币兑换状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityUpdated.CompanyId)
... ... @@ -1475,14 +1464,9 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
//systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
// 判断是否超过平台未兑换现金
if activitySuMoney * activityUpdated.Rate > systemUnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过现金池未兑换现金")
}
// 获取当前现金池
_, cashPoolsFound, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityUpdated.CompanyId,
... ... @@ -1494,25 +1478,28 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
systemExchangedCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cashPoolsFound[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate), 64)
systemExchangedSuMoney := cashPoolsFound[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
// 判断是否超过平台未兑换现金
if activityUpdated.ExchangedSuMoney * activityUpdated.Rate > cashPoolsFound[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
systemExchangedCash := cashPoolsFound[0].ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate
//systemExchangedSuMoney := cashPoolsFound[0].ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney)
// 计算现金池平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
newRate = systemExchangedCash / systemExchangedSuMoney
}
updateCashPoolUnExchangeCash, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cashPoolsFound[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityFound.Rate), 64)
// 更新现金池命令
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPoolsFound[0].CashPoolId,
Cash: cashPoolsFound[0].Cash,
ExchangedCash: systemExchangedCash,
UnExchangeCash: updateCashPoolUnExchangeCash,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash - (updateExchangeCashPersonCommand.ExchangedSuMoney - personFoundExchangedSuMoney) * activityUpdated.Rate,
Rate: newRate,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
... ... @@ -1618,9 +1605,6 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
// 获取兑换清单员工已兑换素币
personFoundExchangedSuMoney := personFound.ExchangedSuMoney
// 删除兑换清单人员
personDeleted, err := exchangeCashPersonListRepository.Remove(personFound)
// 获取相关兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": personFound.ExchangeCashActivityId})
if err != nil {
... ... @@ -1659,8 +1643,8 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
Uid: personFound.EmployeeInfo.Uid,
Operator: removeExchangeCashPersonCommand.Operator,
SuMoney: personFoundExchangedSuMoney,
OperationType: 1, // 增加素币
OperationDescription: activityFound.ExchangeActivityName + "素币调整",
OperationType: 41, // 删除兑换素币清单,撤回已兑换素币
OperationDescription: "删除兑换素币清单",
}
// 操作素币,生成素币流水
... ... @@ -1710,7 +1694,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if systemExchangedSuMoney == 0 {
newRate = 0
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (cashPoolsFound[0].ExchangedCash - personFoundExchangedSuMoney) / systemExchangedSuMoney), 64)
newRate = (cashPoolsFound[0].ExchangedCash - personFoundExchangedSuMoney) / systemExchangedSuMoney
}
// 更新现金池命令
... ... @@ -1719,7 +1703,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
ExchangedCash: cashPoolsFound[0].ExchangedCash - personFoundExchangedSuMoney,
UnExchangeCash: cashPoolsFound[0].UnExchangeCash + personFoundExchangedSuMoney,
Rate: newRate,
ExchangedSuMoney: cashPoolsFound[0].ExchangedSuMoney - personFoundExchangedSuMoney,
ExchangedSuMoney: systemExchangedSuMoney,
UnExchangeSuMoney: systemUnExchangeSuMoney,
}
... ... @@ -1737,6 +1721,9 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 删除兑换清单人员
personDeleted, err := exchangeCashPersonListRepository.Remove(personFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
package domain
import "time"
import (
"fmt"
"strconv"
"time"
)
// 现金池
type CashPool struct {
... ... @@ -27,19 +31,19 @@ func (cashPool *CashPool) Update(data map[string]interface{}) error {
cashPool.Cash = cash.(float64)
}
if exchangedCash, ok := data["exchangedCash"]; ok {
cashPool.ExchangedCash = exchangedCash.(float64)
cashPool.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", exchangedCash.(float64)), 64)
}
if unExchangeCash, ok := data["unExchangeCash"]; ok {
cashPool.UnExchangeCash = unExchangeCash.(float64)
cashPool.UnExchangeCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", unExchangeCash.(float64)), 64)
}
if exchangedSuMoney, ok := data["exchangedSuMoney"]; ok {
cashPool.ExchangedSuMoney = exchangedSuMoney.(float64)
cashPool.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", exchangedSuMoney.(float64)), 64)
}
if unExchangeSuMoney, ok := data["unExchangeSuMoney"]; ok {
cashPool.UnExchangeSuMoney = unExchangeSuMoney.(float64)
cashPool.UnExchangeSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", unExchangeSuMoney.(float64)), 64)
}
if rate, ok := data["rate"]; ok {
cashPool.Rate = rate.(float64)
cashPool.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", rate.(float64)), 64)
}
return nil
}
... ...
package domain
import "time"
import (
"fmt"
"strconv"
"time"
)
// 兑换现金活动
type ExchangeCashActivity struct {
... ... @@ -41,13 +45,13 @@ func (exchangeCashActivity *ExchangeCashActivity) Update(data map[string]interfa
exchangeCashActivity.CountDown = countDown.(int64)
}
if exchangedSuMoney, ok := data["exchangedSuMoney"]; ok && exchangedSuMoney != 0 {
exchangeCashActivity.ExchangedSuMoney = exchangedSuMoney.(float64)
exchangeCashActivity.ExchangedSuMoney, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", exchangedSuMoney.(float64)), 64)
}
if exchangedCash, ok := data["exchangedCash"]; ok && exchangedCash != 0 {
exchangeCashActivity.ExchangedCash = exchangedCash.(float64)
exchangeCashActivity.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", exchangedCash.(float64)), 64)
}
if exchangeRate, ok := data["exchangeRate"]; ok && exchangeRate != 0 {
exchangeCashActivity.Rate = exchangeRate.(float64)
exchangeCashActivity.Rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", exchangeRate.(float64)), 64)
}
return nil
}
... ...
package domain
import (
"fmt"
"strconv"
)
// 参与兑换现金人员
type ExchangeCashPersonList struct {
ListId int64 `json:"listId"` // 清单id
... ... @@ -30,7 +35,7 @@ func (exchangeCashPersonList *ExchangeCashPersonList) Update(data map[string]int
exchangeCashPersonList.ExchangedSuMoney = exchangedSuMoney.(float64)
}
if exchangedCash, ok := data["exchangedCash"]; ok && exchangedCash != 0 {
exchangeCashPersonList.ExchangedCash = exchangedCash.(float64)
exchangeCashPersonList.ExchangedCash, _ = strconv.ParseFloat(fmt.Sprintf("%.2F", exchangedCash.(float64)), 64)
}
return nil
}
\ No newline at end of file
... ...
... ... @@ -8,6 +8,7 @@ const (
SU_MONEY_TRANSACTION_RECORD_TYPE_INCREASE //增加
SU_MONEY_TRANSACTION_RECORD_TYPE_DEDUCT //扣除
SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH //兑换现金
SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE // 兑换现金撤销
)
// 素币事务记录
... ...
... ... @@ -10,49 +10,6 @@ type CashPoolDao struct {
transactionContext *pgTransaction.TransactionContext
}
// 计算系统已兑换素币、未兑换素币
//func (dao *CashPoolDao) CalculateSystemSuMoney(companyId int64) ( map[string]interface{}, error) {
// var systemUnExchangeSuMoney float64
// var systemExchangedSuMoney float64
// tx := dao.transactionContext.PgTx
// employeeModel := new(models.Employee)
// if err := tx.Model(employeeModel).
// ColumnExpr("sum(employee.su_money) AS system_unExchange_su_money").
// Where("employee.company_id = ?", companyId).
// Select(&systemUnExchangeSuMoney); err != nil {
// return nil, err
// }
// exchangeCashActivityModel := new(models.ExchangeCashActivity)
// if err := tx.Model(exchangeCashActivityModel).
// ColumnExpr("sum(exchange_cash_activity.exchanged_su_money) AS system_changed_su_money").
// Where("exchange_cash_activity.company_id = ?", companyId).
// Select(&systemExchangedSuMoney); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "systemUnExchangeSuMoney": systemUnExchangeSuMoney,
// "systemExchangedSuMoney": systemExchangedSuMoney,
// }, nil
//}
// 计算系统已兑换现金、未兑换现金
//func (dao *CashPoolDao) CalculateSystemCash(companyId int64) (map[string]interface{}, error) {
// var systemExchangedCash float64 // 系统已兑换现金
// var systemUnExchangeCash float64 // 系统未兑换现金
// tx := dao.transactionContext.PgTx
// exchangeCashActivityModel := new(models.ExchangeCashActivity)
// if err := tx.Model(exchangeCashActivityModel).
// ColumnExpr("sum(exchange_cash_activity.exchanged_cash) AS system_exchanged_cash").
// Where("exchange_cash_activity.company_id = ?", companyId).
// Select(&systemExchangedCash) ; err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "systemExchangedCash": systemExchangedCash,
// "systemUnExchangeCash": systemUnExchangeCash,
// }, nil
//}
// 兑换活动兑换清单已兑换素币、已兑换现金
func (dao *CashPoolDao) CalculateActivityExchangedSuMoney(activityId int64) (map[string]interface{}, error) {
var activityExchangedSuMoney float64
... ...
... ... @@ -144,6 +144,7 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{
func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] interface{}, error) {
var systemUnExchangeSuMoney float64
var systemExchangedSuMoney float64
var systemExchangedSuMoneyRestore float64
tx := dao.transactionContext.PgTx
// 系统未兑换素币
employeeModel := new(models.Employee)
... ... @@ -160,13 +161,22 @@ func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] int
ColumnExpr("sum(su_money_transaction_record.su_money) AS system_exchanged_su_money").
Where("e.company_id = ?", companyId).
Where("e.status = ?", 1).
Where(`su_money_transaction_record.record_type = ?`, 5).
Where(`su_money_transaction_record.record_type = ?`, domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH).
Select(&systemExchangedSuMoney); err != nil {
return nil, err
}
// 系统撤销的兑换现金素币
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
ColumnExpr("sum(su_money_transaction_record.su_money) AS system_exchanged_su_money").
Where("e.company_id = ?", companyId).
Where("e.status = ?", 1).
Where(`su_money_transaction_record.record_type = ?`, domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE).
Select(&systemExchangedSuMoneyRestore); err != nil {
return nil, err
}
return map[string] interface{} {
"systemUnExchangeSuMoney": systemUnExchangeSuMoney,
"systemExchangedSuMoney": systemExchangedSuMoney,
"systemExchangedSuMoney": systemExchangedSuMoney - systemExchangedSuMoneyRestore,
},nil
}
... ... @@ -205,7 +215,7 @@ func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interf
// 计算个人素币收支
func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
var (
incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加 6: 兑换素币撤回
expendSuMoney float64 // 消耗的素币(1:兑换物资,4:扣除, 5: 兑换现金)
)
tx := dao.transactionContext.PgTx
... ... @@ -214,7 +224,7 @@ func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transaction
if err := tx.Model(suMoneyTransactionRecordModel).
ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3, 6})).
Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
Select(&incomeSuMoney); err != nil {
... ... @@ -270,54 +280,6 @@ func (dao *EmployeeDao) CalculateContributionsTransactionRecord(uid int64, trans
}, nil
}
//func (dao *EmployeeDao) CalculateEmployeesSuMoney(queryOptions map[string]interface{}) (map[string]interface{}, error) {
// var ret []struct {
// Uid int
// EmployeeName string
// EmployeeSuMoney float64
// }
// tx := dao.transactionContext.PgTx
// var (
// startTime time.Time
// endTime time.Time
// companyId int
// )
// if company, ok := queryOptions["companyId"].(int); ok {
// companyId = company
// }
// if start, ok := queryOptions["startTime"].(time.Time); ok {
// startTime = start
// } else {
// startTime = time.Date(1971, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location())
// }
// if end, ok := queryOptions["endTime"].(time.Time); ok {
// endTime = end
// } else {
// endTime = time.Now()
// }
// fmt.Print(startTime, "\n")
// fmt.Print(endTime, "\n")
// fmt.Print(companyId, "\n")
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
// ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
// ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
// ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
// Where(`e.company_id = ?`, companyId).
// Where(`e.status = ?`, 1).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
// Where(`su_money_transaction_record.create_time > ?`, startTime).
// Where(`su_money_transaction_record.create_time < ?`, endTime).
// Group("su_money_transaction_record.employee").
// Order("employee_su_money DESC").
// Select(&ret); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "employeesSuMoney": ret,
// }, nil
//}
// 贡献值、财富值总榜和年榜,
func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]interface{}) (interface{}, error) {
var retWealth []struct { // 个人财富值
... ... @@ -356,7 +318,7 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
queryWealth = queryWealth.ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money")
queryWealth = queryWealth.ColumnExpr("ROW_NUMBER() OVER (ORDER BY sum(su_money_transaction_record.su_money) DESC) AS ranking")
queryWealth = queryWealth.Where(`e.status = ?`, 1)
queryWealth = queryWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3}))
queryWealth = queryWealth.Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3, 6}))
if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
queryWealth = queryWealth.Where("e.company_id = ?", companyId)
}
... ... @@ -508,98 +470,6 @@ func (dao *EmployeeDao) ContributionsWealthRanking(queryOptions map[string]inter
}, nil
}
// 排行榜统计,排名
//func (dao *EmployeeDao) CalculateEmployeesRankingList(companyId int, startTime time.Time, endTime time.Time, offset int, limit int) (map[string]interface{}, error) {
// var retWealth []struct {
// Uid int
// EmployeeName string
// EmployeeSuMoney float64
// Ranking int
// }
// var retContributions []struct { // 员工贡献值
// Uid int
// EmployeeName string
// EmployeesContributions float64
// Ranking int
// }
// var retContributionDecrease []struct { // 员工减少的贡献值
// Uid int
// EmployeeName string
// EmployeesContributions float64
// Ranking int
// }
// tx := dao.transactionContext.PgTx
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// if limit < -1 {
// limit = 20
// }
// if offset < -1 {
// offset = 0
// }
// // 员工财富值
// if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
// ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
// ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
// ColumnExpr("sum(su_money_transaction_record.su_money) AS employee_su_money").
// Where(`e.company_id = ?`, companyId).
// Where(`e.status = ?`, 1).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})). // 增加,任务奖励的
// Where(`su_money_transaction_record.create_time > ?`, startTime).
// Where(`su_money_transaction_record.create_time < ?`, endTime).
// Group("su_money_transaction_record.employee").
// Order("employee_su_money DESC").
// Limit(limit).
// Offset(offset).
// Select(&retWealth); err != nil {
// return nil, err
// }
// // 增加的贡献值
// if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
// ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
// ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
// ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
// Where(`e.company_id = ?`, companyId).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
// Where(`e.status = ?`, 1).
// Where(`su_money_transaction_record.create_time > ?`, startTime).
// Where(`su_money_transaction_record.create_time < ?`, endTime).
// Group("su_money_transaction_record.employee").
// Order("employees_contributions DESC").
// Limit(limit).
// Offset(offset).
// Select(&retContributions); err != nil {
// return nil, err
// }
// // 减少的贡献值
// if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
// ColumnExpr("su_money_transaction_record.employee->>'uid' AS uid").
// ColumnExpr("su_money_transaction_record.employee->>'employeeName' AS employee_name").
// ColumnExpr("sum(su_money_transaction_record.su_money) AS employees_contributions").
// Where(`e.company_id = ?`, companyId).
// Where(`su_money_transaction_record.record_type = ?`, 4).
// Where(`e.status = ?`, 1).
// Where(`su_money_transaction_record.create_time > ?`, startTime).
// Where(`su_money_transaction_record.create_time < ?`, endTime).
// Group("su_money_transaction_record.employee").
// Order("employees_contributions DESC").
// Limit(limit).
// Offset(offset).
// Select(&retContributionDecrease); err != nil {
// return nil, err
// }
// for i := 0; i < len(retContributions); i++ {
// for j := 0; j < len(retContributionDecrease); j++ {
// if retContributions[i].Uid == retContributionDecrease[j].Uid {
// retContributions[i].EmployeesContributions -= retContributionDecrease[j].EmployeesContributions
// }
// }
// }
// return map[string]interface{}{
// "employeesContributions": retContributions,
// "employeesWealth": retWealth,
// }, nil
//}
// 公司员工贡献值分组统计
func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
var ret []struct { // 员工贡献值
... ...
... ... @@ -73,6 +73,10 @@ func (service *OperationSuMoneyService) Operation(uid int64, operatorUid int64,
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH
transferSuMoney = 0 - suMoney
}
if operationType == 41 { // 兑换素币调整退回
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_EXCHANGE_CASH_RESTORE
transferSuMoney = suMoney
}
if operationType == 5 { // 任务奖励
recordType = domain.SU_MONEY_TRANSACTION_RECORD_TYPE_AWARD
transferSuMoney = suMoney
... ...