pg_reward_summary_repository.go
4.9 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
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
169
170
171
172
173
174
package repository
import (
"fmt"
"time"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)
type RewardSummaryRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.RewardSummaryRepository = (*RewardSummaryRepository)(nil)
func NewRewardSummaryRepository(transactionContext *pgTransaction.TransactionContext) (*RewardSummaryRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RewardSummaryRepository{
transactionContext: transactionContext,
}, nil
}
}
func (repo *RewardSummaryRepository) Save(param *domain.RewardSummary) (*domain.RewardSummary, error) {
m := models.RewardSummary{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RecordDate: param.RecordDate,
RecordDateStr: param.RecordDateStr,
WorkStation: param.WorkStation,
Worker: param.Worker,
UpToStandard: param.UpToStandard,
Yield: param.Yield,
AccidentNum1: param.AccidentNum1,
AccidentAmount1: param.AccidentAmount1,
AccidentNum2: param.AccidentNum2,
AccidentAmount2: param.AccidentAmount2,
AccidentNum3: param.AccidentNum3,
AccidentNum4: param.AccidentNum4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
SummaryResult: param.SummaryResult,
}
tx := repo.transactionContext.PgTx
if param.Id == 0 {
_, err := tx.Model(&m).Insert()
if err != nil {
return nil, err
}
param.Id = m.Id
} else {
_, err := tx.Model(&m).WherePK().Update()
if err != nil {
return nil, err
}
}
return param, nil
}
func (repo *RewardSummaryRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardSummary, error) {
tx := repo.transactionContext.PgTx
m := new(models.RewardSummary)
query := tx.Model(m)
if v, ok := queryOptions["id"]; ok {
query.Where("id=?", v)
}
err := query.First()
if err != nil {
if err == pg.ErrNoRows {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
result := repo.TransformToDomain(m)
return result, nil
}
func (repo *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardSummary, error) {
tx := repo.transactionContext.PgTx
m := []models.RewardSummary{}
query := tx.Model(&m).
Limit(20)
if v, ok := queryOptions["limit"].(int); ok && v >= 0 {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok && v >= 0 {
query.Offset(v)
}
if v, ok := queryOptions["companyId"]; ok {
query.Where("company_id=?", v)
}
if v, ok := queryOptions["orgId"]; ok {
query.Where("org_id=?", v)
}
//车间名称
if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'workshopName' like ?`, fmt.Sprintf("%%%v%%", v))
}
//线别名称
if v, ok := queryOptions["lineName"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'lineName' like ?`, fmt.Sprintf("%%%v%%", v))
}
//工段id
if v, ok := queryOptions["sectionId"]; ok && len(v.(string)) > 0 {
query.Where(`Work_station->>'sectionId' = '?'`, v)
}
//车间id
if v, ok := queryOptions["workshopId"]; ok {
query.Where(`Work_station->>'workshopId'='?'`, v)
}
//线别id
if v, ok := queryOptions["lineId"]; ok {
query.Where(`Work_station->>'lineId' = '?'`, v)
}
//日期开始
if v, ok := queryOptions["beginDate"]; ok {
if !(v.(time.Time)).IsZero() {
query.Where("record_date>=?", v)
}
}
//日期结束
if v, ok := queryOptions["endDate"]; ok {
if !(v.(time.Time)).IsZero() {
query.Where("record_date<=?", v)
}
}
//指定查询某个日期
if v, ok := queryOptions["recordDateStr"]; ok {
query.Where("record_date_str=?", v)
}
cnt, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var listData []*domain.RewardSummary
for i := range m {
temp := repo.TransformToDomain(&m[i])
listData = append(listData, temp)
}
return int64(cnt), listData, nil
}
func (repo *RewardSummaryRepository) TransformToDomain(param *models.RewardSummary) *domain.RewardSummary {
return &domain.RewardSummary{
Id: param.Id,
CompanyId: param.CompanyId,
OrgId: param.OrgId,
RecordDate: param.RecordDate,
WorkStation: param.WorkStation,
Worker: param.Worker,
UpToStandard: param.UpToStandard,
Yield: param.Yield,
AccidentNum1: param.AccidentNum1,
AccidentAmount1: param.AccidentAmount1,
AccidentNum2: param.AccidentNum2,
AccidentAmount2: param.AccidentAmount2,
AccidentNum3: param.AccidentNum3,
AccidentNum4: param.AccidentNum4,
CreatedAt: param.CreatedAt,
UpdatedAt: param.UpdatedAt,
SummaryResult: param.SummaryResult,
RecordDateStr: param.RecordDateStr,
}
}