pg_cooperation_contract_undertaker_dao.go 3.1 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"github.com/go-pg/pg/v10/orm"
	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)

type CooperationContractUndertakerDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *CooperationContractUndertakerDao) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
	cooperationContractUndertakers := make([]*domain.CooperationContractUndertaker, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractUndertakerModels), queryOptions)
	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 orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
		newOrgIds := utils.SliceItoa(orgIds.([]int64))
		query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
	}
	if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
		query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
			query.WhereOr("user_base_id = ? ", userBaseId)
			query.WhereOr("referrer->>'userBaseId' = '?' ", userBaseId)
			query.WhereOr("salesman->>'userBaseId' = '?' ", userBaseId)
			return query, nil
		})
	}
	offsetLimitFlag := true
	if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
		offsetLimitFlag = offsetLimit.(bool)
	}
	if offsetLimitFlag {
		query.SetOffsetAndLimit(20)
	}
	query.SetOrderDirect("cooperation_contract_undertaker_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, cooperationContractUndertakers, err
	} else {
		for _, cooperationContractUndertakerModel := range cooperationContractUndertakerModels {
			if cooperationContractUndertaker, err := transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel); err != nil {
				return 0, cooperationContractUndertakers, err
			} else {
				cooperationContractUndertakers = append(cooperationContractUndertakers, cooperationContractUndertaker)
			}
		}
		return int64(count), cooperationContractUndertakers, nil
	}
}

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