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.

285 lines
8.1 KiB
C#

12 months ago
using DS.Module.Core.Data;
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>
10 months ago
public DataResult(ResultCode resultType, string message,string multiCode = "") : this(resultType, message, null,multiCode)
12 months ago
{
}
/// <summary>
/// 初始化一个<see cref="DataResult"/>类型的新实例
/// </summary>
10 months ago
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>
10 months ago
public static DataResult Successed(string message, object data,string multiCode = "")
12 months ago
{
10 months ago
return new DataResult(ResultCode.Success, message, data,multiCode);
12 months ago
}
10 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
}
/// <summary>
/// 其他操作
/// </summary>
/// <param name="message"></param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
10 months ago
public static DataResult Other(string message,string multiCode = "")
12 months ago
{
10 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>
10 months ago
public static DataResult Failed(string message,string multiCode = "")
12 months ago
{
10 months ago
return new DataResult(ResultCode.Error, message,multiCode);
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>
10 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;
}
10 months ago
public DataResult(ResultCode type, string message,string multiCode = "")
{
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;
public TData Data { get; set; }
/// <summary>
/// 返回成功的结果
/// </summary>
/// <param name="message">消息</param>
/// <param name="data">结果</param>
10 months ago
/// <param name="multiCode"></param>
12 months ago
/// <returns></returns>
10 months ago
public static DataResult<TData> Success(string message, TData data,string multiCode ="")
12 months ago
{
10 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>
public static DataResult<TData> Success(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>
10 months ago
public static DataResult<TData> Success(TData data,string multiCode ="")
12 months ago
{
10 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="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>
10 months ago
public static DataResult<TData> PageList(int count, TData data,string multiCode ="")
12 months ago
{
10 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>
10 months ago
public static DataResult<TData> Error(string message,string multiCode ="")
12 months ago
{
10 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
}
}