pg_permission_repository.go 3.8 KB
package repository

import (
	"errors"
	"fmt"
	"time"

	"github.com/go-pg/pg/v10"
	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 PermissionRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewPermissionRepository(transactionContext *pgTransaction.TransactionContext) *PermissionRepository {
	return &PermissionRepository{transactionContext: transactionContext}
}

func (repo *PermissionRepository) TransformToDomain(m *models.Permission) domain.Permission {
	return domain.Permission{
		Id:             m.Id,
		CompanyId:      m.CompanyId,
		OptHrScore:     m.OptHrScore,
		OptEvalScore:   m.OptEvalScore,
		OptConfirmPerf: m.OptConfirmPerf,
		CycleDeadline:  m.CycleDeadline,
		CreatedAt:      m.CreatedAt.Local(),
		UpdatedAt:      m.UpdatedAt.Local(),
		DeletedAt:      m.DeletedAt,
	}
}

func (repo *PermissionRepository) TransformToModel(d *domain.Permission) models.Permission {
	return models.Permission{
		Id:             d.Id,
		CompanyId:      d.CompanyId,
		OptHrScore:     d.OptHrScore,
		OptEvalScore:   d.OptEvalScore,
		OptConfirmPerf: d.OptConfirmPerf,
		CycleDeadline:  d.CycleDeadline,
		CreatedAt:      d.CreatedAt,
		UpdatedAt:      d.UpdatedAt,
		DeletedAt:      d.DeletedAt,
	}
}

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

func (repo *PermissionRepository) Insert(d *domain.Permission) (*domain.Permission, 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 *PermissionRepository) FindOne(queryOptions map[string]interface{}) (*domain.Permission, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.Permission)
	query := tx.Model(m)
	query.Where("deleted_at isnull")
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	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 *PermissionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Permission, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.Permission
	query := tx.Model(&m).Where("deleted_at isnull")

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

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

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

//func (repo *PermissionRepository) FindByCompanyId(companyId int64) (*domain.Permission, error) {
//	tx := repo.transactionContext.PgTx
//	m := new(models.Permission)
//	query := tx.Model(m)
//	query.Where("deleted_at isnull")
//	query.Where("company_id = ?", companyId)
//	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
//}