...
|
...
|
@@ -5,6 +5,7 @@ import ( |
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"
|
|
|
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
|
|
|
"math"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
...
|
...
|
@@ -34,6 +35,7 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
// 计算系统平均兑换汇率
|
|
|
var employeeDao *dao.EmployeeDao
|
|
|
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -42,7 +44,6 @@ 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())
|
...
|
...
|
@@ -50,16 +51,18 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
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 cashPoolRepository domain.CashPoolRepository
|
...
|
...
|
@@ -88,13 +91,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
Rate: rate,
|
|
|
CreateTime: time.Now(),
|
|
|
}
|
|
|
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
cashPoolRepository = value
|
|
|
}
|
|
|
if cashPool, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
...
|
...
|
@@ -105,6 +101,38 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新现金池
|
|
|
func (cashPoolService *CashPoolService) UpdateCashPool(updateCashPoolCommand *command.UpdateCashPoolCommand) (interface{}, error) {
|
|
|
if err := updateCashPoolCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
//var cashPoolRepository domain.CashPoolRepository
|
|
|
//if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
|
|
|
// "transactionContext": transactionContext,
|
|
|
//}); err != nil {
|
|
|
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
//} else {
|
|
|
// cashPoolRepository = value
|
|
|
//}
|
|
|
//
|
|
|
//count, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
|
|
|
// "companyId":
|
|
|
//})
|
|
|
|
|
|
return nil , nil
|
|
|
}
|
|
|
|
|
|
// 返回现金池
|
|
|
func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetCashPoolQuery) (interface{}, error) {
|
|
|
if err := getCashPoolQuery.ValidateQuery(); err != nil {
|
...
|
...
|
@@ -240,6 +268,9 @@ func (cashPoolService *CashPoolService) ListExchangeCashActivity(listExchangeCas |
|
|
} else {
|
|
|
exchangeActivityRepository = value
|
|
|
}
|
|
|
|
|
|
// TODO 需要更新兑换活动结束倒计时
|
|
|
|
|
|
if count, activities, err := exchangeActivityRepository.Find(tool_funs.SimpleStructToMap(listExchangeCashActivityQuery)); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
...
|
...
|
@@ -316,6 +347,9 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA |
|
|
} else {
|
|
|
exchangeCashActivityRepository = value
|
|
|
}
|
|
|
|
|
|
// TODO 需要更新兑换活动结束倒计时
|
|
|
|
|
|
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"exchangeCashActivityId": getExchangeCashActivityQuery.ExchangeCashActivityId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
...
|
...
|
@@ -330,7 +364,7 @@ func (cashPoolService *CashPoolService) GetExchangeCashActivity(getExchangeCashA |
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新兑换现金活动
|
|
|
// 更新兑换现金活动,名称,截止日期、汇率
|
|
|
func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchangeCashActivityCommand *command.UpdateExchangeCashActivityCommand) (interface{}, error) {
|
|
|
if err := updateExchangeCashActivityCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
...
|
...
|
@@ -345,9 +379,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// TODO 更新汇率判断
|
|
|
|
|
|
var exchangeCashActivityRepository domain.ExchangeActivityRepository
|
|
|
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -366,20 +397,104 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang |
|
|
if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
activityUpdated, err := exchangeCashActivityRepository.Save(activity)
|
|
|
if updateExchangeCashActivityCommand.ExchangeRate != 0 {
|
|
|
var cashPoolRepository domain.CashPoolRepository
|
|
|
if value, err := factory.CreateCashPoolRepository(map[string] interface{} {
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
cashPoolRepository = value
|
|
|
}
|
|
|
_, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
|
|
|
"companyId": activity.CompanyId,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if cashPools == nil {
|
|
|
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{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
} else {
|
|
|
cashPoolDao = value
|
|
|
}
|
|
|
activitySuMoneyStatistics, err := cashPoolDao.CalculateActivityExchangedSuMoney(activity.CompanyId)
|
|
|
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 > cashPools[0].UnExchangeCash {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
|
|
|
}
|
|
|
// 计算系统平均兑换汇率
|
|
|
var employeeDao *dao.EmployeeDao
|
|
|
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
} else {
|
|
|
employeeDao = value
|
|
|
}
|
|
|
systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.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(activity.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)
|
|
|
rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemExchangedSuMoney), 64) // 平均兑换汇率
|
|
|
// 更新现金池
|
|
|
updateCashPoolCommand := &command.UpdateCashPoolCommand{
|
|
|
CashPoolId: cashPools[0].CashPoolId,
|
|
|
//ExchangedSuMoney: systemExchangedSuMoney,
|
|
|
ExchangedCash: systemExchangedCash,
|
|
|
UnExchangeCash: systemUnExchangeCash,
|
|
|
//UnExchangeSuMoney: systemUnExchangeSuMoney,
|
|
|
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())
|
|
|
}
|
|
|
if cashPoolUpdated == nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// TODO 更新现金池
|
|
|
// 获取现金池
|
|
|
// TODO 更新兑换清单中已兑换现金值
|
|
|
|
|
|
// 更新现金池
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 新增兑换素币清单
|
...
|
...
|
@@ -407,7 +522,7 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC |
|
|
if activity != nil {
|
|
|
rate = activity.Rate
|
|
|
}
|
|
|
// 根据uid/手机账号判断成员是否存在
|
|
|
// 根据uid/手机账号判断成员是否存在,素币是否超过本人持有的真实素币
|
|
|
var employeeRepository domain.EmployeeRepository
|
|
|
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -490,8 +605,12 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC |
|
|
if activityFound == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(createExchangeCashPersonCommand.ExchangeCashActivityId)))
|
|
|
}
|
|
|
createExchangeCashPersonCommand.ExchangedSuMoney += activityFound.ExchangedSuMoney
|
|
|
if err := activityFound.Update(tool_funs.SimpleStructToMap(createExchangeCashPersonCommand)); err != nil {
|
|
|
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)
|
...
|
...
|
@@ -663,7 +782,7 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC |
|
|
}
|
|
|
updateExchangeCashActivityCommand := &command.UpdateExchangeCashActivityCommand{
|
|
|
ExchangeCashActivityId: person.ExchangeCashActivityId,
|
|
|
ExchangedSuMoney: activityFound.ExchangedSuMoney + person.ExchangedSuMoney,
|
|
|
ExchangedSuMoney: activityFound.ExchangedSuMoney - person.ExchangedSuMoney,
|
|
|
ExchangedCash: activityFound.ExchangedCash + person.ExchangedCash,
|
|
|
}
|
|
|
if err := activityFound.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
|
...
|
...
|
@@ -728,27 +847,57 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
if err := person.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
personUpdated, err := exchangeCashPersonListRepository.Save(person)
|
|
|
// 更新兑换活动
|
|
|
var exchangeCashActivityRepository domain.ExchangeActivityRepository
|
|
|
if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
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())
|
|
|
}
|
|
|
|
|
|
// TODO 更新兑换活动
|
|
|
|
|
|
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())
|
|
|
}
|
|
|
if activityUpdated == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
|
|
|
}
|
|
|
|
|
|
// TODO 判断现金池未兑换现金
|
|
|
|
|
|
|
|
|
// TODO 更新现金池
|
|
|
|
|
|
// 生成素币兑换流水记录,更新员工素币
|
|
|
operationSuMoneyCommand := &command.OperationSuMoneyCommand{
|
|
|
Uid: person.EmployeeInfo.Uid,
|
|
|
Operator: updateExchangeCashPersonCommand.Operator,
|
|
|
SuMoney: updateExchangeCashPersonCommand.ExchangedSuMoney,
|
|
|
OperationType: 3,
|
|
|
SuMoney: 0,
|
|
|
OperationType: 0,
|
|
|
OperationDescription: "参与兑换素币活动",
|
|
|
}
|
|
|
if updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney > 0 {
|
|
|
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney)
|
|
|
operationSuMoneyCommand.OperationType = 5
|
|
|
} else {
|
|
|
operationSuMoneyCommand.SuMoney = math.Abs(updateExchangeCashActivityCommand.ExchangedSuMoney - person.ExchangedSuMoney)
|
|
|
operationSuMoneyCommand.OperationType = 3
|
|
|
}
|
|
|
task, err := operationSuMoneyService.Operation(operationSuMoneyCommand.Uid, operationSuMoneyCommand.Operator, operationSuMoneyCommand.SuMoney, operationSuMoneyCommand.OperationType, operationSuMoneyCommand.OperationDescription)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
...
|
...
|
@@ -756,6 +905,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
if task == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(operationSuMoneyCommand.Uid)))
|
|
|
}
|
|
|
personUpdated, err := exchangeCashPersonListRepository.Save(person)
|
|
|
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())
|
|
|
}
|
...
|
...
|
|