pg_dividends_order_dao.go 4.3 KB
package dao

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
	"time"
)

type DividendsOrderDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// GenerateDividendsOrderNumber 生成分红订单号
func (dao *DividendsOrderDao) GenerateDividendsOrderNumber() (string, error) {
	tx := dao.transactionContext.PgTx
	var dividendsOrderModels []*models.DividendsOrder
	query := tx.Model(&dividendsOrderModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("dividends_order.created_at >= ?", todayZeroTime)
	query.Where("dividends_order.created_at < ?", nextDayZeroTime)
	if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
		return "", err
	} else {
		if count < 1000 {
			countStr := fmt.Sprintf("%03d", count+1)
			timestamp := currentTime.Unix()
			timeNow := time.Unix(timestamp, 0)
			timeString := timeNow.Format("20060102")
			timeString = timeString[2:len(timeString)]
			dividendsOrderNumber := "SL" + timeString + "#" + countStr
			return dividendsOrderNumber, nil
		} else {
			countStr := fmt.Sprintf("%d", count+1)
			timestamp := currentTime.Unix()
			timeNow := time.Unix(timestamp, 0)
			timeString := timeNow.Format("20060102")
			timeString = timeString[2:len(timeString)]
			dividendsOrderNumber := "SL" + timeString + "#" + countStr
			return dividendsOrderNumber, nil
		}
	}
}

// CheckDividendsOrderNumberAvailable 校验分红订单是否唯一
func (dao *DividendsOrderDao) CheckDividendsOrderNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var dividendsOrderModels []*models.DividendsOrder
	query := tx.Model(&dividendsOrderModels)
	if dividendsOrderNumber, ok := queryOptions["dividendsOrderNumber"]; ok && dividendsOrderNumber != "" {
		query = query.Where("dividends_order_number = ?", dividendsOrderNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`dividends_order.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`dividends_order.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

// CalculateDividendsOrderAmount 计算分红订单金额
func (dao *DividendsOrderDao) CalculateDividendsOrderAmount(queryOptions map[string]interface{}) (float64, error) {
	tx := dao.transactionContext.PgTx
	var dividendsOrderModel = new(models.DividendsOrder)
	query := tx.Model(dividendsOrderModel)
	query.ColumnExpr("sum(dividends_order_amount) dividends_order_amount")
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query.Where("company->>'companyId' = '?'", companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query.Where("org->>'orgId' = '?'", orgId)
	}
	if beginTime, ok := queryOptions["beginTime"]; ok {
		query.Where("order_time>= ?", beginTime)
	}
	if endTime, ok := queryOptions["endTime"]; ok {
		query.Where("order_time< ?", endTime)
	}
	err := query.Select()
	if err != nil {
		return 0, err
	}
	return dividendsOrderModel.DividendsOrderAmount, nil
}

// CountDividendsOrder 统计当前订单总数
func (dao *DividendsOrderDao) CountDividendsOrder(queryOptions map[string]interface{}) (int, error) {
	tx := dao.transactionContext.PgTx
	var dividendsOrderModels []*models.DividendsOrder
	query := tx.Model(&dividendsOrderModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("dividends_order.created_at >= ?", todayZeroTime)
	query.Where("dividends_order.created_at < ?", nextDayZeroTime)
	if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
		return 0, err
	} else {
		return count, nil
	}
}

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