|
|
1
|
+package repository
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+
|
|
|
6
|
+ "github.com/go-pg/pg/v10"
|
|
|
7
|
+ pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
8
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
9
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+type RewardSummaryRepository struct {
|
|
|
13
|
+ transactionContext *pgTransaction.TransactionContext
|
|
|
14
|
+}
|
|
|
15
|
+
|
|
|
16
|
+var _ domain.RewardSummaryRepository = (*RewardSummaryRepository)(nil)
|
|
|
17
|
+
|
|
|
18
|
+func NewRewardSummaryRepository(transactionContext *pgTransaction.TransactionContext) (*RewardSummaryRepository, error) {
|
|
|
19
|
+ if transactionContext == nil {
|
|
|
20
|
+ return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
21
|
+ } else {
|
|
|
22
|
+ return &RewardSummaryRepository{
|
|
|
23
|
+ transactionContext: transactionContext,
|
|
|
24
|
+ }, nil
|
|
|
25
|
+ }
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+func (repo *RewardSummaryRepository) Save(param *domain.RewardSummary) (*domain.RewardSummary, error) {
|
|
|
29
|
+ m := models.RewardSummary{
|
|
|
30
|
+ Id: param.Id,
|
|
|
31
|
+ CompanyId: param.CompanyId,
|
|
|
32
|
+ OrgId: param.OrgId,
|
|
|
33
|
+ RecordDate: param.RecordDate,
|
|
|
34
|
+ WorkStation: param.WorkStation,
|
|
|
35
|
+ Worker: param.Worker,
|
|
|
36
|
+ UpToStandard: param.UpToStandard,
|
|
|
37
|
+ Yield: param.Yield,
|
|
|
38
|
+ AccidentNum1: param.AccidentNum1,
|
|
|
39
|
+ AccidentAmount1: param.AccidentAmount1,
|
|
|
40
|
+ AccidentNum2: param.AccidentNum2,
|
|
|
41
|
+ AccidentAmount2: param.AccidentAmount2,
|
|
|
42
|
+ AccidentNum3: param.AccidentNum3,
|
|
|
43
|
+ AccidentNum4: param.AccidentNum4,
|
|
|
44
|
+ CreatedAt: param.CreatedAt,
|
|
|
45
|
+ UpdatedAt: param.UpdatedAt,
|
|
|
46
|
+ SummaryResult: param.SummaryResult,
|
|
|
47
|
+ }
|
|
|
48
|
+ tx := repo.transactionContext.PgTx
|
|
|
49
|
+ if param.Id == 0 {
|
|
|
50
|
+ _, err := tx.Model(&m).Insert()
|
|
|
51
|
+ if err != nil {
|
|
|
52
|
+ return nil, err
|
|
|
53
|
+ }
|
|
|
54
|
+ param.Id = m.Id
|
|
|
55
|
+ } else {
|
|
|
56
|
+ _, err := tx.Model(&m).WherePK().Update()
|
|
|
57
|
+ if err != nil {
|
|
|
58
|
+ return nil, err
|
|
|
59
|
+ }
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ return param, nil
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+func (repo *RewardSummaryRepository) FindOne(queryOptions map[string]interface{}) (*domain.RewardSummary, error) {
|
|
|
66
|
+ tx := repo.transactionContext.PgTx
|
|
|
67
|
+ m := new(models.RewardSummary)
|
|
|
68
|
+ query := tx.Model(m)
|
|
|
69
|
+ if v, ok := queryOptions["id"]; ok {
|
|
|
70
|
+ query.Where("id=?", v)
|
|
|
71
|
+ }
|
|
|
72
|
+ err := query.First()
|
|
|
73
|
+ if err != nil {
|
|
|
74
|
+ if err == pg.ErrNoRows {
|
|
|
75
|
+ return nil, domain.ErrorNotFound
|
|
|
76
|
+ } else {
|
|
|
77
|
+ return nil, err
|
|
|
78
|
+ }
|
|
|
79
|
+ }
|
|
|
80
|
+ result := repo.TransformToDomain(m)
|
|
|
81
|
+ return result, nil
|
|
|
82
|
+}
|
|
|
83
|
+
|
|
|
84
|
+func (repo *RewardSummaryRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RewardSummary, error) {
|
|
|
85
|
+ tx := repo.transactionContext.PgTx
|
|
|
86
|
+ m := []models.RewardSummary{}
|
|
|
87
|
+ query := tx.Model(&m).
|
|
|
88
|
+ Limit(20)
|
|
|
89
|
+ if v, ok := queryOptions["limit"].(int); ok {
|
|
|
90
|
+ query.Limit(v)
|
|
|
91
|
+ }
|
|
|
92
|
+ if v, ok := queryOptions["offset"].(int); ok {
|
|
|
93
|
+ query.Offset(v)
|
|
|
94
|
+ }
|
|
|
95
|
+ if v, ok := queryOptions["companyId"]; ok {
|
|
|
96
|
+ query.Where("company_id=?", v)
|
|
|
97
|
+ }
|
|
|
98
|
+ if v, ok := queryOptions["orgId"]; ok {
|
|
|
99
|
+ query.Where("org_id=?", v)
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
|
|
|
103
|
+ query.Where(`Work_station->>'workshopName' like '%?%'`, v)
|
|
|
104
|
+ }
|
|
|
105
|
+ cnt, err := query.SelectAndCount()
|
|
|
106
|
+ if err != nil {
|
|
|
107
|
+ return 0, nil, err
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ var listData []*domain.RewardSummary
|
|
|
111
|
+ for i := range m {
|
|
|
112
|
+ temp := repo.TransformToDomain(&m[i])
|
|
|
113
|
+ listData = append(listData, temp)
|
|
|
114
|
+ }
|
|
|
115
|
+ return int64(cnt), listData, nil
|
|
|
116
|
+}
|
|
|
117
|
+
|
|
|
118
|
+func (repo *RewardSummaryRepository) TransformToDomain(param *models.RewardSummary) *domain.RewardSummary {
|
|
|
119
|
+ return &domain.RewardSummary{
|
|
|
120
|
+ Id: param.Id,
|
|
|
121
|
+ CompanyId: param.CompanyId,
|
|
|
122
|
+ OrgId: param.OrgId,
|
|
|
123
|
+ RecordDate: param.RecordDate,
|
|
|
124
|
+ WorkStation: param.WorkStation,
|
|
|
125
|
+ Worker: param.Worker,
|
|
|
126
|
+ UpToStandard: param.UpToStandard,
|
|
|
127
|
+ Yield: param.Yield,
|
|
|
128
|
+ AccidentNum1: param.AccidentNum1,
|
|
|
129
|
+ AccidentAmount1: param.AccidentAmount1,
|
|
|
130
|
+ AccidentNum2: param.AccidentNum2,
|
|
|
131
|
+ AccidentAmount2: param.AccidentAmount2,
|
|
|
132
|
+ AccidentNum3: param.AccidentNum3,
|
|
|
133
|
+ AccidentNum4: param.AccidentNum4,
|
|
|
134
|
+ CreatedAt: param.CreatedAt,
|
|
|
135
|
+ UpdatedAt: param.UpdatedAt,
|
|
|
136
|
+ SummaryResult: param.SummaryResult,
|
|
|
137
|
+ }
|
|
|
138
|
+} |