1
|
-package repository
|
|
|
2
|
-
|
|
|
3
|
-import (
|
|
|
4
|
- "errors"
|
|
|
5
|
- "fmt"
|
|
|
6
|
-
|
|
|
7
|
- "github.com/go-pg/pg/v10"
|
|
|
8
|
- pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
9
|
- "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
10
|
- "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
|
|
|
11
|
-)
|
|
|
12
|
-
|
|
|
13
|
-type StaffAssessContentTempRepository struct {
|
|
|
14
|
- transactionContext *pgTransaction.TransactionContext
|
|
|
15
|
-}
|
|
|
16
|
-
|
|
|
17
|
-var _ domain.StaffAssessContentTempRepository = (*StaffAssessContentTempRepository)(nil)
|
|
|
18
|
-
|
|
|
19
|
-func NewStaffAssessContentTempRepository(transactionContext *pgTransaction.TransactionContext) *StaffAssessContentTempRepository {
|
|
|
20
|
- return &StaffAssessContentTempRepository{transactionContext: transactionContext}
|
|
|
21
|
-}
|
|
|
22
|
-
|
|
|
23
|
-func (repo *StaffAssessContentTempRepository) TransformToDomain(d *models.StaffAssessContentTemp) *domain.StaffAssessContentTemp {
|
|
|
24
|
- return &domain.StaffAssessContentTemp{
|
|
|
25
|
- Id: d.Id,
|
|
|
26
|
- StaffAssessId: d.StaffAssessId,
|
|
|
27
|
- SortBy: d.SortBy,
|
|
|
28
|
- Category: d.Category,
|
|
|
29
|
- Name: d.Name,
|
|
|
30
|
- Remark: d.Remark,
|
|
|
31
|
- Value: d.Value,
|
|
|
32
|
- CreatedAt: d.CreatedAt,
|
|
|
33
|
- UpdatedAt: d.UpdatedAt,
|
|
|
34
|
- }
|
|
|
35
|
-}
|
|
|
36
|
-
|
|
|
37
|
-func (repo *StaffAssessContentTempRepository) Save(d *domain.StaffAssessContentTemp) (*domain.StaffAssessContentTemp, error) {
|
|
|
38
|
- saveModel := models.StaffAssessContentTemp{
|
|
|
39
|
- Id: d.Id,
|
|
|
40
|
- StaffAssessId: d.StaffAssessId,
|
|
|
41
|
- SortBy: d.SortBy,
|
|
|
42
|
- Category: d.Category,
|
|
|
43
|
- Name: d.Name,
|
|
|
44
|
- Remark: d.Remark,
|
|
|
45
|
- Value: d.Value,
|
|
|
46
|
- CreatedAt: d.CreatedAt,
|
|
|
47
|
- UpdatedAt: d.UpdatedAt,
|
|
|
48
|
- }
|
|
|
49
|
- tx := repo.transactionContext.PgTx
|
|
|
50
|
- var err error
|
|
|
51
|
- if saveModel.Id == 0 {
|
|
|
52
|
- _, err = tx.Model(&saveModel).Insert()
|
|
|
53
|
- if err != nil {
|
|
|
54
|
- return nil, err
|
|
|
55
|
- }
|
|
|
56
|
- } else {
|
|
|
57
|
- _, err = tx.Model(&saveModel).WherePK().Update()
|
|
|
58
|
- if err != nil {
|
|
|
59
|
- return nil, err
|
|
|
60
|
- }
|
|
|
61
|
- }
|
|
|
62
|
- d.Id = saveModel.Id
|
|
|
63
|
- return d, nil
|
|
|
64
|
-}
|
|
|
65
|
-
|
|
|
66
|
-func (repo *StaffAssessContentTempRepository) Remove(id int) error {
|
|
|
67
|
- tx := repo.transactionContext.PgTx
|
|
|
68
|
- _, err := tx.Model(&models.StaffAssessContentTemp{}).
|
|
|
69
|
- Where("id=?", id).Delete()
|
|
|
70
|
- return err
|
|
|
71
|
-}
|
|
|
72
|
-
|
|
|
73
|
-func (repo *StaffAssessContentTempRepository) FindOne(queryOptions map[string]interface{}) (*domain.StaffAssessContentTemp, error) {
|
|
|
74
|
- tx := repo.transactionContext.PgTx
|
|
|
75
|
- m := new(models.StaffAssessContentTemp)
|
|
|
76
|
- query := tx.Model(m)
|
|
|
77
|
- if id, ok := queryOptions["id"]; ok {
|
|
|
78
|
- query.Where("id=?", id)
|
|
|
79
|
- }
|
|
|
80
|
- if err := query.First(); err != nil {
|
|
|
81
|
- if errors.Is(err, pg.ErrNoRows) {
|
|
|
82
|
- return nil, fmt.Errorf("没有此资源")
|
|
|
83
|
- } else {
|
|
|
84
|
- return nil, err
|
|
|
85
|
- }
|
|
|
86
|
- }
|
|
|
87
|
- u := repo.TransformToDomain(m)
|
|
|
88
|
- return u, nil
|
|
|
89
|
-}
|
|
|
90
|
-
|
|
|
91
|
-func (repo *StaffAssessContentTempRepository) Find(queryOptions map[string]interface{}) (int, []*domain.StaffAssessContentTemp, error) {
|
|
|
92
|
- tx := repo.transactionContext.PgTx
|
|
|
93
|
- var m []*models.StaffAssessContentTemp
|
|
|
94
|
- query := tx.Model(&m)
|
|
|
95
|
- if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
96
|
- query.Where("company_id = ?", companyId)
|
|
|
97
|
- }
|
|
|
98
|
- if v, ok := queryOptions["staffAssessId"]; ok {
|
|
|
99
|
- query.Where("staff_assess_id=?", v)
|
|
|
100
|
- }
|
|
|
101
|
- count, err := query.SelectAndCount()
|
|
|
102
|
- if err != nil {
|
|
|
103
|
- return 0, nil, err
|
|
|
104
|
- }
|
|
|
105
|
- var arrays []*domain.StaffAssessContentTemp
|
|
|
106
|
- for _, v := range m {
|
|
|
107
|
- d := repo.TransformToDomain(v)
|
|
|
108
|
- arrays = append(arrays, d)
|
|
|
109
|
- }
|
|
|
110
|
- return count, arrays, nil
|
|
|
111
|
-} |
|
|