作者 陈志颖

fix:增加更新兑换活动汇率时同步更新兑换清单操作

@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 type UpdateExchangeCashPersonCommand struct { 8 type UpdateExchangeCashPersonCommand struct {
9 ListId int64 `json:"listId" valid:"Required"` // 兑换现金人员编号 9 ListId int64 `json:"listId" valid:"Required"` // 兑换现金人员编号
10 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币(需要和当前的已兑换素币进行比较,少于当前已兑换素币则生成一条扣除素币记录,大于当前已兑换素币则生成一条增加素币记录) 10 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币(需要和当前的已兑换素币进行比较,少于当前已兑换素币则生成一条扣除素币记录,大于当前已兑换素币则生成一条增加素币记录)
  11 + ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金
11 Operator int64 `json:"operator,omitempty"` // 操作人 12 Operator int64 `json:"operator,omitempty"` // 操作人
12 } 13 }
13 14
@@ -2,13 +2,14 @@ package service @@ -2,13 +2,14 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 - "github.com/linmadan/egglib-go/utils/tool_funs"  
6 - "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"  
7 - "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"  
8 "math" 5 "math"
9 "strconv" 6 "strconv"
10 "time" 7 "time"
11 8
  9 + "github.com/linmadan/egglib-go/utils/tool_funs"
  10 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"
  11 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
  12 +
12 "github.com/linmadan/egglib-go/core/application" 13 "github.com/linmadan/egglib-go/core/application"
13 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory" 14 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
14 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command" 15 "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command"
@@ -480,8 +481,34 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -480,8 +481,34 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
480 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPools[0].UnExchangeCash { 481 if activitySuMoney * updateExchangeCashActivityCommand.ExchangeRate > cashPools[0].UnExchangeCash {
481 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金") 482 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已超过投入现金池的未兑换现金")
482 } 483 }
483 - // TODO 批量更新兑换清单中已兑换现金值  
484 - 484 + // 批量更新兑换清单中已兑换现金值
  485 + // 获取兑换清单列表
  486 + var exchangeCashPersonListRepository domain.ExchangeCashPersonListRepository
  487 + if value, err := factory.CreateExchangeCashPersonListRepository(map[string]interface{}{
  488 + "transactionContext": transactionContext,
  489 + }); err != nil {
  490 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  491 + } else {
  492 + exchangeCashPersonListRepository = value
  493 + }
  494 + _, people, err := exchangeCashPersonListRepository.Find(map[string]interface{}{
  495 + "exchangeCashActivityId": updateExchangeCashActivityCommand.ExchangeCashActivityId,
  496 + });
  497 + if err != nil {
  498 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  499 + }
  500 + // 更新兑换清单,更新已兑换现金值
  501 + for _, person := range people {
  502 + updateExchangeCashPerson := &command.UpdateExchangeCashPersonCommand {
  503 + ExchangedCash: updateExchangeCashActivityCommand.ExchangeRate * person.ExchangedSuMoney,
  504 + }
  505 + if err := person.Update(tool_funs.SimpleStructToMap(updateExchangeCashPerson)); err != nil {
  506 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  507 + }
  508 + if _, err := exchangeCashPersonListRepository.Save(person);err != nil {
  509 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  510 + }
  511 + }
485 // 计算系统平均兑换汇率并更新现金池 512 // 计算系统平均兑换汇率并更新现金池
486 var employeeDao *dao.EmployeeDao 513 var employeeDao *dao.EmployeeDao
487 if value, err := factory.CreateEmployeeDao(map[string]interface{}{ 514 if value, err := factory.CreateEmployeeDao(map[string]interface{}{
@@ -491,6 +518,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -491,6 +518,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
491 } else { 518 } else {
492 employeeDao = value 519 employeeDao = value
493 } 520 }
  521 + // 获取平台素币状况
494 systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId) 522 systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(activity.CompanyId)
495 if err != nil { 523 if err != nil {
496 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 524 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -499,6 +527,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang @@ -499,6 +527,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashActivity(updateExchang
499 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司") 527 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的公司")
500 } 528 }
501 systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64) 529 systemExchangedSuMoney := systemSuMoneyStatistics["systemExchangedSuMoney"].(float64)
  530 + // 获取平台现金状况
502 systemCashStatistics, err := employeeDao.CalculateSystemCash(activity.CompanyId) 531 systemCashStatistics, err := employeeDao.CalculateSystemCash(activity.CompanyId)
503 if err != nil { 532 if err != nil {
504 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 533 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -1190,6 +1219,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC @@ -1190,6 +1219,7 @@ func (cashPoolService *CashPoolService) UpdateExchangeCashPerson(updateExchangeC
1190 if cashPoolUpdated == nil { 1219 if cashPoolUpdated == nil {
1191 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 1220 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
1192 } 1221 }
  1222 +
1193 if personUpdated, err := exchangeCashPersonListRepository.Save(person);err != nil { 1223 if personUpdated, err := exchangeCashPersonListRepository.Save(person);err != nil {
1194 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 1224 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
1195 } else { 1225 } else {
@@ -152,18 +152,27 @@ func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interf @@ -152,18 +152,27 @@ func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interf
152 systemExchangedCash float64 152 systemExchangedCash float64
153 ) 153 )
154 // 系统已兑换现金 154 // 系统已兑换现金
155 - suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)  
156 - if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").  
157 - ColumnExpr("sum(su_money_transaction_record.cash) AS system_exchanged_cash").  
158 - Where("e.company_id = ?", companyId).  
159 - //Where(`e.status = ?`, 1).  
160 - Where(`su_money_transaction_record.record_type = ?`, 5).  
161 - Select(&systemExchangedCash) ; err != nil {  
162 - return nil, err  
163 - } 155 + //suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
  156 + //if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS e ON e.uid = (su_money_transaction_record.employee->>'uid')::bigint").
  157 + // ColumnExpr("sum(su_money_transaction_record.cash) AS system_exchanged_cash").
  158 + // Where("e.company_id = ?", companyId).
  159 + // //Where(`e.status = ?`, 1).
  160 + // Where(`su_money_transaction_record.record_type = ?`, 5).
  161 + // Select(&systemExchangedCash) ; err != nil {
  162 + // return nil, err
  163 + //}
  164 +
164 // 系统未兑换现金 165 // 系统未兑换现金
165 cashPool := new(models.CashPool) 166 cashPool := new(models.CashPool)
166 if err := tx.Model(cashPool). 167 if err := tx.Model(cashPool).
  168 + Column("exchanged_cash").
  169 + Where("cash_pool.company_id = ?", companyId).
  170 + Order("id DESC").
  171 + Limit(1).
  172 + Select(&systemUnExchangeCash) ; err != nil {
  173 + return nil, err
  174 + }
  175 + if err := tx.Model(cashPool).
167 Column("un_exchange_cash"). 176 Column("un_exchange_cash").
168 Where("cash_pool.company_id = ?", companyId). 177 Where("cash_pool.company_id = ?", companyId).
169 Order("id DESC"). 178 Order("id DESC").
@@ -15,6 +15,7 @@ type OperationSuMoneyService struct { @@ -15,6 +15,7 @@ type OperationSuMoneyService struct {
15 transactionContext *pgTransaction.TransactionContext 15 transactionContext *pgTransaction.TransactionContext
16 } 16 }
17 17
  18 +// TODO 增加兑换现金事务记录
18 func (service *OperationSuMoneyService) Operation(uid int64, operatorUid int64, suMoney float64, operationType int, recordDescription string) (*domain.SuMoneyTransactionRecord, error) { 19 func (service *OperationSuMoneyService) Operation(uid int64, operatorUid int64, suMoney float64, operationType int, recordDescription string) (*domain.SuMoneyTransactionRecord, error) {
19 var employeeRepository domain.EmployeeRepository 20 var employeeRepository domain.EmployeeRepository
20 var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository 21 var suMoneyTransactionRecordRepository domain.SuMoneyTransactionRecordRepository