pg_summary_evaluation_repository.go 6.1 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"
)

type SummaryEvaluationRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

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

func NewSummaryEvaluationRepository(transactionContext *pgTransaction.TransactionContext) *SummaryEvaluationRepository {
	return &SummaryEvaluationRepository{transactionContext: transactionContext}
}

func (repo *SummaryEvaluationRepository) TransformToDomain(d *models.SummaryEvaluation) *domain.SummaryEvaluation {
	return &domain.SummaryEvaluation{
		Id:                    d.Id,
		CompanyId:             d.CompanyId,
		EvaluationProjectId:   d.EvaluationProjectId,
		EvaluationProjectName: d.EvaluationProjectName,
		CycleId:               d.CycleId,
		CycleName:             d.CycleName,
		NodeId:                d.NodeId,
		TargetUser:            d.TargetUser,
		TargetDepartment:      d.TargetDepartment,
		Executor:              d.Executor,
		Types:                 domain.EvaluationType(d.Types),
		Status:                domain.EvaluationStatus(d.Status),
		CheckResult:           domain.EvaluationCheckResult(d.CheckResult),
		BeginTime:             d.BeginTime,
		EndTime:               d.EndTime,
		TotalScore:            d.TotalScore,
		TotalRating:           d.TotalRating,
		CreatedAt:             d.CreatedAt,
		UpdatedAt:             d.UpdatedAt,
		DeletedAt:             d.DeletedAt,
	}
}

func (repo *SummaryEvaluationRepository) Save(param *domain.SummaryEvaluation) error {
	m := models.SummaryEvaluation{
		Id:                    param.Id,
		CompanyId:             param.CompanyId,
		EvaluationProjectId:   param.EvaluationProjectId,
		EvaluationProjectName: param.EvaluationProjectName,
		CycleId:               param.CycleId,
		CycleName:             param.CycleName,
		NodeId:                param.NodeId,
		TargetUser:            param.TargetUser,
		TargetDepartment:      param.TargetDepartment,
		Executor:              param.Executor,
		Types:                 int(param.Types),
		Status:                string(param.Status),
		CheckResult:           string(param.CheckResult),
		BeginTime:             param.BeginTime,
		EndTime:               param.EndTime,
		TotalScore:            param.TotalScore,
		TotalRating:           param.TotalRating,
		CreatedAt:             param.CreatedAt,
		UpdatedAt:             param.UpdatedAt,
		DeletedAt:             param.DeletedAt,
	}
	db := repo.transactionContext.PgTx
	if m.Id == 0 {
		m.CreatedAt = time.Now()
		m.UpdatedAt = time.Now()
		_, err := db.Model(&m).Insert()
		if err != nil {
			return err
		}
	} else {
		m.UpdatedAt = time.Now()
		_, err := db.Model(&m).WherePK().Update()
		if err != nil {
			return err
		}
	}
	param.Id = m.Id
	return nil
}

func (repo *SummaryEvaluationRepository) Remove(id int) error {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	_, err := tx.Model(&models.SummaryEvaluation{}).
		Where("id=?", id).
		Set("deleted_at=?", nowTime).
		Update()
	return err
}

func (repo *SummaryEvaluationRepository) FindOne(queryOptions map[string]interface{}) (*domain.SummaryEvaluation, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.SummaryEvaluation)
	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 *SummaryEvaluationRepository) Find(queryOptions map[string]interface{}) (int, []*domain.SummaryEvaluation, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.SummaryEvaluation
	query := tx.Model(&m).
		Where("deleted_at isnull").
		Limit(20)

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

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

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

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

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

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

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

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

	if v, ok := queryOptions["targetUserId"]; ok {
		query.Where(`summary_evaluation.target_user->>'userId'='?'`, v)
	}

	if v, ok := queryOptions["targetUserName"].(string); ok && len(v) > 0 {
		query.Where(`summary_evaluation.target_user->>'userName' like ?`, v)
	}

	if v, ok := queryOptions["executorId"]; ok {
		query.Where(`summary_evaluation.executor->>'userId'='?'`, v)
	}

	if v, ok := queryOptions["beginTime"]; ok {
		t := v.(time.Time)
		query.Where(`summary_evaluation.begin_time<=?`, t)
	}
	if v, ok := queryOptions["endTime"]; ok {
		t := v.(time.Time)
		query.Where(`summary_evaluation.end_time>=?`, t)
	}
	if v, ok := queryOptions["id"]; ok {
		query.Where(`summary_evaluation.id in (?)`, pg.In(v))
	}

	query.Order("id desc")
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	arrays := make([]*domain.SummaryEvaluation, 0)
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, d)
	}
	return count, arrays, nil
}

func (repo *SummaryEvaluationRepository) RemoveByProjectId(id int) error {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	_, err := tx.Model(&models.SummaryEvaluation{}).
		Where("evaluation_project_id=?", id).
		Set("deleted_at=?", nowTime).
		Update()
	return err
}

func (repo *SummaryEvaluationRepository) RemoveByCycleId(id int64) error {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	_, err := tx.Model(&models.SummaryEvaluation{}).
		Where("cycle_id=?", id).
		Set("deleted_at=?", nowTime).
		Update()
	return err
}