作者 陈志颖

feat:增加现金池未兑换现金累积

@@ -2,7 +2,6 @@ package service @@ -2,7 +2,6 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - "github.com/astaxie/beego"  
6 "github.com/linmadan/egglib-go/utils/tool_funs" 5 "github.com/linmadan/egglib-go/utils/tool_funs"
7 "strconv" 6 "strconv"
8 "time" 7 "time"
@@ -59,22 +58,34 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co @@ -59,22 +58,34 @@ func (cashPoolService *CashPoolService) CreateCashPool(createCashPoolCommand *co
59 systemUnChangeSuMoney = value2 58 systemUnChangeSuMoney = value2
60 } 59 }
61 60
62 - // TODO 获取上一个现金池现金值 61 + var cashPoolRepository domain.CashPoolRepository
  62 + if value, err := factory.CreateCashPoolRepository(map[string]interface{}{
  63 + "transactionContext": transactionContext,
  64 + }); err != nil {
  65 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  66 + } else {
  67 + cashPoolRepository = value
  68 + }
63 69
64 - rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemChangedSuMoney), 64) 70 + _, cashPools, err := cashPoolRepository.Find(map[string]interface{}{
  71 + "companyId": createCashPoolCommand.CompanyId,
  72 + })
  73 + if err != nil {
  74 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  75 + }
65 76
  77 + rate, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", systemExchangedCash / systemChangedSuMoney), 64) // 平均兑换汇率
66 newCashPool := &domain.CashPool{ 78 newCashPool := &domain.CashPool{
67 CompanyId: createCashPoolCommand.CompanyId, 79 CompanyId: createCashPoolCommand.CompanyId,
68 Cash: createCashPoolCommand.Cash, 80 Cash: createCashPoolCommand.Cash,
69 ExchangedCash: systemExchangedCash, 81 ExchangedCash: systemExchangedCash,
70 - UnExchangeCash: createCashPoolCommand.Cash, // TODO 投入的现金 + 系统未兑换的现金 82 + UnExchangeCash: createCashPoolCommand.Cash + cashPools[0].UnExchangeCash,
71 ExchangedSuMoney: systemChangedSuMoney, 83 ExchangedSuMoney: systemChangedSuMoney,
72 UnExchangeSuMoney: systemUnChangeSuMoney, 84 UnExchangeSuMoney: systemUnChangeSuMoney,
73 Rate: rate, 85 Rate: rate,
74 CreateTime: time.Now(), 86 CreateTime: time.Now(),
75 } 87 }
76 88
77 - var cashPoolRepository domain.CashPoolRepository  
78 if value, err := factory.CreateCashPoolRepository(map[string]interface{}{ 89 if value, err := factory.CreateCashPoolRepository(map[string]interface{}{
79 "transactionContext": transactionContext, 90 "transactionContext": transactionContext,
80 }); err != nil { 91 }); err != nil {
@@ -131,12 +142,9 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC @@ -131,12 +142,9 @@ func (cashPoolService *CashPoolService) GetCashPool(getCashPoolQuery *query.GetC
131 142
132 // 新增兑换现金活动 143 // 新增兑换现金活动
133 func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchangeCashActivityCommand *command.CreateExchangeCashActivityCommand) (interface{}, error) { 144 func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchangeCashActivityCommand *command.CreateExchangeCashActivityCommand) (interface{}, error) {
134 - // 校验新增任务命令  
135 if err := createExchangeCashActivityCommand.ValidateCommand(); err != nil { 145 if err := createExchangeCashActivityCommand.ValidateCommand(); err != nil {
136 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 146 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
137 } 147 }
138 -  
139 - // 新建校验任务事务  
140 transactionContext, err := factory.CreateTransactionContext(nil) 148 transactionContext, err := factory.CreateTransactionContext(nil)
141 if err != nil { 149 if err != nil {
142 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 150 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -148,7 +156,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang @@ -148,7 +156,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang
148 transactionContext.RollbackTransaction() 156 transactionContext.RollbackTransaction()
149 }() 157 }()
150 158
151 - // 创建兑换活动  
152 newActivity := &domain.ExchangeCashActivity{ 159 newActivity := &domain.ExchangeCashActivity{
153 ExchangeActivityName: createExchangeCashActivityCommand.ExchangeActivityName, 160 ExchangeActivityName: createExchangeCashActivityCommand.ExchangeActivityName,
154 CompanyId: createExchangeCashActivityCommand.CompanyId, 161 CompanyId: createExchangeCashActivityCommand.CompanyId,
@@ -160,7 +167,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang @@ -160,7 +167,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashActivity(createExchang
160 CreateTime: time.Now(), 167 CreateTime: time.Now(),
161 } 168 }
162 169
163 - // 兑换活动资源库  
164 var exchangeCashActivityRepository domain.ExchangeActivityRepository 170 var exchangeCashActivityRepository domain.ExchangeActivityRepository
165 if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{ 171 if value, err := factory.CreateExchangeCashActivityRepository(map[string]interface{}{
166 "transactionContext": transactionContext, 172 "transactionContext": transactionContext,
@@ -252,7 +258,6 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang @@ -252,7 +258,6 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashActivity(removeExchang
252 if activity == nil { 258 if activity == nil {
253 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashActivityCommand.ActivityId))) 259 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashActivityCommand.ActivityId)))
254 } 260 }
255 -  
256 if activityDeleted, err := exchangeCashActivityRepository.Remove(activity); err != nil { 261 if activityDeleted, err := exchangeCashActivityRepository.Remove(activity); err != nil {
257 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 262 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
258 } else { 263 } else {
@@ -335,15 +340,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -335,15 +340,12 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
335 if activity == nil { 340 if activity == nil {
336 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId))) 341 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
337 } 342 }
338 -  
339 if activity == nil { 343 if activity == nil {
340 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId))) 344 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateExchangeCashActivityCommand.ExchangeCashActivityId)))
341 } 345 }
342 -  
343 if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil { 346 if err := activity.Update(tool_funs.SimpleStructToMap(updateExchangeCashActivityCommand)); err != nil {
344 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 347 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
345 } 348 }
346 -  
347 if activityUpdated, err := exchangeCashActivityRepository.Save(activity); err != nil { 349 if activityUpdated, err := exchangeCashActivityRepository.Save(activity); err != nil {
348 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 350 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
349 } else { 351 } else {
@@ -383,7 +385,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -383,7 +385,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
383 385
384 // TODO 新增兑换人员时,判断成员是否存在,以手机账号为判断依据 386 // TODO 新增兑换人员时,判断成员是否存在,以手机账号为判断依据
385 387
386 - // 创建兑换清单  
387 newPerson := &domain.ExchangeCashPersonList{ 388 newPerson := &domain.ExchangeCashPersonList{
388 EmployeeInfo: &domain.EmployeeInfo{ 389 EmployeeInfo: &domain.EmployeeInfo{
389 EmployeeName: createExchangeCashPersonCommand.PersonName, 390 EmployeeName: createExchangeCashPersonCommand.PersonName,
@@ -402,7 +403,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC @@ -402,7 +403,6 @@ func (cashPoolService *CashPoolService) CreateExchangeCashPerson(createExchangeC
402 } else { 403 } else {
403 exchangeCashPersonListRepository = value 404 exchangeCashPersonListRepository = value
404 } 405 }
405 -  
406 if person, err := exchangeCashPersonListRepository.Save(newPerson); err != nil { 406 if person, err := exchangeCashPersonListRepository.Save(newPerson); err != nil {
407 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 407 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
408 } else { 408 } else {
@@ -524,9 +524,6 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC @@ -524,9 +524,6 @@ func (cashPoolService *CashPoolService) RemoveExchangeCashPerson(removeExchangeC
524 if person == nil { 524 if person == nil {
525 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashPersonCommand.ListId))) 525 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeExchangeCashPersonCommand.ListId)))
526 } 526 }
527 -  
528 - beego.Info(person)  
529 -  
530 if personDeleted, err := exchangeCashPersonListRepository.Remove(person); err != nil { 527 if personDeleted, err := exchangeCashPersonListRepository.Remove(person); err != nil {
531 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 528 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
532 } else { 529 } else {
@@ -4,14 +4,14 @@ import "time" @@ -4,14 +4,14 @@ import "time"
4 4
5 // 兑换现金活动 5 // 兑换现金活动
6 type ExchangeCashActivity struct { 6 type ExchangeCashActivity struct {
7 - ActivityId int64 `json:"activityId"` // 兑换现金活动id 7 + ActivityId int64 `json:"activityId"` // 兑换现金活动id
8 ExchangeActivityName string `json:"exchangeActivityName"` // 兑换现金活动名称 8 ExchangeActivityName string `json:"exchangeActivityName"` // 兑换现金活动名称
9 CompanyId int64 `json:"companyId"` // 公司ID 9 CompanyId int64 `json:"companyId"` // 公司ID
10 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金 10 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金
11 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币 11 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币
12 CreateTime time.Time `json:"createTime"` // 创建兑换现金活动时间 12 CreateTime time.Time `json:"createTime"` // 创建兑换现金活动时间
13 Deadline time.Time `json:"deadline"` // 兑换现金活动截止时间 13 Deadline time.Time `json:"deadline"` // 兑换现金活动截止时间
14 - CountDown int64 `json:"countDown"` // 兑换现金活动结束倒计时 14 + CountDown int64 `json:"countDown"` // 兑换现金活动结束倒计时
15 Rate float64 `json:"rate"` // 兑换汇率 15 Rate float64 `json:"rate"` // 兑换汇率
16 } 16 }
17 17
@@ -56,11 +56,6 @@ func (exchangeCashActivity *ExchangeCashActivity) TransferSuMoney(rate float64) @@ -56,11 +56,6 @@ func (exchangeCashActivity *ExchangeCashActivity) TransferSuMoney(rate float64)
56 return nil 56 return nil
57 } 57 }
58 58
59 -//func (exchangeCashActivity *ExchangeCashActivity) TransferLastRate(lastRate float64) error {  
60 -// exchangeCashActivity.LastRate = lastRate  
61 -// return nil  
62 -//}  
63 -  
64 func (exchangeCashActivity *ExchangeCashActivity) TransferCountDown() error { 59 func (exchangeCashActivity *ExchangeCashActivity) TransferCountDown() error {
65 exchangeCashActivity.CountDown = int64(time.Until(exchangeCashActivity.Deadline)) 60 exchangeCashActivity.CountDown = int64(time.Until(exchangeCashActivity.Deadline))
66 return nil 61 return nil
@@ -19,16 +19,16 @@ func (dao *CashPoolDao) CalculateSystemSuMoney(companyId int64) ( float64, float @@ -19,16 +19,16 @@ func (dao *CashPoolDao) CalculateSystemSuMoney(companyId int64) ( float64, float
19 19
20 employeeModel := new(models.Employee) 20 employeeModel := new(models.Employee)
21 if err := tx.Model(employeeModel). 21 if err := tx.Model(employeeModel).
22 - ColumnExpr("sum(employees.su_money) AS system_unExchange_su_money").  
23 - Where("employees.company_id = ?", companyId). 22 + ColumnExpr("sum(employee.su_money) AS system_unExchange_su_money").
  23 + Where("employee.company_id = ?", companyId).
24 Select(&systemUnExchangeSuMoney); err != nil { 24 Select(&systemUnExchangeSuMoney); err != nil {
25 return 0, 0, err 25 return 0, 0, err
26 } 26 }
27 27
28 exchangeCashActivityModel := new(models.ExchangeCashActivity) 28 exchangeCashActivityModel := new(models.ExchangeCashActivity)
29 if err := tx.Model(exchangeCashActivityModel). 29 if err := tx.Model(exchangeCashActivityModel).
30 - ColumnExpr("sum(exchange_cash_activities.exchanged_su_money) AS system_changed_su_money").  
31 - Where("exchange_cash_activities.company_id = ?", companyId). 30 + ColumnExpr("sum(exchange_cash_activity.exchanged_su_money) AS system_changed_su_money").
  31 + Where("exchange_cash_activity.company_id = ?", companyId).
32 Select(&systemExchangedSuMoney); err != nil { 32 Select(&systemExchangedSuMoney); err != nil {
33 return 0, 0, err 33 return 0, 0, err
34 } 34 }
@@ -44,8 +44,8 @@ func (dao *CashPoolDao) CalculateSystemCash(companyId int64) (float64, error) { @@ -44,8 +44,8 @@ func (dao *CashPoolDao) CalculateSystemCash(companyId int64) (float64, error) {
44 exchangeCashActivityModel := new(models.ExchangeCashActivity) 44 exchangeCashActivityModel := new(models.ExchangeCashActivity)
45 45
46 if err := tx.Model(exchangeCashActivityModel). 46 if err := tx.Model(exchangeCashActivityModel).
47 - ColumnExpr("sum(exchange_cash_activities.exchanged_cash) AS system_exchanged_cash").  
48 - Where("exchange_cash_activities.company_id = ?", companyId). 47 + ColumnExpr("sum(exchange_cash_activity.exchanged_cash) AS system_exchanged_cash").
  48 + Where("exchange_cash_activity.company_id = ?", companyId).
49 Select(&systemExchangedCash) ; err != nil { 49 Select(&systemExchangedCash) ; err != nil {
50 return 0, err 50 return 0, err
51 } 51 }
@@ -60,15 +60,12 @@ func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]i @@ -60,15 +60,12 @@ func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]i
60 var exchangeCashActivityModels []*models.ExchangeCashActivity 60 var exchangeCashActivityModels []*models.ExchangeCashActivity
61 exchangeCashActivities := make([]*domain.ExchangeCashActivity, 0) 61 exchangeCashActivities := make([]*domain.ExchangeCashActivity, 0)
62 query := tx.Model(&exchangeCashActivityModels) 62 query := tx.Model(&exchangeCashActivityModels)
63 - // 公司相关活动搜索  
64 if companyId, ok := queryOptions["companyId"]; ok { 63 if companyId, ok := queryOptions["companyId"]; ok {
65 query = query.Where(`exchange_cash_activity.company_id = ?`, companyId) 64 query = query.Where(`exchange_cash_activity.company_id = ?`, companyId)
66 } 65 }
67 - // 活动名称模糊搜索  
68 if exchangeCashActivityNameMatch, ok := queryOptions["exchangeCashActivityNameMatch"]; ok && (exchangeCashActivityNameMatch != "") { 66 if exchangeCashActivityNameMatch, ok := queryOptions["exchangeCashActivityNameMatch"]; ok && (exchangeCashActivityNameMatch != "") {
69 query = query.Where(`exchange_cash_activity.activity_name LIKE ?`, fmt.Sprintf("%%%s%%", exchangeCashActivityNameMatch.(string))) 67 query = query.Where(`exchange_cash_activity.activity_name LIKE ?`, fmt.Sprintf("%%%s%%", exchangeCashActivityNameMatch.(string)))
70 } 68 }
71 - // 活动截止时间搜索  
72 if deadline, ok := queryOptions["exchangeCashActivityDeadline"]; ok && !deadline.(time.Time).IsZero() { 69 if deadline, ok := queryOptions["exchangeCashActivityDeadline"]; ok && !deadline.(time.Time).IsZero() {
73 query = query.Where(`exchange_cash_activity.deadline < ?`, deadline) 70 query = query.Where(`exchange_cash_activity.deadline < ?`, deadline)
74 } 71 }
@@ -105,7 +102,6 @@ func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]i @@ -105,7 +102,6 @@ func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]i
105 func (repository *ExchangeCashActivityRepository) Remove(exchangeCashActivity *domain.ExchangeCashActivity) (*domain.ExchangeCashActivity, error) { 102 func (repository *ExchangeCashActivityRepository) Remove(exchangeCashActivity *domain.ExchangeCashActivity) (*domain.ExchangeCashActivity, error) {
106 tx := repository.transactionContext.PgTx 103 tx := repository.transactionContext.PgTx
107 exchangeCashActivityModel := new(models.ExchangeCashActivity) 104 exchangeCashActivityModel := new(models.ExchangeCashActivity)
108 - //exchangeCashActivity.ActivityId = exchangeCashActivity.Identity().(int64)  
109 exchangeCashActivityModel.Id = exchangeCashActivity.ActivityId 105 exchangeCashActivityModel.Id = exchangeCashActivity.ActivityId
110 if _, err := tx.Model(exchangeCashActivityModel).WherePK().Delete(); err != nil { 106 if _, err := tx.Model(exchangeCashActivityModel).WherePK().Delete(); err != nil {
111 return exchangeCashActivity, err 107 return exchangeCashActivity, err
@@ -98,7 +98,6 @@ func (repository *ExchangeCashPersonListRepository) Find(queryOptions map[string @@ -98,7 +98,6 @@ func (repository *ExchangeCashPersonListRepository) Find(queryOptions map[string
98 func (repository *ExchangeCashPersonListRepository) Remove(exchangeCashList *domain.ExchangeCashPersonList) (*domain.ExchangeCashPersonList, error) { 98 func (repository *ExchangeCashPersonListRepository) Remove(exchangeCashList *domain.ExchangeCashPersonList) (*domain.ExchangeCashPersonList, error) {
99 tx := repository.transactionContext.PgTx 99 tx := repository.transactionContext.PgTx
100 exchangeCashListModel := new(models.ExchangeCashPersonList) 100 exchangeCashListModel := new(models.ExchangeCashPersonList)
101 - //exchangeCashList.ListId = exchangeCashList.Identity().(int64)  
102 exchangeCashListModel.Id = exchangeCashList.ListId 101 exchangeCashListModel.Id = exchangeCashList.ListId
103 if _, err := tx.Model(exchangeCashListModel).WherePK().Delete(); err != nil { 102 if _, err := tx.Model(exchangeCashListModel).WherePK().Delete(); err != nil {
104 return exchangeCashList, err 103 return exchangeCashList, err