作者 陈志颖

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

package service
import (
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/listInterval/command"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/listInterval/query"
)
type ListIntervalService struct {
}
// TODO 创建排行榜时间
func (listIntervalService *ListIntervalService) CreateListInterval(createListIntervalComand *command.CreateListIntervalCommand) (interface{}, error) {
return nil, nil
}
// TODO 返回排行榜时间列表
func (listIntervalService *ListIntervalService) ListListInterval(listListIntervalQuery *query.ListListIntervalQuery) (interface{}, error) {
return nil, nil
}
// TODO 返回排行榜时间
func (listIntervalService *ListIntervalService) GetListInterval(getListIntervalQuery *query.GetListIntervalQuery) (interface{}, error) {
return nil, nil
}
// TODO 更新排行榜时间
func (listIntervalService *ListIntervalService) UpdateListInterval(updateListIntervalCommand *command.UpdateListIntervalCommand) (interface{}, error) {
return nil, nil
}
// TODO 移除排行榜时间
func (listIntervalService *ListIntervalService) RemoveListInterval(removeListIntervalCommand *command.RemoveListIntervalCommand) (interface{}, error) {
return nil, nil
}
func NewListIntervalService(options map[string] interface{}) *ListIntervalService {
newListIntervalService := &ListIntervalService{}
... ...
... ... @@ -170,6 +170,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
employeeDao = value
}
// 企业员工资源库
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -178,6 +179,8 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
} else {
employeeRepository = value
}
// 判断企业员工是否有效
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personSuMoneyStatisticsCommand.Uid,
})
... ... @@ -188,6 +191,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
// 计算个人素币
if personSuMoneyStatistics, err := employeeDao.CalculatePersonSuMoney(personSuMoneyStatisticsCommand.Uid); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ... @@ -199,7 +203,7 @@ func (statisticsService *StatisticsService) PersonSuMoneyStatistics(personSuMone
}
}
// 系统素币统计
// TODO 系统素币统计
func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMoneyStatisticsCommand *command.SystemSuMoneyStatisticsCommand) (interface{}, error) {
if err := systemSuMoneyStatisticsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -214,15 +218,35 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
defer func() {
transactionContext.RollbackTransaction()
}()
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
//var cashPoolDao *dao.CashPoolDao
//if value, err := factory.CreateCashPoolDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// cashPoolDao = value
//}
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
employeeDao = value
}
if systemSuMoneyStatistics, err := cashPoolDao.CalculateSystemCash(systemSuMoneyStatisticsCommand.CompanyId); err != nil {
//var employeeRepository domain.EmployeeRepository
//if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// employeeRepository = value
//}
if systemSuMoneyStatistics, err := employeeDao.CalculateSystemSuMoney(systemSuMoneyStatisticsCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ... @@ -232,7 +256,7 @@ func (statisticsService *StatisticsService) SystemSuMoneyStatistics(systemSuMone
}
}
// 系统现金统计
// TODO 系统现金统计
func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStatisticsCommand *command.SystemCashStatisticsCommand) (interface{}, error) {
if err := systemCashStatisticsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -247,15 +271,26 @@ func (statisticsService *StatisticsService) SystemCashStatistics(systemCashStati
defer func() {
transactionContext.RollbackTransaction()
}()
var cashPoolDao *dao.CashPoolDao
if value, err := factory.CreateCashPoolDao(map[string]interface{}{
//var cashPoolDao *dao.CashPoolDao
//if value, err := factory.CreateCashPoolDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// cashPoolDao = value
//}
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cashPoolDao = value
employeeDao = value
}
if systemCashStatistics, err := cashPoolDao.CalculateSystemCash(systemCashStatisticsCommand.CompanyId); err != nil {
if systemCashStatistics, err := employeeDao.CalculateSystemCash(systemCashStatisticsCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
... ...
... ... @@ -7,7 +7,7 @@ import (
// 获取现金池
type GetCashPoolQuery struct {
CompanyId int64 `json:"companyId"` // 公司id
CompanyId int64 `json:"companyId" valid:"Required"` // 公司id
}
func (getCashPoolQuery *GetCashPoolQuery) ValidateQuery() error {
... ...
... ... @@ -114,9 +114,64 @@ func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{
}, nil
}
// TODO 计算系统素币
func (dao *EmployeeDao) CalculateSystemSuMoney(companyId int64) (map[string] interface{}, error) {
var systemUnExchangeSuMoney float64
var systemExchangedSuMoney float64
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
ColumnExpr("sum(su_money_transaction_record.current_su_money) AS system_unExchange_su_money").
Where("employee.company_id = ?", companyId).
Where(`su_money_transaction_record.record_type = ?`, 5).
Select(&systemUnExchangeSuMoney); err != nil {
return nil, err
}
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
ColumnExpr("sum(su_money_transaction_record.su_money) AS system_changed_su_money").
Where("employee.company_id = ?", companyId).
Where(`su_money_transaction_record.record_type = ?`, 5).
Select(&systemExchangedSuMoney); err != nil {
return nil, err
}
return map[string] interface{} {
"systemUnExchangeSuMoney": systemUnExchangeSuMoney,
"systemExchangedSuMoney": systemExchangedSuMoney,
},nil
}
// TODO 计算系统现金
func (dao *EmployeeDao) CalculateSystemCash(companyId int64) (map[string] interface{}, error) {
var (
systemUnExchangeCash float64
systemExchangedCash float64
)
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).Join("JOIN employees AS employee ON employee.uid = employee.uid").
ColumnExpr("sum(su_money_transaction_record.cash) AS system_exchanged_cash").
Where("employee.company_id = ?", companyId).
Where(`su_money_transaction_record.record_type = ?`, 5).
Select(&systemExchangedCash) ; err != nil {
return nil, err
}
cashPool := new(models.CashPool)
if err := tx.Model(cashPool).
Where("cash_pool.company_id = ?", companyId).
Select(&systemUnExchangeCash) ; err != nil {
return nil, err
}
return map[string] interface{} {
"systemUnExchangeCash": systemUnExchangeCash,
"systemExchangedCash": systemExchangedCash,
}, nil
}
func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
var incomeSuMoney float64 // 收入的素币(2:任务奖励,3:增加)
var expendSuMoney float64 // 消耗的素币(1:兑换(物资、现金),4: 扣除
var expendSuMoney float64 // 消耗的素币(1:兑换物资,4:扣除, 5: 兑换现金
tx := dao.transactionContext.PgTx
suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
if err := tx.Model(suMoneyTransactionRecordModel).
... ...
... ... @@ -36,8 +36,8 @@ func init() {
(*models.RejectTaskRecord)(nil),
(*models.Notification)(nil),
(*models.SentNotification)(nil),
(*models.ExchangeCashActivity)(nil),
(*models.CashPool)(nil),
(*models.ExchangeCashActivity)(nil),
(*models.ExchangeCashPersonList)(nil),
} {
err := DB.CreateTable(model, &orm.CreateTableOptions{
... ...
package models
import (
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"time"
)
type SuMoneyExchangeTransactionRecord struct {
TableName string `pg:"su_money_exchange_transaction_records,alias:su_money_exchange_transaction_record"`
Id int64 `pg:",pk"` // 现金兑换务记录ID
RecordType int // 记录类型
Employee *domain.EmployeeInfo // 记录关联员工
//CashPoolBeforeTransaction *domain.CashPool // 事务处理前现金池
//CurrentCashPool *domain.CashPool // 当前现金池
SuMoney float64 // 事务素币值
Cash float64 // 事务现金值
Operator *domain.EmployeeInfo // 操作人
RecordDescription string // 现金投入事务记录描述
CreateTime time.Time // 创建时间
}
... ... @@ -19,6 +19,12 @@ type SuMoneyTransactionRecord struct {
CurrentSuMoney float64
// 事务素币值
SuMoney float64
// 事务处理前现金值
CashBeforeTransaction float64
// 当前现金值
CurrentCash float64
// 事务现金值
Cash float64
// 操作人
Operator *domain.EmployeeInfo
// 素币事务记录描述
... ...
... ... @@ -17,12 +17,14 @@ func init() {
beego.Router("/cash-pool/input", &controllers.SuMoneyController{}, "Post:CashInput") // 现金池投入
beego.Router("/cash-pool/cash-pool", &controllers.SuMoneyController{}, "Get:GetCashPool") // 返回现金池统计
/********************************************素币兑换活动***************************************/
beego.Router("/cash-pool/activity/", &controllers.SuMoneyController{}, "Get:ListExchangeActivities") // 返回兑换活动列表
beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Get:GetExchangeCashActivity") // 返回兑换活动
beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Put:UpdateExchangeActivities") // 编辑兑换活动
beego.Router("/cash-pool/activity", &controllers.SuMoneyController{}, "Post:CreateExchangeActivities") // 新增兑换活动
beego.Router("/cash-pool/activity/:activityId", &controllers.SuMoneyController{}, "Delete:RemoveExchangeActivities") // 删除兑换活动
/********************************************素币兑换清单*************************************/
beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Get:ListExchangeList") // 返回素币兑换清单
beego.Router("/cash-pool/activity/exchange-list/:listId", &controllers.SuMoneyController{}, "Get:GetExchangeCashPerson") // 返回素币兑换人员
beego.Router("/cash-pool/activity/exchange-list", &controllers.SuMoneyController{}, "Post:CreateExchangeList") // 新增素币兑换清单
... ...