-
Notifications
You must be signed in to change notification settings - Fork 66
/
plan.go
495 lines (413 loc) · 14.2 KB
/
plan.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package repo
import (
"fmt"
v1 "github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
serverConsts "github.com/aaronchen2k/deeptest/internal/server/consts"
"github.com/aaronchen2k/deeptest/internal/server/core/dao"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/pkg/domain"
commonUtils "github.com/aaronchen2k/deeptest/pkg/lib/comm"
logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
"go.uber.org/zap"
"gorm.io/gorm"
"strconv"
"strings"
)
type PlanRepo struct {
DB *gorm.DB `inject:""`
*BaseRepo `inject:""`
ProjectRepo *ProjectRepo `inject:""`
UserRepo *UserRepo `inject:""`
PlanReportRepo *PlanReportRepo `inject:""`
RelaPlanScenarioRepo *RelaPlanScenarioRepo `inject:""`
}
func (r *PlanRepo) Paginate(tenantId consts.TenantId, req v1.PlanReqPaginate, projectId int) (data _domain.PageData, err error) {
var count int64
var categoryIds []uint
if req.CategoryId > 0 {
categoryIds, err = r.BaseRepo.GetDescendantIds(tenantId, uint(req.CategoryId), model.Category{}.TableName(),
serverConsts.PlanCategory, projectId)
if err != nil {
return
}
}
db := r.GetDB(tenantId).Model(&model.Plan{}).
Where("project_id = ? AND NOT deleted",
projectId)
if len(categoryIds) > 0 {
db.Where("category_id IN(?)", categoryIds)
} else if req.CategoryId == -1 {
db.Where("category_id IN(?)", -1)
}
if req.Keywords != "" {
db = db.Where("name LIKE ?", fmt.Sprintf("%%%s%%", req.Keywords))
}
if req.Enabled != "" {
db = db.Where("disabled = ?", commonUtils.IsDisable(req.Enabled))
}
if req.Status != "" {
db = db.Where("status IN (?)", strings.Split(req.Status, ","))
}
if req.AdminId != "" {
db = db.Where("admin_id IN (?)", strings.Split(req.AdminId, ","))
}
err = db.Count(&count).Error
if err != nil {
logUtils.Errorf("count scenario error", zap.String("error:", err.Error()))
return
}
plans := make([]*model.Plan, 0)
err = db.
Scopes(dao.PaginateScope(req.Page, req.PageSize, req.Order, req.Field)).
Find(&plans).Error
if err != nil {
logUtils.Errorf("query scenario error", zap.String("error:", err.Error()))
return
}
r.CombinePassRate(tenantId, plans)
r.CombineUserName(tenantId, plans)
data.Populate(plans, count, req.Page, req.PageSize)
return
}
func (r *PlanRepo) CombineUserName(tenantId consts.TenantId, data []*model.Plan) {
userIds := make([]uint, 0)
for _, v := range data {
userIds = append(userIds, v.AdminId)
userIds = append(userIds, v.UpdateUserId)
userIds = append(userIds, v.CreateUserId)
}
userIds = commonUtils.ArrayRemoveUintDuplication(userIds)
users, _ := r.UserRepo.FindByIds(tenantId, userIds)
userIdNameMap := make(map[uint]string)
for _, v := range users {
userIdNameMap[v.ID] = v.Name
}
for _, v := range data {
if adminName, ok := userIdNameMap[v.AdminId]; ok {
v.AdminName = adminName
}
if updateUserName, ok := userIdNameMap[v.UpdateUserId]; ok {
v.UpdateUserName = updateUserName
}
if createUserName, ok := userIdNameMap[v.CreateUserId]; ok {
v.CreateUserName = createUserName
}
}
}
func (r *PlanRepo) CombinePassRate(tenantId consts.TenantId, data []*model.Plan) {
for _, v := range data {
planReport, err := r.PlanReportRepo.GetLastByPlanId(tenantId, v.ID)
if err != nil && err != gorm.ErrRecordNotFound {
logUtils.Errorf("get plan report err", zap.String("error:", err.Error()))
continue
}
if planReport.ID == 0 {
v.TestPassRate = "n/a"
continue
}
if planReport.PassScenarioNum == 0 || planReport.TotalScenarioNum == 0 {
v.TestPassRate = "0%"
} else {
v.TestPassRate = strconv.Itoa(planReport.PassScenarioNum*100/planReport.TotalScenarioNum) + "%"
}
}
}
func (r *PlanRepo) Get(tenantId consts.TenantId, id uint) (scenario model.Plan, err error) {
err = r.GetDB(tenantId).Model(&model.Plan{}).Where("id = ?", id).First(&scenario).Error
if err != nil {
logUtils.Errorf("find scenario by id error", zap.String("error:", err.Error()))
return scenario, err
}
return scenario, nil
}
func (r *PlanRepo) FindByName(tenantId consts.TenantId, scenarioName string, id uint) (scenario model.Plan, err error) {
db := r.GetDB(tenantId).Model(&model.Plan{}).
Where("name = ? AND NOT deleted", scenarioName)
if id > 0 {
db.Where("id != ?", id)
}
db.First(&scenario)
return
}
func (r *PlanRepo) Create(tenantId consts.TenantId, scenario model.Plan) (ret model.Plan, bizErr *_domain.BizErr) {
//po, err := r.FindExpressionByName(scenario.Name, 0)
//if po.Name != "" {
// bizErr = &_domain.BizErr{Code: _domain.ErrNameExist.Code}
// return
//}
err := r.GetDB(tenantId).Model(&model.Plan{}).Create(&scenario).Error
if err != nil {
logUtils.Errorf("add scenario error", zap.String("error:", err.Error()))
bizErr = &_domain.BizErr{Code: _domain.SystemErr.Code}
return
}
err = r.UpdateSerialNumber(tenantId, scenario.ID, scenario.ProjectId)
if err != nil {
logUtils.Errorf("add plan serial number error", zap.String("error:", err.Error()))
bizErr = &_domain.BizErr{Code: _domain.SystemErr.Code}
return
}
ret = scenario
return
}
func (r *PlanRepo) Update(tenantId consts.TenantId, req model.Plan) error {
values := map[string]interface{}{
"name": req.Name,
"desc": req.Desc,
"status": req.Status,
"admin_id": req.AdminId,
"category_id": req.CategoryId,
"test_stage": req.TestStage,
"update_user_id": req.UpdateUserId,
"disabled": req.Disabled,
}
err := r.GetDB(tenantId).Model(&req).Where("id = ?", req.ID).Updates(values).Error
if err != nil {
logUtils.Errorf("update scenario error", zap.String("error:", err.Error()))
return err
}
return nil
}
func (r *PlanRepo) DeleteById(tenantId consts.TenantId, id uint) (err error) {
err = r.GetDB(tenantId).Model(&model.Plan{}).Where("id = ?", id).
Updates(map[string]interface{}{"deleted": true}).Error
if err != nil {
logUtils.Errorf("delete scenario by id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *PlanRepo) DeleteChildren(tenantId consts.TenantId, ids []int, tx *gorm.DB) (err error) {
err = tx.Model(&model.Plan{}).Where("id IN (?)", ids).
Updates(map[string]interface{}{"deleted": true}).Error
if err != nil {
logUtils.Errorf("batch delete scenario error", zap.String("error:", err.Error()))
return err
}
return nil
}
func (r *PlanRepo) GetChildrenIds(tenantId consts.TenantId, id uint) (ids []int, err error) {
tmpl := `
WITH RECURSIVE scenario AS (
SELECT * FROM biz_scenario WHERE id = %d
UNION ALL
SELECT child.* FROM biz_scenario child, scenario WHERE child.parent_id = scenario.id
)
SELECT id FROM scenario WHERE id != %d
`
sql := fmt.Sprintf(tmpl, id, id)
err = r.GetDB(tenantId).Raw(sql).Scan(&ids).Error
if err != nil {
logUtils.Errorf("get children scenario error", zap.String("error:", err.Error()))
return
}
return
}
func (r *PlanRepo) AddScenarios(tenantId consts.TenantId, planId uint, scenarioIds []uint) (err error) {
relations, _ := r.ListScenarioRelation(tenantId, planId)
existMap := map[uint]bool{}
for _, item := range relations {
existMap[item.ScenarioId] = true
}
var pos []model.RelaPlanScenario
for _, id := range scenarioIds {
if existMap[id] {
continue
}
po := model.RelaPlanScenario{
PlanId: planId,
ScenarioId: id,
}
pos = append(pos, po)
}
if len(pos) == 0 {
return
}
for _, po := range pos {
po.Ordr = r.RelaPlanScenarioRepo.GetMaxOrder(tenantId, planId)
err = r.GetDB(tenantId).Create(&po).Error
if err != nil {
return
}
}
return
}
func (r *PlanRepo) ListScenario(tenantId consts.TenantId, id uint) (pos []model.Scenario, err error) {
relations, _ := r.ListScenarioRelation(tenantId, id)
var scenarioIds []uint
for _, item := range relations {
scenarioIds = append(scenarioIds, item.ScenarioId)
}
if len(scenarioIds) == 0 {
return
}
err = r.GetDB(tenantId).Model(model.Scenario{}).
Where("id IN (?)", scenarioIds).
Where("NOT deleted").Order(fmt.Sprintf("field(id,%s)", commonUtils.UintArrToStr(scenarioIds))).
Find(&pos).Error
return
}
func (r *PlanRepo) ListScenarioRelation(tenantId consts.TenantId, id uint) (pos []model.RelaPlanScenario, err error) {
err = r.GetDB(tenantId).Model(model.RelaPlanScenario{}).
Where("plan_id=?", id).
Where("NOT deleted").Order("ordr asc").
Find(&pos).Error
return
}
func (r *PlanRepo) RemoveScenario(tenantId consts.TenantId, planId int, scenarioId int) (err error) {
r.GetDB(tenantId).Where("plan_id = ? && scenario_id = ?", planId, scenarioId).Delete(&model.RelaPlanScenario{})
return
}
func (r *PlanRepo) RemoveScenarios(tenantId consts.TenantId, planId int, scenarioIds []uint) (err error) {
r.GetDB(tenantId).Where("plan_id = ? && scenario_id In (?)", planId, scenarioIds).Delete(&model.RelaPlanScenario{})
return
}
func (r *PlanRepo) UpdateSerialNumber(tenantId consts.TenantId, id, projectId uint) (err error) {
var project model.Project
project, err = r.ProjectRepo.Get(tenantId, projectId)
if err != nil {
return
}
err = r.GetDB(tenantId).Model(&model.Plan{}).Where("id=?", id).Update("serial_number", project.ShortName+"-TP-"+strconv.Itoa(int(id))).Error
return
}
func (r *PlanRepo) StatusDropDownOptions() map[consts.TestStatus]string {
return map[consts.TestStatus]string{
consts.Draft: "草稿",
consts.Disabled: "已禁用",
consts.ToExecute: "待执行",
consts.Executed: "已执行",
}
}
func (r *PlanRepo) TestStageDropDownOptions() map[consts.TestStage]string {
return map[consts.TestStage]string{
consts.UintTest: "单元测试",
consts.IntegrationTest: "集成测试",
consts.SystemTest: "系统测试",
consts.AcceptanceTest: "验收测试",
}
}
func (r *PlanRepo) PlanScenariosPaginate(tenantId consts.TenantId, req v1.PlanScenariosReqPaginate, planId uint) (data _domain.PageData, err error) {
var count int64
db := r.GetDB(tenantId).Model(&model.Scenario{}).
Select("biz_scenario.*, c.name category_name,r.id ref_id").
Joins("LEFT JOIN biz_plan_scenario_r r ON biz_scenario.id=r.scenario_id").
Joins("LEFT JOIN biz_category c ON biz_scenario.category_id=c.id").
Where("r.plan_id = ? AND NOT biz_scenario.deleted AND NOT r.deleted", planId)
if req.Keywords != "" {
db = db.Where("biz_scenario.name LIKE ?", fmt.Sprintf("%%%s%%", req.Keywords))
}
if req.Enabled != "" {
db = db.Where("biz_scenario.disabled = ?", commonUtils.IsDisable(req.Enabled))
}
if req.Priority != "" {
db = db.Where("biz_scenario.priority = ?", req.Priority)
}
if req.CreateUserId != 0 {
db = db.Where("biz_scenario.create_user_id = ?", req.CreateUserId)
}
err = db.Count(&count).Error
if err != nil {
logUtils.Errorf("count scenario error", zap.String("error:", err.Error()))
return
}
scenarios := make([]*model.ScenarioDetail, 0)
req.Field, req.Order = "r.ordr", "asc"
err = db.
Scopes(dao.PaginateScope(req.Page, req.PageSize, req.Order, req.Field)).
Find(&scenarios).Error
if err != nil {
logUtils.Errorf("query scenario error", zap.String("error:", err.Error()))
return
}
data.Populate(scenarios, count, req.Page, req.PageSize)
return
}
func (r *PlanRepo) GetScenarioNumByPlan(tenantId consts.TenantId, planId uint) (num int64, err error) {
err = r.GetDB(tenantId).Model(model.RelaPlanScenario{}).
Where("plan_id = ? AND NOT deleted", planId).
Count(&num).Error
return
}
func (r *PlanRepo) NotRelationScenarioList(tenantId consts.TenantId, req v1.NotRelationScenarioReqPaginate, projectId int) (data _domain.PageData, err error) {
relations, _ := r.ListScenarioRelation(tenantId, req.PlanId)
var scenarioIds []uint
for _, v := range relations {
scenarioIds = append(scenarioIds, v.ScenarioId)
}
var count int64
db := r.GetDB(tenantId).Model(&model.Scenario{}).
Where("project_id = ? AND NOT deleted",
projectId)
if req.Keywords != "" {
db = db.Where("name LIKE ?", fmt.Sprintf("%%%s%%", req.Keywords))
}
if req.Enabled != "" {
db = db.Where("disabled = ?", commonUtils.IsDisable(req.Enabled))
}
if req.Status != "" {
db = db.Where("status = ?", req.Status)
}
if req.Priority != "" {
db = db.Where("priority = ?", req.Priority)
}
if req.Type != "" {
db = db.Where("type = ?", req.Type)
}
if req.CreateUserId != 0 {
db = db.Where("create_user_id = ?", req.CreateUserId)
}
if len(scenarioIds) > 0 {
db = db.Where("id NOT IN (?)", scenarioIds)
}
err = db.Count(&count).Error
if err != nil {
logUtils.Errorf("count scenario error", zap.String("error:", err.Error()))
return
}
scenarios := make([]*model.Scenario, 0)
req.Order = "desc"
err = db.
Scopes(dao.PaginateScope(req.Page, req.PageSize, req.Order, req.Field)).
Find(&scenarios).Error
if err != nil {
logUtils.Errorf("query scenario error", zap.String("error:", err.Error()))
return
}
data.Populate(scenarios, count, req.Page, req.PageSize)
return
}
func (r *PlanRepo) GetCategoryCount(tenantId consts.TenantId, result interface{}, projectId uint) (err error) {
err = r.GetDB(tenantId).Raw("select count(id) count, category_id from "+model.Plan{}.TableName()+" where not deleted and not disabled and project_id=? group by category_id", projectId).Scan(result).Error
return
}
func (r *PlanRepo) DeleteByCategoryIds(tenantId consts.TenantId, categoryIds []uint) (err error) {
err = r.GetDB(tenantId).Model(&model.Plan{}).
Where("category_id IN (?)", categoryIds).
Update("deleted", 1).Error
return
}
func (r *PlanRepo) UpdateCurrEnvId(tenantId consts.TenantId, id, currEnvId uint) error {
return r.GetDB(tenantId).Model(&model.Plan{}).Where("id = ?", id).UpdateColumn("curr_env_id", currEnvId).Error
}
func (r *PlanRepo) MoveScenario(tenantId consts.TenantId, req v1.MoveReq) (err error) {
destination, err := r.RelaPlanScenarioRepo.Get(tenantId, req.DestinationId)
if err != nil {
return
}
souurce, err := r.RelaPlanScenarioRepo.Get(tenantId, req.SourceId)
if err != nil {
return
}
if destination.Ordr < souurce.Ordr {
err = r.RelaPlanScenarioRepo.IncreaseOrderAfter(tenantId, destination.Ordr, req.PlanId)
} else {
err = r.RelaPlanScenarioRepo.DecreaseOrderBefore(tenantId, destination.Ordr, req.PlanId)
}
if err != nil {
return
}
err = r.RelaPlanScenarioRepo.UpdateOrdrById(tenantId, req.SourceId, destination.Ordr)
return
}