pg_cash_pool_dao.go 1.9 KB
package dao

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
)

type CashPoolDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// 计算系统素币
func (dao *CashPoolDao) CalculateSystemSuMoney(companyId int64) ( float64, float64, error) {
	var systemUnExchangeSuMoney float64
	var systemExchangedSuMoney float64

	tx := dao.transactionContext.PgTx

	employeeModel := new(models.Employee)
	if err := tx.Model(employeeModel).
		ColumnExpr("sum(employee.su_money) AS system_unExchange_su_money").
		Where("employee.company_id = ?", companyId).
		Select(&systemUnExchangeSuMoney); err != nil {
		return 0, 0, err
	}

	exchangeCashActivityModel := new(models.ExchangeCashActivity)
	if err := tx.Model(exchangeCashActivityModel).
		ColumnExpr("sum(exchange_cash_activity.exchanged_su_money) AS system_changed_su_money").
		Where("exchange_cash_activity.company_id = ?", companyId).
		Select(&systemExchangedSuMoney); err != nil {
		return 0, 0, err
	}

	return  systemUnExchangeSuMoney, systemExchangedSuMoney, nil
}

// 计算系统已兑换现金
func (dao *CashPoolDao) CalculateSystemCash(companyId int64) (float64, error) {
	var systemExchangedCash float64  // 系统已兑换现金

	tx := dao.transactionContext.PgTx
	exchangeCashActivityModel := new(models.ExchangeCashActivity)

	if err := tx.Model(exchangeCashActivityModel).
		ColumnExpr("sum(exchange_cash_activity.exchanged_cash) AS system_exchanged_cash").
		Where("exchange_cash_activity.company_id = ?", companyId).
		Select(&systemExchangedCash) ; err != nil {
		return 0, err
	}

	return systemExchangedCash, nil
}

func NewCashPoolDao(transactionContext *pgTransaction.TransactionContext) (*CashPoolDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CashPoolDao{
			transactionContext: transactionContext,
		}, nil
	}
}