From 4ec43c220ec5e88d769e193c0859abfbe4028e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=AA=E5=B8=A6=E5=A4=A7=E4=BD=AC=E6=B0=94=E5=9C=BA?= <188633308@qq.com> Date: Thu, 20 May 2021 15:26:28 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=A7=A3=E5=86=B3dapper=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=97=A0=E6=B3=95=E8=BD=AC=E4=B8=BA=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Ewide.Core/Ewide.Core.xml | 2 +- Api/Ewide.Core/Extension/PageExtensions.cs | 52 +++++++++---------- .../Extension/XnRestfulResultProvider.cs | 20 +++++-- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/Api/Ewide.Core/Ewide.Core.xml b/Api/Ewide.Core/Ewide.Core.xml index 3da9f82..8d9d44b 100644 --- a/Api/Ewide.Core/Ewide.Core.xml +++ b/Api/Ewide.Core/Ewide.Core.xml @@ -4075,7 +4075,7 @@ 删除字典值 id数组传入 - + diff --git a/Api/Ewide.Core/Extension/PageExtensions.cs b/Api/Ewide.Core/Extension/PageExtensions.cs index 71222f8..f80f55f 100644 --- a/Api/Ewide.Core/Extension/PageExtensions.cs +++ b/Api/Ewide.Core/Extension/PageExtensions.cs @@ -1,15 +1,11 @@ -using System; +using Dapper; +using Furion.DatabaseAccessor; +using Mapster; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Dynamic.Core; -using System.Linq.Expressions; -using System.Reflection; -using System.Text; using System.Threading.Tasks; -using Dapper; -using Furion.DatabaseAccessor; -using Furion.DatabaseAccessor.Extensions; -using Mapster; namespace Ewide.Core.Extension { @@ -44,6 +40,13 @@ namespace Ewide.Core.Extension return orderStr; } + private static string PageSqlBuilder(string sql, PageInputBase input) + { + var orderStr = OrderBuilder(input); + var r = "SELECT * FROM (" + sql + ") T " + (string.IsNullOrEmpty(orderStr) ? string.Empty : "ORDER BY " + orderStr) + " LIMIT " + ((input.PageNo - 1) * input.PageSize).ToString() + "," + input.PageSize.ToString(); + return r; + } + public static Task> ToPageData(this IQueryable source, PageInputBase input) where T : new() { return source.OrderBy(OrderBuilder(input)).ToPagedListAsync(input.PageNo, input.PageSize); @@ -54,31 +57,26 @@ namespace Ewide.Core.Extension return source.OrderBy(OrderBuilder(input)).Select(u => u.Adapt()).ToPagedListAsync(input.PageNo, input.PageSize); } - public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, System.Data.IDbTransaction transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null) + public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { - return source.QueryAsync(PageSqlBuild(sql,input), + return source.QueryAsync( + PageSqlBuilder(sql, input), + param: param, + transaction: transaction, + commandTimeout: commandTimeout, + commandType: commandType + ); + } + + public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) + { + return source.QueryAsync( + PageSqlBuilder(sql, input), param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); } - - public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, System.Data.IDbTransaction transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null) - { - return source.QueryAsync(sql, - param: param, - transaction: transaction, - commandTimeout: commandTimeout, - commandType: commandType - ); - } - - private static string PageSqlBuild(string sql , PageInputBase input) - { - var orderStr = OrderBuilder(input); - var r = "SELECT * FROM (" + sql + ") T " + (string.IsNullOrEmpty(orderStr) ? string.Empty : "Order by " + orderStr) + " LIMIT " + ((input.PageNo - 1) * input.PageSize).ToString() + "," + input.PageSize.ToString(); - return r; - } } } diff --git a/Api/Ewide.Core/Extension/XnRestfulResultProvider.cs b/Api/Ewide.Core/Extension/XnRestfulResultProvider.cs index b4a1634..4996707 100644 --- a/Api/Ewide.Core/Extension/XnRestfulResultProvider.cs +++ b/Api/Ewide.Core/Extension/XnRestfulResultProvider.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System.Collections.Generic; using System.Threading.Tasks; +using Newtonsoft.Json; namespace Ewide.Core { @@ -17,6 +18,19 @@ namespace Ewide.Core [SkipScan, UnifyModel(typeof(XnRestfulResult<>))] public class XnRestfulResultProvider : IUnifyResultProvider { + private IActionResult DisplayJson(object data) + { + return new ContentResult + { + Content = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings + { + ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(), + DateFormatString = "yyyy-MM-dd HH:mm:ss" + }), + ContentType = "application/json" + }; + } + /// /// 异常返回值 /// @@ -27,7 +41,7 @@ namespace Ewide.Core // 解析异常信息 var (StatusCode, ErrorCode, Errors) = UnifyContext.GetExceptionMetadata(context); - return new JsonResult(new XnRestfulResult + return DisplayJson(new XnRestfulResult { Code = StatusCode, Success = false, @@ -93,7 +107,7 @@ namespace Ewide.Core else if (context.Result is EmptyResult) data = null; else return null; - return new JsonResult(new XnRestfulResult + return DisplayJson(new XnRestfulResult { Code = context.Result is EmptyResult ? StatusCodes.Status204NoContent : StatusCodes.Status200OK, // 处理没有返回值情况 204 Success = true, @@ -114,7 +128,7 @@ namespace Ewide.Core /// public IActionResult OnValidateFailed(ActionExecutingContext context, ModelStateDictionary modelStates, IEnumerable validationResults, string validateFailedMessage) { - return new JsonResult(new XnRestfulResult + return DisplayJson(new XnRestfulResult { Code = StatusCodes.Status400BadRequest, Success = false,