作者 陈志颖

feat:素币管理应用层以及资源库

正在显示 38 个修改的文件 包含 585 行增加76 行删除
@@ -109,3 +109,12 @@ func CreateExchangeSuMoneyService(options map[string]interface{}) (service.Excha @@ -109,3 +109,12 @@ func CreateExchangeSuMoneyService(options map[string]interface{}) (service.Excha
109 } 109 }
110 return domainService.NewExchangeSuMoneyService(transactionContext) 110 return domainService.NewExchangeSuMoneyService(transactionContext)
111 } 111 }
  112 +
  113 +func CreateInputCashPoolService(options map[string]interface{}) (service.InputCashPoolService, error) {
  114 + var transactionContext *pgTransaction.TransactionContext
  115 + if value, ok := options["transactionContext"]; ok {
  116 + transactionContext = value.(*pgTransaction.TransactionContext)
  117 + }
  118 + return domainService.NewCashPoolService(transactionContext)
  119 +}
  120 +
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "github.com/astaxie/beego/validation" 6 "github.com/astaxie/beego/validation"
7 ) 7 )
8 8
  9 +// 获取系统现金值
9 type SystemCashStatisticsCommand struct { 10 type SystemCashStatisticsCommand struct {
10 CompanyId int64 `json:"companyId" valid:"Required"` // 公司ID 11 CompanyId int64 `json:"companyId" valid:"Required"` // 公司ID
11 } 12 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/astaxie/beego/validation" 5 "github.com/astaxie/beego/validation"
6 ) 6 )
7 7
  8 +// 获取系统素币值
8 type SystemSuMoneyStatisticsCommand struct { 9 type SystemSuMoneyStatisticsCommand struct {
9 CompanyId int64 `json:"companyId" valid:"Required"` // 公司ID 10 CompanyId int64 `json:"companyId" valid:"Required"` // 公司ID
10 } 11 }
@@ -206,6 +206,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone @@ -206,6 +206,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
206 if err := systemSuMoneyStatisticsCommand.ValidateCommand(); err != nil { 206 if err := systemSuMoneyStatisticsCommand.ValidateCommand(); err != nil {
207 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 207 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
208 } 208 }
  209 +
209 transactionContext, err := factory.CreateTransactionContext(nil) 210 transactionContext, err := factory.CreateTransactionContext(nil)
210 if err != nil { 211 if err != nil {
211 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 212 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -227,7 +228,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone @@ -227,7 +228,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
227 } 228 }
228 229
229 230
230 - if systemSuMoneyStatistics, err := employeeDao.CalculateSystemExchangedCash(systemSuMoneyStatisticsCommand.CompanyId); err != nil { 231 + if systemSuMoneyStatistics, err := employeeDao.CalculateSystemCash(systemSuMoneyStatisticsCommand.CompanyId); err != nil {
231 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 232 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
232 } else { 233 } else {
233 if err := transactionContext.CommitTransaction(); err != nil { 234 if err := transactionContext.CommitTransaction(); err != nil {
@@ -238,13 +239,6 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone @@ -238,13 +239,6 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
238 } 239 }
239 } 240 }
240 241
241 -/**  
242 - * 获取系统现金统计  
243 - */  
244 -//func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStatisticsCommand *command.SystemCashStatisticsCommand) (interface{}, error) {  
245 -//  
246 -//}  
247 -  
248 func NewStatisticsService(options map[string]interface{}) *StatisticsService { 242 func NewStatisticsService(options map[string]interface{}) *StatisticsService {
249 newStatisticsService := &StatisticsService{} 243 newStatisticsService := &StatisticsService{}
250 return newStatisticsService 244 return newStatisticsService
@@ -6,10 +6,15 @@ import ( @@ -6,10 +6,15 @@ import (
6 "github.com/astaxie/beego/validation" 6 "github.com/astaxie/beego/validation"
7 ) 7 )
8 8
  9 +// 创建现金池
9 type CreateCashPoolCommand struct { 10 type CreateCashPoolCommand struct {
10 - CashPoolId int64 `json:"cashPoolId" valid:"Required"` // 现金池ID  
11 - CompanyId int64 `json:"companyId" valid:"Requires"` // 公司ID 11 + //CashPoolId int64 `json:"cashPoolId" valid:"Required"` // 现金池ID
  12 + CompanyId int64 `json:"companyId" valid:"Required"` // 公司ID
12 Cash float64 `json:"cash" valid:"Required"` // 投入的现金值 13 Cash float64 `json:"cash" valid:"Required"` // 投入的现金值
  14 + ExchangedCash float64 `json:"exchangedCash,omitempty"` // 现金池已兑换现金值
  15 + UnExchangeCash float64 `json:"unExchangeCash,omitempty"` // 现金池未兑换现金值
  16 + ExchangedSuMoney float64 `json:"exchangedSuMoney,omitempty"` // 现金池已兑换素币值
  17 + UnExchangeSuMoney float64 `json:"unExchangeSuMoney,omitempty"` // 现金池未兑换素币值
13 Operator int64 `json:"operator,omitempty"` // 操作人UID 18 Operator int64 `json:"operator,omitempty"` // 操作人UID
14 } 19 }
15 20
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  7 + "time"
  8 +)
  9 +
  10 +// 创建现金兑换活动
  11 +type CreateExchangeActivityCommand struct {
  12 + CompanyId int64 `json:"companyId" valid:"Required"` // 公司id
  13 + ExchangeActivityName string `json:"exchangeActivityName"` // 活动名称
  14 + CashPool *domain.CashPool `json:"cashPool"` // 兑换现金活动关联的现金池,最新的现金池
  15 + Deadline time.Time `json:"deadline"` // 活动截止时间
  16 + CountDown int64 `json:"countDown"` // 活动倒计时
  17 + ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换素币
  18 + ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金
  19 + ExchangeRate float64 `json:"exchangeRate"` // 兑换汇率
  20 + ExchangeCashPeople []*domain.ExchangeCashPerson `json:"exchangeList"` // 兑换活动清单
  21 +}
  22 +
  23 +func (createExchangeActivityCommand *CreateExchangeActivityCommand) ValidateCommand() error {
  24 + valid := validation.Validation{}
  25 + b, err := valid.Valid(createExchangeActivityCommand)
  26 + if err != nil {
  27 + return err
  28 + }
  29 + if !b {
  30 + for _, validErr := range valid.Errors {
  31 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  32 + }
  33 + }
  34 + return nil
  35 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  7 +)
  8 +
  9 +type CreateExchangeCashPersonCommand struct {
  10 + ExchangeCashPerson *domain.Employee `json:"exchangeCashPerson"` // 兑换现金人员
  11 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId"` // 参与的兑换活动id
  12 + ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换素币值
  13 + ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金值
  14 +}
  15 +
  16 +func (createExchangeCashPersonCommand *CreateExchangeCashPersonCommand) ValidateCommand() error {
  17 + valid := validation.Validation{}
  18 + b, err := valid.Valid(createExchangeCashPersonCommand)
  19 + if err != nil {
  20 + return err
  21 + }
  22 + if !b {
  23 + for _, validErr := range valid.Errors {
  24 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  25 + }
  26 + }
  27 + return nil
  28 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 删除兑换现金活动
  9 +type RemoveExchangeCashActivityCommand struct {
  10 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId" valid:"Required"`
  11 +}
  12 +
  13 +func (removeExchangeCashActivityCommand *RemoveExchangeCashActivityCommand) ValidateCommand() error {
  14 + valid := validation.Validation{}
  15 + b, err := valid.Valid(removeExchangeCashActivityCommand)
  16 + if err != nil {
  17 + return err
  18 + }
  19 + if !b {
  20 + for _, validErr := range valid.Errors {
  21 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  22 + }
  23 + }
  24 + return nil
  25 +}
1 package command 1 package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 移除兑换现金人员
  9 +type RemoveExchangeCashPersonCommand struct {
  10 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId" valid:"Required"` // 兑换现金人员参与的活动ID
  11 + ExchangeCashPersonId int64 `json:"exchangeCashId" valid:"Required"` // 兑换现金人员编号
  12 +}
  13 +
  14 +func (removeExchangeCashPersonCommand *RemoveExchangeCashPersonCommand) ValidateCommand() error {
  15 + valid := validation.Validation{}
  16 + b, err := valid.Valid(removeExchangeCashPersonCommand)
  17 + if err != nil {
  18 + return err
  19 + }
  20 + if !b {
  21 + for _, validErr := range valid.Errors {
  22 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  23 + }
  24 + }
  25 + return nil
  26 +}
@@ -14,12 +14,12 @@ type SearchSuMoneyTransactionRecordCommand struct { @@ -14,12 +14,12 @@ type SearchSuMoneyTransactionRecordCommand struct {
14 RecordType int `json:"recordType,omitempty"` 14 RecordType int `json:"recordType,omitempty"`
15 // 记录类型列表(1兑换,2任务奖励,3增加,4扣除) 15 // 记录类型列表(1兑换,2任务奖励,3增加,4扣除)
16 RecordTypes []int `json:"recordTypes,omitempty"` 16 RecordTypes []int `json:"recordTypes,omitempty"`
17 - // 操作人UID  
18 // 事务时间区间-开始时间 17 // 事务时间区间-开始时间
19 TransactionStartTime time.Time `json:"transactionStartTime,omitempty"` 18 TransactionStartTime time.Time `json:"transactionStartTime,omitempty"`
20 // 事务时间区间-截止时间 19 // 事务时间区间-截止时间
21 TransactionEndTime time.Time `json:"transactionEndTime,omitempty"` 20 TransactionEndTime time.Time `json:"transactionEndTime,omitempty"`
22 - Operator int64 `json:"operator,omitempty"` 21 + // 操作人UID
  22 + Operator int64 `json:"operator,omitempty"`
23 // 查询偏离量 23 // 查询偏离量
24 Offset int `json:"offset,omitempty"` 24 Offset int `json:"offset,omitempty"`
25 // 查询限制 25 // 查询限制
@@ -7,7 +7,8 @@ import ( @@ -7,7 +7,8 @@ import (
7 "time" 7 "time"
8 ) 8 )
9 9
10 -type CreateExchangeActivityCommand struct { 10 +type UpdateExchangeCashActivity struct {
  11 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId"` // 兑换现金活动id
11 CompanyId int64 `json:"companyId" valid:"Required"` // 公司id 12 CompanyId int64 `json:"companyId" valid:"Required"` // 公司id
12 ExchangeActivityName string `json:"exchangeActivityName"` // 活动名称 13 ExchangeActivityName string `json:"exchangeActivityName"` // 活动名称
13 Deadline time.Time `json:"deadline"` // 活动截止时间 14 Deadline time.Time `json:"deadline"` // 活动截止时间
@@ -15,12 +16,12 @@ type CreateExchangeActivityCommand struct { @@ -15,12 +16,12 @@ type CreateExchangeActivityCommand struct {
15 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换素币 16 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换素币
16 ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金 17 ExchangedCash float64 `json:"exchangedCash"` // 已兑换现金
17 ExchangeRate float64 `json:"exchangeRate"` // 兑换汇率 18 ExchangeRate float64 `json:"exchangeRate"` // 兑换汇率
18 - ExchangeCashPeople []*domain.ExchangeCashPerson `json:"exchangeList"` // 兑换活动清单 19 + ExchangeCashPeople []*domain.ExchangeCashPerson `json:"exchangeCashPeople"` // 兑换活动清单
19 } 20 }
20 21
21 -func (createExchangeActivityCommand *CreateExchangeActivityCommand) ValidateCommand() error { 22 +func (updateExchangeCashActivity *UpdateExchangeCashActivity) ValidateCommand() error {
22 valid := validation.Validation{} 23 valid := validation.Validation{}
23 - b, err := valid.Valid(createExchangeActivityCommand) 24 + b, err := valid.Valid(updateExchangeCashActivity)
24 if err != nil { 25 if err != nil {
25 return err 26 return err
26 } 27 }
1 package command 1 package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +type UpdateExchangeCashPersonCommand struct {
  9 + ExchangeCashPersonId int64 `json:"exchangeCashPersonId" valid:"Required"` // 兑换现金人员编号
  10 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId" valid:"Required"` // 参与的兑换现金活动id
  11 + ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币(需要和当前的已兑换素币进行比较,少于当前已兑换素币则生成一条扣除素币记录,大于当前已兑换素币则生成一条增加素币记录)
  12 + ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金
  13 +}
  14 +
  15 +func (updateExchangeCashPersonCommand *UpdateExchangeCashPersonCommand) ValidateCommand() error {
  16 + valid := validation.Validation{}
  17 + b, err := valid.Valid(updateExchangeCashPersonCommand)
  18 + if err != nil {
  19 + return err
  20 + }
  21 + if !b {
  22 + for _, validErr := range valid.Errors {
  23 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  24 + }
  25 + }
  26 + return nil
  27 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 获取现金池
  9 +type GetCashPoolQuery struct {
  10 + CompanyId int64 `json:"companyId"` // 公司id
  11 +}
  12 +
  13 +func (getCashPoolQuery *GetCashPoolQuery) ValidateQuery() error {
  14 + valid := validation.Validation{}
  15 + b, err := valid.Valid(getCashPoolQuery)
  16 + if err != nil {
  17 + return err
  18 + }
  19 + if !b {
  20 + for _, validErr := range valid.Errors {
  21 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  22 + }
  23 + }
  24 + return nil
  25 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 获取兑换现金活动
  9 +type GetExchangeCashActivityQuery struct {
  10 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId"` // 兑换现金活动id
  11 +}
  12 +
  13 +func (getExchangeCashActivityQuery *GetExchangeCashActivityQuery) ValidateQuery() error {
  14 + valid := validation.Validation{}
  15 + b, err := valid.Valid(getExchangeCashActivityQuery)
  16 + if err != nil {
  17 + return err
  18 + }
  19 + if !b {
  20 + for _, validErr := range valid.Errors {
  21 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  22 + }
  23 + }
  24 + return nil
  25 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 获取兑换现金活动列表
  9 +type ListExchangeCashActivityQuery struct {
  10 + CompanyId int64 `json:"companyId"` // 公司id
  11 +}
  12 +
  13 +func (listExchangeCashActivityQuery *ListExchangeCashActivityQuery) ValidateQuery() error {
  14 + valid := validation.Validation{}
  15 + b, err := valid.Valid(listExchangeCashActivityQuery)
  16 + if err != nil {
  17 + return err
  18 + }
  19 + if !b {
  20 + for _, validErr := range valid.Errors {
  21 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  22 + }
  23 + }
  24 + return nil
  25 +}
1 package query 1 package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego/validation"
  6 +)
  7 +
  8 +// 获取兑换活动兑换清单
  9 +type ListExchangeCashPersonQuery struct {
  10 + ExchangeCashActivityId int64 `json:"exchangeCashActivityId"` // 兑换现金活动id
  11 +}
  12 +
  13 +func (listExchangeCashPersonQuery *ListExchangeCashPersonQuery) ValidateQuery() error {
  14 + valid := validation.Validation{}
  15 + b, err := valid.Valid(listExchangeCashPersonQuery)
  16 + if err != nil {
  17 + return err
  18 + }
  19 + if !b {
  20 + for _, validErr := range valid.Errors {
  21 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  22 + }
  23 + }
  24 + return nil
  25 +}
1 package service 1 package service
2 2
3 -// 投入现金服务  
4 -type CashService struct { 3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/suMoney/command"
  7 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain/service"
  8 +)
  9 +
  10 +// 现金池服务
  11 +type CashPoolService struct {
5 } 12 }
6 13
7 -// 投入现金  
8 -//func (cashService *CashService) InputCashPool(inputCashPoolCommand *command.InputCashPooCommand) (interface{}, error) {  
9 -//  
10 -//}  
  14 +// 操作现金池
  15 +func (cashService *CashPoolService) OperationCashPool(createCashPoolCommand *command.CreateCashPoolCommand) (interface{}, error) {
  16 + if err := createCashPoolCommand.ValidateCommand(); err != nil {
  17 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  18 + }
  19 +
  20 + transactionContext, err := factory.CreateTransactionContext(nil)
  21 +
  22 + if err != nil {
  23 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  24 + }
  25 +
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 +
  30 + defer func() {
  31 + transactionContext.RollbackTransaction()
  32 + }()
  33 +
  34 + var cashPoolService service.InputCashPoolService
  35 +
  36 + if value, err := factory.CreateInputCashPoolService(map[string]interface{}{
  37 + "transactionContext": transactionContext,
  38 + }); err != nil {
  39 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  40 + } else {
  41 + cashPoolService = value
  42 + }
  43 +
  44 + if task, err := cashPoolService.Input(createCashPoolCommand.CompanyId, createCashPoolCommand.Operator, createCashPoolCommand.Cash); err != nil {
  45 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  46 + } else {
  47 + if err := transactionContext.CommitTransaction(); err != nil {
  48 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  49 + }
  50 + return task, nil
  51 + }
  52 +}
  53 +
  54 +func NewCashPoolService(options map[string]interface{}) *CashPoolService {
  55 + newCashPoolService := &CashPoolService{}
  56 + return newCashPoolService
  57 +}
@@ -114,9 +114,6 @@ func (suMoneyService *SuMoneyService) ExchangeSuMoney(exchangeSuMoneyCommand *co @@ -114,9 +114,6 @@ func (suMoneyService *SuMoneyService) ExchangeSuMoney(exchangeSuMoneyCommand *co
114 } 114 }
115 } 115 }
116 116
117 -// 素币兑换现金  
118 -//func (suMoneyService *SuMoneyService) ExchangeCash(exchangeCashComman *)  
119 -  
120 // 搜索素币事务记录 117 // 搜索素币事务记录
121 func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMoneyTransactionRecordCommand *command.SearchSuMoneyTransactionRecordCommand) (interface{}, error) { 118 func (suMoneyService *SuMoneyService) SearchSuMoneyTransactionRecord(searchSuMoneyTransactionRecordCommand *command.SearchSuMoneyTransactionRecordCommand) (interface{}, error) {
122 if err := searchSuMoneyTransactionRecordCommand.ValidateCommand(); err != nil { 119 if err := searchSuMoneyTransactionRecordCommand.ValidateCommand(); err != nil {
1 package domain 1 package domain
2 2
  3 +import "time"
  4 +
3 // 现金池 5 // 现金池
4 type CashPool struct { 6 type CashPool struct {
5 - CashPoolId int64 `json:"cashPoolId"` // 现金池id 7 + CashPoolId int64 `json:"cashPoolId"` // 现金池id
6 CompanyId int64 `json:"companyId"` // 公司id 8 CompanyId int64 `json:"companyId"` // 公司id
7 Cash float64 `json:"cashPool"` // 投入现金池的现金 9 Cash float64 `json:"cashPool"` // 投入现金池的现金
8 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金 10 ExchangedCash float64 `json:"exchangedCash"` // 已兑换的现金
9 UnExchangeCash float64 `json:"unExchangeCash"` // 未兑换的现金 11 UnExchangeCash float64 `json:"unExchangeCash"` // 未兑换的现金
10 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币 12 ExchangedSuMoney float64 `json:"exchangedSuMoney"` // 已兑换的素币
11 UnExchangeSuMoney float64 `json:"unExchangeSuMoney"` // 未兑换的素币 13 UnExchangeSuMoney float64 `json:"unExchangeSuMoney"` // 未兑换的素币
  14 + Operator *EmployeeInfo `json:"operator"` // 操作人
12 Rate float64 `json:"rate"` // 平均兑换汇率 15 Rate float64 `json:"rate"` // 平均兑换汇率
  16 + CreateTime time.Time `json:"createTime"` // 现金投入现金池时间
13 } 17 }
14 18
15 type CashPoolRepository interface { 19 type CashPoolRepository interface {
16 Save(cashPool *CashPool) (*CashPool, error) 20 Save(cashPool *CashPool) (*CashPool, error)
  21 + FindOne(queryOptions map[string]interface{}) (*CashPool, error)
17 } 22 }
18 23
19 func (cashPool *CashPool) Identity() interface{} { 24 func (cashPool *CashPool) Identity() interface{} {
@@ -2,11 +2,11 @@ package domain @@ -2,11 +2,11 @@ package domain
2 2
3 // 参与兑换现金人员 3 // 参与兑换现金人员
4 type ExchangeCashPerson struct { 4 type ExchangeCashPerson struct {
5 - PersonId int64 `json:"employeeId"` // 兑换人员ID  
6 - CompanyId int64 `json:"companyId"` // 公司id  
7 - PersonInfo *EmployeeInfo `json:"employeeInfo"` // 兑换人员信息  
8 - ExchangedSuMoney float64 `json:"suMoney"` // 已兑换的素币  
9 - ExchangedCash float64 `json:"cash"` // 已兑换的现金 5 + ExchangeCashPersonId int64 `json:"employeeId"` // 兑换人员ID
  6 + CompanyId int64 `json:"companyId"` // 公司id
  7 + ExchangeCashPersonInfo *EmployeeInfo `json:"employeeInfo"` // 兑换人员信息
  8 + ExchangedSuMoney float64 `json:"suMoney"` // 已兑换的素币
  9 + ExchangedCash float64 `json:"cash"` // 已兑换的现金
10 } 10 }
11 11
12 type ExchangeCashPersonRepository interface { 12 type ExchangeCashPersonRepository interface {
@@ -17,8 +17,8 @@ type ExchangeCashPersonRepository interface { @@ -17,8 +17,8 @@ type ExchangeCashPersonRepository interface {
17 } 17 }
18 18
19 func (exchangeCashPerson *ExchangeCashPerson) Identity() interface{} { 19 func (exchangeCashPerson *ExchangeCashPerson) Identity() interface{} {
20 - if exchangeCashPerson.PersonId == 0 { 20 + if exchangeCashPerson.ExchangeCashPersonId == 0 {
21 return nil 21 return nil
22 } 22 }
23 - return exchangeCashPerson.PersonId 23 + return exchangeCashPerson.ExchangeCashPersonId
24 } 24 }
  1 +package service
  2 +
  3 +import (
  4 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  6 +)
  7 +
  8 +type InputCashPoolService interface {
  9 + coreDomain.DomainEventPublisher
  10 + Input(companyId int64, operatorUid int64, cash float64) (*domain.CashPool, error)
  11 +}
@@ -11,18 +11,18 @@ const ( @@ -11,18 +11,18 @@ const (
11 11
12 // 素币事务记录 12 // 素币事务记录
13 type SuMoneyTransactionRecord struct { 13 type SuMoneyTransactionRecord struct {
14 - SuMoneyTransactionRecordId int64 `json:"suMoneyTransactionRecordId"` // 素币事务记录ID  
15 - RecordType int `json:"recordType"` // 记录类型  
16 - Employee *EmployeeInfo `json:"employee"` // 记录关联员工  
17 - SuMoneyBeforeTransaction float64 `json:"suMoneyBeforeTransaction"` // 事务处理前素币值  
18 - CurrentSuMoney float64 `json:"currentSuMoney"` // 当前素币值  
19 - CashPoolBeforeTransaction *CashPool `json:"cashPoolBeforeTransaction"` // 事务处理前现金池,用于兑换现金操作  
20 - CurrentCashPool *CashPool `json:"currentCashPool"` // 当前现金池,用于兑换现金操作  
21 - SuMoney float64 `json:"suMoney"` // 事务素币值  
22 - Cash float64 `json:"cash"` // 事务现金值,用于兑换现金  
23 - Operator *EmployeeInfo `json:"operator"` // 操作人  
24 - RecordDescription string `json:"recordDescription"` // 素币事务记录描述  
25 - CreateTime time.Time `json:"createTime"` // 创建时间 14 + SuMoneyTransactionRecordId int64 `json:"suMoneyTransactionRecordId"` // 素币事务记录ID
  15 + RecordType int `json:"recordType"` // 记录类型
  16 + Employee *EmployeeInfo `json:"employee"` // 记录关联员工
  17 + SuMoneyBeforeTransaction float64 `json:"suMoneyBeforeTransaction"` // 事务处理前素币值
  18 + CurrentSuMoney float64 `json:"currentSuMoney"` // 当前素币值
  19 + CashPoolBeforeTransaction *CashPool `json:"cashPoolBeforeTransaction"` // 事务处理前现金池,用于兑换现金操作
  20 + CurrentCashPool *CashPool `json:"currentCashPool"` // 当前现金池,用于兑换现金操作
  21 + SuMoney float64 `json:"suMoney"` // 事务素币值
  22 + Cash float64 `json:"cash"` // 事务现金值,用于兑换现金操作
  23 + Operator *EmployeeInfo `json:"operator"` // 操作人
  24 + RecordDescription string `json:"recordDescription"` // 素币事务记录描述
  25 + CreateTime time.Time `json:"createTime"` // 创建时间
26 } 26 }
27 27
28 type SuMoneyTransactionRecordRepository interface { 28 type SuMoneyTransactionRecordRepository interface {
@@ -115,8 +115,8 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{ @@ -115,8 +115,8 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{
115 } 115 }
116 116
117 func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) { 117 func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
118 - var incomeSuMoney float64  
119 - var expendSuMoney float64 118 + var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
  119 + var expendSuMoney float64 // 消耗的素币(1:兑换(物资、现金),4: 扣除)
120 tx := dao.transactionContext.PgTx 120 tx := dao.transactionContext.PgTx
121 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord) 121 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
122 if err := tx.Model(suMoneyTransactionRecordModel). 122 if err := tx.Model(suMoneyTransactionRecordModel).
@@ -143,40 +143,47 @@ func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transaction @@ -143,40 +143,47 @@ func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transaction
143 }, nil 143 }, nil
144 } 144 }
145 145
146 -func (dao *EmployeeDao) CalculateSystemUnChangeSuMoney(companyId int64) (map[string]interface{}, error) {  
147 - var unExchangeSuMoney float64 146 +// 计算系统素币
  147 +func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) ( float64, float64, error) {
  148 + var systemUnExchangeSuMoney float64
  149 + var systemExchangedSuMoney float64
148 150
149 tx := dao.transactionContext.PgTx 151 tx := dao.transactionContext.PgTx
150 - employeeModel := new(models.Employee)  
151 152
  153 + employeeModel := new(models.Employee)
152 if err := tx.Model(employeeModel). 154 if err := tx.Model(employeeModel).
153 - ColumnExpr("sum(employee.su_money) AS unExchangeSuMoney"). 155 + ColumnExpr("sum(employee.su_money) AS system_unExchange_su_money").
154 Where("employee.company_id = ?", companyId). 156 Where("employee.company_id = ?", companyId).
155 - Select(&employeeModel); err != nil {  
156 - return nil, err 157 + Select(&systemUnExchangeSuMoney); err != nil {
  158 + return 0, 0, err
157 } 159 }
158 160
159 - return map[string]interface{}{  
160 - "unExchangeSuMoney": unExchangeSuMoney,  
161 - }, nil 161 + exchangeCashActivityModel := new(models.ExchangeCashActivity)
  162 + if err := tx.Model(exchangeCashActivityModel).
  163 + ColumnExpr("sum(exchange_cash_activity.exchanged_su_money) AS system_changed_su_money").
  164 + Where("exchange_cash_activity.company_id = ?", companyId).
  165 + Select(&systemExchangedSuMoney); err != nil {
  166 + return 0, 0, err
  167 + }
  168 +
  169 + return systemUnExchangeSuMoney, systemExchangedSuMoney, nil
162 } 170 }
163 171
164 -func (dao *EmployeeDao) CalculateSystemExchangedCash(companyId int64) (map[string]interface{}, error) {  
165 - var exchangedCash float64 172 +// 计算系统已兑换现金
  173 +func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (float64, error) {
  174 + var systemExchangedCash float64 // 系统已兑换现金
166 175
167 tx := dao.transactionContext.PgTx 176 tx := dao.transactionContext.PgTx
168 - suMoneyExchangeTransactionRecordModel := new(models.SuMoneyExchangeTransactionRecord) 177 + exchangeCashActivityModel := new(models.ExchangeCashActivity)
169 178
170 - if err := tx.Model(suMoneyExchangeTransactionRecordModel).  
171 - ColumnExpr("sum(su_money_exchange_transaction_record.cash) AS exchangedCash").  
172 - Where("su_money_exchange_transaction_record.company_id = ?", companyId).  
173 - Select(&suMoneyExchangeTransactionRecordModel) ; err != nil {  
174 - return nil, err 179 + if err := tx.Model(exchangeCashActivityModel).
  180 + ColumnExpr("sum(exchange_cash_activity.exchanged_cash) AS system_exchanged_cash").
  181 + Where("exchange_cash_activity.company_id = ?", companyId).
  182 + Select(&systemExchangedCash) ; err != nil {
  183 + return 0, err
175 } 184 }
176 185
177 - return map[string]interface{}{  
178 - "exchangedCash": exchangedCash,  
179 - }, nil 186 + return systemExchangedCash, nil
180 } 187 }
181 188
182 func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) { 189 func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
1 package domain_service 1 package domain_service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + coreDomain "github.com/linmadan/egglib-go/core/domain"
  7 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  8 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
  9 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/dao"
  11 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/repository"
  12 + "time"
  13 +)
  14 +
  15 +type CashPoolService struct {
  16 + coreDomain.BaseEventPublisher
  17 + transactionContext *pgTransaction.TransactionContext
  18 +}
  19 +
  20 +func (service *CashPoolService) Input(companyId int64, operatorUid int64, cash float64) (*domain.CashPool, error) {
  21 + var cashPoolRepository domain.CashPoolRepository
  22 + var employeeRepository domain.EmployeeRepository
  23 +
  24 + if repository, err := repository.NewEmployeeRepository(service.transactionContext); err != nil {
  25 + return nil, err
  26 + } else {
  27 + employeeRepository = repository
  28 + }
  29 +
  30 + transactionContext, err := factory.CreateTransactionContext(nil)
  31 +
  32 + var employeeDao *dao.EmployeeDao
  33 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
  34 + "transactionContext": transactionContext,
  35 + }); err != nil {
  36 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  37 + } else {
  38 + employeeDao = value
  39 + }
  40 +
  41 + var systemExchangedCash float64
  42 + if value, err := employeeDao.CalculateSystemCash(companyId); err != nil {
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  44 + } else {
  45 + systemExchangedCash = value
  46 + }
  47 +
  48 + var (
  49 + systemChangedSuMoney float64
  50 + systemUnChangeSuMoney float64
  51 + )
  52 + if value1, value2, err := employeeDao.CalculateSystemSuMoney(companyId); err != nil {
  53 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  54 + } else {
  55 + systemChangedSuMoney = value1
  56 + systemUnChangeSuMoney = value2
  57 + }
  58 +
  59 + operator, err := employeeRepository.FindOne(map[string]interface{}{
  60 + "uid": operatorUid,
  61 + })
  62 + if err != nil {
  63 + return nil, err
  64 + }
  65 + if operator == nil {
  66 + return nil, fmt.Errorf("无效的操作者")
  67 + }
  68 +
  69 + currentCashPool, err := cashPoolRepository.FindOne(map[string]interface{}{
  70 + "companyId": companyId,
  71 + })
  72 + if err != nil {
  73 + return nil, err
  74 + }
  75 + if currentCashPool.ExchangedCash > cash {
  76 + return nil, fmt.Errorf("当前投入现金少于系统已兑换现金值")
  77 + }
  78 +
  79 + cashPoolInfo := &domain.CashPool{
  80 + CompanyId: companyId,
  81 + Cash: cash,
  82 + ExchangedCash: systemExchangedCash,
  83 + UnExchangeCash: currentCashPool.UnExchangeCash + cash,
  84 + ExchangedSuMoney: systemChangedSuMoney,
  85 + UnExchangeSuMoney: systemUnChangeSuMoney,
  86 + Rate: systemExchangedCash / systemChangedSuMoney,
  87 + Operator: operator.EmployeeInfo,
  88 + CreateTime: time.Now(),
  89 + }
  90 +
  91 + if cashPool, err := cashPoolRepository.Save(cashPoolInfo); err != nil {
  92 + return nil, err
  93 + } else {
  94 + return cashPool, nil
  95 + }
  96 +}
  97 +
  98 +func NewCashPoolService(transactionContext *pgTransaction.TransactionContext) (*CashPoolService, error) {
  99 + if transactionContext == nil {
  100 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  101 + } else {
  102 + return &CashPoolService{
  103 + transactionContext: transactionContext,
  104 + }, nil
  105 + }
  106 +}
@@ -37,6 +37,7 @@ func init() { @@ -37,6 +37,7 @@ func init() {
37 (*models.Notification)(nil), 37 (*models.Notification)(nil),
38 (*models.SentNotification)(nil), 38 (*models.SentNotification)(nil),
39 (*models.ExchangeActivities)(nil), 39 (*models.ExchangeActivities)(nil),
  40 + (*models.CashPool)(nil),
40 } { 41 } {
41 err := DB.CreateTable(model, &orm.CreateTableOptions{ 42 err := DB.CreateTable(model, &orm.CreateTableOptions{
42 Temp: false, 43 Temp: false,
  1 +package models
  2 +
  3 +type CashPool struct {
  4 + Id int64 `json:"id"` // 现金池id
  5 +}
@@ -5,7 +5,7 @@ import ( @@ -5,7 +5,7 @@ import (
5 "time" 5 "time"
6 ) 6 )
7 7
8 -type ExchangeActivities struct { 8 +type ExchangeCashActivity struct {
9 TableName string `pg:"exchange_activities,alias:exchange_activity"` 9 TableName string `pg:"exchange_activities,alias:exchange_activity"`
10 Id int64 `pg:",pk"` // 兑换活动ID 10 Id int64 `pg:",pk"` // 兑换活动ID
11 CompanyId int64 // 公司ID 11 CompanyId int64 // 公司ID
  1 +package models
  2 +
  3 +type ExchangeCashPerson struct {
  4 +
  5 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
  8 +)
  9 +
  10 +type CashPoolRepository struct {
  11 + transactionContext *pgTransaction.TransactionContext
  12 +}
  13 +
  14 +func (repostitory *CashPoolRepository) Save() (*domain.CashPool, error) {
  15 +
  16 + return nil, nil
  17 +}
  18 +
  19 +func (repostitory *CashPoolRepository) FindOne(queryOptions map[string]interface{}) (*domain.CashPool, error) {
  20 +
  21 + return nil, nil
  22 +}
  23 +
  24 +func (repository *CashPoolRepository) transformPgModelToDomainModel(cashPoolModel *models.CashPool) (*domain.CashPool, error) {
  25 + return &domain.CashPool{
  26 + CashPoolId: cashPoolModel.Id,
  27 + }, nil
  28 +}
  29 +
  30 +func NewCashPoolRepository(transactionContext *pgTransaction.TransactionContext) (*CashPoolRepository, error) {
  31 + if transactionContext == nil {
  32 + return nil, fmt.Errorf("transactionContext参数不能为null")
  33 + } else {
  34 + return &CashPoolRepository{
  35 + transactionContext: transactionContext,
  36 + }, nil
  37 + }
  38 +}
  1 +package repository
  2 +
  3 +import (
  4 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  5 +)
  6 +
  7 +type ExchangeCashActivityRepository struct {
  8 + transactionContext *pgTransaction.TransactionContext
  9 +}
  10 +
  11 +func (repository *ExchangeCashActivityRepository) Save() (*ExchangeCashActivityRepository, error) {
  12 +
  13 + return nil, nil
  14 +}
  15 +
  16 +func (repository *ExchangeCashActivityRepository) FindOne(queryOptions map[string]interface{}) (*ExchangeCashActivityRepository, error) {
  17 +
  18 + return nil, nil
  19 +}
  20 +
  21 +func (repository *ExchangeCashActivityRepository) Find(queryOptions map[string]interface{}) (*ExchangeCashActivityRepository, error) {
  22 +
  23 + return nil, nil
  24 +}
  25 +
  26 +func (repository *ExchangeCashActivityRepository) Remove() (*ExchangeCashActivityRepository, error) {
  27 +
  28 + return nil, nil
  29 +}
  30 +
  31 +//func (repository *ExchangeCashActivityRepository) transformPgModelToDomainModel(customerValueModel *models.CustomerValue) (*domain.CustomerValue, error) {
  32 +// return &domain.CustomerValue{
  33 +// CustomerValueId: customerValueModel.Id,
  34 +// CustomerValueName: customerValueModel.CustomerValueName,
  35 +// CompanyId: customerValueModel.CompanyId,
  36 +// }, nil
  37 +//}
  38 +//func NewExchangeCashActivityRepository(transactionContext *pgTransaction.TransactionContext) (*CustomerValueRepository, error) {
  39 +// if transactionContext == nil {
  40 +// return nil, fmt.Errorf("transactionContext参数不能为nil")
  41 +// } else {
  42 +// return &CustomerValueRepository{
  43 +// transactionContext: transactionContext,
  44 +// }, nil
  45 +// }
  46 +//}