pg_partner_info_dao.go 1.6 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type PartnerInfoDao struct {
	transactionContext *transaction.TransactionContext
}

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

func (dao PartnerInfoDao) PartnerAccountExist(account string, companyId int64) (bool, error) {
	tx := dao.transactionContext.PgDd
	m := &models.PartnerInfo{}
	ok, err := tx.Model(m).
		Where("account=?", account).
		Where("company_id=?", companyId).
		Exists()
	return ok, err
}

func (dao PartnerInfoDao) UpdatePartnerStatus(ids []int64, companyId int64, status int) error {
	tx := dao.transactionContext.PgDd
	m := &models.PartnerInfo{}
	_, err := tx.Model(m).
		WhereIn("id in (?)", ids).
		Where("company_id=?", companyId).
		Set("status=?", status).
		Update()
	return err
}

/**
 * @Author SteveChan
 * @Description // 编号查重
 * @Date 17:55 2020/12/28
 * @Param
 * @return
 **/
func (dao PartnerInfoDao) PartnerCodeExist(categoryId int64, code string, companyId int64, id int64) (bool, error) {
	tx := dao.transactionContext.PgDd
	m := &models.PartnerInfo{}
	ok, err := tx.Model(m).
		Where(`partner_category_infos@> '[{"id":?,"code":?}]'`, categoryId, pg.Ident(code)).
		Where("company_id=?", companyId).
		Where("id <> ?", id).
		Exists()
	return ok, err
}