代码优化

master
xiaochanghai 1 year ago
parent 5c0e07ab49
commit c6ae7371ec
  1. 36
      Tiobon.Core.Services/BASE/BaseServices.cs
  2. 19
      Tiobon.Core.Services/Ghre/Ghre_ExamPaperServices.cs
  3. 66
      Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs

@ -5,6 +5,7 @@ using System.Linq.Expressions;
using AgileObjects.AgileMapper;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using NPOI.Util;
using SqlSugar;
using Tiobon.Core.Common;
using Tiobon.Core.Common.Caches;
@ -679,6 +680,8 @@ public class BaseServices<TEntity, TEntityDto, TInsertDto, TEditDto> : IBaseServ
public virtual async Task<ServicePageResult<TEntityDto>> QueryFilterPage(QueryBody body)
{
if (string.IsNullOrWhiteSpace(body.orderBy))
body.orderBy = "CreateTime DESC";
var data = await BaseDal.QueryFilterPage(body);
return new ServicePageResult<TEntityDto>(body.pageNum, data.result.DT_TablePageInfoT1.TotalCount, body.pageSize, Mapper.Map(data.result.DT_TableDataT1).ToANew<List<TEntityDto>>());
@ -1012,5 +1015,38 @@ public class BaseServices<TEntity, TEntityDto, TInsertDto, TEditDto> : IBaseServ
}
#endregion
public string GetQueryString(string sqlSelect, int? currentPage = null, int? pageSize = null, string sortField = null, string sortDirection = null)
{
string queryString = string.Empty;
if (string.IsNullOrEmpty(sortField))
queryString = "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY ROW_ID) NUM FROM (SELECT * FROM (" + sqlSelect + " WHERE 1=1 ";
else
{
if (!string.IsNullOrEmpty(sortDirection))
queryString = "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY " + sortField + " " + sortDirection + ") NUM FROM (SELECT * FROM (" + sqlSelect + " ";
else
queryString = "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY " + sortField + " DESC) NUM FROM (SELECT * FROM (" + sqlSelect + " ";
}
queryString += ") A ) B ) C";
if (currentPage != null && pageSize != null)
queryString += " WHERE NUM <= " + currentPage * pageSize + " AND NUM >" + (currentPage - 1) * pageSize;
return queryString;
}
//public int GetTotalCount(string sqlSelect)
//{
// string sql = string.Empty;
// try
// {
// int count = Convert.ToInt32(DBHelper.ExecuteScalar(sql));
// return count;
// }
// catch (Exception Ex)
// {
// throw;
// }
//}
#endregion
}

@ -120,6 +120,8 @@ namespace Tiobon.Core.Services
public async Task<ServicePageResult<Ghre_ExamPaper>> QueryFilterPage1(QueryBody filter, string status = null)
{
if (string.IsNullOrWhiteSpace(filter.orderBy))
filter.orderBy = "CreateTime DESC";
RefAsync<int> totalCount = 0;
var query = Db.Queryable<Ghre_ExamPaper>();
if (!string.IsNullOrWhiteSpace(status))
@ -576,6 +578,9 @@ namespace Tiobon.Core.Services
public async Task<ServiceResult<long>> Insert1(DefaultGhre_ExamPaperPageData insertModel)
{
ValidForm(insertModel);
await Db.Ado.BeginTranAsync();
try
@ -623,6 +628,9 @@ namespace Tiobon.Core.Services
public async Task<ServiceResult> Update1(long id, DefaultGhre_ExamPaperPageData insertModel)
{
ValidForm(insertModel);
await Db.Ado.BeginTranAsync();
try
@ -707,5 +715,16 @@ delete from Ghre_ExamPaperQuestion WHERE ExamPaperId='{id}';");
return ServiceResult.OprateSuccess("停用成功!");
}
public static void ValidForm(DefaultGhre_ExamPaperPageData model)
{
if (model.baseData.PassScore == 0)
throw new Exception("及格分需大于0!");
if (model.baseData.PassScore >= model.baseData.TotalScore)
throw new Exception("及格分需小于卷面总分!");
if (model.baseData.AnswerTime <= 0)
throw new Exception("答题时间需大于0!");
}
}
}

@ -1,6 +1,7 @@

using System.Data;
using AgileObjects.AgileMapper;
using MathNet.Numerics.Distributions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
@ -37,7 +38,7 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
_ghre_CourseClassServices = ghre_CourseClassServices;
}
public override async Task<ServicePageResult<Ghre_QuestionDto>> QueryFilterPage(QueryBody filter)
public async Task<ServicePageResult<Ghre_QuestionDto>> QueryFilterPage1(QueryBody filter)
{
//var data1 = await BaseDal.QueryFilterPage(body);
@ -154,6 +155,69 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
return new ServicePageResult<Ghre_QuestionDto>(filter.pageNum, data1.result.DT_TablePageInfoT1.TotalCount, filter.pageSize, data);
}
public override async Task<ServicePageResult<Ghre_QuestionDto>> QueryFilterPage(QueryBody filter)
{
RefAsync<int> totalCount = 0;
string sql = @"SELECT *
FROM (SELECT A.*,
B.CourseName,
C.Id CourseTypeId,
C.ClassName CourseType
FROM Ghre_Question A
LEFT JOIN Ghre_Course B ON A.CourseId = B.Id
LEFT JOIN Ghre_CourseClass C ON B.CourseClassId = C.Id
WHERE A.IsEnable = 1) A";
if (string.IsNullOrWhiteSpace(filter.orderBy))
filter.orderBy = "CreateTime DESC";
string conditions = "1=1";
if (filter.jsonParam != null)
foreach (JProperty jProperty in filter.jsonParam.Properties())
{
var name = jProperty.Name;
var value = jProperty.Value.ToString();
if (name == "page" || name == "pageSize")
continue;
if (!string.IsNullOrWhiteSpace(value))
{
var jsonParam = JsonConvert.DeserializeObject<JsonParam>(value);
switch (jsonParam.operationKey)
{
case "Include":
conditions += $" AND {name} LIKE '%{jsonParam.columnValue}%'";
break;
case "NotInclude":
conditions += $" AND {name} NOT LIKE '%{jsonParam.columnValue}%'";
break;
case "IsNull":
conditions += $" AND {name} IS NULL";
break;
case "NotNull":
conditions += $" AND {name} IS NOT NULL";
break;
case "Equal":
conditions += $" AND {name} ='{jsonParam.columnValue}'";
break;
case "NotEqual":
conditions += $" AND {name} !='{jsonParam.columnValue}'";
break;
default:
break;
}
}
}
if (filter.pageSize == 0)
filter.pageSize = 10000;
var data = await Db.SqlQueryable<Ghre_QuestionDto>(sql)
.OrderBy(filter.orderBy)
.ToPageListAsync(filter.pageNum, filter.pageSize, totalCount);
return new ServicePageResult<Ghre_QuestionDto>(filter.pageNum, totalCount, filter.pageSize, data);
}
/// <summary>
///

Loading…
Cancel
Save