作者 陈志颖

fix:修改更新兑换现金活动时的现金池更新机制

package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type UpdateCashPoolCommand struct {
CashPoolId int64 `json:"cashPoolId"` // 现金池编号
ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金
ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换素币
UnExchangeCash float64 `json:"unExchangeCash"` // 未兑换现金
UnExchangeSuMoney float64 `json:"unExchangedSuMoney"` // 未兑换素币
Rate float64 `json:"rate"` // 平均兑换汇率
}
func (updateCashPoolCommand *UpdateCashPoolCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateCashPoolCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -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())
}
... ...
... ... @@ -29,8 +29,10 @@ type CreateTaskCommand struct {
TaskNature int `json:"taskNature,omitempty"`
// 奖励素币
SuMoney float64 `json:"suMoney,omitempty"`
// 奖励素币参考值范围(1000~3000)
ReferenceSuMoney string `json:"referenceSuMoney"`
// 最小奖励素币
MinSuMoney float64 `json:"minSuMoney"`
// 最大奖励素币
MaxSuMoney float64 `json:"maxSuMoney"`
// 验收标准
AcceptanceStandard string `json:"acceptanceStandard,omitempty"`
// 任务描述
... ...
... ... @@ -23,8 +23,12 @@ type UpdateTaskCommand struct {
CustomerValues []int `json:"customerValues,omitempty"`
// 任务性质
TaskNature int `json:"taskNature,omitempty"`
// 奖励素币
// 实际奖励素币
SuMoney float64 `json:"suMoney,omitempty"`
// 最小奖励素币
MinSuMoney float64 `json:"minSuMoney,omitempty"`
// 最大奖励素币
MaxSuMoney float64 `json:"maxSuMoney,omitempty"`
// 验收标准
AcceptanceStandard string `json:"acceptanceStandard,omitempty"`
// 任务描述
... ...
... ... @@ -841,7 +841,8 @@ func (taskService *TaskService) CreateTask(createTaskCommand *command.CreateTask
CustomerValues: createTaskCommand.CustomerValues,
TaskNature: createTaskCommand.TaskNature,
SuMoney: createTaskCommand.SuMoney,
ReferenceSuMoney: createTaskCommand.ReferenceSuMoney, // 奖励素币范围
MinSuMoney: createTaskCommand.MinSuMoney,
MaxSuMoney: createTaskCommand.MaxSuMoney,
AcceptanceStandard: createTaskCommand.AcceptanceStandard,
TaskDescription: createTaskCommand.TaskDescription,
TaskPictureUrls: createTaskCommand.TaskPictureUrls,
... ... @@ -1209,11 +1210,11 @@ func (taskService *TaskService) ListTask(listTaskQuery *query.ListTaskQuery) (in
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
// 拼接奖励素币范围值
var retTasks []*domain.Task
for _, task := range tasks {
if task.ReferenceSuMoney == "" {
task.ReferenceSuMoney = "0~" + fmt.Sprintf("%.0f", task.SuMoney)
if task.TaskStatus != 5 && task.MinSuMoney == 0 && task.MaxSuMoney == 0 {
task.MinSuMoney = 0
task.MaxSuMoney = task.SuMoney
}
retTasks = append(retTasks, task)
}
... ...
... ... @@ -50,8 +50,10 @@ type Task struct {
TaskNature int `json:"taskNature"`
// 奖励素币
SuMoney float64 `json:"suMoney"`
// 奖励素币参考值
ReferenceSuMoney string `json:"referenceSuMoney"`
// 最小奖励素币
MinSuMoney float64 `json:"minSuMoney"`
// 最大奖励素币
MaxSuMoney float64 `json:"maxSuMoney"`
// 验收标准
AcceptanceStandard string `json:"acceptanceStandard"`
// 任务描述
... ...
... ... @@ -53,7 +53,7 @@ type CashPoolDao struct {
// }, nil
//}
// 兑换兑换活动已兑换素币、已兑换现金
// 兑换活动兑换清单已兑换素币、已兑换现金
func (dao *CashPoolDao) CalculateActivityExchangedSuMoney(activityId int64) (map[string]interface{}, error) {
var activityExchangedSuMoney float64
var activityExchangedCash float64
... ...
... ... @@ -29,8 +29,10 @@ type Task struct {
TaskNature int
// 奖励素币
SuMoney float64
// 奖励素币参考值范围值
ReferenceSuMoney string
// 最小奖励素币
MinSuMoney float64
// 最大奖励素币
MaxSuMoney float64
// 验收标准
AcceptanceStandard string
// 任务描述
... ...