pg_department_repository.go 4.2 KB
package repository

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

type DepartmentRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.DepartmentRepository = (*DepartmentRepository)(nil)

func NewDepartmentRepository(tx *pgTransaction.TransactionContext) *DepartmentRepository {
	return &DepartmentRepository{
		transactionContext: tx,
	}
}

func (repo *DepartmentRepository) Insert(u *domain.Department) (*domain.Department, error) {
	departmentModel := models.Department{
		Id:            u.Id,
		CompanyId:     u.CompanyId,
		Level:         u.Level,
		Name:          u.Name,
		ParentId:      u.ParentId,
		ChargeUserIds: u.ChargeUserIds,
		Path:          u.Path,
		CreatedAt:     u.CreatedAt,
		UpdatedAt:     u.UpdatedAt,
		DeletedAt:     nil,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&departmentModel).Insert()
	if err != nil {
		return nil, err
	}
	u.Id = departmentModel.Id
	return u, nil
}

func (repo *DepartmentRepository) Update(u *domain.Department) (*domain.Department, error) {
	departmentModel := models.Department{
		Id:            u.Id,
		CompanyId:     u.CompanyId,
		Level:         u.Level,
		Name:          u.Name,
		ParentId:      u.ParentId,
		ChargeUserIds: u.ChargeUserIds,
		Path:          u.Path,
		CreatedAt:     u.CreatedAt,
		UpdatedAt:     u.UpdatedAt,
		DeletedAt:     u.DeletedAt,
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&departmentModel).WherePK().Update()
	if err != nil {
		return nil, err
	}
	return u, nil
}

func (repo *DepartmentRepository) Remove(ids []int64) error {
	tx := repo.transactionContext.PgTx
	uModel := models.Department{}
	_, err := tx.Model(&uModel).
		Where("id in (?)", pg.In(ids)).
		Delete()
	return err
}

func (repo *DepartmentRepository) FindOne(queryOptions map[string]interface{}) (*domain.Department, error) {
	tx := repo.transactionContext.PgTx
	departmentModel := models.Department{}
	query := tx.Model(&departmentModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	err := query.First()
	if err == pg.ErrNoRows {
		return nil, ErrNoRows
	}
	if err != nil {
		return nil, err
	}
	result := repo.TransformToCompanyDomain(&departmentModel)
	return result, nil
}

func (repo *DepartmentRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Department, error) {
	tx := repo.transactionContext.PgTx
	dparmentModel := []models.Department{}
	query := tx.Model(&dparmentModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in (?)", pg.In(v))
	}
	if v, ok := queryOptions["limit"]; ok {
		query.Limit(v.(int))
	}
	if v, ok := queryOptions["offset"]; ok {
		query.Offset(v.(int))
	}
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
	}
	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var resultList []*domain.Department
	for i := range dparmentModel {
		result := repo.TransformToCompanyDomain(&dparmentModel[i])
		resultList = append(resultList, result)
	}
	return cnt, resultList, nil
}

func (repo *DepartmentRepository) FindAll(companyId int64) ([]*domain.Department, error) {
	tx := repo.transactionContext.PgTx
	var departmentModels []models.Department

	query := tx.Model(&departmentModels).Where("deleted_at isnull") // 不为空
	if companyId > 0 {
		query.Where("company_id=?", companyId)
	}
	query.Order("level ASC") // 按等级升序
	err := query.Select()
	if err != nil {
		return nil, err
	}
	var list []*domain.Department
	for i := range departmentModels {
		result := repo.TransformToCompanyDomain(&departmentModels[i])
		list = append(list, result)
	}
	return list, nil
}

func (repo *DepartmentRepository) TransformToCompanyDomain(u *models.Department) *domain.Department {
	return &domain.Department{
		Id:            u.Id,
		CompanyId:     u.CompanyId,
		Level:         u.Level,
		Name:          u.Name,
		ParentId:      u.ParentId,
		ChargeUserIds: u.ChargeUserIds,
		Path:          u.Path,
		CreatedAt:     u.CreatedAt,
		UpdatedAt:     u.UpdatedAt,
		DeletedAt:     u.DeletedAt,
	}
}