pg_task_repository.go 3.6 KB
package repository

import (
	"errors"
	"fmt"
	"strconv"
	"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 TaskAnomalyRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.TaskAnomalyRepository = (*TaskAnomalyRepository)(nil)

func NewTaskAnomalyRepository(transactionContext *pgTransaction.TransactionContext) *TaskAnomalyRepository {
	return &TaskAnomalyRepository{transactionContext: transactionContext}
}

func (repo *TaskAnomalyRepository) TransformToDomain(d *models.TaskAnomaly) *domain.TaskAnomaly {
	return &domain.TaskAnomaly{
		Id:           d.Id,
		CreatedAt:    d.CreatedAt,
		UpdatedAt:    d.UpdatedAt,
		CompanyId:    d.CompanyId,
		TaskId:       d.TaskId,
		Category:     d.Category,
		CurrentStage: d.CurrentStage,
		LastStage:    d.LastStage,
		AssessFlag:   d.AssessFlag,
		WarnFlag:     d.WarnFlag,
		AssistFlag:   d.AssistFlag,
		IsLast:       d.IsLast,
		TaskLevel:    d.TaskLevel,
		TaskName:     d.TaskName,
		TaskAlias:    d.TaskAlias,
		TaskSortBy:   domain.TaskSortBy(d.TaskSortBy),
		TaskEndTime:  d.TaskEndTime,
		RecordBegin:  d.RecordBegin,
		NoticeWho:    d.NoticeWho,
		Marks:        d.Marks,
	}
}

func (repo *TaskAnomalyRepository) Save(param *domain.TaskAnomaly) error {
	param.UpdatedAt = time.Now()
	if param.Id == 0 {
		param.CreatedAt = time.Now()
	}
	m := models.TaskAnomaly{
		Id:           param.Id,
		CreatedAt:    param.CreatedAt,
		UpdatedAt:    param.UpdatedAt,
		CompanyId:    param.CompanyId,
		TaskId:       param.TaskId,
		Category:     param.Category,
		CurrentStage: param.CurrentStage,
		LastStage:    param.LastStage,
		AssessFlag:   param.AssessFlag,
		WarnFlag:     param.WarnFlag,
		AssistFlag:   param.AssistFlag,
		IsLast:       param.IsLast,
		TaskLevel:    param.TaskLevel,
		TaskName:     param.TaskName,
		TaskAlias:    param.TaskAlias,
		TaskSortBy:   int(param.TaskSortBy),
		TaskEndTime:  param.TaskEndTime,
		RecordBegin:  param.RecordBegin,
		NoticeWho:    param.NoticeWho,
		Marks:        param.Marks,
	}
	db := repo.transactionContext.PgTx
	if m.Id == 0 {
		_, err := db.Model(&m).Insert()
		if err != nil {
			return err
		}
	} else {
		_, err := db.Model(&m).WherePK().Update()
		if err != nil {
			return err
		}
	}
	param.Id = m.Id
	return nil
}

func (repo *TaskAnomalyRepository) FindOne(queryOptions map[string]interface{}) (*domain.TaskAnomaly, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.TaskAnomaly)
	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("没有找到TaskAnomaly数据")
		} else {
			return nil, err
		}
	}
	u := repo.TransformToDomain(m)
	return u, nil
}

func (repo *TaskAnomalyRepository) Find(queryOptions map[string]interface{}) (int, []*domain.TaskAnomaly, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.TaskAnomaly
	query := tx.Model(&m)
	if val, ok := queryOptions["limit"]; ok {
		if limit, err := strconv.Atoi(fmt.Sprintf("%v", val)); err == nil {
			query.Limit(limit)
		}
	}
	if val, ok := queryOptions["offset"]; ok {
		if offset, err := strconv.Atoi(fmt.Sprintf("%v", val)); err == nil {
			query.Offset(offset)
		}
	}
	query.Order("id desc")
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var datas []*domain.TaskAnomaly
	for _, v := range m {
		d := repo.TransformToDomain(v)
		datas = append(datas, d)
	}
	return count, datas, nil
}