pg_cooperation_application_dao.go 2.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"
)

type CooperationApplicationDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// CheckApplicationExist 判断该申请人是否提交过申请
func (dao *CooperationApplicationDao) CheckApplicationExist(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var cooperationApplicationModels []*models.CooperationApplication
	query := tx.Model(&cooperationApplicationModels)
	if cooperationProjectNumber, ok := queryOptions["cooperationProjectNumber"]; ok && cooperationProjectNumber != "" {
		query = query.Where("cooperation_project_number = ?", cooperationProjectNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`cooperation_application.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`cooperation_application.org @> '{"orgId":"?"}'`, orgId)
	}
	if applicantId, ok := queryOptions["applicantId"]; ok && applicantId.(int64) != 0 {
		query = query.Where(`cooperation_application.cooperation_application_applicant @>'{"userId":"?"}'`, applicantId)
	}
	if visitorUserBaseId, ok := queryOptions["visitorUserBaseId"]; ok && visitorUserBaseId.(int64) != 0 {
		query = query.Where(`cooperation_application.cooperation_application_applicant @>'{"userBaseId":"?"}'`, visitorUserBaseId)
	}
	if applicantBaseId, ok := queryOptions["applicantBaseId"]; ok && applicantBaseId.(int64) != 0 {
		query = query.Where(`cooperation_application.cooperation_application_applicant @>'{"userBaseId":"?"}'`, applicantBaseId)
	}
	query.Where("cooperation_application.cooperation_application_status <> 3")
	query.Where("cooperation_application.is_canceled <> 2")
	ok, err := query.Exists()
	return ok, err
}

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