审查视图

pkg/infrastructure/repository/pg_task_repository.go 3.9 KB
tangxvhui authored
1 2
package repository
tangxvhui authored
3 4 5
import (
	"errors"
	"fmt"
tangxvhui authored
6
	"strconv"
tangxvhui authored
7 8 9 10 11 12 13 14
	"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"
)
tangxvhui authored
15
//任务
tangxvhui authored
16 17 18 19 20 21 22

type TaskRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.TaskRepository = (*TaskRepository)(nil)
tangxvhui authored
23 24 25 26
func NewTaskRepository(transactionContext *pgTransaction.TransactionContext) *TaskRepository {
	return &TaskRepository{transactionContext: transactionContext}
}
tangxvhui authored
27 28
func (repo *TaskRepository) TransformToDomain(d *models.Task) *domain.Task {
	return &domain.Task{
tangxvhui authored
29 30 31 32
		Id:          d.Id,
		CreatedAt:   d.CreatedAt,
		UpdatedAt:   d.UpdatedAt,
		DeletedAt:   d.DeletedAt,
tangxvhui authored
33
		CompanyId:   d.CompanyId,
tangxvhui authored
34 35 36 37 38
		Name:        d.Name,
		Alias:       d.Alias,
		Leader:      d.Leader,
		Status:      domain.TaskState(d.Status),
		Level:       d.Level,
tangxvhui authored
39
		LevelName:   d.LevelName,
tangxvhui authored
40 41 42
		RelatedUser: d.RelatedUser,
		RunAt:       d.RunAt,
		StopAt:      d.StopAt,
tangxvhui authored
43
		Anomaly:     d.Anomaly,
44
		WarnFlag:    domain.TaskWarnFlag(d.WarnFlag),
tangxvhui authored
45 46 47 48 49 50 51 52 53
	}
}

func (repo *TaskRepository) Save(param *domain.Task) error {
	param.UpdatedAt = time.Now()
	if param.Id == 0 {
		param.CreatedAt = time.Now()
	}
	m := models.Task{
tangxvhui authored
54 55 56 57 58 59
		Id:          param.Id,
		CreatedAt:   param.CreatedAt,
		UpdatedAt:   param.UpdatedAt,
		DeletedAt:   param.DeletedAt,
		Name:        param.Name,
		Alias:       param.Alias,
60
		CompanyId:   param.CompanyId,
tangxvhui authored
61 62 63
		Leader:      param.Leader,
		Status:      int(param.Status),
		Level:       param.Level,
tangxvhui authored
64
		LevelName:   param.LevelName,
tangxvhui authored
65 66 67
		RelatedUser: param.RelatedUser,
		RunAt:       param.RunAt,
		StopAt:      param.StopAt,
tangxvhui authored
68
		Anomaly:     param.Anomaly,
69
		WarnFlag:    int(param.WarnFlag),
tangxvhui authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	}
	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 *TaskRepository) Remove(id int) error {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	_, err := tx.Model(&models.Task{}).
		Where("id=?", id).
		Set("deleted_at=?", nowTime).
		Update()
	return err

}

func (repo *TaskRepository) FindOne(queryOptions map[string]interface{}) (*domain.Task, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.Task)
	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 *TaskRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Task, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.Task
	query := tx.Model(&m).
		Where("deleted_at isnull").
		Limit(20)
tangxvhui authored
123 124 125
	if val, ok := queryOptions["name"]; ok {
		query.Where("task.name like ?", val)
	}
tangxvhui authored
126
	if val, ok := queryOptions["companyId"]; ok {
tangxvhui authored
127
		query.Where("task.company_id = ?", val)
tangxvhui authored
128 129
	}
tangxvhui authored
130
	if val, ok := queryOptions["leaderId"]; ok {
131
		query.Where("task.leader->>'id'=?", val)
tangxvhui authored
132
	}
tangxvhui authored
133 134 135
	if val, ok := queryOptions["aliasOrLeader"]; ok {
		query.Where(`task.alias like ? or task.leader->>'name' like ?`, val, val)
	}
tangxvhui authored
136 137 138
	if val, ok := queryOptions["status"]; ok {
		query.Where("task.status=?", val)
	}
tangxvhui authored
139 140 141 142 143 144 145 146 147 148
	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)
		}
	}
tangxvhui authored
149 150 151 152 153 154 155 156 157 158 159 160
	query.Order("id desc")
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var datas []*domain.Task
	for _, v := range m {
		d := repo.TransformToDomain(v)
		datas = append(datas, d)
	}
	return count, datas, nil
}