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.

233 lines
6.1 KiB
C#

2 years 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>
public DataResult(ResultCode resultType, string message) : this(resultType, message, null)
{
}
/// <summary>
/// 初始化一个<see cref="DataResult"/>类型的新实例
/// </summary>
public DataResult(ResultCode type, string message, object data) : base(type, message, data)
{
}
/// <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)
{
return new DataResult(ResultCode.Success, message, data);
}
public static DataResult Successed(string message)
{
return new DataResult(ResultCode.Success, message, null);
}
/// <summary>
/// 返回失败的结果
/// </summary>
/// <param name="message">消息</param>
/// <returns></returns>
public static DataResult Failed(string message)
{
return new DataResult(ResultCode.Error, message);
}
}
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, int count = 0)
{
if (message == null)
{
message = (type as Enum)?.EnumDescription();
}
Code = type;
_message = message;
Data = data;
Count = count;
}
/// <summary>
/// 结果码
/// </summary>
public ResultCode Code { get; set; }
private string _message;
/// <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>
/// <returns></returns>
public static DataResult<TData> Success(string message, TData data)
{
return new DataResult<TData>(ResultCode.Success, message, data);
}
/// <summary>
/// 返回成功的结果
/// </summary>
/// <param name="data">结果</param>
/// <returns></returns>
public static DataResult<TData> Success(TData data)
{
return new DataResult<TData>(ResultCode.Success, string.Empty, data);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="ResultCode"></param>
/// <returns></returns>
public static DataResult<TData> Failed(string message, ResultCode ResultCode = ResultCode.Error)
{
return new DataResult<TData>(ResultCode, message);
}
/// <summary>
///
/// </summary>
/// <param name="count"></param>
/// <param name="data"></param>
/// <returns></returns>
public static DataResult<TData> PageList(int count, TData data)
{
return new DataResult<TData>(ResultCode.Success, string.Empty, data, count);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static DataResult<TData> Error(string message)
{
return new DataResult<TData>(ResultCode.Error, message, default(TData));
}
/// <summary>
/// 异常的DataResult
/// </summary>
public static DataResult<TData> Exception()
{
return new DataResult<TData>(ResultCode.Error, "程序异常,请联系管理员!", default(TData));
}
}