作者 陈志颖

refactor:重构系统素币统计和系统现金统计

1 package service 1 package service
2 2
  3 +import (
  4 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/listInterval/command"
  5 + "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/listInterval/query"
  6 +)
  7 +
3 type ListIntervalService struct { 8 type ListIntervalService struct {
4 9
5 } 10 }
6 11
7 // TODO 创建排行榜时间 12 // TODO 创建排行榜时间
8 - 13 +func (listIntervalService *ListIntervalService) CreateListInterval(createListIntervalComand *command.CreateListIntervalCommand) (interface{}, error) {
  14 + return nil, nil
  15 +}
9 16
10 // TODO 返回排行榜时间列表 17 // TODO 返回排行榜时间列表
11 - 18 +func (listIntervalService *ListIntervalService) ListListInterval(listListIntervalQuery *query.ListListIntervalQuery) (interface{}, error) {
  19 + return nil, nil
  20 +}
12 21
13 // TODO 返回排行榜时间 22 // TODO 返回排行榜时间
14 - 23 +func (listIntervalService *ListIntervalService) GetListInterval(getListIntervalQuery *query.GetListIntervalQuery) (interface{}, error) {
  24 + return nil, nil
  25 +}
15 26
16 // TODO 更新排行榜时间 27 // TODO 更新排行榜时间
17 - 28 +func (listIntervalService *ListIntervalService) UpdateListInterval(updateListIntervalCommand *command.UpdateListIntervalCommand) (interface{}, error) {
  29 + return nil, nil
  30 +}
18 31
19 // TODO 移除排行榜时间 32 // TODO 移除排行榜时间
20 - 33 +func (listIntervalService *ListIntervalService) RemoveListInterval(removeListIntervalCommand *command.RemoveListIntervalCommand) (interface{}, error) {
  34 + return nil, nil
  35 +}
21 36
22 func NewListIntervalService(options map[string] interface{}) *ListIntervalService { 37 func NewListIntervalService(options map[string] interface{}) *ListIntervalService {
23 newListIntervalService := &ListIntervalService{} 38 newListIntervalService := &ListIntervalService{}
@@ -170,6 +170,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone @@ -170,6 +170,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
170 employeeDao = value 170 employeeDao = value
171 } 171 }
172 172
  173 + // 企业员工资源库
173 var employeeRepository domain.EmployeeRepository 174 var employeeRepository domain.EmployeeRepository
174 if value, err := factory.CreateEmployeeRepository(map[string]interface{}{ 175 if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
175 "transactionContext": transactionContext, 176 "transactionContext": transactionContext,
@@ -178,6 +179,8 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone @@ -178,6 +179,8 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
178 } else { 179 } else {
179 employeeRepository = value 180 employeeRepository = value
180 } 181 }
  182 +
  183 + // 判断企业员工是否有效
181 employee, err := employeeRepository.FindOne(map[string]interface{}{ 184 employee, err := employeeRepository.FindOne(map[string]interface{}{
182 "uid": personSuMoneyStatisticsCommand.Uid, 185 "uid": personSuMoneyStatisticsCommand.Uid,
183 }) 186 })
@@ -188,6 +191,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone @@ -188,6 +191,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
188 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工") 191 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
189 } 192 }
190 193
  194 + // 计算个人素币
191 if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); err != nil { 195 if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); err != nil {
192 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 196 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
193 } else { 197 } else {
@@ -199,7 +203,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone @@ -199,7 +203,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
199 } 203 }
200 } 204 }
201 205
202 -// 系统素币统计 206 +// TODO 系统素币统计
203 func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMoneyStatisticsCommand *command.SystemSuMoneyStatisticsCommand) (interface{}, error) { 207 func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMoneyStatisticsCommand *command.SystemSuMoneyStatisticsCommand) (interface{}, error) {
204 if err := systemSuMoneyStatisticsCommand.ValidateCommand(); err != nil { 208 if err := systemSuMoneyStatisticsCommand.ValidateCommand(); err != nil {
205 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 209 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -214,15 +218,35 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone @@ -214,15 +218,35 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
214 defer func() { 218 defer func() {
215 transactionContext.RollbackTransaction() 219 transactionContext.RollbackTransaction()
216 }() 220 }()
217 - var cashPoolDao *dao.CashPoolDao  
218 - if value, err := factory.CreateCashPoolDao(map[string]interface{}{ 221 +
  222 + //var cashPoolDao *dao.CashPoolDao
  223 + //if value, err := factory.CreateCashPoolDao(map[string]interface{}{
  224 + // "transactionContext": transactionContext,
  225 + //}); err != nil {
  226 + // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  227 + //} else {
  228 + // cashPoolDao = value
  229 + //}
  230 +
  231 + var employeeDao *dao.EmployeeDao
  232 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
219 "transactionContext": transactionContext, 233 "transactionContext": transactionContext,
220 }); err != nil { 234 }); err != nil {
221 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 235 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
222 } else { 236 } else {
223 - cashPoolDao = value 237 + employeeDao = value
224 } 238 }
225 - if systemSuMoneyStatistics, err := cashPoolDao.CalculateSystemCash(systemSuMoneyStatisticsCommand.CompanyId); err != nil { 239 +
  240 + //var employeeRepository domain.EmployeeRepository
  241 + //if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
  242 + // "transactionContext": transactionContext,
  243 + //}); err != nil {
  244 + // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  245 + //} else {
  246 + // employeeRepository = value
  247 + //}
  248 +
  249 + if systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(systemSuMoneyStatisticsCommand.CompanyId); err != nil {
226 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 250 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
227 } else { 251 } else {
228 if err := transactionContext.CommitTransaction(); err != nil { 252 if err := transactionContext.CommitTransaction(); err != nil {
@@ -232,7 +256,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone @@ -232,7 +256,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
232 } 256 }
233 } 257 }
234 258
235 -// 系统现金统计 259 +// TODO 系统现金统计
236 func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStatisticsCommand *command.SystemCashStatisticsCommand) (interface{}, error) { 260 func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStatisticsCommand *command.SystemCashStatisticsCommand) (interface{}, error) {
237 if err := systemCashStatisticsCommand.ValidateCommand(); err != nil { 261 if err := systemCashStatisticsCommand.ValidateCommand(); err != nil {
238 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 262 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -247,15 +271,26 @@ func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStati @@ -247,15 +271,26 @@ func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStati
247 defer func() { 271 defer func() {
248 transactionContext.RollbackTransaction() 272 transactionContext.RollbackTransaction()
249 }() 273 }()
250 - var cashPoolDao *dao.CashPoolDao  
251 - if value, err := factory.CreateCashPoolDao(map[string]interface{}{ 274 +
  275 + //var cashPoolDao *dao.CashPoolDao
  276 + //if value, err := factory.CreateCashPoolDao(map[string]interface{}{
  277 + // "transactionContext": transactionContext,
  278 + //}); err != nil {
  279 + // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  280 + //} else {
  281 + // cashPoolDao = value
  282 + //}
  283 +
  284 + var employeeDao *dao.EmployeeDao
  285 + if value, err := factory.CreateEmployeeDao(map[string]interface{}{
252 "transactionContext": transactionContext, 286 "transactionContext": transactionContext,
253 }); err != nil { 287 }); err != nil {
254 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 288 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
255 } else { 289 } else {
256 - cashPoolDao = value 290 + employeeDao = value
257 } 291 }
258 - if systemCashStatistics, err := cashPoolDao.CalculateSystemCash(systemCashStatisticsCommand.CompanyId); err != nil { 292 +
  293 + if systemCashStatistics, err := employeeDao.CalculateSystemCash(systemCashStatisticsCommand.CompanyId); err != nil {
259 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 294 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
260 } else { 295 } else {
261 if err := transactionContext.CommitTransaction(); err != nil { 296 if err := transactionContext.CommitTransaction(); err != nil {
@@ -7,7 +7,7 @@ import ( @@ -7,7 +7,7 @@ import (
7 7
8 // 获取现金池 8 // 获取现金池
9 type GetCashPoolQuery struct { 9 type GetCashPoolQuery struct {
10 - CompanyId int64 `json:"companyId"` // 公司id 10 + CompanyId int64 `json:"companyId" valid:"Required"` // 公司id
11 } 11 }
12 12
13 func (getCashPoolQuery *GetCashPoolQuery) ValidateQuery() error { 13 func (getCashPoolQuery *GetCashPoolQuery) ValidateQuery() error {
@@ -114,9 +114,64 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{ @@ -114,9 +114,64 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{
114 }, nil 114 }, nil
115 } 115 }
116 116
  117 +// TODO 计算系统素币
  118 +func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] interface{}, error) {
  119 + var systemUnExchangeSuMoney float64
  120 + var systemExchangedSuMoney float64
  121 + tx := dao.transactionContext.PgTx
  122 + suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
  123 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
  124 + ColumnExpr("sum(su_money_transaction_record.current_su_money) AS system_unExchange_su_money").
  125 + Where("employee.company_id = ?", companyId).
  126 + Where(`su_money_transaction_record.record_type = ?`, 5).
  127 + Select(&systemUnExchangeSuMoney); err != nil {
  128 + return nil, err
  129 + }
  130 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
  131 + ColumnExpr("sum(su_money_transaction_record.su_money) AS system_changed_su_money").
  132 + Where("employee.company_id = ?", companyId).
  133 + Where(`su_money_transaction_record.record_type = ?`, 5).
  134 + Select(&systemExchangedSuMoney); err != nil {
  135 + return nil, err
  136 + }
  137 + return map[string] interface{} {
  138 + "systemUnExchangeSuMoney": systemUnExchangeSuMoney,
  139 + "systemExchangedSuMoney": systemExchangedSuMoney,
  140 + },nil
  141 +}
  142 +
  143 +// TODO 计算系统现金
  144 +func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interface{}, error) {
  145 + var (
  146 + systemUnExchangeCash float64
  147 + systemExchangedCash float64
  148 + )
  149 + tx := dao.transactionContext.PgTx
  150 + suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
  151 + if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
  152 + ColumnExpr("sum(su_money_transaction_record.cash) AS system_exchanged_cash").
  153 + Where("employee.company_id = ?", companyId).
  154 + Where(`su_money_transaction_record.record_type = ?`, 5).
  155 + Select(&systemExchangedCash) ; err != nil {
  156 + return nil, err
  157 + }
  158 +
  159 + cashPool := new(models.CashPool)
  160 + if err := tx.Model(cashPool).
  161 + Where("cash_pool.company_id = ?", companyId).
  162 + Select(&systemUnExchangeCash) ; err != nil {
  163 + return nil, err
  164 + }
  165 +
  166 + return map[string] interface{} {
  167 + "systemUnExchangeCash": systemUnExchangeCash,
  168 + "systemExchangedCash": systemExchangedCash,
  169 + }, nil
  170 +}
  171 +
117 func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) { 172 func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
118 var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加) 173 var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
119 - var expendSuMoney float64 // 消耗的素币(1:兑换(物资、现金),4: 扣除 174 + var expendSuMoney float64 // 消耗的素币(1:兑换物资,4:扣除, 5: 兑换现金
120 tx := dao.transactionContext.PgTx 175 tx := dao.transactionContext.PgTx
121 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord) 176 suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
122 if err := tx.Model(suMoneyTransactionRecordModel). 177 if err := tx.Model(suMoneyTransactionRecordModel).
@@ -36,8 +36,8 @@ func init() { @@ -36,8 +36,8 @@ func init() {
36 (*models.RejectTaskRecord)(nil), 36 (*models.RejectTaskRecord)(nil),
37 (*models.Notification)(nil), 37 (*models.Notification)(nil),
38 (*models.SentNotification)(nil), 38 (*models.SentNotification)(nil),
39 - (*models.ExchangeCashActivity)(nil),  
40 (*models.CashPool)(nil), 39 (*models.CashPool)(nil),
  40 + (*models.ExchangeCashActivity)(nil),
41 (*models.ExchangeCashPersonList)(nil), 41 (*models.ExchangeCashPersonList)(nil),
42 } { 42 } {
43 err := DB.CreateTable(model, &orm.CreateTableOptions{ 43 err := DB.CreateTable(model, &orm.CreateTableOptions{
1 -package models  
2 -  
3 -import (  
4 - "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"  
5 - "time"  
6 -)  
7 -  
8 -type SuMoneyExchangeTransactionRecord struct {  
9 - TableName string `pg:"su_money_exchange_transaction_records,alias:su_money_exchange_transaction_record"`  
10 - Id int64 `pg:",pk"` // 现金兑换务记录ID  
11 - RecordType int // 记录类型  
12 - Employee *domain.EmployeeInfo // 记录关联员工  
13 - //CashPoolBeforeTransaction *domain.CashPool // 事务处理前现金池  
14 - //CurrentCashPool *domain.CashPool // 当前现金池  
15 - SuMoney float64 // 事务素币值  
16 - Cash float64 // 事务现金值  
17 - Operator *domain.EmployeeInfo // 操作人  
18 - RecordDescription string // 现金投入事务记录描述  
19 - CreateTime time.Time // 创建时间  
20 -}  
@@ -19,6 +19,12 @@ type SuMoneyTransactionRecord struct { @@ -19,6 +19,12 @@ type SuMoneyTransactionRecord struct {
19 CurrentSuMoney float64 19 CurrentSuMoney float64
20 // 事务素币值 20 // 事务素币值
21 SuMoney float64 21 SuMoney float64
  22 + // 事务处理前现金值
  23 + CashBeforeTransaction float64
  24 + // 当前现金值
  25 + CurrentCash float64
  26 + // 事务现金值
  27 + Cash float64
22 // 操作人 28 // 操作人
23 Operator *domain.EmployeeInfo 29 Operator *domain.EmployeeInfo
24 // 素币事务记录描述 30 // 素币事务记录描述
@@ -17,12 +17,14 @@ func init() { @@ -17,12 +17,14 @@ func init() {
17 beego.Router("/cash-pool/input", &controllers.SuMoneyController{}, "Post:CashInput") // 现金池投入 17 beego.Router("/cash-pool/input", &controllers.SuMoneyController{}, "Post:CashInput") // 现金池投入
18 beego.Router("/cash-pool/cash-pool", &controllers.SuMoneyController{}, "Get:GetCashPool") // 返回现金池统计 18 beego.Router("/cash-pool/cash-pool", &controllers.SuMoneyController{}, "Get:GetCashPool") // 返回现金池统计
19 19
  20 + /********************************************素币兑换活动***************************************/
20 beego.Router("/cash-pool/activity/", &controllers.SuMoneyController{}, "Get:ListExchangeActivities") // 返回兑换活动列表 21 beego.Router("/cash-pool/activity/", &controllers.SuMoneyController{}, "Get:ListExchangeActivities") // 返回兑换活动列表
21 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Get:GetExchangeCashActivity") // 返回兑换活动 22 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Get:GetExchangeCashActivity") // 返回兑换活动
22 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Put:UpdateExchangeActivities") // 编辑兑换活动 23 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Put:UpdateExchangeActivities") // 编辑兑换活动
23 beego.Router("/cash-pool/activity", &controllers.SuMoneyController{}, "Post:CreateExchangeActivities") // 新增兑换活动 24 beego.Router("/cash-pool/activity", &controllers.SuMoneyController{}, "Post:CreateExchangeActivities") // 新增兑换活动
24 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Delete:RemoveExchangeActivities") // 删除兑换活动 25 beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Delete:RemoveExchangeActivities") // 删除兑换活动
25 26
  27 + /********************************************素币兑换清单*************************************/
26 beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Get:ListExchangeList") // 返回素币兑换清单 28 beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Get:ListExchangeList") // 返回素币兑换清单
27 beego.Router("/cash-pool/activity/exchange-list/:listId", &controllers.SuMoneyController{}, "Get:GetExchangeCashPerson") // 返回素币兑换人员 29 beego.Router("/cash-pool/activity/exchange-list/:listId", &controllers.SuMoneyController{}, "Get:GetExchangeCashPerson") // 返回素币兑换人员
28 beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Post:CreateExchangeList") // 新增素币兑换清单 30 beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Post:CreateExchangeList") // 新增素币兑换清单