73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
|
package test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/gogf/gf/v2/frame/g"
|
||
|
"github.com/samber/lo"
|
||
|
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||
|
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
|
||
|
)
|
||
|
|
||
|
// TestGetMatrixsByProjectId 删除一共子项目的所有方阵数据
|
||
|
func TestGetMatrixsByProjectId(t *testing.T) {
|
||
|
subprojectID := 33
|
||
|
matrixs := getMatrixsByProjectID(subprojectID)
|
||
|
|
||
|
for _, matrix := range matrixs {
|
||
|
if deleteMatrixByID(matrix) != nil {
|
||
|
t.Error("删除方阵下的数据失败")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TestDeleteMatrixsByProjectId 删除一个子项目的所有方阵
|
||
|
func TestDeleteMatrixsByProjectId(t *testing.T) {
|
||
|
subprojectID := 33
|
||
|
if deleteMatrixsByProjectID(subprojectID) != nil {
|
||
|
t.Error("删除子项目的所有方阵失败")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 删除一个子项目的所有方阵
|
||
|
func deleteMatrixsByProjectID(subprojectID int) error {
|
||
|
matrixs := getMatrixsByProjectID(subprojectID)
|
||
|
|
||
|
if _, err := dao.QianqiFangzhen.Ctx(context.Background()).
|
||
|
WhereIn(dao.QianqiFangzhen.Columns().Id, matrixs).Unscoped().Delete(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// 根据方阵ID在数据库中删除数据
|
||
|
func deleteMatrixByID(fangzhenId int) error {
|
||
|
ctx := context.Background()
|
||
|
|
||
|
return g.Try(ctx, func(ctx context.Context) {
|
||
|
// 删除任务
|
||
|
if _, err := dao.WorkStatus.Ctx(ctx).Where(dao.WorkStatus.Columns().FangzhenId, fangzhenId).Delete(); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
// 删除计划
|
||
|
if _, err := dao.WorkSchedule.Ctx(ctx).Where(dao.WorkSchedule.Columns().FangzhenId, fangzhenId).Delete(); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// 获取一个子项目的所有方阵ID
|
||
|
func getMatrixsByProjectID(subprojectID int) []int {
|
||
|
ctx := context.Background()
|
||
|
var matrixs []entity.QianqiFangzhen
|
||
|
if err := dao.QianqiFangzhen.Ctx(ctx).Where(dao.QianqiFangzhen.Columns().ProjectId, subprojectID).Scan(&matrixs); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return lo.Map(matrixs, func(matrix entity.QianqiFangzhen, _ int) int {
|
||
|
return matrix.Id
|
||
|
})
|
||
|
}
|