pg_task_anomaly_repository.go
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
134
135
136
137
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,
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,
}
}
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,
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,
}
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["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
}