正在显示
18 个修改的文件
包含
1101 行增加
和
34 行删除
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/core/validation" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
6 | +) | ||
7 | + | ||
8 | +type CreateRuleCommand struct { | ||
9 | + CompanyId int64 `cname:"公司ID" json:"companyId"` | ||
10 | + CreatorId int64 `cname:"创建人ID" json:"creatorId"` | ||
11 | + Name string `cname:"规则名称" json:"name" valid:"Required"` | ||
12 | + Remark string `cname:"规则备注" json:"remark"` | ||
13 | + Type int `cname:"评估方式" json:"type"` | ||
14 | + Rating *domain.Rating `cname:"评级" json:"rating"` | ||
15 | + Score *domain.Score `cname:"评分" json:"score"` | ||
16 | +} | ||
17 | + | ||
18 | +func (in *CreateRuleCommand) Valid(validation *validation.Validation) { | ||
19 | + if in.CompanyId == 0 { | ||
20 | + validation.SetError("companyId", "公司ID无效") | ||
21 | + return | ||
22 | + } | ||
23 | + if in.CreatorId == 0 { | ||
24 | + validation.SetError("creatorId", "创建人ID无效") | ||
25 | + return | ||
26 | + } | ||
27 | + | ||
28 | + if len(in.Name) > 40 { | ||
29 | + validation.SetError("name", "角色名称最大长度40个字符") | ||
30 | + return | ||
31 | + } | ||
32 | + | ||
33 | + if in.Type == domain.EvaluationTypeRating { | ||
34 | + if nil == in.Rating || len(in.Rating.Levels) == 0 { | ||
35 | + validation.SetError("rating", "评级内容不能为空") | ||
36 | + return | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + if in.Type == domain.EvaluationTypeScore { | ||
41 | + if nil == in.Score { | ||
42 | + validation.SetError("rating", "评分内容不能为空") | ||
43 | + return | ||
44 | + } | ||
45 | + } | ||
46 | +} |
1 | +package command | ||
2 | + | ||
3 | +import "github.com/beego/beego/v2/core/validation" | ||
4 | + | ||
5 | +type DeleteRuleCommand struct { | ||
6 | + CompanyId int64 `cname:"公司ID" json:"companyId"` | ||
7 | + Id int64 `cname:"规则ID" json:"id,string" valid:"Required"` | ||
8 | +} | ||
9 | + | ||
10 | +func (in *DeleteRuleCommand) Valid(validation *validation.Validation) { | ||
11 | + if in.CompanyId == 0 { | ||
12 | + validation.SetError("companyId", "公司ID无效") | ||
13 | + return | ||
14 | + } | ||
15 | +} |
1 | +package command | ||
2 | + | ||
3 | +import "github.com/beego/beego/v2/core/validation" | ||
4 | + | ||
5 | +type QueryRuleCommand struct { | ||
6 | + CompanyId int64 `cname:"公司ID" json:"companyId"` | ||
7 | + PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"` | ||
8 | + PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"` | ||
9 | +} | ||
10 | + | ||
11 | +func (in *QueryRuleCommand) Valid(validation *validation.Validation) { | ||
12 | + if in.CompanyId == 0 { | ||
13 | + validation.SetError("companyId", "公司ID无效") | ||
14 | + return | ||
15 | + } | ||
16 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/core/validation" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
6 | +) | ||
7 | + | ||
8 | +type UpdateRuleCommand struct { | ||
9 | + Id int64 `cname:"规则ID" json:"id,string" valid:"Required"` | ||
10 | + CompanyId int64 `cname:"公司ID" json:"companyId"` | ||
11 | + CreatorId int64 `cname:"创建人ID" json:"creatorId"` | ||
12 | + Name string `cname:"规则名称" json:"name" valid:"Required"` | ||
13 | + Remark string `cname:"规则备注" json:"remark"` | ||
14 | + Type int `cname:"评估方式" json:"type"` | ||
15 | + Rating *domain.Rating `cname:"评级" json:"rating"` | ||
16 | + Score *domain.Score `cname:"评分" json:"score"` | ||
17 | +} | ||
18 | + | ||
19 | +func (in *UpdateRuleCommand) Valid(validation *validation.Validation) { | ||
20 | + if in.CompanyId == 0 { | ||
21 | + validation.SetError("companyId", "公司ID无效") | ||
22 | + return | ||
23 | + } | ||
24 | + if in.CreatorId == 0 { | ||
25 | + validation.SetError("creatorId", "创建人ID无效") | ||
26 | + return | ||
27 | + } | ||
28 | + | ||
29 | + if len(in.Name) > 40 { | ||
30 | + validation.SetError("name", "角色名称最大长度40个字符") | ||
31 | + return | ||
32 | + } | ||
33 | + | ||
34 | + if in.Type == domain.EvaluationTypeRating { | ||
35 | + if nil == in.Rating || len(in.Rating.Levels) == 0 { | ||
36 | + validation.SetError("rating", "评级内容不能为空") | ||
37 | + return | ||
38 | + } | ||
39 | + } | ||
40 | + | ||
41 | + if in.Type == domain.EvaluationTypeScore { | ||
42 | + if nil == in.Score { | ||
43 | + validation.SetError("rating", "评分内容不能为空") | ||
44 | + return | ||
45 | + } | ||
46 | + } | ||
47 | +} |
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/linmadan/egglib-go/core/application" | ||
5 | + "github.com/linmadan/egglib-go/utils/tool_funs" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_rule/adapter" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_rule/command" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
10 | +) | ||
11 | + | ||
12 | +type EvaluationRuleService struct { | ||
13 | +} | ||
14 | + | ||
15 | +func NewEvaluationRuleService() *EvaluationRuleService { | ||
16 | + newRoleService := &EvaluationRuleService{} | ||
17 | + return newRoleService | ||
18 | +} | ||
19 | + | ||
20 | +// Create 创建 | ||
21 | +func (rs *EvaluationRuleService) Create(in *command.CreateRuleCommand) (interface{}, error) { | ||
22 | + transactionContext, err := factory.ValidateStartTransaction(in) | ||
23 | + if err != nil { | ||
24 | + return nil, err | ||
25 | + } | ||
26 | + defer func() { | ||
27 | + transactionContext.RollbackTransaction() | ||
28 | + }() | ||
29 | + ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
30 | + | ||
31 | + // 检测名称重复 | ||
32 | + count, err := ruleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId}) | ||
33 | + if err != nil { | ||
34 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
35 | + } | ||
36 | + if count > 0 { | ||
37 | + return nil, application.ThrowError(application.BUSINESS_ERROR, "名称已存在") | ||
38 | + } | ||
39 | + newRule := &domain.EvaluationRule{ | ||
40 | + Id: 0, | ||
41 | + Name: in.Name, | ||
42 | + Remark: in.Remark, | ||
43 | + CompanyId: in.CompanyId, | ||
44 | + CreatorId: in.CreatorId, | ||
45 | + Type: in.Type, | ||
46 | + Rating: in.Rating, | ||
47 | + Score: in.Score, | ||
48 | + } | ||
49 | + rule, err := ruleRepository.Insert(newRule) | ||
50 | + if err != nil { | ||
51 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
52 | + } | ||
53 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
54 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
55 | + } | ||
56 | + return rule, nil | ||
57 | + | ||
58 | +} | ||
59 | + | ||
60 | +func (rs *EvaluationRuleService) Update(in *command.UpdateRuleCommand) (interface{}, error) { | ||
61 | + transactionContext, err := factory.ValidateStartTransaction(in) | ||
62 | + if err != nil { | ||
63 | + return nil, err | ||
64 | + } | ||
65 | + defer func() { | ||
66 | + transactionContext.RollbackTransaction() | ||
67 | + }() | ||
68 | + | ||
69 | + ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
70 | + | ||
71 | + // 检测名称重复(排除自己) | ||
72 | + count, err := ruleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id}) | ||
73 | + if err != nil { | ||
74 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
75 | + } | ||
76 | + if count > 0 { | ||
77 | + return nil, application.ThrowError(application.BUSINESS_ERROR, "名称已存在") | ||
78 | + } | ||
79 | + | ||
80 | + rule, err := ruleRepository.FindOne(map[string]interface{}{"id": in.Id}) | ||
81 | + if err != nil { | ||
82 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
83 | + } | ||
84 | + | ||
85 | + rule.Name = in.Name | ||
86 | + rule.Remark = in.Remark | ||
87 | + rule.Type = in.Type | ||
88 | + rule.Rating = in.Rating | ||
89 | + rule.Score = in.Score | ||
90 | + | ||
91 | + rule, err = ruleRepository.Insert(rule) | ||
92 | + if err != nil { | ||
93 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
94 | + } | ||
95 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
96 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
97 | + } | ||
98 | + return rule, nil | ||
99 | +} | ||
100 | + | ||
101 | +func (rs *EvaluationRuleService) Remove(in *command.DeleteRuleCommand) (interface{}, error) { | ||
102 | + transactionContext, err := factory.ValidateStartTransaction(in) | ||
103 | + if err != nil { | ||
104 | + return nil, err | ||
105 | + } | ||
106 | + defer func() { | ||
107 | + transactionContext.RollbackTransaction() | ||
108 | + }() | ||
109 | + | ||
110 | + ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
111 | + | ||
112 | + rule, err := ruleRepository.FindOne(map[string]interface{}{"id": in.Id}) | ||
113 | + if err != nil { | ||
114 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
115 | + } | ||
116 | + if _, err := ruleRepository.Remove(rule); err != nil { | ||
117 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
118 | + } | ||
119 | + | ||
120 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
121 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
122 | + } | ||
123 | + return rule, nil | ||
124 | +} | ||
125 | + | ||
126 | +func (rs *EvaluationRuleService) List(in *command.QueryRuleCommand) (interface{}, error) { | ||
127 | + transactionContext, err := factory.StartTransaction() | ||
128 | + if err != nil { | ||
129 | + return nil, err | ||
130 | + } | ||
131 | + defer func() { | ||
132 | + transactionContext.RollbackTransaction() | ||
133 | + }() | ||
134 | + ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
135 | + userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
136 | + | ||
137 | + // Fixme 总数量是否使用Count获取一个总数量 | ||
138 | + count, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in)) | ||
139 | + if err != nil { | ||
140 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
141 | + } | ||
142 | + | ||
143 | + ras := make([]*adapter.RuleAdapter, 0) | ||
144 | + creatorIds := make([]int64, 0) | ||
145 | + for i := range rules { | ||
146 | + ra := &adapter.RuleAdapter{} | ||
147 | + ra.EvaluationRule = rules[i] | ||
148 | + ras = append(ras, ra) | ||
149 | + creatorIds = append(creatorIds, rules[i].CreatorId) | ||
150 | + } | ||
151 | + _, users, _ := userRepository.Find(map[string]interface{}{"ids": creatorIds, "limit": len(creatorIds)}) | ||
152 | + userNameMap := map[int64]string{} | ||
153 | + if users != nil { | ||
154 | + for i := range users { | ||
155 | + userNameMap[users[i].Id] = users[i].Name | ||
156 | + } | ||
157 | + } | ||
158 | + for i := range ras { | ||
159 | + if v, ok := userNameMap[ras[i].CreatorId]; ok { | ||
160 | + ras[i].CreatorName = v | ||
161 | + } | ||
162 | + } | ||
163 | + return tool_funs.SimpleWrapGridMap(count, ras), nil | ||
164 | +} |
@@ -80,3 +80,35 @@ func CreateReceivedMessageRepository(options map[string]interface{}) domain.Rece | @@ -80,3 +80,35 @@ func CreateReceivedMessageRepository(options map[string]interface{}) domain.Rece | ||
80 | } | 80 | } |
81 | return repository.NewReceivedMessageRepository(transactionContext) | 81 | return repository.NewReceivedMessageRepository(transactionContext) |
82 | } | 82 | } |
83 | + | ||
84 | +func CreateEvaluationRuleRepository(options map[string]interface{}) domain.EvaluationRuleRepository { | ||
85 | + var transactionContext *pg.TransactionContext | ||
86 | + if value, ok := options["transactionContext"]; ok { | ||
87 | + transactionContext = value.(*pg.TransactionContext) | ||
88 | + } | ||
89 | + return repository.NewEvaluationRuleRepository(transactionContext) | ||
90 | +} | ||
91 | + | ||
92 | +func CreateEvaluationTemplateRepository(options map[string]interface{}) domain.EvaluationTemplateRepository { | ||
93 | + var transactionContext *pg.TransactionContext | ||
94 | + if value, ok := options["transactionContext"]; ok { | ||
95 | + transactionContext = value.(*pg.TransactionContext) | ||
96 | + } | ||
97 | + return repository.NewEvaluationTemplateRepository(transactionContext) | ||
98 | +} | ||
99 | + | ||
100 | +func CreateEvaluationCycleRepository(options map[string]interface{}) domain.EvaluationCycleRepository { | ||
101 | + var transactionContext *pg.TransactionContext | ||
102 | + if value, ok := options["transactionContext"]; ok { | ||
103 | + transactionContext = value.(*pg.TransactionContext) | ||
104 | + } | ||
105 | + return repository.NewEvaluationCycleRepository(transactionContext) | ||
106 | +} | ||
107 | + | ||
108 | +func CreateEvaluationProjectRepository(options map[string]interface{}) domain.EvaluationProjectRepository { | ||
109 | + var transactionContext *pg.TransactionContext | ||
110 | + if value, ok := options["transactionContext"]; ok { | ||
111 | + transactionContext = value.(*pg.TransactionContext) | ||
112 | + } | ||
113 | + return repository.NewEvaluationProjectRepository(transactionContext) | ||
114 | +} |
@@ -4,13 +4,6 @@ import ( | @@ -4,13 +4,6 @@ import ( | ||
4 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | 4 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" |
5 | ) | 5 | ) |
6 | 6 | ||
7 | -//type RoleContainUser struct { | ||
8 | -// RoleId int64 `json:"roleId,string"` | ||
9 | -// UserId int64 `json:"userId,string"` | ||
10 | -// UserName string `json:"userName"` | ||
11 | -// UserEmail string `json:"userEmail"` | ||
12 | -//} | ||
13 | - | ||
14 | type RoleUserAdapter struct { | 7 | type RoleUserAdapter struct { |
15 | domain.Role | 8 | domain.Role |
16 | Users []*domain.RoleContainUser `json:"users"` | 9 | Users []*domain.RoleContainUser `json:"users"` |
@@ -17,3 +17,11 @@ type EvaluationCycle struct { | @@ -17,3 +17,11 @@ type EvaluationCycle struct { | ||
17 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` | 17 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` |
18 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` | 18 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` |
19 | } | 19 | } |
20 | + | ||
21 | +type EvaluationCycleRepository interface { | ||
22 | + Insert(cycle *EvaluationCycle) (*EvaluationCycle, error) | ||
23 | + Remove(cycle *EvaluationCycle) (*EvaluationCycle, error) | ||
24 | + FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error) | ||
25 | + Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error) | ||
26 | + Count(queryOptions map[string]interface{}) (int64, error) | ||
27 | +} |
@@ -18,3 +18,11 @@ type EvaluationProject struct { | @@ -18,3 +18,11 @@ type EvaluationProject struct { | ||
18 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` | 18 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` |
19 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` | 19 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` |
20 | } | 20 | } |
21 | + | ||
22 | +type EvaluationProjectRepository interface { | ||
23 | + Insert(project *EvaluationProject) (*EvaluationProject, error) | ||
24 | + Remove(project *EvaluationProject) (*EvaluationProject, error) | ||
25 | + FindOne(queryOptions map[string]interface{}) (*EvaluationProject, error) | ||
26 | + Find(queryOptions map[string]interface{}) (int64, []*EvaluationProject, error) | ||
27 | + Count(queryOptions map[string]interface{}) (int64, error) | ||
28 | +} |
@@ -10,7 +10,7 @@ const ( | @@ -10,7 +10,7 @@ const ( | ||
10 | ) | 10 | ) |
11 | 11 | ||
12 | type Rating struct { | 12 | type Rating struct { |
13 | - Levels []RatingLevel `comment:"配置等级"` | 13 | + Levels []RatingLevel `json:"levels" comment:"配置等级"` |
14 | } | 14 | } |
15 | 15 | ||
16 | type RatingLevel struct { | 16 | type RatingLevel struct { |
@@ -48,3 +48,11 @@ type EvaluationRule struct { | @@ -48,3 +48,11 @@ type EvaluationRule struct { | ||
48 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` | 48 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` |
49 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` | 49 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` |
50 | } | 50 | } |
51 | + | ||
52 | +type EvaluationRuleRepository interface { | ||
53 | + Insert(rule *EvaluationRule) (*EvaluationRule, error) | ||
54 | + Remove(rule *EvaluationRule) (*EvaluationRule, error) | ||
55 | + FindOne(queryOptions map[string]interface{}) (*EvaluationRule, error) | ||
56 | + Find(queryOptions map[string]interface{}) (int64, []*EvaluationRule, error) | ||
57 | + Count(queryOptions map[string]interface{}) (int64, error) | ||
58 | +} |
@@ -68,6 +68,13 @@ type EvaluationTemplate struct { | @@ -68,6 +68,13 @@ type EvaluationTemplate struct { | ||
68 | CreatedAt time.Time `json:"createdAt" comment:"创建时间"` | 68 | CreatedAt time.Time `json:"createdAt" comment:"创建时间"` |
69 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` | 69 | UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"` |
70 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` | 70 | DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` |
71 | - | ||
72 | //Link EvaluationLink `json:"link" comment:"评估流程"` | 71 | //Link EvaluationLink `json:"link" comment:"评估流程"` |
73 | } | 72 | } |
73 | + | ||
74 | +type EvaluationTemplateRepository interface { | ||
75 | + Insert(template *EvaluationTemplate) (*EvaluationTemplate, error) | ||
76 | + Remove(template *EvaluationTemplate) (*EvaluationTemplate, error) | ||
77 | + FindOne(queryOptions map[string]interface{}) (*EvaluationTemplate, error) | ||
78 | + Find(queryOptions map[string]interface{}) (int64, []*EvaluationTemplate, error) | ||
79 | + Count(queryOptions map[string]interface{}) (int64, error) | ||
80 | +} |
1 | package models | 1 | package models |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
4 | "time" | 5 | "time" |
5 | ) | 6 | ) |
6 | 7 | ||
7 | type EvaluationCycle struct { | 8 | type EvaluationCycle struct { |
8 | - tableName struct{} `pg:"evaluation_cycle" comment:"评估周期"` | ||
9 | - Id int64 `pg:"pk:id" comment:"周期ID"` | ||
10 | - Name string `comment:"名称"` | ||
11 | - TimeStart *time.Time `comment:"起始时间"` | ||
12 | - TimeEnd *time.Time `comment:"截至时间"` | ||
13 | - CompanyId int64 `comment:"公司ID"` | ||
14 | - CreatorId int64 `comment:"创建人ID"` | ||
15 | - KpiCycle int `comment:"考核周期(0日、1周、2月)"` | ||
16 | - Template []EvaluationTemplate `comment:"周期使用模板"` | ||
17 | - CreatedAt time.Time `comment:"创建时间"` | ||
18 | - UpdatedAt time.Time `comment:"更新时间"` | ||
19 | - DeletedAt *time.Time `comment:"删除时间"` | 9 | + tableName struct{} `pg:"evaluation_cycle" comment:"评估周期"` |
10 | + Id int64 `pg:"pk:id" comment:"周期ID"` | ||
11 | + Name string `comment:"名称"` | ||
12 | + TimeStart *time.Time `comment:"起始时间"` | ||
13 | + TimeEnd *time.Time `comment:"截至时间"` | ||
14 | + CompanyId int64 `comment:"公司ID"` | ||
15 | + CreatorId int64 `comment:"创建人ID"` | ||
16 | + KpiCycle int `comment:"考核周期(0日、1周、2月)"` | ||
17 | + Template []domain.EvaluationTemplate `comment:"周期使用模板"` | ||
18 | + CreatedAt time.Time `comment:"创建时间"` | ||
19 | + UpdatedAt time.Time `comment:"更新时间"` | ||
20 | + DeletedAt *time.Time `comment:"删除时间"` | ||
20 | } | 21 | } |
1 | package models | 1 | package models |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
4 | "time" | 5 | "time" |
5 | ) | 6 | ) |
6 | 7 | ||
7 | type EvaluationProject struct { | 8 | type EvaluationProject struct { |
8 | - tableName struct{} `pg:"evaluation_project" comment:"评估项目"` | ||
9 | - Id int64 `pg:"pk:id" comment:"ID"` | ||
10 | - Name string `comment:"名称"` | ||
11 | - Describe string `comment:"描述"` | ||
12 | - CompanyId int64 `comment:"公司ID"` | ||
13 | - CreatorId int64 `comment:"创建人ID"` | ||
14 | - HrBp int `comment:"HR角色权限"` | ||
15 | - Pms []string `comment:"项目管理员ID"` | ||
16 | - Recipients []string `comment:"被评估人ID"` | ||
17 | - Template *EvaluationTemplate `comment:"评估模板"` | ||
18 | - CreatedAt time.Time `comment:"创建时间"` | ||
19 | - UpdatedAt time.Time `comment:"更新时间"` | ||
20 | - DeletedAt *time.Time `comment:"删除时间"` | 9 | + tableName struct{} `pg:"evaluation_project" comment:"评估项目"` |
10 | + Id int64 `pg:"pk:id" comment:"ID"` | ||
11 | + Name string `comment:"名称"` | ||
12 | + Describe string `comment:"描述"` | ||
13 | + CompanyId int64 `comment:"公司ID"` | ||
14 | + CreatorId int64 `comment:"创建人ID"` | ||
15 | + HrBp int `comment:"HR角色权限"` | ||
16 | + Pms []string `comment:"项目管理员ID"` | ||
17 | + Recipients []string `comment:"被评估人ID"` | ||
18 | + Template *domain.EvaluationTemplate `comment:"评估模板"` | ||
19 | + CreatedAt time.Time `comment:"创建时间"` | ||
20 | + UpdatedAt time.Time `comment:"更新时间"` | ||
21 | + DeletedAt *time.Time `comment:"删除时间"` | ||
21 | } | 22 | } |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "github.com/go-pg/pg/v10" | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
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 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" | ||
12 | + "time" | ||
13 | +) | ||
14 | + | ||
15 | +type EvaluationCycleRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func NewEvaluationCycleRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationCycleRepository { | ||
20 | + return &EvaluationCycleRepository{transactionContext: transactionContext} | ||
21 | +} | ||
22 | + | ||
23 | +func (repo *EvaluationCycleRepository) TransformToDomain(m *models.EvaluationCycle) domain.EvaluationCycle { | ||
24 | + return domain.EvaluationCycle{ | ||
25 | + Id: m.Id, | ||
26 | + Name: m.Name, | ||
27 | + TimeStart: m.TimeStart, | ||
28 | + TimeEnd: m.TimeEnd, | ||
29 | + CompanyId: m.CompanyId, | ||
30 | + CreatorId: m.CreatorId, | ||
31 | + KpiCycle: m.KpiCycle, | ||
32 | + Template: m.Template, | ||
33 | + CreatedAt: m.CreatedAt, | ||
34 | + UpdatedAt: m.UpdatedAt, | ||
35 | + DeletedAt: m.DeletedAt, | ||
36 | + } | ||
37 | +} | ||
38 | + | ||
39 | +func (repo *EvaluationCycleRepository) TransformToModel(d *domain.EvaluationCycle) models.EvaluationCycle { | ||
40 | + return models.EvaluationCycle{ | ||
41 | + Id: d.Id, | ||
42 | + Name: d.Name, | ||
43 | + TimeStart: d.TimeStart, | ||
44 | + TimeEnd: d.TimeEnd, | ||
45 | + CompanyId: d.CompanyId, | ||
46 | + CreatorId: d.CreatorId, | ||
47 | + KpiCycle: d.KpiCycle, | ||
48 | + Template: d.Template, | ||
49 | + CreatedAt: d.CreatedAt, | ||
50 | + UpdatedAt: d.UpdatedAt, | ||
51 | + DeletedAt: d.DeletedAt, | ||
52 | + } | ||
53 | +} | ||
54 | + | ||
55 | +func (repo *EvaluationCycleRepository) nextIdentify() (int64, error) { | ||
56 | + return utils.NewSnowflakeId() | ||
57 | +} | ||
58 | + | ||
59 | +func (repo *EvaluationCycleRepository) Insert(d *domain.EvaluationCycle) (*domain.EvaluationCycle, error) { | ||
60 | + var isCreate = d.Id == 0 | ||
61 | + if isCreate { | ||
62 | + id, err := repo.nextIdentify() | ||
63 | + if err != nil { | ||
64 | + return d, err | ||
65 | + } | ||
66 | + d.Id = id | ||
67 | + d.CreatedAt = time.Now() | ||
68 | + d.UpdatedAt = d.CreatedAt | ||
69 | + } else { | ||
70 | + d.UpdatedAt = time.Now() | ||
71 | + } | ||
72 | + m := repo.TransformToModel(d) | ||
73 | + tx := repo.transactionContext.PgTx | ||
74 | + var err error | ||
75 | + if isCreate { | ||
76 | + _, err = tx.Model(&m).Returning("id").Insert() | ||
77 | + } else { | ||
78 | + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件 | ||
79 | + } | ||
80 | + if err != nil { | ||
81 | + return nil, err | ||
82 | + } | ||
83 | + d.Id = m.Id | ||
84 | + return d, nil | ||
85 | +} | ||
86 | + | ||
87 | +func (repo *EvaluationCycleRepository) Remove(d *domain.EvaluationCycle) (*domain.EvaluationCycle, error) { | ||
88 | + tx := repo.transactionContext.PgTx | ||
89 | + nowTime := time.Now() | ||
90 | + m := repo.TransformToModel(d) | ||
91 | + m.DeletedAt = &nowTime | ||
92 | + if _, err := tx.Model(&m).WherePK().Update(); err != nil { | ||
93 | + return d, err | ||
94 | + } | ||
95 | + return d, nil | ||
96 | +} | ||
97 | + | ||
98 | +func (repo *EvaluationCycleRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationCycle, error) { | ||
99 | + tx := repo.transactionContext.PgTx | ||
100 | + m := new(models.EvaluationCycle) | ||
101 | + query := tx.Model(m) | ||
102 | + query.Where("deleted_at isnull") | ||
103 | + if id, ok := queryOptions["id"]; ok { | ||
104 | + query.Where("id=?", id) | ||
105 | + } | ||
106 | + if err := query.First(); err != nil { | ||
107 | + if errors.Is(err, pg.ErrNoRows) { | ||
108 | + return nil, fmt.Errorf("没有此资源") | ||
109 | + } else { | ||
110 | + return nil, err | ||
111 | + } | ||
112 | + } | ||
113 | + u := repo.TransformToDomain(m) | ||
114 | + return &u, nil | ||
115 | +} | ||
116 | + | ||
117 | +func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationCycle, error) { | ||
118 | + tx := repo.transactionContext.PgTx | ||
119 | + var m []*models.EvaluationCycle | ||
120 | + query := tx.Model(&m). | ||
121 | + Where("deleted_at isnull"). | ||
122 | + Limit(20) | ||
123 | + | ||
124 | + if name, ok := queryOptions["name"]; ok { | ||
125 | + query.Where("name = ?", name) | ||
126 | + } | ||
127 | + | ||
128 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
129 | + query.Where("company_id = ?", companyId) | ||
130 | + } | ||
131 | + | ||
132 | + if v, ok := queryOptions["limit"].(int); ok { | ||
133 | + query.Limit(v) | ||
134 | + } | ||
135 | + if v, ok := queryOptions["offset"].(int); ok { | ||
136 | + query.Offset(v) | ||
137 | + } | ||
138 | + | ||
139 | + count, err := query.SelectAndCount() | ||
140 | + if err != nil { | ||
141 | + return 0, nil, err | ||
142 | + } | ||
143 | + var arrays []*domain.EvaluationCycle | ||
144 | + for _, v := range m { | ||
145 | + d := repo.TransformToDomain(v) | ||
146 | + arrays = append(arrays, &d) | ||
147 | + } | ||
148 | + return int64(count), arrays, nil | ||
149 | +} | ||
150 | + | ||
151 | +func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{}) (int64, error) { | ||
152 | + tx := repo.transactionContext.PgTx | ||
153 | + m := new(models.EvaluationCycle) | ||
154 | + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) | ||
155 | + query.Where("deleted_at isnull") | ||
156 | + | ||
157 | + if id, ok := queryOptions["id"]; ok { | ||
158 | + query.Where("id = ?", id) | ||
159 | + } | ||
160 | + | ||
161 | + if notId, ok := queryOptions["notId"]; ok { | ||
162 | + query.Where("id != ?", notId) | ||
163 | + } | ||
164 | + | ||
165 | + if name, ok := queryOptions["name"]; ok { | ||
166 | + query.Where("name = ?", name) | ||
167 | + } | ||
168 | + | ||
169 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
170 | + query.Where("company_id = ?", companyId) | ||
171 | + } | ||
172 | + | ||
173 | + count, err := query.Count() | ||
174 | + if err != nil { | ||
175 | + return 0, err | ||
176 | + } | ||
177 | + return int64(count), nil | ||
178 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "github.com/go-pg/pg/v10" | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
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 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" | ||
12 | + "time" | ||
13 | +) | ||
14 | + | ||
15 | +type EvaluationProjectRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func NewEvaluationProjectRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationProjectRepository { | ||
20 | + return &EvaluationProjectRepository{transactionContext: transactionContext} | ||
21 | +} | ||
22 | + | ||
23 | +func (repo *EvaluationProjectRepository) TransformToDomain(m *models.EvaluationProject) domain.EvaluationProject { | ||
24 | + return domain.EvaluationProject{ | ||
25 | + Id: m.Id, | ||
26 | + Name: m.Name, | ||
27 | + Describe: m.Describe, | ||
28 | + CompanyId: m.CompanyId, | ||
29 | + CreatorId: m.CreatorId, | ||
30 | + HrBp: m.HrBp, | ||
31 | + Pms: m.Pms, | ||
32 | + Recipients: m.Recipients, | ||
33 | + Template: m.Template, | ||
34 | + CreatedAt: m.CreatedAt, | ||
35 | + UpdatedAt: m.UpdatedAt, | ||
36 | + DeletedAt: m.DeletedAt, | ||
37 | + } | ||
38 | +} | ||
39 | + | ||
40 | +func (repo *EvaluationProjectRepository) TransformToModel(d *domain.EvaluationProject) models.EvaluationProject { | ||
41 | + return models.EvaluationProject{ | ||
42 | + Id: d.Id, | ||
43 | + Name: d.Name, | ||
44 | + Describe: d.Describe, | ||
45 | + CompanyId: d.CompanyId, | ||
46 | + CreatorId: d.CreatorId, | ||
47 | + HrBp: d.HrBp, | ||
48 | + Pms: d.Pms, | ||
49 | + Recipients: d.Recipients, | ||
50 | + Template: d.Template, | ||
51 | + CreatedAt: d.CreatedAt, | ||
52 | + UpdatedAt: d.UpdatedAt, | ||
53 | + DeletedAt: d.DeletedAt, | ||
54 | + } | ||
55 | +} | ||
56 | + | ||
57 | +func (repo *EvaluationProjectRepository) nextIdentify() (int64, error) { | ||
58 | + return utils.NewSnowflakeId() | ||
59 | +} | ||
60 | + | ||
61 | +func (repo *EvaluationProjectRepository) Insert(d *domain.EvaluationProject) (*domain.EvaluationProject, error) { | ||
62 | + var isCreate = d.Id == 0 | ||
63 | + if isCreate { | ||
64 | + id, err := repo.nextIdentify() | ||
65 | + if err != nil { | ||
66 | + return d, err | ||
67 | + } | ||
68 | + d.Id = id | ||
69 | + d.CreatedAt = time.Now() | ||
70 | + d.UpdatedAt = d.CreatedAt | ||
71 | + } else { | ||
72 | + d.UpdatedAt = time.Now() | ||
73 | + } | ||
74 | + m := repo.TransformToModel(d) | ||
75 | + tx := repo.transactionContext.PgTx | ||
76 | + var err error | ||
77 | + if isCreate { | ||
78 | + _, err = tx.Model(&m).Returning("id").Insert() | ||
79 | + } else { | ||
80 | + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件 | ||
81 | + } | ||
82 | + if err != nil { | ||
83 | + return nil, err | ||
84 | + } | ||
85 | + d.Id = m.Id | ||
86 | + return d, nil | ||
87 | +} | ||
88 | + | ||
89 | +func (repo *EvaluationProjectRepository) Remove(d *domain.EvaluationProject) (*domain.EvaluationProject, error) { | ||
90 | + tx := repo.transactionContext.PgTx | ||
91 | + nowTime := time.Now() | ||
92 | + m := repo.TransformToModel(d) | ||
93 | + m.DeletedAt = &nowTime | ||
94 | + if _, err := tx.Model(&m).WherePK().Update(); err != nil { | ||
95 | + return d, err | ||
96 | + } | ||
97 | + return d, nil | ||
98 | +} | ||
99 | + | ||
100 | +func (repo *EvaluationProjectRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationProject, error) { | ||
101 | + tx := repo.transactionContext.PgTx | ||
102 | + m := new(models.EvaluationProject) | ||
103 | + query := tx.Model(m) | ||
104 | + query.Where("deleted_at isnull") | ||
105 | + if id, ok := queryOptions["id"]; ok { | ||
106 | + query.Where("id=?", id) | ||
107 | + } | ||
108 | + if err := query.First(); err != nil { | ||
109 | + if errors.Is(err, pg.ErrNoRows) { | ||
110 | + return nil, fmt.Errorf("没有此资源") | ||
111 | + } else { | ||
112 | + return nil, err | ||
113 | + } | ||
114 | + } | ||
115 | + u := repo.TransformToDomain(m) | ||
116 | + return &u, nil | ||
117 | +} | ||
118 | + | ||
119 | +func (repo *EvaluationProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationProject, error) { | ||
120 | + tx := repo.transactionContext.PgTx | ||
121 | + var m []*models.EvaluationProject | ||
122 | + query := tx.Model(&m). | ||
123 | + Where("deleted_at isnull"). | ||
124 | + Limit(20) | ||
125 | + | ||
126 | + if name, ok := queryOptions["name"]; ok { | ||
127 | + query.Where("name = ?", name) | ||
128 | + } | ||
129 | + | ||
130 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
131 | + query.Where("company_id = ?", companyId) | ||
132 | + } | ||
133 | + | ||
134 | + if v, ok := queryOptions["limit"].(int); ok { | ||
135 | + query.Limit(v) | ||
136 | + } | ||
137 | + if v, ok := queryOptions["offset"].(int); ok { | ||
138 | + query.Offset(v) | ||
139 | + } | ||
140 | + | ||
141 | + count, err := query.SelectAndCount() | ||
142 | + if err != nil { | ||
143 | + return 0, nil, err | ||
144 | + } | ||
145 | + var arrays []*domain.EvaluationProject | ||
146 | + for _, v := range m { | ||
147 | + d := repo.TransformToDomain(v) | ||
148 | + arrays = append(arrays, &d) | ||
149 | + } | ||
150 | + return int64(count), arrays, nil | ||
151 | +} | ||
152 | + | ||
153 | +func (repo *EvaluationProjectRepository) Count(queryOptions map[string]interface{}) (int64, error) { | ||
154 | + tx := repo.transactionContext.PgTx | ||
155 | + m := new(models.EvaluationProject) | ||
156 | + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) | ||
157 | + query.Where("deleted_at isnull") | ||
158 | + | ||
159 | + if id, ok := queryOptions["id"]; ok { | ||
160 | + query.Where("id = ?", id) | ||
161 | + } | ||
162 | + | ||
163 | + if notId, ok := queryOptions["notId"]; ok { | ||
164 | + query.Where("id != ?", notId) | ||
165 | + } | ||
166 | + | ||
167 | + if name, ok := queryOptions["name"]; ok { | ||
168 | + query.Where("name = ?", name) | ||
169 | + } | ||
170 | + | ||
171 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
172 | + query.Where("company_id = ?", companyId) | ||
173 | + } | ||
174 | + | ||
175 | + count, err := query.Count() | ||
176 | + if err != nil { | ||
177 | + return 0, err | ||
178 | + } | ||
179 | + return int64(count), nil | ||
180 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "github.com/go-pg/pg/v10" | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
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 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" | ||
12 | + "time" | ||
13 | +) | ||
14 | + | ||
15 | +type EvaluationRuleRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func NewEvaluationRuleRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationRuleRepository { | ||
20 | + return &EvaluationRuleRepository{transactionContext: transactionContext} | ||
21 | +} | ||
22 | + | ||
23 | +func (repo *EvaluationRuleRepository) TransformToDomain(m *models.EvaluationRule) domain.EvaluationRule { | ||
24 | + return domain.EvaluationRule{ | ||
25 | + Id: m.Id, | ||
26 | + Name: m.Name, | ||
27 | + Remark: m.Remark, | ||
28 | + CompanyId: m.CompanyId, | ||
29 | + CreatorId: m.CreatorId, | ||
30 | + Type: m.Type, | ||
31 | + Rating: m.Rating, | ||
32 | + Score: m.Score, | ||
33 | + CreatedAt: m.CreatedAt, | ||
34 | + UpdatedAt: m.UpdatedAt, | ||
35 | + DeletedAt: m.DeletedAt, | ||
36 | + } | ||
37 | +} | ||
38 | + | ||
39 | +func (repo *EvaluationRuleRepository) TransformToModel(d *domain.EvaluationRule) models.EvaluationRule { | ||
40 | + return models.EvaluationRule{ | ||
41 | + Id: d.Id, | ||
42 | + Name: d.Name, | ||
43 | + Remark: d.Remark, | ||
44 | + CompanyId: d.CompanyId, | ||
45 | + CreatorId: d.CreatorId, | ||
46 | + Type: d.Type, | ||
47 | + Rating: d.Rating, | ||
48 | + Score: d.Score, | ||
49 | + CreatedAt: d.CreatedAt, | ||
50 | + UpdatedAt: d.UpdatedAt, | ||
51 | + DeletedAt: d.DeletedAt, | ||
52 | + } | ||
53 | +} | ||
54 | + | ||
55 | +func (repo *EvaluationRuleRepository) nextIdentify() (int64, error) { | ||
56 | + return utils.NewSnowflakeId() | ||
57 | +} | ||
58 | + | ||
59 | +func (repo *EvaluationRuleRepository) Insert(d *domain.EvaluationRule) (*domain.EvaluationRule, error) { | ||
60 | + var isCreate = d.Id == 0 | ||
61 | + if isCreate { | ||
62 | + id, err := repo.nextIdentify() | ||
63 | + if err != nil { | ||
64 | + return d, err | ||
65 | + } | ||
66 | + d.Id = id | ||
67 | + d.CreatedAt = time.Now() | ||
68 | + d.UpdatedAt = d.CreatedAt | ||
69 | + } else { | ||
70 | + d.UpdatedAt = time.Now() | ||
71 | + } | ||
72 | + m := repo.TransformToModel(d) | ||
73 | + tx := repo.transactionContext.PgTx | ||
74 | + var err error | ||
75 | + if isCreate { | ||
76 | + _, err = tx.Model(&m).Returning("id").Insert() | ||
77 | + } else { | ||
78 | + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件 | ||
79 | + } | ||
80 | + if err != nil { | ||
81 | + return nil, err | ||
82 | + } | ||
83 | + d.Id = m.Id | ||
84 | + return d, nil | ||
85 | +} | ||
86 | + | ||
87 | +func (repo *EvaluationRuleRepository) Remove(d *domain.EvaluationRule) (*domain.EvaluationRule, error) { | ||
88 | + tx := repo.transactionContext.PgTx | ||
89 | + nowTime := time.Now() | ||
90 | + m := repo.TransformToModel(d) | ||
91 | + m.DeletedAt = &nowTime | ||
92 | + if _, err := tx.Model(&m).WherePK().Update(); err != nil { | ||
93 | + return d, err | ||
94 | + } | ||
95 | + return d, nil | ||
96 | +} | ||
97 | + | ||
98 | +func (repo *EvaluationRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationRule, error) { | ||
99 | + tx := repo.transactionContext.PgTx | ||
100 | + m := new(models.EvaluationRule) | ||
101 | + query := tx.Model(m) | ||
102 | + query.Where("deleted_at isnull") | ||
103 | + if id, ok := queryOptions["id"]; ok { | ||
104 | + query.Where("id=?", id) | ||
105 | + } | ||
106 | + if err := query.First(); err != nil { | ||
107 | + if errors.Is(err, pg.ErrNoRows) { | ||
108 | + return nil, fmt.Errorf("没有此资源") | ||
109 | + } else { | ||
110 | + return nil, err | ||
111 | + } | ||
112 | + } | ||
113 | + u := repo.TransformToDomain(m) | ||
114 | + return &u, nil | ||
115 | +} | ||
116 | + | ||
117 | +func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationRule, error) { | ||
118 | + tx := repo.transactionContext.PgTx | ||
119 | + var m []*models.EvaluationRule | ||
120 | + query := tx.Model(&m).Where("deleted_at isnull").Limit(20) | ||
121 | + | ||
122 | + if name, ok := queryOptions["name"]; ok { | ||
123 | + query.Where("name = ?", name) | ||
124 | + } | ||
125 | + | ||
126 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
127 | + query.Where("company_id = ?", companyId) | ||
128 | + } | ||
129 | + | ||
130 | + if v, ok := queryOptions["limit"].(int); ok { | ||
131 | + query.Limit(v) | ||
132 | + } | ||
133 | + if v, ok := queryOptions["offset"].(int); ok { | ||
134 | + query.Offset(v) | ||
135 | + } | ||
136 | + | ||
137 | + query.Count() | ||
138 | + count, err := query.SelectAndCount() | ||
139 | + if err != nil { | ||
140 | + return 0, nil, err | ||
141 | + } | ||
142 | + var arrays []*domain.EvaluationRule | ||
143 | + for _, v := range m { | ||
144 | + d := repo.TransformToDomain(v) | ||
145 | + arrays = append(arrays, &d) | ||
146 | + } | ||
147 | + return int64(count), arrays, nil | ||
148 | +} | ||
149 | + | ||
150 | +func (repo *EvaluationRuleRepository) Count(queryOptions map[string]interface{}) (int64, error) { | ||
151 | + tx := repo.transactionContext.PgTx | ||
152 | + m := new(models.EvaluationRule) | ||
153 | + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) | ||
154 | + query.Where("deleted_at isnull") | ||
155 | + | ||
156 | + if id, ok := queryOptions["id"]; ok { | ||
157 | + query.Where("id = ?", id) | ||
158 | + } | ||
159 | + | ||
160 | + if notId, ok := queryOptions["notId"]; ok { | ||
161 | + query.Where("id != ?", notId) | ||
162 | + } | ||
163 | + | ||
164 | + if name, ok := queryOptions["name"]; ok { | ||
165 | + query.Where("name = ?", name) | ||
166 | + } | ||
167 | + | ||
168 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
169 | + query.Where("company_id = ?", companyId) | ||
170 | + } | ||
171 | + | ||
172 | + count, err := query.Count() | ||
173 | + if err != nil { | ||
174 | + return 0, err | ||
175 | + } | ||
176 | + return int64(count), nil | ||
177 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "errors" | ||
5 | + "fmt" | ||
6 | + "github.com/go-pg/pg/v10" | ||
7 | + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" | ||
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 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" | ||
12 | + "time" | ||
13 | +) | ||
14 | + | ||
15 | +type EvaluationTemplateRepository struct { | ||
16 | + transactionContext *pgTransaction.TransactionContext | ||
17 | +} | ||
18 | + | ||
19 | +func NewEvaluationTemplateRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationTemplateRepository { | ||
20 | + return &EvaluationTemplateRepository{transactionContext: transactionContext} | ||
21 | +} | ||
22 | + | ||
23 | +func (repo *EvaluationTemplateRepository) TransformToDomain(m *models.EvaluationTemplate) domain.EvaluationTemplate { | ||
24 | + return domain.EvaluationTemplate{ | ||
25 | + Id: m.Id, | ||
26 | + Name: m.Name, | ||
27 | + Describe: m.Describe, | ||
28 | + CompanyId: m.CompanyId, | ||
29 | + CreatorId: m.CreatorId, | ||
30 | + State: m.State, | ||
31 | + Link: m.Link, | ||
32 | + CreatedAt: m.CreatedAt, | ||
33 | + UpdatedAt: m.UpdatedAt, | ||
34 | + DeletedAt: m.DeletedAt, | ||
35 | + } | ||
36 | +} | ||
37 | + | ||
38 | +func (repo *EvaluationTemplateRepository) TransformToModel(d *domain.EvaluationTemplate) models.EvaluationTemplate { | ||
39 | + return models.EvaluationTemplate{ | ||
40 | + Id: d.Id, | ||
41 | + Name: d.Name, | ||
42 | + Describe: d.Describe, | ||
43 | + CompanyId: d.CompanyId, | ||
44 | + CreatorId: d.CreatorId, | ||
45 | + State: d.State, | ||
46 | + Link: d.Link, | ||
47 | + CreatedAt: d.CreatedAt, | ||
48 | + UpdatedAt: d.UpdatedAt, | ||
49 | + DeletedAt: d.DeletedAt, | ||
50 | + } | ||
51 | +} | ||
52 | + | ||
53 | +func (repo *EvaluationTemplateRepository) nextIdentify() (int64, error) { | ||
54 | + return utils.NewSnowflakeId() | ||
55 | +} | ||
56 | + | ||
57 | +func (repo *EvaluationTemplateRepository) Insert(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, error) { | ||
58 | + var isCreate = d.Id == 0 | ||
59 | + if isCreate { | ||
60 | + id, err := repo.nextIdentify() | ||
61 | + if err != nil { | ||
62 | + return d, err | ||
63 | + } | ||
64 | + d.Id = id | ||
65 | + d.CreatedAt = time.Now() | ||
66 | + d.UpdatedAt = d.CreatedAt | ||
67 | + } else { | ||
68 | + d.UpdatedAt = time.Now() | ||
69 | + } | ||
70 | + m := repo.TransformToModel(d) | ||
71 | + tx := repo.transactionContext.PgTx | ||
72 | + var err error | ||
73 | + if isCreate { | ||
74 | + _, err = tx.Model(&m).Returning("id").Insert() | ||
75 | + } else { | ||
76 | + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件 | ||
77 | + } | ||
78 | + if err != nil { | ||
79 | + return nil, err | ||
80 | + } | ||
81 | + d.Id = m.Id | ||
82 | + return d, nil | ||
83 | +} | ||
84 | + | ||
85 | +func (repo *EvaluationTemplateRepository) Remove(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, error) { | ||
86 | + tx := repo.transactionContext.PgTx | ||
87 | + nowTime := time.Now() | ||
88 | + m := repo.TransformToModel(d) | ||
89 | + m.DeletedAt = &nowTime | ||
90 | + if _, err := tx.Model(&m).WherePK().Update(); err != nil { | ||
91 | + return d, err | ||
92 | + } | ||
93 | + return d, nil | ||
94 | +} | ||
95 | + | ||
96 | +func (repo *EvaluationTemplateRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationTemplate, error) { | ||
97 | + tx := repo.transactionContext.PgTx | ||
98 | + m := new(models.EvaluationTemplate) | ||
99 | + query := tx.Model(m) | ||
100 | + query.Where("deleted_at isnull") | ||
101 | + if id, ok := queryOptions["id"]; ok { | ||
102 | + query.Where("id=?", id) | ||
103 | + } | ||
104 | + if err := query.First(); err != nil { | ||
105 | + if errors.Is(err, pg.ErrNoRows) { | ||
106 | + return nil, fmt.Errorf("没有此资源") | ||
107 | + } else { | ||
108 | + return nil, err | ||
109 | + } | ||
110 | + } | ||
111 | + u := repo.TransformToDomain(m) | ||
112 | + return &u, nil | ||
113 | +} | ||
114 | + | ||
115 | +func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationTemplate, error) { | ||
116 | + tx := repo.transactionContext.PgTx | ||
117 | + var m []*models.EvaluationTemplate | ||
118 | + query := tx.Model(&m). | ||
119 | + Where("deleted_at isnull"). | ||
120 | + Limit(20) | ||
121 | + | ||
122 | + if name, ok := queryOptions["name"]; ok { | ||
123 | + query.Where("name = ?", name) | ||
124 | + } | ||
125 | + | ||
126 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
127 | + query.Where("company_id = ?", companyId) | ||
128 | + } | ||
129 | + | ||
130 | + if v, ok := queryOptions["limit"].(int); ok { | ||
131 | + query.Limit(v) | ||
132 | + } | ||
133 | + if v, ok := queryOptions["offset"].(int); ok { | ||
134 | + query.Offset(v) | ||
135 | + } | ||
136 | + | ||
137 | + count, err := query.SelectAndCount() | ||
138 | + if err != nil { | ||
139 | + return 0, nil, err | ||
140 | + } | ||
141 | + var arrays []*domain.EvaluationTemplate | ||
142 | + for _, v := range m { | ||
143 | + d := repo.TransformToDomain(v) | ||
144 | + arrays = append(arrays, &d) | ||
145 | + } | ||
146 | + return int64(count), arrays, nil | ||
147 | +} | ||
148 | + | ||
149 | +func (repo *EvaluationTemplateRepository) Count(queryOptions map[string]interface{}) (int64, error) { | ||
150 | + tx := repo.transactionContext.PgTx | ||
151 | + m := new(models.EvaluationTemplate) | ||
152 | + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) | ||
153 | + query.Where("deleted_at isnull") | ||
154 | + | ||
155 | + if id, ok := queryOptions["id"]; ok { | ||
156 | + query.Where("id = ?", id) | ||
157 | + } | ||
158 | + | ||
159 | + if notId, ok := queryOptions["notId"]; ok { | ||
160 | + query.Where("id != ?", notId) | ||
161 | + } | ||
162 | + | ||
163 | + if name, ok := queryOptions["name"]; ok { | ||
164 | + query.Where("name = ?", name) | ||
165 | + } | ||
166 | + | ||
167 | + if companyId, ok := queryOptions["companyId"]; ok { | ||
168 | + query.Where("company_id = ?", companyId) | ||
169 | + } | ||
170 | + | ||
171 | + count, err := query.Count() | ||
172 | + if err != nil { | ||
173 | + return 0, err | ||
174 | + } | ||
175 | + return int64(count), nil | ||
176 | +} |
-
请 注册 或 登录 后发表评论