pg_log_opt_repository.go 4.3 KB
package repository

import (
	"errors"
	"fmt"
	"time"

	"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"
)

type LogOptRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewLogOptRepository(transactionContext *pgTransaction.TransactionContext) *LogOptRepository {
	return &LogOptRepository{transactionContext: transactionContext}
}

func (repo *LogOptRepository) TransformToDomain(m *models.LogOpt) domain.LogOpt {
	return domain.LogOpt{
		Id:          m.Id,
		CompanyId:   m.CompanyId,
		Operator:    m.Operator,
		OptMethod:   m.OptMethod,
		OptTargetId: m.OptTargetId,
		OptField:    m.OptField,
		OptValue:    m.OptValue,
		CreatedAt:   m.CreatedAt.Local(),
		UpdatedAt:   m.UpdatedAt.Local(),
		DeletedAt:   m.DeletedAt,
	}
}

func (repo *LogOptRepository) TransformToModel(d *domain.LogOpt) models.LogOpt {
	return models.LogOpt{
		Id:          d.Id,
		CompanyId:   d.CompanyId,
		Operator:    d.Operator,
		OptMethod:   d.OptMethod,
		OptTargetId: d.OptTargetId,
		OptField:    d.OptField,
		OptValue:    d.OptValue,
		CreatedAt:   d.CreatedAt,
		UpdatedAt:   d.UpdatedAt,
		DeletedAt:   d.DeletedAt,
	}
}

func (repo *LogOptRepository) Insert(d *domain.LogOpt) (*domain.LogOpt, error) {
	var isCreate = d.Id == 0
	if isCreate {
		id, err := utils.NewSnowflakeId()
		if err != nil {
			return d, err
		}
		d.Id = int(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 *LogOptRepository) Remove(d *domain.LogOpt) (*domain.LogOpt, 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 *LogOptRepository) FindOne(queryOptions map[string]interface{}) (*domain.LogOpt, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.LogOpt)
	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 *LogOptRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.LogOpt, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.LogOpt

	query := tx.Model(&m).Where("deleted_at isnull")

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

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

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

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

	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
	}
	arrays := make([]*domain.LogOpt, 0)
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, &d)
	}
	return int64(count), arrays, nil
}

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

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

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

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

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