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.

265 lines
7.4 KiB
C#

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,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);
}
}
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>
/// <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="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="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);
}
}