|
|
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,
|
|
|
TargetUser: d.TargetUser,
|
|
|
TargetDepartment: d.TargetDepartment,
|
|
|
Executor: d.Executor,
|
|
|
Types: domain.EvaluationType(d.Types),
|
|
|
Status: domain.EvaluationStatus(d.Status),
|
|
|
CheckResult: d.CheckResult,
|
|
|
BeginTime: d.BeginTime,
|
|
|
EndTime: d.EndTime,
|
|
|
TotalScore: d.TotalScore,
|
|
|
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,
|
|
|
TargetUser: param.TargetUser,
|
|
|
TargetDepartment: param.TargetDepartment,
|
|
|
Executor: param.Executor,
|
|
|
Types: int(param.Types),
|
|
|
Status: string(param.Status),
|
|
|
CheckResult: param.CheckResult,
|
|
|
BeginTime: param.BeginTime,
|
|
|
EndTime: param.EndTime,
|
|
|
TotalScore: param.TotalScore,
|
|
|
CreatedAt: param.CreatedAt,
|
|
|
UpdatedAt: param.UpdatedAt,
|
|
|
DeletedAt: param.DeletedAt,
|
|
|
}
|
|
|
db := repo.transactionContext.PgTx
|
|
|
if m.Id == 0 {
|
|
|
_, err := db.Model(&m).Insert()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
} else {
|
|
|
_, err := db.Model(&m).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)
|
|
|
}
|
|
|
count, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var datas []*domain.SummaryEvaluation
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
datas = append(datas, d)
|
|
|
}
|
|
|
return count, datas, nil
|
|
|
} |
...
|
...
|
|