pg_business_bonus.go 3.3 KB
package dao

import (
	"fmt"
	"strings"
	"time"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type BusinessBonusDao struct {
	transactionContext *transaction.TransactionContext
}

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

type CustomBusinessBonus struct {
	Id           int64
	Bonus        string
	BonusNot     string
	BonusExpense string
	BonusHas     string
	BonusStatus  int8
	PartnerName  string
	UpdateAt     time.Time
}

func (dao BusinessBonusDao) SearchBusinessBonus(partnerId int64, partnerNameMatch string,
	companyId int64, limit int, offset int) ([]CustomBusinessBonus, error) {
	sql := `SELECT business_bonus.id, business_bonus.bonus,business_bonus.bonus_not
	,business_bonus.bonus_expense,business_bonus.bonus_status,business_bonus.update_at
	,partner_info.partner_name,business_bonus.bonus_has
	FROM business_bonus 
	JOIN partner_info ON business_bonus.partner_info_id=partner_info.id
	WHERE business_bonus.is_disable=? AND business_bonus.company_id = ? `
	partnerCondition := []string{}
	allParam := []interface{}{domain.BUSINESS_BONUS_ENABLE, companyId}
	if partnerId > 0 {
		partnerCondition = append(partnerCondition, ` business_bonus.partner_info_id=? `)
		allParam = append(allParam, partnerId)
	}
	if len(partnerNameMatch) > 0 {
		allParam = append(allParam, "%"+partnerNameMatch+"%")
		partnerCondition = append(partnerCondition, ` partner_info.partner_name like ? `)
	}
	if len(partnerCondition) > 0 {
		sql += fmt.Sprintf(" AND (%s)", strings.Join(partnerCondition, " OR "))
	}
	sql += ` limit ? OFFSET ? `
	allParam = append(allParam, limit, offset)
	tx := dao.transactionContext.PgTx
	var (
		result []CustomBusinessBonus
		err    error
	)
	_, err = tx.Query(&result, sql, allParam...)
	return result, err
}

func (dao BusinessBonusDao) CountBusinessBonus(partnerId int64, partnerNameMatch string,
	companyId int64, limit int, offset int) (int, error) {
	sql := `SELECT count(*) 
	FROM business_bonus 
	JOIN partner_info ON business_bonus.partner_info_id=partner_info.id
	WHERE business_bonus.is_disable=? AND business_bonus.company_id = ? `
	partnerCondition := []string{}
	allParam := []interface{}{domain.BUSINESS_BONUS_ENABLE, companyId}
	if partnerId > 0 {
		partnerCondition = append(partnerCondition, ` business_bonus.partner_info_id=? `)
		allParam = append(allParam, partnerId)
	}
	if len(partnerNameMatch) > 0 {
		allParam = append(allParam, "%"+partnerNameMatch+"%")
		partnerCondition = append(partnerCondition, ` partner_info.partner_name like ? `)
	}
	if len(partnerCondition) > 0 {
		sql += fmt.Sprintf(" AND (%s)", strings.Join(partnerCondition, " OR "))
	}
	tx := dao.transactionContext.PgTx
	var (
		result int
		err    error
	)
	_, err = tx.Query(&result, sql, allParam...)
	return result, err
}

func (dao BusinessBonusDao) ExistBusinessBonus(userId int64) (bool, error) {
	tx := dao.transactionContext.PgTx
	ok, err := tx.Model(&models.BusinessBonus{}).
		Where("user_id=?", userId).
		Exists()
	return ok, err
}