...
|
...
|
@@ -2,7 +2,9 @@ package service |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego"
|
|
|
"github.com/linmadan/egglib-go/utils/tool_funs"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
...
|
...
|
@@ -17,14 +19,11 @@ import ( |
|
|
type CashPoolService struct {
|
|
|
}
|
|
|
|
|
|
// 投入现金池
|
|
|
// 新增现金池
|
|
|
func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *command.CreateCashPoolCommand) (interface{}, error) {
|
|
|
// 校验命令
|
|
|
if err := createCashPoolCommand.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 新建现金池事务
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -35,8 +34,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// 新建现金池数据访问对象
|
|
|
var cashPoolDao *dao.CashPoolDao
|
|
|
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -45,16 +42,12 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
} else {
|
|
|
cashPoolDao = value
|
|
|
}
|
|
|
|
|
|
// 获取系统已兑换现金值
|
|
|
var systemExchangedCash float64
|
|
|
if value, err := cashPoolDao.CalculateSystemCash(createCashPoolCommand.CompanyId); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
|
|
systemExchangedCash = value
|
|
|
}
|
|
|
|
|
|
// 获取系统素币兑换情况
|
|
|
var (
|
|
|
systemChangedSuMoney float64
|
|
|
systemUnChangeSuMoney float64
|
...
|
...
|
@@ -66,16 +59,18 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
systemUnChangeSuMoney = value2
|
|
|
}
|
|
|
|
|
|
fmt.Print("1234567", createCashPoolCommand)
|
|
|
// TODO 获取上一个现金池现金值
|
|
|
|
|
|
rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemChangedSuMoney), 64)
|
|
|
|
|
|
newCashPool := &domain.CashPool{
|
|
|
CompanyId: createCashPoolCommand.CompanyId,
|
|
|
Cash: createCashPoolCommand.Cash,
|
|
|
ExchangedCash: systemExchangedCash,
|
|
|
UnExchangeCash: 0,
|
|
|
UnExchangeCash: createCashPoolCommand.Cash, // TODO 投入的现金 + 系统未兑换的现金
|
|
|
ExchangedSuMoney: systemChangedSuMoney,
|
|
|
UnExchangeSuMoney: systemUnChangeSuMoney,
|
|
|
Rate: 0,
|
|
|
Rate: rate,
|
|
|
CreateTime: time.Now(),
|
|
|
}
|
|
|
|
...
|
...
|
@@ -88,7 +83,6 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co |
|
|
cashPoolRepository = value
|
|
|
}
|
|
|
|
|
|
// 每个公司一个现金池
|
|
|
if activity, err := cashPoolRepository.Save(newCashPool); err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
} else {
|
...
|
...
|
@@ -104,8 +98,6 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC |
|
|
if err := getCashPoolQuery.ValidateQuery(); 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())
|
...
|
...
|
@@ -116,8 +108,6 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// 现金池资源库
|
|
|
var cashPoolRepository domain.CashPoolRepository
|
|
|
if value, err := factory.CreateCashPoolRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -162,11 +152,12 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang |
|
|
newActivity := &domain.ExchangeCashActivity{
|
|
|
ExchangeActivityName: createExchangeCashActivityCommand.ExchangeActivityName,
|
|
|
CompanyId: createExchangeCashActivityCommand.CompanyId,
|
|
|
ExchangedCash: createExchangeCashActivityCommand.ExchangedCash,
|
|
|
ExchangedSuMoney: createExchangeCashActivityCommand.ExchangedSuMoney,
|
|
|
ExchangedCash: 0,
|
|
|
ExchangedSuMoney: 0,
|
|
|
Deadline: createExchangeCashActivityCommand.Deadline,
|
|
|
CountDown: createExchangeCashActivityCommand.CountDown,
|
|
|
CountDown: 0,
|
|
|
Rate: createExchangeCashActivityCommand.ExchangeRate,
|
|
|
CreateTime: time.Now(),
|
|
|
}
|
|
|
|
|
|
// 兑换活动资源库
|
...
|
...
|
@@ -254,12 +245,12 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang |
|
|
exchangeCashActivityRepository = value
|
|
|
}
|
|
|
|
|
|
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": removeExchangeCashActivityCommand.ExchangeCashActivityId})
|
|
|
activity, err := exchangeCashActivityRepository.FindOne(map[string]interface{}{"activityId": removeExchangeCashActivityCommand.ActivityId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if activity == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashActivityCommand.ExchangeCashActivityId)))
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashActivityCommand.ActivityId)))
|
|
|
}
|
|
|
|
|
|
if activityDeleted, err := exchangeCashActivityRepository.Remove(activity); err != nil {
|
...
|
...
|
@@ -359,6 +350,8 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang |
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
// TODO 更新现金池已兑换素币、已兑换现金值、未兑换素币、未兑换现金值、平均汇率
|
|
|
|
|
|
return activityUpdated, nil
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -380,18 +373,27 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC |
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// TODO 需要判断新增兑换现金人员的素币值是否合理(人员参与兑换的素币不得超过该人员还未兑换的素币总值)
|
|
|
|
|
|
// TODO 判断活动的汇率是否为空,为空则不允许新建,提示:请设置汇率
|
|
|
|
|
|
// TODO 获取参与的兑换活动的汇率,计算已兑换的现金值
|
|
|
|
|
|
// TODO 清单中现金总额超过现金池时创建失败,提示:“已超过投入现金池的未兑换现金”
|
|
|
|
|
|
// TODO 新增兑换人员时,判断成员是否存在,以手机账号为判断依据
|
|
|
|
|
|
// 创建兑换清单
|
|
|
newPerson := &domain.ExchangeCashPersonList{
|
|
|
EmployeeInfo: &domain.EmployeeInfo{
|
|
|
Uid: createExchangeCashPersonCommand.PersonId,
|
|
|
EmployeeName: createExchangeCashPersonCommand.PersonName,
|
|
|
EmployeeAccount: createExchangeCashPersonCommand.PersonAccount,
|
|
|
},
|
|
|
ExchangeCashActivityId: createExchangeCashPersonCommand.ExchangeCashActivityId,
|
|
|
ExchangedSuMoney: createExchangeCashPersonCommand.ExchangedSuMoney,
|
|
|
|
|
|
}
|
|
|
|
|
|
// 兑换活动资源库
|
|
|
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
|
|
|
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -407,6 +409,8 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC |
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
// TODO 更新活动已兑换素币值、已兑换现金值、兑换汇率
|
|
|
|
|
|
return person, nil
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -455,7 +459,6 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP |
|
|
if err := listExchangeCashPersonQuery.ValidateQuery(); 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())
|
...
|
...
|
@@ -466,7 +469,6 @@ func (cashPoolService *CashPoolService) ListExchangeCashPerson(listExchangeCashP |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
|
|
|
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -515,20 +517,24 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC |
|
|
exchangeCashPersonListRepository = value
|
|
|
}
|
|
|
|
|
|
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"personId": removeExchangeCashPersonCommand.ExchangeCashPersonId})
|
|
|
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"id": removeExchangeCashPersonCommand.ListId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if person == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashPersonCommand.ExchangeCashPersonId)))
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashPersonCommand.ListId)))
|
|
|
}
|
|
|
|
|
|
beego.Info(person)
|
|
|
|
|
|
if personDeleted, err := exchangeCashPersonListRepository.Remove(person); 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())
|
|
|
}
|
|
|
// TODO 个人素币值需要还原,理由:素币兑换现金调整
|
|
|
|
|
|
return personDeleted, nil
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -538,7 +544,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
if err := updateExchangeCashPersonCommand.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())
|
...
|
...
|
@@ -549,9 +554,6 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// TODO 新增流水记录
|
|
|
|
|
|
var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
|
|
|
if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
...
|
...
|
@@ -560,13 +562,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
} else {
|
|
|
exchangeCashPersonListRepository = value
|
|
|
}
|
|
|
|
|
|
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"personId": updateExchangeCashPersonCommand.ExchangeCashPersonId})
|
|
|
person, err := exchangeCashPersonListRepository.FindOne(map[string]interface{}{"listId": updateExchangeCashPersonCommand.ListId})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if person == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ExchangeCashPersonId)))
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashPersonCommand.ListId)))
|
|
|
}
|
|
|
if err := person.Update(tool_funs.SimpleStructToMap(updateExchangeCashPersonCommand)); err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
...
|
...
|
@@ -577,6 +578,10 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC |
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
// TODO 更新个人当前可用素币值,生成素币兑换流水记录(获取更新前的已兑换素币值,判断是扣除还是增加),记录描述:参与素币兑换现金活动(红色表示取活动名称)
|
|
|
|
|
|
// TODO 更新相应兑换活动已兑换素币值、已兑换现金值、兑换汇率
|
|
|
|
|
|
return personUpdated, nil
|
|
|
}
|
|
|
}
|
...
|
...
|
|