作者 陈志颖

refactor:修改新建现金池时判断公司id方法

... ... @@ -27,6 +27,7 @@ func (statisticsService *StatisticsService) PersonNotificationStatistics(personN
defer func() {
transactionContext.RollbackTransaction()
}()
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -35,6 +36,7 @@ func (statisticsService *StatisticsService) PersonNotificationStatistics(personN
} else {
employeeDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -43,6 +45,7 @@ func (statisticsService *StatisticsService) PersonNotificationStatistics(personN
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personNotificationStatisticsCommand.Uid,
})
... ... @@ -52,6 +55,7 @@ func (statisticsService *StatisticsService) PersonNotificationStatistics(personN
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personNotificationStatistics, err := employeeDao.CalculatePersonUnReadNotification(personNotificationStatisticsCommand.Uid); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -77,6 +81,7 @@ func (statisticsService *StatisticsService) SystemTaskStatistics(systemTaskStati
defer func() {
transactionContext.RollbackTransaction()
}()
var taskDao *dao.TaskDao
if value, err := factory.CreateTaskDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -85,6 +90,7 @@ func (statisticsService *StatisticsService) SystemTaskStatistics(systemTaskStati
} else {
taskDao = value
}
if systemTaskStatistics, err := taskDao.CalculateSystemTask(systemTaskStatisticsCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -110,6 +116,7 @@ func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStati
defer func() {
transactionContext.RollbackTransaction()
}()
var taskDao *dao.TaskDao
if value, err := factory.CreateTaskDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -118,6 +125,7 @@ func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStati
} else {
taskDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -126,6 +134,7 @@ func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStati
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personTaskStatisticsCommand.Uid,
})
... ... @@ -135,6 +144,7 @@ func (statisticsService *StatisticsService) PersonTaskStatistics(personTaskStati
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personTaskStatistics, err := taskDao.CalculatePersonTask(personTaskStatisticsCommand.Uid); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -160,6 +170,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
defer func() {
transactionContext.RollbackTransaction()
}()
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -168,6 +179,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
} else {
employeeDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -176,6 +188,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personSuMoneyStatisticsCommand.Uid,
})
... ... @@ -185,6 +198,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -266,7 +280,7 @@ func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStati
}
}
// TODO 获取员工财富值分组统计
// 获取员工财富值分组统计
func (statisticsService *StatisticsService) EmployeesSuMoneyStatistics(employeesSuMoneyStatisticsCommand *command.EmployeesSuMoneyStatisticsCommand) (interface{}, error) {
if err := employeesSuMoneyStatisticsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -301,7 +315,7 @@ func (statisticsService *StatisticsService) EmployeesSuMoneyStatistics(employees
}
}
// TODO 获取员工贡献值分组统计
// 获取员工贡献值分组统计
func (statisticsService *StatisticsService) EmployeesContributionsStatistics(employeesContributionsStatisticsCommand *command.EmployeesContributionsStatisticsCommand) (interface{}, error) {
if err := employeesContributionsStatisticsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -36,6 +36,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
defer func() {
transactionContext.RollbackTransaction()
}()
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -44,16 +45,27 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
} else {
cashPoolRepository = value
}
count, _, err := cashPoolRepository.Find(map[string]interface{}{
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
// 增加公司id判断
count, _, err := employeeRepository.Find(map[string]interface{}{
"companyId": createCashPoolCommand.CompanyId,
})
if count == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
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")
}
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -62,18 +74,29 @@ 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)
systemCashStatistics, err := employeeDao.CalculateSystemCash(createCashPoolCommand.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)
// 计算系统平均兑换汇率
var rate float64
if systemExchangedSuMoney == 0 {
... ... @@ -81,6 +104,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
} else {
rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64) // 平均兑换汇率
}
// 新建现金池
newCashPool := &domain.CashPool{
CompanyId: createCashPoolCommand.CompanyId,
... ... @@ -92,6 +116,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
Rate: rate,
CreateTime: time.Now(),
}
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -117,7 +142,7 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
defer func() {
transactionContext.RollbackTransaction()
}()
// 统计系统素币
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -126,6 +151,8 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
} else {
employeeDao = value
}
// 计算系统素币
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(getCashPoolQuery.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -133,8 +160,10 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64)
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) // 平台已兑换素币
systemUnExchangeSuMoney := systemSuMoneyStatistics["systemUnExchangeSuMoney"].(float64) // 平台未兑换素币
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -143,12 +172,14 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
} 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 {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if count == 0 {
return map[string] interface{} {
"cashPoolId": 0,
... ... @@ -182,20 +213,24 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang
defer func() {
transactionContext.RollbackTransaction()
}()
// 时间格式化
var timeNow = time.Now()
var deadline = createExchangeCashActivityCommand.Deadline
var t1 = time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
var t2 = time.Date(deadline.Year(), deadline.Month(), deadline.Day(), 0, 0, 0, 0, time.Local)
newActivity := &domain.ExchangeCashActivity{
ExchangeActivityName: createExchangeCashActivityCommand.ExchangeActivityName,
CompanyId: createExchangeCashActivityCommand.CompanyId,
ExchangedCash: 0,
ExchangedSuMoney: 0,
Deadline: time.Date(deadline.Year(), deadline.Month(), deadline.Day(), deadline.Hour(), deadline.Minute(), 0, 0, time.Local),
CountDown: int64(t2.Sub(t1).Hours() / 24),
Deadline: time.Date(deadline.Year(), deadline.Month(), deadline.Day(), deadline.Hour(), deadline.Minute(), deadline.Second(), 0, time.Local),
CountDown: int64(t2.Sub(t1).Hours() / 24), // 计算活动截止倒计时
Rate: createExchangeCashActivityCommand.ExchangeRate,
CreateTime: time.Now(),
}
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -204,6 +239,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang
} else {
exchangeCashActivityRepository = value
}
if activity, err := exchangeCashActivityRepository.Save(newActivity); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -229,6 +265,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivityDeadline(listExc
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -237,19 +274,23 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivityDeadline(listExc
} else {
exchangeActivityRepository = value
}
if _, activities, err := exchangeActivityRepository.FindAll(tool_funs.SimpleStructToMap(listExchangeCashActivityDeadlineQuery)); 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())
}
var deadlines []interface{}
for _, activity := range activities {
deadlines = append(deadlines, activity.Deadline)
}
if len(deadlines) == 0 {
deadlines = []interface{}{}
}
return map[string]interface{}{
"deadlines": deadlines,
}, nil
... ... @@ -271,6 +312,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -279,30 +321,37 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas
} else {
exchangeActivityRepository = value
}
if count, activities, err := exchangeActivityRepository.Find(tool_funs.SimpleStructToMap(listExchangeCashActivityQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
// 更新兑换活动结束倒计时
// TODO 更新兑换活动结束倒计时,增加倒计时结束判断
for _, activity := range activities {
var timeNow = time.Now()
var deadline = activity.Deadline
var t1 = time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
var t2 = time.Date(deadline.Year(), deadline.Month(), deadline.Day(), 0, 0, 0, 0, time.Local)
updateExchangeCashActivity := &command.UpdateExchangeCashActivityCommand {
ExchangeCashActivityId: activity.ActivityId,
CountDown: int64(t2.Sub(t1).Hours() / 24),
}
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())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"activities": activities,
... ... @@ -325,6 +374,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -333,6 +383,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang
} else {
exchangeCashActivityRepository = value
}
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": removeExchangeCashActivityCommand.ActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -340,6 +391,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang
if activity == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashActivityCommand.ActivityId)))
}
if activityDeleted, err := exchangeCashActivityRepository.Remove(activity); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -365,6 +417,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -373,6 +426,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA
} else {
exchangeCashActivityRepository = value
}
// 需要更新兑换活动结束倒计时
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": getExchangeCashActivityQuery.ExchangeCashActivityId})
if err != nil {
... ... @@ -381,22 +435,26 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA
if activity == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getExchangeCashActivityQuery.ExchangeCashActivityId)))
}
var timeNow = time.Now()
var deadline = activity.Deadline
var t1 = time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
var t2 = time.Date(deadline.Year(), deadline.Month(), deadline.Day(), 0, 0, 0, 0, time.Local)
updateExchangeCashActivity := &command.UpdateExchangeCashActivityCommand {
ExchangeCashActivityId: activity.ActivityId,
CountDown: int64(t2.Sub(t1).Hours() / 24),
}
if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivity)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
activityFound, err := exchangeCashActivityRepository.Save(activity)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 返回兑换现金活动
if activityFound == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -422,6 +480,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
defer func() {
transactionContext.RollbackTransaction()
}()
// 更新兑换活动
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
... ... @@ -431,6 +490,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
exchangeCashActivityRepository = value
}
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": updateExchangeCashActivityCommand.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -438,9 +498,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if activity == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 更新兑换活动兑换汇率
if updateExchangeCashActivityCommand.ExchangeRate != 0 {
// 获取当前现金池
... ... @@ -452,6 +510,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
cashPoolRepository = value
}
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activity.CompanyId,
})
... ... @@ -461,6 +520,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if len(cashPools) == 0 {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId)))
}
// 判断兑换活动清单中现金总金额是否超过平台未兑换现金值
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
... ... @@ -470,6 +530,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
cashPoolDao = value
}
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activity.ActivityId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -477,10 +538,13 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if activitySuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPools[0].UnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 批量更新兑换清单中已兑换现金值
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
... ... @@ -490,24 +554,29 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
exchangeCashPersonListRepository = value
}
_, people, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
"exchangeCashActivityId": updateExchangeCashActivityCommand.ExchangeCashActivityId,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 更新兑换清单,更新已兑换现金值
// 批量更新兑换清单,更新已兑换现金值
for _, person := range people {
updateExchangeCashPerson := &command.UpdateExchangeCashPersonCommand {
ExchangedCash: updateExchangeCashActivityCommand.ExchangeRate * person.ExchangedSuMoney,
}
if err := person.Update(tool_funs.SimpleStructToMap(updateExchangeCashPerson)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if _, err := exchangeCashPersonListRepository.Save(person);err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
// 计算系统平均兑换汇率并更新现金池
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ... @@ -517,6 +586,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
employeeDao = value
}
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
if err != nil {
... ... @@ -525,7 +595,9 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 获取平台现金状况
systemCashStatistics, err := employeeDao.CalculateSystemCash(activity.CompanyId)
if err != nil {
... ... @@ -534,8 +606,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 平均兑换汇率
var rate float64
if systemExchangedSuMoney == 0 {
... ... @@ -543,16 +617,19 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
} else {
rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: rate,
}
// 更新现金池
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())
... ... @@ -560,13 +637,22 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 更新兑换活动已兑换现金
updateExchangeCashActivityCommand.ExchangedCash = updateExchangeCashActivityCommand.ExchangeRate * activity.ExchangedSuMoney
}
if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if activityUpdated, err := exchangeCashActivityRepository.Save(activity); 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())
}
return activityUpdated, nil
}
}
... ... @@ -586,6 +672,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
defer func() {
transactionContext.RollbackTransaction()
}()
// 获取兑换活动兑换汇率
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
... ... @@ -595,11 +682,14 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
exchangeCashActivityRepository = value
}
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": createExchangeCashPersonCommand.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
rate := activity.Rate
// 根据uid/手机账号判断成员是否存在,素币是否超过本人持有的真实素币
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
... ... @@ -609,6 +699,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
employeeRepository = value
}
getEmployee := map[string]interface{}{}
if createExchangeCashPersonCommand.PersonAccount == "" {
getEmployee = map[string]interface{}{
... ... @@ -619,7 +710,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
"account": createExchangeCashPersonCommand.PersonAccount,
}
}
fmt.Print(getEmployee, "\n")
employee, err := employeeRepository.FindOne(getEmployee)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -627,9 +718,11 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的员工")
}
if employee.SuMoney < createExchangeCashPersonCommand.ExchangedSuMoney {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前兑换素币超过本人持有的素币值")
}
// 新增兑换清单
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
... ... @@ -639,6 +732,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
exchangeCashPersonListRepository = value
}
newPerson := &domain.ExchangeCashPersonList{
EmployeeInfo: &domain.EmployeeInfo{
Uid: employee.EmployeeInfo.Uid,
... ... @@ -649,10 +743,12 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
ExchangedSuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
ExchangedCash: createExchangeCashPersonCommand.ExchangedSuMoney * rate,
}
person, err := exchangeCashPersonListRepository.Save(newPerson)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 生成素币流水,更新个人素币
var operationSuMoneyService service.OperationSuMoneyService
if value, err := factory.CreateOperationSuMoneyService(map[string]interface{}{
... ... @@ -662,6 +758,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
operationSuMoneyService = value
}
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: person.EmployeeInfo.Uid,
Operator: createExchangeCashPersonCommand.Operator,
... ... @@ -669,7 +766,9 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
OperationType: 3,
OperationDescription: "素币兑换现金",
}
fmt.Print(operationSuMoneyCommand, "\n")
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())
... ... @@ -677,8 +776,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
fmt.Print(task, "\n")
// 更新兑换活动
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
... ... @@ -686,6 +784,8 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
exchangeCashActivityRepository = value
}
// 获取相关兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": createExchangeCashPersonCommand.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -693,14 +793,18 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if activityFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: person.ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + person.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash - person.ExchangedCash,
}
// 更新兑换活动
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())
... ... @@ -708,7 +812,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
}
// 获取兑换清单素币状况
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -717,6 +821,8 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
cashPoolDao = value
}
// 获取兑换活动素币兑换情况
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -724,7 +830,9 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if activitySuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业")
}
activitySuMoney := activitySuMoneyStatistics["activityExchangedSuMoney"].(float64)
// 现金池操作
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ... @@ -734,6 +842,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
employeeDao = value
}
// 获取平台现金状况
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
... ... @@ -742,12 +851,15 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 判断兑换的现金是否超过现金池未兑换现金
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemUnExchangeCash {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
if err != nil {
... ... @@ -756,8 +868,9 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 更新现金池
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -766,6 +879,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
cashPoolRepository = value
}
// 获取现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activity.CompanyId,
... ... @@ -773,9 +887,10 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if cashPools == nil {
if len(cashPools) == 0 {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activity.CompanyId)))
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
... ... @@ -783,16 +898,19 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: newRate,
}
// 更新现金池
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())
... ... @@ -800,6 +918,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ... @@ -821,6 +940,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -829,6 +949,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashPerson(getExchangeCashPer
} else {
exchangeCashPersonListRepository = value
}
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"listId": getExchangeCashPersonQuery.ExchangeCashPersonId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -858,6 +979,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP
defer func() {
transactionContext.RollbackTransaction()
}()
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -866,6 +988,7 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP
} else {
exchangeCashPersonListRepository = value
}
// TODO 增加总榜单查询
if count, people, err := exchangeCashPersonListRepository.Find(tool_funs.SimpleStructToMap(listExchangeCashPersonQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -895,6 +1018,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
defer func() {
transactionContext.RollbackTransaction()
}()
// 移除兑换素币清单
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
... ... @@ -904,6 +1028,8 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
} else {
exchangeCashPersonListRepository = value
}
// 获取待删除人员
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"id": removeExchangeCashPersonCommand.ListId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -911,35 +1037,8 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if person == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashPersonCommand.ListId)))
}
personDeleted, err := exchangeCashPersonListRepository.Remove(person)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 还原个人素币值,生成素币流水
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: person.EmployeeInfo.Uid,
Operator: removeExchangeCashPersonCommand.Operator,
SuMoney: person.ExchangedSuMoney,
OperationType: 1,
OperationDescription: "素币兑换现金调整",
}
fmt.Print(removeExchangeCashPersonCommand.Operator, "\n")
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新兑换活动兑换情况
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -948,6 +1047,8 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
} else {
exchangeCashActivityRepository = value
}
// 获取相关兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": person.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -955,14 +1056,18 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if activityFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(person.ExchangeCashActivityId)))
}
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: person.ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney - person.ExchangedSuMoney,
ExchangedCash: activityFound.ExchangedCash + person.ExchangedCash,
}
// 更新兑换活动
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())
... ... @@ -970,6 +1075,35 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if activityUpdated == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
}
// 还原个人素币值,生成素币流水,描述修改成和活动相关
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: person.EmployeeInfo.Uid,
Operator: removeExchangeCashPersonCommand.Operator,
SuMoney: person.ExchangedSuMoney,
OperationType: 1,
OperationDescription: activityFound.ExchangeActivityName + "现金调整",
}
fmt.Print(removeExchangeCashPersonCommand.Operator, "\n")
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新现金池
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
... ... @@ -979,6 +1113,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
} else {
employeeDao = value
}
// 获取平台现金状况
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
... ... @@ -987,8 +1122,10 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if systemCashStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedCash := systemCashStatistics["systemExchangedCash"].(float64)
systemUnExchangeCash := systemCashStatistics["systemUnExchangeCash"].(float64)
// 获取平台素币状况
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activityFound.CompanyId)
if err != nil {
... ... @@ -997,8 +1134,9 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 更新现金池
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -1007,6 +1145,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
} else {
cashPoolRepository = value
}
// 获取现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
... ... @@ -1017,6 +1156,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if cashPools == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
... ... @@ -1024,16 +1164,19 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: newRate,
}
// 更新现金池
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())
... ... @@ -1041,10 +1184,17 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
if cashPoolUpdated == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
personDeleted, err := exchangeCashPersonListRepository.Remove(person)
if 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())
}
return personDeleted, nil
}
}
// 更新兑换清单
... ... @@ -1063,15 +1213,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
defer func() {
transactionContext.RollbackTransaction()
}()
// 更新兑换清单
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
}
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -1080,6 +1222,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
exchangeCashPersonListRepository = value
}
// 获取兑换清单
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"id": updateExchangeCashPersonCommand.ListId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1087,11 +1231,14 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if person == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ListId)))
}
// TODO 更新兑换清单,个人已兑换现金计算
if err := person.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
fmt.Print(person, "\n")
// 更新兑换活动
var exchangeCashActivityRepository domain.ExchangeActivityRepository
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -1100,6 +1247,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
exchangeCashActivityRepository = value
}
// 获取相关兑换活动
activityFound, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": person.ExchangeCashActivityId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1107,14 +1256,18 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if activityFound == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(person.ExchangeCashActivityId)))
}
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
ExchangeCashActivityId: person.ExchangeCashActivityId,
ExchangedSuMoney: activityFound.ExchangedSuMoney + (updateExchangeCashPersonCommand.ExchangedSuMoney - person.ExchangedSuMoney),
ExchangedCash: activityFound.ExchangedCash + (updateExchangeCashPersonCommand.ExchangedSuMoney - person.ExchangedSuMoney) * activityFound.Rate,
}
// 更新兑换活动
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
activityUpdated, err := exchangeCashActivityRepository.Save(activityFound)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1122,7 +1275,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if activityUpdated == nil {
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,
... ... @@ -1131,6 +1285,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
cashPoolDao = value
}
// 统计活动已兑换素币
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activityFound.ActivityId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1138,7 +1294,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if activitySuMoneyStatistics == nil {
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,
... ... @@ -1147,21 +1306,22 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
employeeDao = value
}
systemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
fmt.Print(activitySuMoney , "\n")
fmt.Print(updateExchangeCashActivityCommand.ExchangeRate, "\n")
fmt.Print(systemCashStatistics["systemUnExchangeCash"].(float64), "\n")
// 判断是否超过平台未兑换现金
if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > systemCashStatistics["systemUnExchangeCash"].(float64) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
}
// 更新员工素币,生成素币兑换流水记录
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
Uid: person.EmployeeInfo.Uid,
Operator: updateExchangeCashPersonCommand.Operator,
SuMoney: 0,
OperationType: 0,
OperationDescription: "参与兑换素币活动",
OperationDescription: activityUpdated.ExchangeActivityName + "素币调整",
}
if updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney > 0 {
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney)
operationSuMoneyCommand.OperationType = 5
... ... @@ -1169,7 +1329,17 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney)
operationSuMoneyCommand.OperationType = 3
}
fmt.Print(updateExchangeCashPersonCommand.Operator, "\n")
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
}
// 更新员工素币,生成素币兑换流水记录
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())
... ... @@ -1177,7 +1347,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if task == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
}
// 更新现金池
// 重新获取系统现金兑换情况
newSystemCashStatistics, err := employeeDao.CalculateSystemCash(activityFound.CompanyId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1185,8 +1356,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
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)
if err != nil {
... ... @@ -1195,8 +1368,9 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if systemSuMoneyStatistics == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
}
systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
// 更新现金池
var cashPoolRepository domain.CashPoolRepository
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
"transactionContext": transactionContext,
... ... @@ -1205,6 +1379,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
cashPoolRepository = value
}
// 获取现金池
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
"companyId": activityFound.CompanyId,
... ... @@ -1215,6 +1390,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if cashPools == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(activityFound.CompanyId)))
}
// 计算平均兑换汇率
var newRate float64
if systemExchangedSuMoney == 0 {
... ... @@ -1222,16 +1398,19 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
} else {
newRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64)
}
// 更新现金池
updateCashPoolCommand := &command.UpdateCashPoolCommand{
CashPoolId: cashPools[0].CashPoolId,
ExchangedCash: systemUnExchangeCash,
UnExchangeCash: systemUnExchangeCash,
Rate: newRate,
}
// 更新现金池
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())
... ... @@ -1246,6 +1425,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personUpdated, nil
}
}
... ...
... ... @@ -207,12 +207,12 @@ func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transaction
// 员工财富值统计
func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
tx := dao.transactionContext.PgTx
var ret []struct {
Uid int
EmployeeName string
EmployeeSuMoney float64
}
tx := dao.transactionContext.PgTx
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").
... ... @@ -235,7 +235,6 @@ func (dao *EmployeeDao) CalculateEmployeesSuMoney(companyId int, startTime time.
// 员工贡献值统计
func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime time.Time, endTime time.Time) (map[string]interface{}, error) {
tx := dao.transactionContext.PgTx
var ret []struct { // 员工贡献值
Uid int
EmployeeName string
... ... @@ -246,6 +245,7 @@ func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime
EmployeeName string
EmployeesContributions float64
}
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// 增加的贡献值
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
... ... @@ -277,7 +277,6 @@ func (dao *EmployeeDao) CalculateEmployeesContributions(companyId int, startTime
Select(&retDecrease); err != nil {
return nil, err
}
// merge两个贡献值
for i := 0; i < len(ret); i++ {
for j := 0; j < len(retDecrease); j++ {
if ret[i].Uid == retDecrease[j].Uid {
... ...