pg_cooperation_mode_dao.go 2.2 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"
)

type CooperationModeDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// CheckModeNameAvailable 校验模式名称唯一性
func (dao *CooperationModeDao) CheckModeNameAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var cooperationModeModels []*models.CooperationMode
	query := tx.Model(&cooperationModeModels)
	if cooperationModeName, ok := queryOptions["cooperationModeName"]; ok && cooperationModeName != "" {
		query = query.Where("cooperation_mode_name = ?", cooperationModeName)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

// CheckModeNumberAvailable 校验模式编码唯一性
func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var cooperationModeModels []*models.CooperationMode
	query := tx.Model(&cooperationModeModels)
	if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber != "" {
		query = query.Where("cooperation_mode_number = ?", cooperationModeNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

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