From eb48f57e6c8da9fcbf57d46b5a2b16e41d589d00 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Tue, 31 Jan 2023 19:00:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/tech_converge_info.go | 22 ++++------------------ routers/api/v1/tech/tech.go | 6 ++++-- services/tech/tech.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/models/tech_converge_info.go b/models/tech_converge_info.go index a6b596e7d..c9657baf8 100644 --- a/models/tech_converge_info.go +++ b/models/tech_converge_info.go @@ -437,14 +437,7 @@ func GetTechRepoInfoForUser(opts *SearchUserRepoOpt) ([]*RepoConvergeInfo, int64 func loadAttributes(infos []*RepoConvergeInfo) { for _, info := range infos { info.User, _ = GetUserByID(info.UID) - var err error - info.Repo, err = GetRepositoryByID(info.RepoID) - if err != nil && IsErrRepoNotExist(err) { - //不存在的项目更新状态为不存在 - info.Status = TechNotExist - x.ID(info.ID).Cols("status").Update(info) - } - + info.Repo, _ = GetRepositoryByID(info.RepoID) info.BaseInfo, _ = GetTechConvergeBaseInfoById(info.BaseInfoID) } } @@ -467,17 +460,17 @@ func GetAvailableRepoConvergeInfo(opt *SearchRepoOpt) ([]*RepoConvergeInfo, erro func buildRepoFilterCond(opt *SearchRepoOpt) string { - sql := "" + sql := "status=" + strconv.Itoa(TechShow) if opt.Institution != "" { - sql += getPrefixWithoutWhere(sql) + " (institution like '%" + opt.Institution + ",%'" + " or institution like '%," + opt.Institution + "%'" + " or institution = '" + opt.Institution + "')" + sql += " and (institution like '%" + opt.Institution + ",%'" + " or institution like '%," + opt.Institution + "%'" + " or institution = '" + opt.Institution + "')" } if opt.ProjectName != "" { baseInfoIds := GetIdByProjectName(opt.ProjectName) if len(baseInfoIds) > 0 { - sql += getPrefixWithoutWhere(sql) + " base_info_id in (" + strings.Join(baseInfoIds, ",") + ")" + sql += " and base_info_id in (" + strings.Join(baseInfoIds, ",") + ")" } } @@ -552,13 +545,6 @@ func getWherePrefix(sql string) string { return " and " } -func getPrefixWithoutWhere(sql string) string { - if sql == "" { - return "" - } - return " and " -} - func loadRepoInfoForTech(list []*TechRepoInfo) { for _, techRepo := range list { diff --git a/routers/api/v1/tech/tech.go b/routers/api/v1/tech/tech.go index 395cd5239..3aff6d38f 100644 --- a/routers/api/v1/tech/tech.go +++ b/routers/api/v1/tech/tech.go @@ -47,6 +47,7 @@ func GetFilterInfo(ctx *context.APIContext) { } func GetAdminRepoInfo(ctx *context.APIContext) { + go techService.UpdateTechStatus() opts := &models.SearchUserRepoOpt{} page := ctx.QueryInt("page") if page <= 0 { @@ -95,6 +96,7 @@ type ActionIDs struct { } func GetMyRepoInfo(ctx *context.APIContext) { + go techService.UpdateTechStatus() opts := &models.SearchUserRepoOpt{} page := ctx.QueryInt("page") if page <= 0 { @@ -120,7 +122,7 @@ func GetMyRepoInfo(ctx *context.APIContext) { } func SearchRepoInfo(ctx *context.APIContext) { - + go techService.UpdateTechStatus() opts := &models.SearchRepoOpt{} opts.Q = ctx.Query("name") opts.ProjectName = ctx.Query("tech_name") @@ -151,7 +153,7 @@ func SearchRepoInfo(ctx *context.APIContext) { } func SearchTechProjectInfo(ctx *context.APIContext) { - + go techService.UpdateTechStatus() opts := &models.SearchTechOpt{} opts.Q = ctx.Query("name") opts.ApplyYear = ctx.QueryInt("apply_year") diff --git a/services/tech/tech.go b/services/tech/tech.go index 1e259bbc7..3698a2500 100644 --- a/services/tech/tech.go +++ b/services/tech/tech.go @@ -2,6 +2,7 @@ package tech import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/services/repository" ) @@ -114,6 +115,9 @@ func SearchRepoInfoWithInstitution(opt *models.SearchRepoOpt) ([]*models.RepoWit if err != nil { return nil, 0, err } + if len(infos) == 0 { + return []*models.RepoWithInstitution{}, 0, nil + } repoIds, institutionMap := getRepoIdAndInstitutionMap(infos) result, err := repository.FindRepos(repository.FindReposOptions{ @@ -172,6 +176,38 @@ func IsValidRepoConvergeExists(repoId, baseInfoId int64) (bool, error) { return len(list) > 0, nil } +func UpdateTechStatus() { + err := UpdateTechRepoDeleteStatus() + if err != nil { + log.Error("update delete status fail", err) + } + + err = UpdateTechMigrateStatus() + if err != nil { + log.Error("update migrate status fail", err) + } +} + +func UpdateTechRepoDeleteStatus() error { + needDeleteCheckRepos, err := models.GetRepoConverge(models.GetRepoConvergeOpts{ + Status: []int{models.TechShow, models.TechHide}, + }) + if err != nil { + return err + } + + for _, r := range needDeleteCheckRepos { + _, err := models.GetRepositoryByID(r.RepoID) + if err != nil && models.IsErrRepoNotExist(err) { + models.UpdateRepoConvergeStatus(r.ID, models.TechNotExist) + continue + } + + } + return nil + +} + func UpdateTechMigrateStatus() error { migratingRepos, err := models.GetRepoConverge(models.GetRepoConvergeOpts{ Status: []int{models.TechMigrating},