|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
|
|
|
"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 StaffAssessContentTempRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
var _ domain.StaffAssessContentTempRepository = (*StaffAssessContentTempRepository)(nil)
|
|
|
|
|
|
func NewStaffAssessContentTempRepository(transactionContext *pgTransaction.TransactionContext) *StaffAssessContentTempRepository {
|
|
|
return &StaffAssessContentTempRepository{transactionContext: transactionContext}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessContentTempRepository) TransformToDomain(d *models.StaffAssessContentTemp) *domain.StaffAssessContentTemp {
|
|
|
return &domain.StaffAssessContentTemp{
|
|
|
Id: d.Id,
|
|
|
StaffAssessId: d.StaffAssessId,
|
|
|
SortBy: d.SortBy,
|
|
|
Category: d.Category,
|
|
|
Name: d.Name,
|
|
|
Remark: d.Remark,
|
|
|
Value: d.Value,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessContentTempRepository) Save(d *domain.StaffAssessContentTemp) (*domain.StaffAssessContentTemp, error) {
|
|
|
saveModel := models.StaffAssessContentTemp{
|
|
|
Id: d.Id,
|
|
|
StaffAssessId: d.StaffAssessId,
|
|
|
SortBy: d.SortBy,
|
|
|
Category: d.Category,
|
|
|
Name: d.Name,
|
|
|
Remark: d.Remark,
|
|
|
Value: d.Value,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
}
|
|
|
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 *StaffAssessContentTempRepository) Remove(id int) error {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
_, err := tx.Model(&models.StaffAssessContentTemp{}).
|
|
|
Where("id=?", id).Delete()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessContentTempRepository) FindOne(queryOptions map[string]interface{}) (*domain.StaffAssessContentTemp, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.StaffAssessContentTemp)
|
|
|
query := tx.Model(m)
|
|
|
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 *StaffAssessContentTempRepository) Find(queryOptions map[string]interface{}) (int, []*domain.StaffAssessContentTemp, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.StaffAssessContentTemp
|
|
|
query := tx.Model(&m)
|
|
|
if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id = ?", companyId)
|
|
|
}
|
|
|
if v, ok := queryOptions["staffAssessId"]; ok {
|
|
|
query.Where("staff_assess_id=?", v)
|
|
|
}
|
|
|
count, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var arrays []*domain.StaffAssessContentTemp
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
arrays = append(arrays, d)
|
|
|
}
|
|
|
return count, arrays, nil
|
|
|
} |