pg_cooperation_contract_undertaker_repository.go 10.3 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"

	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"github.com/linmadan/egglib-go/utils/snowflake"
	"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"
)

type CooperationContractUndertakerRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func (repository *CooperationContractUndertakerRepository) nextIdentify() (int64, error) {
	IdWorker, err := snowflake.NewIdWorker(1)
	if err != nil {
		return 0, err
	}
	id, err := IdWorker.NextId()
	return id, err
}

func (repository *CooperationContractUndertakerRepository) Save(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
	sqlBuildFields := []string{
		"cooperation_contract_undertaker_id",
		"cooperation_contract_number",
		"user_id",
		"user_base_id",
		"org",
		"orgs",
		"department",
		"role",
		"user_info",
		"user_type",
		"referrer",
		"salesman",
		"status",
		"company",
		"contract_attachment",
		"created_at",
		"updated_at",
		"deleted_at",
	}
	insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
	returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
	updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "cooperationContractUndertaker_id")
	updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
	tx := repository.transactionContext.PgTx
	if cooperationContractUndertaker.Identify() == nil {
		cooperationContractUndertakerId, err := repository.nextIdentify()
		if err != nil {
			return cooperationContractUndertaker, err
		} else {
			cooperationContractUndertaker.CooperationContractUndertakerId = cooperationContractUndertakerId
		}
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContractUndertaker.CooperationContractUndertakerId,
				&cooperationContractUndertaker.CooperationContractNumber,
				&cooperationContractUndertaker.Undertaker.UserId,
				&cooperationContractUndertaker.Undertaker.UserBaseId,
				&cooperationContractUndertaker.Undertaker.Org,
				&cooperationContractUndertaker.Undertaker.Orgs,
				&cooperationContractUndertaker.Undertaker.Department,
				&cooperationContractUndertaker.Undertaker.Roles,
				&cooperationContractUndertaker.Undertaker.UserInfo,
				&cooperationContractUndertaker.Undertaker.UserType,
				&cooperationContractUndertaker.Undertaker.Referrer,
				&cooperationContractUndertaker.Undertaker.Salesman,
				&cooperationContractUndertaker.Undertaker.Status,
				&cooperationContractUndertaker.Undertaker.Company,
				&cooperationContractUndertaker.Undertaker.ContractAttachment,
				&cooperationContractUndertaker.CreatedAt,
				&cooperationContractUndertaker.UpdatedAt,
				&cooperationContractUndertaker.DeletedAt,
			),
			fmt.Sprintf("INSERT INTO cooperation_contract_undertakers (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
			cooperationContractUndertaker.CooperationContractUndertakerId,
			cooperationContractUndertaker.CooperationContractNumber,
			&cooperationContractUndertaker.Undertaker.UserId,
			&cooperationContractUndertaker.Undertaker.UserBaseId,
			&cooperationContractUndertaker.Undertaker.Org,
			&cooperationContractUndertaker.Undertaker.Orgs,
			&cooperationContractUndertaker.Undertaker.Department,
			&cooperationContractUndertaker.Undertaker.Roles,
			&cooperationContractUndertaker.Undertaker.UserInfo,
			&cooperationContractUndertaker.Undertaker.UserType,
			&cooperationContractUndertaker.Undertaker.Referrer,
			&cooperationContractUndertaker.Undertaker.Salesman,
			&cooperationContractUndertaker.Undertaker.Status,
			&cooperationContractUndertaker.Undertaker.Company,
			&cooperationContractUndertaker.Undertaker.ContractAttachment,
			cooperationContractUndertaker.CreatedAt,
			cooperationContractUndertaker.UpdatedAt,
			cooperationContractUndertaker.DeletedAt,
		); err != nil {
			return cooperationContractUndertaker, err
		}
	} else {
		if _, err := tx.QueryOne(
			pg.Scan(
				&cooperationContractUndertaker.CooperationContractUndertakerId,
				&cooperationContractUndertaker.CooperationContractNumber,
				&cooperationContractUndertaker.Undertaker.UserId,
				&cooperationContractUndertaker.Undertaker.UserBaseId,
				&cooperationContractUndertaker.Undertaker.Org,
				&cooperationContractUndertaker.Undertaker.Orgs,
				&cooperationContractUndertaker.Undertaker.Department,
				&cooperationContractUndertaker.Undertaker.Roles,
				&cooperationContractUndertaker.Undertaker.UserInfo,
				&cooperationContractUndertaker.Undertaker.UserType,
				&cooperationContractUndertaker.Undertaker.Referrer,
				&cooperationContractUndertaker.Undertaker.Salesman,
				&cooperationContractUndertaker.Undertaker.Status,
				&cooperationContractUndertaker.Undertaker.Company,
				&cooperationContractUndertaker.Undertaker.ContractAttachment,
				&cooperationContractUndertaker.CreatedAt,
				&cooperationContractUndertaker.UpdatedAt,
				&cooperationContractUndertaker.DeletedAt,
			),
			fmt.Sprintf("UPDATE cooperation_contract_undertakers SET %s WHERE cooperation_contract_undertaker_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
			cooperationContractUndertaker.CooperationContractUndertakerId,
			cooperationContractUndertaker.CooperationContractNumber,
			cooperationContractUndertaker.Undertaker.UserId,
			cooperationContractUndertaker.Undertaker.UserBaseId,
			cooperationContractUndertaker.Undertaker.Org,
			cooperationContractUndertaker.Undertaker.Orgs,
			cooperationContractUndertaker.Undertaker.Department,
			cooperationContractUndertaker.Undertaker.Roles,
			cooperationContractUndertaker.Undertaker.UserInfo,
			cooperationContractUndertaker.Undertaker.UserType,
			cooperationContractUndertaker.Undertaker.Referrer,
			cooperationContractUndertaker.Undertaker.Salesman,
			cooperationContractUndertaker.Undertaker.Status,
			cooperationContractUndertaker.Undertaker.Company,
			cooperationContractUndertaker.Undertaker.ContractAttachment,
			cooperationContractUndertaker.CreatedAt,
			cooperationContractUndertaker.UpdatedAt,
			cooperationContractUndertaker.DeletedAt,
			cooperationContractUndertaker.Identify(),
		); err != nil {
			return cooperationContractUndertaker, err
		}
	}
	return cooperationContractUndertaker, nil
}

func (repository *CooperationContractUndertakerRepository) Remove(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
	cooperationContractUndertakerModel.CooperationContractUndertakerId = cooperationContractUndertaker.Identify().(int64)
	if _, err := tx.Model(cooperationContractUndertakerModel).WherePK().Delete(); err != nil {
		return cooperationContractUndertaker, err
	}
	return cooperationContractUndertaker, nil
}

func (repository *CooperationContractUndertakerRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractUndertaker, error) {
	tx := repository.transactionContext.PgTx
	cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
	query := sqlbuilder.BuildQuery(tx.Model(cooperationContractUndertakerModel), queryOptions)
	query.SetWhereByQueryOption("cooperation_contract_undertaker.cooperation_contract_undertaker_id = ?", "cooperationContractUndertakerId")
	if err := query.First(); err != nil {
		if err.Error() == "pg: no rows in result set" {
			return nil, fmt.Errorf("合约承接人不存在")
		} else {
			return nil, err
		}
	}
	if cooperationContractUndertakerModel.CooperationContractUndertakerId == 0 {
		return nil, nil
	} else {
		return transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel)
	}
}

func (repository *CooperationContractUndertakerRepository) 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.Where("user_base_id = ? ", userBaseId)
	}
	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 NewCooperationContractUndertakerRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CooperationContractUndertakerRepository{
			transactionContext: transactionContext,
		}, nil
	}
}