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.

399 lines
12 KiB
C#

using System.ComponentModel;
using System.Reflection;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Http;
using SqlSugar;
12 months ago
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)
12 months ago
{
}
/// <summary>
/// 初始化一个<see cref="DataResult"/>类型的新实例
/// </summary>
public DataResult(ResultCode type, string message, object data, string multiCode = "") : base(type, message, data, multiCode)
12 months ago
{
}
/// <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 = "")
12 months ago
{
return new DataResult(ResultCode.Success, message, data, multiCode);
12 months ago
}
public static DataResult Successed(string message, string multiCode = "")
12 months ago
{
10 months ago
return new DataResult(ResultCode.Success, message, multiCode);
12 months ago
}
public static DataResult SuccessedWithDesc(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 Successed(desc, value);
}
12 months ago
/// <summary>
/// 其他操作
/// </summary>
/// <param name="message"></param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult Other(string message, string multiCode = "")
12 months ago
{
return new DataResult(ResultCode.Other, message, null, multiCode);
12 months ago
}
/// <summary>
/// 返回失败的结果
/// </summary>
/// <param name="message">消息</param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult Failed(string message, string multiCode = "")
12 months ago
{
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, value);
12 months ago
}
}
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>
4 months ago
public DataResult(ResultCode type, string message, TData? data, string multiCode = "", int count = 0)
12 months ago
{
if (message == null)
{
message = (type as Enum)?.EnumDescription();
}
Code = type;
10 months ago
MultiCode = multiCode;
12 months ago
_message = message;
Data = data;
Count = count;
}
public DataResult(ResultCode type, string message, string multiCode = "")
10 months ago
{
if (message == null)
{
message = (type as Enum)?.EnumDescription();
}
12 months ago
10 months ago
Code = type;
MultiCode = multiCode;
_message = message;
}
12 months ago
/// <summary>
/// 结果码
/// </summary>
public ResultCode Code { get; set; }
private string _message;
10 months ago
/// <summary>
/// 多语言代码
/// </summary>
public string MultiCode { get; set; }
12 months ago
/// <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;
4 months ago
/// <summary>
/// 数据
/// </summary>
public TData? Data { get; set; }
12 months ago
/// <summary>
/// 附加数据
/// </summary>
public Dictionary<string, object>? AdditionalData { get; set; }
12 months ago
/// <summary>
/// 返回成功的结果
/// </summary>
/// <param name="message">消息</param>
/// <param name="data">结果</param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult<TData> Success(string message, TData data, string multiCode = "")
12 months ago
{
return new DataResult<TData>(ResultCode.Success, message, data, multiCode);
12 months ago
}
/// <summary>
/// 返回成功的结果
/// </summary>
/// <param name="message"></param>
/// <param name="multiCode"></param>
/// <returns></returns>
6 months ago
public static DataResult<TData> SuccessMsg(string message, string multiCode = "")
{
return new DataResult<TData>(ResultCode.Success, message, default(TData), multiCode);
}
12 months ago
/// <summary>
/// 返回成功的结果
/// </summary>
/// <param name="data">结果</param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult<TData> Success(TData data, string multiCode = "")
12 months ago
{
return new DataResult<TData>(ResultCode.Success, string.Empty, data, multiCode);
12 months ago
}
/// <summary>
10 months ago
///
12 months ago
/// </summary>
/// <param name="message"></param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult<TData> Failed(string message, string multiCode = "")
12 months ago
{
return new DataResult<TData>(ResultCode.Error, message, multiCode);
12 months ago
}
/// <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);
//}
12 months ago
/// <summary>
10 months ago
///
12 months ago
/// </summary>
/// <param name="count"></param>
/// <param name="data"></param>
10 months ago
/// <param name="multiCode">多语言代码</param>
12 months ago
/// <returns></returns>
public static DataResult<TData> PageList(int count, TData data, string multiCode = "")
12 months ago
{
return new DataResult<TData>(ResultCode.Success, string.Empty, data, multiCode, count);
12 months ago
}
/// <summary>
10 months ago
///
12 months ago
/// </summary>
/// <param name="message"></param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
public static DataResult<TData> Error(string message, string multiCode = "")
12 months ago
{
return new DataResult<TData>(ResultCode.Error, message, default(TData), multiCode);
12 months ago
}
/// <summary>
/// 异常的DataResult
/// </summary>
public static DataResult<TData> Exception()
{
10 months ago
return new DataResult<TData>(ResultCode.Error, "程序异常,请联系管理员!", MultiLanguageConst.ProgramException);
12 months ago
}
}