|
|
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 StaffAssessRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
var _ domain.StaffAssessRepository = (*StaffAssessRepository)(nil)
|
|
|
|
|
|
func NewStaffAssessRepository(transactionContext *pgTransaction.TransactionContext) *StaffAssessRepository {
|
|
|
return &StaffAssessRepository{transactionContext: transactionContext}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessRepository) TransformToDomain(d *models.StaffAssess) *domain.StaffAssess {
|
|
|
return &domain.StaffAssess{
|
|
|
Id: d.Id,
|
|
|
CompanyId: d.CompanyId,
|
|
|
EvaluationProjectId: d.EvaluationProjectId,
|
|
|
CycleId: d.CycleId,
|
|
|
TargetUser: d.TargetUser,
|
|
|
TargetDepartment: d.TargetDepartment,
|
|
|
Executor: d.Executor,
|
|
|
Types: domain.StaffAssessType(d.Types),
|
|
|
Status: domain.StaffAssessStatus(d.Status),
|
|
|
BeginTime: d.BeginTime,
|
|
|
EndTime: d.EndTime,
|
|
|
CreatedAt: d.EndTime,
|
|
|
UpdatedAt: d.CreatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessRepository) Save(d *domain.StaffAssess) (*domain.StaffAssess, error) {
|
|
|
saveModel := models.StaffAssess{
|
|
|
Id: d.Id,
|
|
|
CompanyId: d.CompanyId,
|
|
|
EvaluationProjectId: d.EvaluationProjectId,
|
|
|
CycleId: d.CycleId,
|
|
|
TargetUser: d.TargetUser,
|
|
|
TargetDepartment: d.TargetDepartment,
|
|
|
Executor: d.Executor,
|
|
|
Types: string(d.Types),
|
|
|
Status: string(d.Status),
|
|
|
BeginTime: d.BeginTime,
|
|
|
EndTime: d.EndTime,
|
|
|
CreatedAt: d.EndTime,
|
|
|
UpdatedAt: d.CreatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
}
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var err error
|
|
|
if saveModel.Id == 0 {
|
|
|
_, err = tx.Model(&saveModel).Insert()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
_, err = tx.Model(&saveModel).WherePK().Update()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
d.Id = saveModel.Id
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessRepository) Remove(id int) error {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
_, err := tx.Model(&models.StaffAssess{}).
|
|
|
Where("id=?", id).
|
|
|
Set("deleted_at=?", nowTime).
|
|
|
Update()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessRepository) FindOne(queryOptions map[string]interface{}) (*domain.StaffAssess, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.StaffAssess)
|
|
|
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 *StaffAssessRepository) Find(queryOptions map[string]interface{}) (int, []*domain.StaffAssess, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.StaffAssess
|
|
|
query := tx.Model(&m).
|
|
|
Where("deleted_at isnull").Limit(20)
|
|
|
if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id = ?", companyId)
|
|
|
}
|
|
|
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 arrays []*domain.StaffAssess
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
arrays = append(arrays, d)
|
|
|
}
|
|
|
return count, arrays, nil
|
|
|
} |
...
|
...
|
|