pg_cash_pool_dao.go 8.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) ( map[string]interface{}, 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 nil, 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 nil, err
//	}
//	return  map[string]interface{}{
//		"systemUnExchangeSuMoney": systemUnExchangeSuMoney,
//		"systemExchangedSuMoney": systemExchangedSuMoney,
//	}, nil
//}

// 计算系统已兑换现金、未兑换现金
//func (dao *CashPoolDao) CalculateSystemCash(companyId int64) (map[string]interface{}, error) {
//	var systemExchangedCash float64  // 系统已兑换现金
//	var systemUnExchangeCash 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 nil, err
//	}
//	return map[string]interface{}{
//		"systemExchangedCash": systemExchangedCash,
//		"systemUnExchangeCash": systemUnExchangeCash,
//	}, nil
//}

// 兑换活动兑换清单已兑换素币、已兑换现金
func (dao *CashPoolDao) CalculateActivityExchangedSuMoney(activityId int64) (map[string]interface{}, error) {
	var activityExchangedSuMoney float64
	var activityExchangedCash float64
	tx := dao.transactionContext.PgTx
	exchangeCashPersonListModels := new(models.ExchangeCashPersonList)
	if err := tx.Model(exchangeCashPersonListModels).
		ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS activity_exchanged_su_money").
		Where("exchange_cash_person_list.activity_id = ?", activityId).
		Select(&activityExchangedSuMoney); err !=nil {
		return nil, err
	}
	if err := tx.Model(exchangeCashPersonListModels).
		ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS activity_exchanged_cash").
		Where("exchange_cash_person_list.activity_id = ?", activityId).
		Select(&activityExchangedCash); err !=nil {
		return nil, err
	}
	return map[string]interface{}{
		"activityExchangedSuMoney": activityExchangedSuMoney,
		"activityExchangedCash": activityExchangedCash,
	}, nil
}

// 返回兑换清单榜单
func (dao *CashPoolDao) ExchangeCashListRanking(queryOptions map[string]interface{}) (map[string]interface{}, error) {
	var retPeople []struct {
		Uid int
		EmployeeName string
		SuMoney float64
		Cash float64
		Ranking int
	}

	var retEmployee []struct {
		Uid int
		EmployeeName string
		SuMoney float64
		Cash float64
		Ranking int
	}

	tx := dao.transactionContext.PgTx

	// 清单人员排名
	exchangeCashPersonListModels := new(models.ExchangeCashPersonList)
	queryPeople := tx.Model(exchangeCashPersonListModels)
	queryPeople = queryPeople.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
	queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.uid AS uid")
	queryPeople = queryPeople.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
	queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
	queryPeople = queryPeople.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
	queryPeople = queryPeople.ColumnExpr("RANK() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC) AS ranking")
	if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
		queryPeople = queryPeople.Where("e.company_id = ?", companyId)
	}
	if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0){
		queryPeople = queryPeople.Where("exchange_cash_person_list.activity_id = ?", activityId)
	}
	queryPeople = queryPeople.Group("exchange_cash_person_list.uid")
	queryPeople = queryPeople.Group("exchange_cash_person_list.employee_name")
	if offset, ok := queryOptions["offset"]; ok {
		offset := offset.(int)
		if offset > -1 {
			queryPeople = queryPeople.Offset(offset)
		}
	} else {
		queryPeople = queryPeople.Offset(0)
	}
	if limit, ok := queryOptions["limit"]; ok {
		limit := limit.(int)
		if limit > -1 {
			queryPeople = queryPeople.Limit(limit)
		}
	} else {
		queryPeople = queryPeople.Limit(20)
	}

	if err := queryPeople.Order("su_money DESC").Select(&retPeople); err != nil {
		return nil, err
	}

	// 当前员工排名
	queryEmployee := tx.Model(exchangeCashPersonListModels)
	queryEmployee = queryEmployee.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
	queryEmployee = queryEmployee.ColumnExpr("exchange_cash_person_list.uid AS uid")
	queryEmployee = queryEmployee.ColumnExpr("exchange_cash_person_list.employee_name AS employee_name")
	queryEmployee = queryEmployee.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS cash")
	queryEmployee = queryEmployee.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS su_money")
	queryEmployee = queryEmployee.ColumnExpr("RANK() OVER (ORDER BY sum(exchange_cash_person_list.exchanged_su_money) DESC) AS ranking")
	if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0)  {
		queryEmployee = queryEmployee.Where("e.company_id = ?", companyId)
	}
	if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
		queryEmployee = queryEmployee.Where("exchange_cash_person_list.activity_id = ?", activityId)
	}
	if uid, ok := queryOptions["uid"]; ok {
		queryEmployee = queryEmployee.Where("exchange_cash_person_list.uid = ?", uid)
	}
	queryEmployee = queryEmployee.Group("exchange_cash_person_list.uid")
	queryEmployee = queryEmployee.Group("exchange_cash_person_list.employee_name")
	if err := queryEmployee.Order("su_money DESC").Select(&retEmployee); err != nil {
		return nil, err
	}
	var currentEmployee interface{}
	if len(retEmployee) == 0 {
		currentEmployee = nil
	} else {
		currentEmployee = retEmployee[0]
	}

	// 清单已兑换素币
	var activityExchangedSuMoney float64
	queryListSuMoney := tx.Model(exchangeCashPersonListModels)
	queryListSuMoney = queryListSuMoney.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
	queryListSuMoney = queryListSuMoney.ColumnExpr("sum(exchange_cash_person_list.exchanged_su_money) AS activity_exchanged_su_money")
	if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
		queryListSuMoney = queryListSuMoney.Where("e.company_id = ?", companyId)
	}
	if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
		queryListSuMoney = queryListSuMoney.Where("exchange_cash_person_list.activity_id = ?", activityId)
	}
	if err := queryListSuMoney.Select(&activityExchangedSuMoney); err != nil {
		return nil, err
	}

	// 清单已兑换现金
	var activityExchangedCash float64
	queryListCash := tx.Model(exchangeCashPersonListModels)
	queryListCash = queryListCash.Join("JOIN employees AS e ON e.uid = exchange_cash_person_list.uid")
	queryListCash = queryListCash.ColumnExpr("sum(exchange_cash_person_list.exchanged_cash) AS activity_exchanged_cash")
	if companyId, ok := queryOptions["companyId"]; ok && (companyId.(int64) != 0) {
		queryListCash = queryListCash.Where("e.company_id = ?", companyId)
	}
	if activityId, ok := queryOptions["activityId"]; ok && (activityId.(int64) != 0) {
		queryListCash = queryListCash.Where("exchange_cash_person_list.activity_id = ?", activityId)
	}
	if err := queryListCash.Select(&activityExchangedCash); err != nil {
		return nil, err
	}

	// 清单计数
	queryCount := tx.Model(exchangeCashPersonListModels)
	count, err := queryCount.Count()
	if err != nil {
		return nil, err
	}

	return map[string]interface{} {
		"people": retPeople,   // 员工排行榜
		"count": count,     // 计数
		"currentEmployee": currentEmployee,   // 当前员工排名
		"exchangedSuMoney": activityExchangedSuMoney,  // 清单已兑换素币
		"exchangedCash": activityExchangedCash,   // 清单已兑换现金
	}, nil
}

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