审查视图

pkg/infrastructure/repository/pg_node_task_repository.go 4.2 KB
郑周 authored
1 2 3 4 5
package repository

import (
	"errors"
	"fmt"
Your Name authored
6 7
	"time"
郑周 authored
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
	"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"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
)

type NodeTaskRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewNodeTaskRepository(transactionContext *pgTransaction.TransactionContext) *NodeTaskRepository {
	return &NodeTaskRepository{transactionContext: transactionContext}
}

func (repo *NodeTaskRepository) TransformToDomain(m *models.NodeTask) domain.NodeTask {
	return domain.NodeTask{
郑周 authored
25 26 27 28 29 30 31 32
		Id:           m.Id,
		CompanyId:    m.CompanyId,
		CycleId:      m.CycleId,
		ProjectId:    m.ProjectId,
		NodeId:       m.NodeId,
		NodeType:     m.NodeType,
		NodeName:     m.NodeName,
		NodeDescribe: m.NodeDescribe,
郑周 authored
33
		NodeSort:     m.NodeSort,
郑周 authored
34 35
		TimeStart:    m.TimeStart,
		TimeEnd:      m.TimeEnd,
郑周 authored
36
		KpiCycle:     m.KpiCycle,
郑周 authored
37
		NextSentAt:   m.NextSentAt,
38
		LastSentAt:   m.LastSentAt,
39 40
		CreatedAt:    m.CreatedAt.Local(),
		UpdatedAt:    m.UpdatedAt.Local(),
郑周 authored
41
		DeletedAt:    m.DeletedAt,
郑周 authored
42 43 44 45 46
	}
}

func (repo *NodeTaskRepository) TransformToModel(d *domain.NodeTask) models.NodeTask {
	return models.NodeTask{
郑周 authored
47 48 49 50 51 52 53 54
		Id:           d.Id,
		CompanyId:    d.CompanyId,
		CycleId:      d.CycleId,
		ProjectId:    d.ProjectId,
		NodeId:       d.NodeId,
		NodeType:     d.NodeType,
		NodeName:     d.NodeName,
		NodeDescribe: d.NodeDescribe,
郑周 authored
55
		NodeSort:     d.NodeSort,
郑周 authored
56 57 58 59
		TimeStart:    d.TimeStart,
		TimeEnd:      d.TimeEnd,
		KpiCycle:     d.KpiCycle,
		NextSentAt:   d.NextSentAt,
60
		LastSentAt:   d.LastSentAt,
郑周 authored
61 62 63
		CreatedAt:    d.CreatedAt,
		UpdatedAt:    d.UpdatedAt,
		DeletedAt:    d.DeletedAt,
郑周 authored
64 65 66 67 68 69 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 123 124 125 126 127 128 129 130 131 132 133
	}
}

func (repo *NodeTaskRepository) nextIdentify() (int64, error) {
	return utils.NewSnowflakeId()
}

func (repo *NodeTaskRepository) Insert(d *domain.NodeTask) (*domain.NodeTask, 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 *NodeTaskRepository) Remove(d *domain.NodeTask) (*domain.NodeTask, error) {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	m := repo.TransformToModel(d)
	m.DeletedAt = &nowTime
	if _, err := tx.Model(&m).WherePK().Update(); err != nil {
		return d, err
	}
	return d, nil
}

func (repo *NodeTaskRepository) FindOne(queryOptions map[string]interface{}) (*domain.NodeTask, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.NodeTask)
	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 *NodeTaskRepository) Find(queryOptions map[string]interface{}) ([]*domain.NodeTask, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.NodeTask
	query := tx.Model(&m).Where("deleted_at isnull")
134
	if v, ok := queryOptions["lessNextSentAt"].(time.Time); ok {
郑周 authored
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		query.Where("next_sent_at <= ?", v)
	}

	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}

	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
	}

	if v, ok := queryOptions["cycleId"]; ok {
		query.Where("cycle_id = ?", v)
	}

	if v, ok := queryOptions["projectId"]; ok {
		query.Where("project_id = ?", v)
	}

	if v, ok := queryOptions["nodeId"]; ok {
		query.Where("node_id = ?", v)
	}

	err := query.Select()
	if err != nil {
		return nil, err
	}
	var arrays []*domain.NodeTask
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, &d)
	}
	return arrays, nil
}