You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
386 lines
12 KiB
C#
386 lines
12 KiB
C#
using System.ComponentModel;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using DS.Module.Core.Data;
|
|
using DS.Module.Core.Extensions;
|
|
using Microsoft.AspNetCore.Http;
|
|
using SqlSugar;
|
|
|
|
namespace DS.Module.Core;
|
|
|
|
/// <summary>
|
|
/// 业务操作结果信息类,对操作结果进行封装
|
|
/// </summary>
|
|
public class DataResult : DataResult<object>
|
|
{
|
|
static DataResult()
|
|
{
|
|
Success = new DataResult(ResultCode.Success);
|
|
NoChanged = new DataResult(ResultCode.NoChanged);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult() : this(ResultCode.NoChanged)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode resultType) : this(resultType, null, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode resultType, string message, string multiCode = "") : this(resultType, message, null, multiCode)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode type, string message, object data, string multiCode = "") : base(type, message, data, multiCode)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 成功的操作结果
|
|
/// </summary>
|
|
public static DataResult Success { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 获取 未变更的操作结果
|
|
/// </summary>
|
|
public new static DataResult NoChanged { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 将<see cref="DataResult{TData}"/>转换为<see cref="DataResult"/>
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public DataResult<T> ToDataResult<T>()
|
|
{
|
|
T data = default(T);
|
|
if (Data is T variable)
|
|
{
|
|
data = variable;
|
|
}
|
|
|
|
return new DataResult<T>(ResultCode.Success, Message, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回成功的结果
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
/// <param name="data">结果</param>
|
|
/// <returns></returns>
|
|
public static DataResult Successed(string message, object data, string multiCode = "")
|
|
{
|
|
return new DataResult(ResultCode.Success, message, data, multiCode);
|
|
}
|
|
|
|
public static DataResult Successed(string message, string multiCode = "")
|
|
{
|
|
return new DataResult(ResultCode.Success, message, multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 其他操作
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult Other(string message, string multiCode = "")
|
|
{
|
|
return new DataResult(ResultCode.Other, message, null, multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回失败的结果
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult Failed(string message, string multiCode = "")
|
|
{
|
|
return new DataResult(ResultCode.Error, message, multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回失败的结果-带占位符
|
|
/// </summary>
|
|
/// <param name="db">数据库db</param>
|
|
/// <param name="accessor">IHttpContextAccessor</param>
|
|
/// <param name="multiCode">多语言代码</param>
|
|
/// <param name="placeholde1">占位符1</param>
|
|
/// <param name="placeholde2">占位符2</param>
|
|
/// <param name="placeholde3">占位符3</param>
|
|
/// <param name="placeholde4">占位符4</param>
|
|
/// <param name="placeholde5">占位符5</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public static DataResult FailedWithPlaceholder(ISqlSugarClient db, IHttpContextAccessor accessor, string multiCode, string placeholde1 = "", string placeholde2 = "", string placeholde3 = "", string placeholde4 = "", string placeholde5 = "")
|
|
{
|
|
|
|
var desc = string.Empty;
|
|
var language = accessor.HttpContext.Request.Headers["LanguageType"].FirstOrDefault();
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = "CN";
|
|
}
|
|
language = language.ToUpper();
|
|
var result = string.Empty;
|
|
var setInfo = db.Queryable<SysLanguageSet>().Where(x => x.MultiCode == multiCode && x.LanguageType == language && x.IsPlaceholder == true)
|
|
.First();
|
|
if (setInfo.IsNotNull())
|
|
{
|
|
desc = setInfo.Content;
|
|
}
|
|
else
|
|
{
|
|
if (language == "CN")
|
|
{
|
|
var fieldInfo = typeof(MultiLanguageConst).GetField(nameof(multiCode));
|
|
var attribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
|
|
desc = attribute?.Description ?? string.Empty;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"未设置多语言:" + language + "下的消息提示代码:" + multiCode);
|
|
}
|
|
}
|
|
|
|
|
|
return new DataResult(ResultCode.Error, string.Format(desc, placeholde1, placeholde2, placeholde3, placeholde4, placeholde5), multiCode);
|
|
}
|
|
/// <summary>
|
|
/// 返回失败的结果
|
|
/// </summary>
|
|
/// <param name="fieldName">类<see cref="MultiLanguageConst"/>中配置的字符串常量名</param>
|
|
/// <returns></returns>
|
|
public static DataResult FailedWithDesc(string fieldName)
|
|
{
|
|
var fieldInfo = typeof(MultiLanguageConst).GetField(fieldName) ?? throw new ArgumentException($"不存在的常量:{fieldName}", fieldName);
|
|
|
|
var attribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
|
|
var desc = attribute?.Description ?? string.Empty;
|
|
var value = fieldInfo.GetValue(null) as string ?? string.Empty;
|
|
|
|
return Failed(desc, fieldName);
|
|
}
|
|
}
|
|
|
|
public class DataResult<TData> : IDataResult<ResultCode, TData>
|
|
{
|
|
static DataResult()
|
|
{
|
|
NoChanged = new DataResult<TData>(ResultCode.NoChanged);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult() : this(ResultCode.NoChanged)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult{TData}"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode resultType) : this(resultType, null, default(TData))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult{TData}"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode resultType, string message) : this(resultType, message, default(TData))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="DataResult{TResultType,TData}"/>类型的新实例
|
|
/// </summary>
|
|
public DataResult(ResultCode type, string message, TData data, string multiCode = "", int count = 0)
|
|
{
|
|
if (message == null)
|
|
{
|
|
message = (type as Enum)?.EnumDescription();
|
|
}
|
|
|
|
Code = type;
|
|
MultiCode = multiCode;
|
|
_message = message;
|
|
Data = data;
|
|
Count = count;
|
|
}
|
|
public DataResult(ResultCode type, string message, string multiCode = "")
|
|
{
|
|
if (message == null)
|
|
{
|
|
message = (type as Enum)?.EnumDescription();
|
|
}
|
|
|
|
Code = type;
|
|
MultiCode = multiCode;
|
|
_message = message;
|
|
}
|
|
/// <summary>
|
|
/// 结果码
|
|
/// </summary>
|
|
public ResultCode Code { get; set; }
|
|
|
|
private string _message;
|
|
|
|
/// <summary>
|
|
/// 多语言代码
|
|
/// </summary>
|
|
public string MultiCode { get; set; }
|
|
/// <summary>
|
|
/// 总记录数
|
|
/// </summary>
|
|
public int Count { get; set; }
|
|
|
|
/// <summary>
|
|
/// 获取 未变更的操作结果
|
|
/// </summary>
|
|
public virtual string Message
|
|
{
|
|
get => _message ?? Code.EnumDescription();
|
|
set => _message = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 未变更的操作结果
|
|
/// </summary>
|
|
public static DataResult<TData> NoChanged { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 获取 是否成功
|
|
/// </summary>
|
|
public bool Succeeded => Code == ResultCode.Success;
|
|
|
|
public TData Data { get; set; }
|
|
|
|
/// <summary>
|
|
/// 附加数据
|
|
/// </summary>
|
|
public Dictionary<string,object>? AdditionalData { get; set; }
|
|
|
|
/// <summary>
|
|
/// 返回成功的结果
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
/// <param name="data">结果</param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> Success(string message, TData data, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Success, message, data, multiCode);
|
|
}
|
|
/// <summary>
|
|
/// 返回成功的结果
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> SuccessMsg(string message, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Success, message, default(TData), multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回成功的结果
|
|
/// </summary>
|
|
/// <param name="data">结果</param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> Success(TData data, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Success, string.Empty, data, multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> Failed(string message, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Error, message, multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回失败的结果
|
|
/// </summary>
|
|
/// <param name="fieldName">类<see cref="MultiLanguageConst"/>中配置的字符串常量名</param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> FailedWithDesc(string fieldName)
|
|
{
|
|
var fieldInfo = typeof(MultiLanguageConst).GetField(fieldName) ?? throw new ArgumentException($"不存在的常量:{fieldName}", fieldName);
|
|
|
|
var attribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
|
|
var desc = attribute?.Description ?? string.Empty;
|
|
var value = fieldInfo.GetValue(null) as string ?? string.Empty;
|
|
|
|
return Failed(desc, value);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> FailedData(TData data, string message = "", string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Error, message, data, multiCode);
|
|
}
|
|
///// <summary>
|
|
/////
|
|
///// </summary>
|
|
///// <param name="message"></param>
|
|
///// <param name="ResultCode"></param>
|
|
///// <param name="multiCode"></param>
|
|
///// <returns></returns>
|
|
//public static DataResult<TData> Failed(string message, ResultCode ResultCode = ResultCode.Error,string multiCode ="")
|
|
//{
|
|
// return new DataResult<TData>(ResultCode, message,multiCode);
|
|
//}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="count"></param>
|
|
/// <param name="data"></param>
|
|
/// <param name="multiCode">多语言代码</param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> PageList(int count, TData data, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Success, string.Empty, data, multiCode, count);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="multiCode"></param>
|
|
/// <returns></returns>
|
|
public static DataResult<TData> Error(string message, string multiCode = "")
|
|
{
|
|
return new DataResult<TData>(ResultCode.Error, message, default(TData), multiCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异常的DataResult
|
|
/// </summary>
|
|
public static DataResult<TData> Exception()
|
|
{
|
|
return new DataResult<TData>(ResultCode.Error, "程序异常,请联系管理员!", MultiLanguageConst.ProgramException);
|
|
}
|
|
} |