|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
|
|
|
"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 StaffAssessCacheRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
//var _ domain.StaffAssessCacheRepository = (*StaffAssessCacheRepository)(nil)
|
|
|
|
|
|
func NewStaffAssessCacheRepository(transactionContext *pgTransaction.TransactionContext) *StaffAssessCacheRepository {
|
|
|
return &StaffAssessCacheRepository{transactionContext: transactionContext}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) TransformToDomain(m *models.StaffAssessCache) domain.StaffAssessCache {
|
|
|
return domain.StaffAssessCache{
|
|
|
Id: m.Id,
|
|
|
AssessId: m.AssessId,
|
|
|
AssessContent: m.AssessContent,
|
|
|
CreatedAt: m.CreatedAt,
|
|
|
UpdatedAt: m.UpdatedAt,
|
|
|
DeletedAt: m.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) TransformToModel(d *domain.StaffAssessCache) models.StaffAssessCache {
|
|
|
return models.StaffAssessCache{
|
|
|
Id: d.Id,
|
|
|
AssessId: d.AssessId,
|
|
|
AssessContent: d.AssessContent,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) nextIdentify() (int64, error) {
|
|
|
return utils.NewSnowflakeId()
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) Save(d *domain.StaffAssessCache) (*domain.StaffAssessCache, error) {
|
|
|
var isCreate = d.Id == 0
|
|
|
if isCreate {
|
|
|
id, err := repo.nextIdentify()
|
|
|
if err != nil {
|
|
|
return d, err
|
|
|
}
|
|
|
d.Id = id
|
|
|
d.CreatedAt = time.Now()
|
|
|
d.UpdatedAt = d.CreatedAt
|
|
|
} else {
|
|
|
d.UpdatedAt = time.Now()
|
|
|
}
|
|
|
m := repo.TransformToModel(d)
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var err error
|
|
|
if isCreate {
|
|
|
_, err = tx.Model(&m).Returning("id").Insert()
|
|
|
} else {
|
|
|
_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
d.Id = m.Id
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) Remove(id int64) error {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
_, err := tx.Model(&models.StaffAssessCache{}).Where("id=?", id).Set("deleted_at=?", nowTime).Update()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (repo *StaffAssessCacheRepository) FindOne(queryOptions map[string]interface{}) (*domain.StaffAssessCache, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.StaffAssessCache)
|
|
|
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 *StaffAssessCacheRepository) Find(queryOptions map[string]interface{}) (int, []*domain.StaffAssessCache, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.StaffAssessCache
|
|
|
query := tx.Model(&m).Where("deleted_at isnull")
|
|
|
|
|
|
if v, ok := queryOptions["assessId"]; ok {
|
|
|
query.Where("assess_id=?", v)
|
|
|
}
|
|
|
|
|
|
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.StaffAssessCache
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
arrays = append(arrays, &d)
|
|
|
}
|
|
|
return count, arrays, nil
|
|
|
} |
...
|
...
|
|