pg_project_module_repository.go 3.8 KB
package repository

import (
	"fmt"
	"github.com/go-pg/pg/v10/orm"
	"github.com/tiptok/gocomm/common"
	. "github.com/tiptok/gocomm/pkg/orm/pgx"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)

type ProjectModuleRepository struct {
	transactionContext *transaction.TransactionContext
}

func (repository *ProjectModuleRepository) Save(dm *domain.ProjectModule) (*domain.ProjectModule, error) {
	var (
		err error
		m   = &models.ProjectModule{}
		tx  = repository.transactionContext.PgTx
	)
	if err = common.GobModelTransform(m, dm); err != nil {
		return nil, err
	}
	if dm.Identify() == nil {
		if err = tx.Insert(m); err != nil {
			return nil, err
		}
		dm.Id = m.Id
		return dm, nil
	}
	if _, err = tx.Model(m).WherePK().Update(); err != nil {
		return nil, err
	}
	return dm, nil
}

func (repository *ProjectModuleRepository) Remove(ProjectModule *domain.ProjectModule) (*domain.ProjectModule, error) {
	var (
		tx                 = repository.transactionContext.PgTx
		ProjectModuleModel = &models.ProjectModule{Id: ProjectModule.Identify().(int64)}
	)
	if _, err := tx.Model(ProjectModuleModel).Where("id = ?", ProjectModule.Id).Delete(); err != nil {
		return ProjectModule, err
	}
	return ProjectModule, nil
}

func (repository *ProjectModuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.ProjectModule, error) {
	tx := repository.transactionContext.PgDd
	ProjectModuleModel := new(models.ProjectModule)
	query := NewQuery(tx.Model(ProjectModuleModel).Relation("ProjectModuleVersion", func(q *orm.Query) (*orm.Query, error) {
		q.Where("status=0")
		return q, nil
	}), queryOptions)
	query.SetWhere("id = ?", "id")
	query.SetWhere("project_key = ?", "projectKey")

	query.SetWhere("status=?", "status")
	if err := query.First(); err != nil {
		return nil, fmt.Errorf("query row not found")
	}
	if ProjectModuleModel.Id == 0 {
		return nil, fmt.Errorf("query row not found")
	}
	return repository.transformPgModelToDomainModel(ProjectModuleModel)
}

func (repository *ProjectModuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.ProjectModule, error) {
	tx := repository.transactionContext.PgDd
	var ProjectModuleModels []*models.ProjectModule
	ProjectModules := make([]*domain.ProjectModule, 0)
	query := NewQuery(tx.Model(&ProjectModuleModels), queryOptions).
		//Relation("ProjectModuleVersion", func(q *orm.Query) (*orm.Query, error) {
		//	q.Where("status=0")
		//	return q, nil
		//}), queryOptions).
		SetWhere("status=?", "status").
		SetOrder("create_time", "sortByCreateTime").
		SetOrder("update_time", "sortByUpdateTime").SetLimit()

	if searchByText, ok := queryOptions["searchByText"]; ok && len(searchByText.(string)) > 0 {
		query.Where(fmt.Sprintf(`project_name like '%%%v%%'`, searchByText))
	}

	var err error
	if query.AffectRow, err = query.SelectAndCount(); err != nil {
		return 0, ProjectModules, err
	}
	for _, ProjectModuleModel := range ProjectModuleModels {
		if ProjectModule, err := repository.transformPgModelToDomainModel(ProjectModuleModel); err != nil {
			return 0, ProjectModules, err
		} else {
			ProjectModules = append(ProjectModules, ProjectModule)
		}
	}
	return int64(query.AffectRow), ProjectModules, nil
}

func (repository *ProjectModuleRepository) transformPgModelToDomainModel(ProjectModuleModel *models.ProjectModule) (*domain.ProjectModule, error) {
	m := &domain.ProjectModule{}
	err := common.GobModelTransform(m, ProjectModuleModel)
	return m, err
}

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