pg_evaluation_template_repository.go 5.4 KB
package repository

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

type EvaluationTemplateRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewEvaluationTemplateRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationTemplateRepository {
	return &EvaluationTemplateRepository{transactionContext: transactionContext}
}

func (repo *EvaluationTemplateRepository) TransformToDomain(m *models.EvaluationTemplate) domain.EvaluationTemplate {
	return domain.EvaluationTemplate{
		Id:        m.Id,
		Name:      m.Name,
		Describe:  m.Describe,
		CompanyId: m.CompanyId,
		CreatorId: m.CreatorId,
		State:     m.State,
		LinkNodes: m.LinkNodes,
		CreatedAt: m.CreatedAt,
		UpdatedAt: m.UpdatedAt,
		DeletedAt: m.DeletedAt,
	}
}

func (repo *EvaluationTemplateRepository) TransformToModel(d *domain.EvaluationTemplate) models.EvaluationTemplate {
	return models.EvaluationTemplate{
		Id:        d.Id,
		Name:      d.Name,
		Describe:  d.Describe,
		CompanyId: d.CompanyId,
		CreatorId: d.CreatorId,
		State:     d.State,
		LinkNodes: d.LinkNodes,
		CreatedAt: d.CreatedAt,
		UpdatedAt: d.UpdatedAt,
		DeletedAt: d.DeletedAt,
	}
}

func (repo *EvaluationTemplateRepository) nextIdentify() (int64, error) {
	return utils.NewSnowflakeId()
}

func (repo *EvaluationTemplateRepository) Insert(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, error) {
	var isCreate = d.Id == 0
	if isCreate {
		id, err := repo.nextIdentify()
		if err != nil {
			return d, err
		}
		d.Id = id
		d.CreatedAt = time.Now()
		d.UpdatedAt = d.CreatedAt
	} else {
		d.UpdatedAt = time.Now()
	}
	m := repo.TransformToModel(d)
	tx := repo.transactionContext.PgTx
	var err error
	if isCreate {
		_, err = tx.Model(&m).Returning("id").Insert()
	} else {
		_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
	}
	if err != nil {
		return nil, err
	}
	d.Id = m.Id
	return d, nil
}

func (repo *EvaluationTemplateRepository) Remove(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, error) {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	m := repo.TransformToModel(d)
	m.DeletedAt = &nowTime
	if _, err := tx.Model(&m).WherePK().Update(); err != nil {
		return d, err
	}
	return d, nil
}

func (repo *EvaluationTemplateRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationTemplate, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationTemplate)
	query := tx.Model(m)
	query.Where("deleted_at isnull")
	if id, ok := queryOptions["id"]; ok {
		query.Where("id=?", id)
	}
	if err := query.First(); err != nil {
		if errors.Is(err, pg.ErrNoRows) {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	u := repo.TransformToDomain(m)
	return &u, nil
}

func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationTemplate, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.EvaluationTemplate
	query := tx.Model(&m).Where("deleted_at isnull")

	if len(excludeColumns) > 0 {
		query.ExcludeColumn(excludeColumns...)
	}

	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}

	if v, ok := queryOptions["notId"]; ok {
		query.Where("id != ?", v)
	}

	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
		query.Where("name LIKE ?", v)
	}

	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
	}

	if v, ok := queryOptions["state"]; ok && v.(int) >= 0 {
		query.Where("state = ?", v)
	}

	if v, ok := queryOptions["createdAt"]; ok {
		ts := v.(string)
		t, err := time.ParseInLocation("2006-01-02", ts, time.Local)
		if err == nil {
			year, month, day := t.Date()
			begin := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
			end := time.Date(year, month, day, 23, 59, 59, 0, time.Local)
			query.Where("created_at >= ?", begin)
			query.Where("created_at <= ?", end)
		}
	}

	if v, ok := queryOptions["limit"].(int64); ok {
		query.Limit(int(v))
	}
	if v, ok := queryOptions["offset"].(int64); ok {
		query.Offset(int(v))
	}

	// 按创建时间降序
	query.Order("created_at DESC")

	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var arrays []*domain.EvaluationTemplate
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, &d)
	}
	return int64(count), arrays, nil
}

func (repo *EvaluationTemplateRepository) Count(queryOptions map[string]interface{}) (int64, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationTemplate)
	query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
	query.Where("deleted_at isnull")

	if v, ok := queryOptions["id"]; ok {
		query.Where("id = ?", v)
	}

	if v, ok := queryOptions["notId"]; ok {
		query.Where("id != ?", v)
	}

	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
		query.Where("name = ?", v)
	}

	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
	}

	if v, ok := queryOptions["state"]; ok {
		query.Where("state = ?", v)
	}

	count, err := query.Count()
	if err != nil {
		return 0, err
	}
	return int64(count), nil
}