|
|
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 TaskStageRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
var _ domain.TaskStageRepository = (*TaskStageRepository)(nil)
|
|
|
|
|
|
func (repo *TaskStageRepository) TransformToDomain(d *models.TaskStage) *domain.TaskStage {
|
|
|
return &domain.TaskStage{
|
|
|
Id: d.Id,
|
|
|
TaskId: 0,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
Name: d.Name,
|
|
|
SortBy: d.SortBy,
|
|
|
Status: domain.TaskStageState(d.Status),
|
|
|
PlanCompletedAt: d.PlanCompletedAt,
|
|
|
RealCompletedAt: d.RealCompletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *TaskStageRepository) Save(param []*domain.TaskStage) error {
|
|
|
db := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
for _, val := range param {
|
|
|
m := models.TaskStage{
|
|
|
Id: val.Id,
|
|
|
TaskId: val.TaskId,
|
|
|
CreatedAt: val.CreatedAt,
|
|
|
UpdatedAt: nowTime,
|
|
|
DeletedAt: val.DeletedAt,
|
|
|
Name: val.Name,
|
|
|
SortBy: val.SortBy,
|
|
|
Status: int(val.Status),
|
|
|
PlanCompletedAt: val.PlanCompletedAt,
|
|
|
RealCompletedAt: val.RealCompletedAt,
|
|
|
}
|
|
|
if m.Id == 0 {
|
|
|
m.CreatedAt = nowTime
|
|
|
_, err := db.Model(&m).Insert()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
} else {
|
|
|
_, err := db.Model(&m).WherePK().Update()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
val.Id = m.Id
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (repo *TaskStageRepository) Remove(id int) error {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
_, err := tx.Model(&models.TaskStage{}).
|
|
|
Where("id=?", id).
|
|
|
Set("deleted_at=?", nowTime).
|
|
|
Update()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (repo *TaskStageRepository) FindOne(queryOptions map[string]interface{}) (*domain.TaskStage, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.TaskStage)
|
|
|
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("没有找到task数据")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
u := repo.TransformToDomain(m)
|
|
|
return u, nil
|
|
|
}
|
|
|
|
|
|
func (repo *TaskStageRepository) Find(queryOptions map[string]interface{}) (int, []*domain.TaskStage, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.TaskStage
|
|
|
query := tx.Model(&m).
|
|
|
Where("deleted_at isnull").
|
|
|
Limit(20)
|
|
|
|
|
|
query.Order("id desc")
|
|
|
count, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var datas []*domain.TaskStage
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
datas = append(datas, d)
|
|
|
}
|
|
|
return count, datas, nil
|
|
|
} |
...
|
...
|
|