pg_task_anomaly_repository.go 3.9 KB
package repository

import (
	"errors"
	"fmt"
	"strconv"

	"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,
		TaskRecordId:   d.TaskRecordId,
		Category:       d.Category,
		CurrentStage:   d.CurrentStage,
		LastStage:      d.LastStage,
		TaskStageCheck: d.TaskStageCheck,
		AssessFlag:     d.AssessFlag,
		WarnFlag:       d.WarnFlag,
		AssistFlag:     d.AssistFlag,
		IsLast:         d.IsLast,
		RecordBegin:    d.RecordBegin,
		NoticeWho:      d.NoticeWho,
		Marks:          d.Marks,
		Remark:         d.Remark,
	}
}

func (repo *TaskAnomalyRepository) Save(param *domain.TaskAnomaly) error {
	m := models.TaskAnomaly{
		Id:             param.Id,
		CreatedAt:      param.CreatedAt,
		UpdatedAt:      param.UpdatedAt,
		CompanyId:      param.CompanyId,
		TaskId:         param.TaskId,
		TaskRecordId:   param.TaskRecordId,
		Category:       param.Category,
		CurrentStage:   param.CurrentStage,
		LastStage:      param.LastStage,
		TaskStageCheck: param.TaskStageCheck,
		AssessFlag:     param.AssessFlag,
		WarnFlag:       param.WarnFlag,
		AssistFlag:     param.AssistFlag,
		IsLast:         param.IsLast,
		RecordBegin:    param.RecordBegin,
		NoticeWho:      param.NoticeWho,
		Marks:          param.Marks,
		Remark:         param.Remark,
	}
	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)
		}
	}
	if val, ok := queryOptions["id"]; ok {
		query.Where(" id=? ", val)
	}
	if val, ok := queryOptions["taskId"]; ok {
		query.Where(" task_id=? ", val)
	}
	if val, ok := queryOptions["category"]; ok {
		query.Where(" category =? ", val)
	}
	if val, ok := queryOptions["taskStageCheck"]; ok {
		query.Where(" task_stage_check @> ? ", val)
	}
	if val, ok := queryOptions["isLast"]; ok {
		query.Where(" is_last=? ", val)
	}
	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
}