From 36401989fbff2cf2ffa02ee60fb46c4c34c0c262 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Tue, 25 Jun 2024 18:52:42 +0800 Subject: [PATCH] fix --- Tiobon.Core.Services/BASE/BaseServices.cs | 14 ++---- Tiobon.Core.Services/CommonServices.cs | 6 +-- .../Ghre/Ghre_CourseServices.cs | 9 ++-- .../Ghre/Ghre_ExamPaperServices.cs | 20 ++++++-- .../Ghre/Ghre_QuestionServices.cs | 46 +++++++++++++++++++ 5 files changed, 74 insertions(+), 21 deletions(-) diff --git a/Tiobon.Core.Services/BASE/BaseServices.cs b/Tiobon.Core.Services/BASE/BaseServices.cs index 4504b518..1590f641 100644 --- a/Tiobon.Core.Services/BASE/BaseServices.cs +++ b/Tiobon.Core.Services/BASE/BaseServices.cs @@ -1,17 +1,10 @@ -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Data; +using System.Data; using System.Dynamic; using System.Linq.Expressions; -using System.Net.Mail; using AgileObjects.AgileMapper; -using MathNet.Numerics.Distributions; using Microsoft.AspNetCore.Http; -using MySqlX.XDevAPI.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using NPOI.HPSF; -using NPOI.Util; using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.Caches; @@ -23,7 +16,6 @@ using Tiobon.Core.Common.Helper; using Tiobon.Core.Common.UserManager; using Tiobon.Core.DataAccess; using Tiobon.Core.IRepository.Base; -using Tiobon.Core.IServices; using Tiobon.Core.IServices.BASE; using Tiobon.Core.Model; using Tiobon.Core.Model.Models; @@ -483,7 +475,7 @@ public class BaseServices : IBaseServ /// /// 主键ID /// - public async Task DeleteById1(object id) + public virtual async Task DeleteById1(object id) { var entity = await BaseDal.QueryById(id); BasePoco ent = entity as BasePoco; @@ -505,7 +497,7 @@ public class BaseServices : IBaseServ /// /// 主键ID集合 /// - public async Task DeleteByIds1(long[] ids) + public virtual async Task DeleteByIds1(long[] ids) { HttpRequest request = UserContext.Context.Request; var api = request.Path.ObjToString().TrimEnd('/').ToLower(); diff --git a/Tiobon.Core.Services/CommonServices.cs b/Tiobon.Core.Services/CommonServices.cs index 096b809f..8656e4a8 100644 --- a/Tiobon.Core.Services/CommonServices.cs +++ b/Tiobon.Core.Services/CommonServices.cs @@ -1083,7 +1083,7 @@ public partial class CommonServices : BaseServices>, ICommon TableColumn.Add(searchItem); sql = "SELECT Id, CourseNo, CourseName FROM Ghre_Course WHERE IsEnable=1 AND Status='Released'"; - if (string.IsNullOrWhiteSpace(keyWords)) + if (!string.IsNullOrWhiteSpace(keyWords)) sql += $"AND ( CourseNo like '%{keyWords}%' or CourseName like '%{keyWords}%')"; dt = await Db.Ado.GetDataTableAsync(sql); @@ -1119,8 +1119,8 @@ public partial class CommonServices : BaseServices>, ICommon ]; TableColumn.Add(searchItem); - sql = "SELECT Id, SceneNo, SceneName FROM Ghre_CourseScene"; - if (string.IsNullOrWhiteSpace(keyWords)) + sql = "SELECT Id, SceneNo, SceneName FROM Ghre_CourseScene WHERE IsEnable=1"; + if (!string.IsNullOrWhiteSpace(keyWords)) sql += $"AND ( SceneNo like '%{keyWords}%' or SceneName like '%{keyWords}%')"; dt = await Db.Ado.GetDataTableAsync(sql); diff --git a/Tiobon.Core.Services/Ghre/Ghre_CourseServices.cs b/Tiobon.Core.Services/Ghre/Ghre_CourseServices.cs index f80103d1..5bc906e2 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_CourseServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_CourseServices.cs @@ -324,7 +324,8 @@ public class Ghre_CourseServices : BaseServices().FirstAsync(x => x.LinkId == id && x.LinkType == "CourseId" && x.Status != "Disabled"); - return ServiceResult.OprateFailed($"课程【{entity.CourseName}({entity.CourseNo})】已与试卷【{examPaper.PaperName}({examPaper.PaperName})】关联,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); + if (examPaper != null) + return ServiceResult.OprateFailed($"课程【{entity.CourseName}({entity.CourseNo})】已与试卷【{examPaper.PaperName}({examPaper.PaperName})】关联,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); //string sql = $"SELECT count(0) FROM Ghre_Exam WHERE CourseId='{id}' and IsEnable=1"; @@ -346,7 +347,7 @@ public class Ghre_CourseServices : BaseServices().AnyAsync(x => x.ExamPaperId == id && x.Status != "Disabled")) { var exam = await Db.Queryable().FirstAsync(x => x.ExamPaperId == id && x.Status != "Disabled"); - return ServiceResult.OprateFailed($"试卷【{entity.PaperName}({entity.PaperNo})】已与考试【{exam.ExamName}({exam.ExamNo})】关联,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); + if (exam != null) return ServiceResult.OprateFailed($"试卷【{entity.PaperName}({entity.PaperNo})】已与考试【{exam.ExamName}({exam.ExamNo})】关联,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); } if (await _ghre_ExamRecordServices.AnyAsync(x => x.ExamPaperId == id && x.Status == "UnderWay")) { var exam = await _ghre_ExamRecordServices.QuerySingleDto(x => x.ExamPaperId == id && x.Status == "UnderWay"); - return ServiceResult.OprateFailed($"试卷【{entity.PaperName}({entity.PaperNo})】,当前有学员正在考试中,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); + if (exam != null) return ServiceResult.OprateFailed($"试卷【{entity.PaperName}({entity.PaperNo})】,当前有学员正在考试中,暂不可{(status == "Draft" ? "取消发布" : "停用")}"); } } + + if (status == "Released" && entity.Status != "Released") + { + string sql = @$"SELECT COUNT(0) + FROM Ghre_ExamPaperQuestion A + JOIN Ghre_Question B ON A.QuestionId = B.Id and B.IsEnable = 0 + WHERE A.IsEnable = 1 AND A.ExamPaperId='{id}'"; + var count = await Db.Ado.GetIntAsync(sql); + if (count > 0) + return ServiceResult.OprateFailed($"试卷【{entity.PaperName}({entity.PaperNo})】,存在关联试题被删除,暂不可发布,请重新维护数据!"); + } #endregion BasePoco ent = entity; @@ -788,7 +800,7 @@ delete from Ghre_ExamPaperQuestion WHERE ExamPaperId='{id}';"); if (status == "Released") return ServiceResult.OprateSuccess("发布成功!"); else if (status == "Draft") - return ServiceResult.OprateSuccess("取消发布成功!"); + return ServiceResult.OprateSuccess(" 已启用成功,请进入草稿箱查看!"); else return ServiceResult.OprateSuccess("停用成功!"); @@ -834,7 +846,7 @@ delete from Ghre_ExamPaperQuestion WHERE ExamPaperId='{id}';"); TableColumn.Add(searchItem); sql = $"SELECT Id, PaperNo, PaperName FROM Ghre_ExamPaper WHERE LinkId='{linkId}' AND Status ='Released' AND IsEnable=1"; - if (string.IsNullOrWhiteSpace(keyWords)) + if (!string.IsNullOrWhiteSpace(keyWords)) sql += $"AND ( PaperNo like '%{keyWords}%' or PaperName like '%{keyWords}%')"; sql += $" ORDER BY CreateTime DESC"; dt = await Db.Ado.GetDataTableAsync(sql); diff --git a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs index 95dbf960..30dcdb1c 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs @@ -9,6 +9,7 @@ using Tiobon.Core.Common; using Tiobon.Core.Common.Caches; using Tiobon.Core.Common.DB.Dapper.Extensions; using Tiobon.Core.Common.Helper; +using Tiobon.Core.Common.UserManager; using Tiobon.Core.IRepository.Base; using Tiobon.Core.IServices; using Tiobon.Core.Model; @@ -1055,4 +1056,49 @@ public class Ghre_QuestionServices : BaseServices + /// 删除指定ID的数据 + /// + /// 主键ID + /// + public override async Task DeleteById1(object id) + { + var entity = await BaseDal.QueryById(id); + if (entity != null) + { + string sql = @$"SELECT A.* FROM Ghre_ExamPaper A JOIN Ghre_ExamPaperQuestion B ON B.ExamPaperId = A.Id WHERE B.QuestionId = '{id}' AND A.Status = 'Released'"; + var entitys = await Db.SqlQueryable(sql) + .ToPageListAsync(0, 10); + if (entitys.Any()) + throw new Exception($"题目【{entity.QuestionNo} 】已被试卷{string.Join("、", entitys.Select(x => $"【{x.PaperName}({x.PaperNo})】"))}关联,暂不可删除!"); + } + + return await base.DeleteById1(id); + } + + /// + /// 删除指定ID集合的数据(批量删除) + /// + /// 主键ID集合 + /// + public override async Task DeleteByIds1(long[] ids) + { + + foreach (var id in ids) + { + + var entity = await BaseDal.QueryById(id); + if (entity != null) + { + string sql = @$"SELECT A.* FROM Ghre_ExamPaper A JOIN Ghre_ExamPaperQuestion B ON B.ExamPaperId = A.Id WHERE B.QuestionId = '{id}' AND A.Status = 'Released'"; + var entitys = await Db.SqlQueryable(sql) + .ToPageListAsync(0, 10); + if (entitys.Any()) + throw new Exception($"题目【{entity.QuestionNo} 】已被试卷{string.Join("、", entitys.Select(x => $"【{x.PaperName}({x.PaperNo})】"))}关联,暂不可删除!"); + } + } + + return await base.DeleteByIds1(ids); + } } \ No newline at end of file