using DS.Module.Core.Data; namespace DS.Module.Core; /// /// 业务操作结果信息类,对操作结果进行封装 /// public class DataResult : DataResult { static DataResult() { Success = new DataResult(ResultCode.Success); NoChanged = new DataResult(ResultCode.NoChanged); } /// /// 初始化一个类型的新实例 /// public DataResult() : this(ResultCode.NoChanged) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode resultType) : this(resultType, null, null) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode resultType, string message) : this(resultType, message, null) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode type, string message, object data) : base(type, message, data) { } /// /// 获取 成功的操作结果 /// public static DataResult Success { get; private set; } /// /// 获取 未变更的操作结果 /// public new static DataResult NoChanged { get; private set; } /// /// 将转换为 /// /// public DataResult ToDataResult() { T data = default(T); if (Data is T variable) { data = variable; } return new DataResult(ResultCode.Success, Message, data); } /// /// 返回成功的结果 /// /// 消息 /// 结果 /// 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); } /// /// 返回失败的结果 /// /// 消息 /// public static DataResult Failed(string message) { return new DataResult(ResultCode.Error, message); } } public class DataResult : IDataResult { static DataResult() { NoChanged = new DataResult(ResultCode.NoChanged); } /// /// 初始化一个类型的新实例 /// public DataResult() : this(ResultCode.NoChanged) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode resultType) : this(resultType, null, default(TData)) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode resultType, string message) : this(resultType, message, default(TData)) { } /// /// 初始化一个类型的新实例 /// 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; } /// /// 结果码 /// public ResultCode Code { get; set; } private string _message; /// /// 总记录数 /// public int Count { get; set; } /// /// 获取 未变更的操作结果 /// public virtual string Message { get => _message ?? Code.EnumDescription(); set => _message = value; } /// /// 获取 未变更的操作结果 /// public static DataResult NoChanged { get; private set; } /// /// 获取 是否成功 /// public bool Succeeded => Code == ResultCode.Success; public TData Data { get; set; } /// /// 返回成功的结果 /// /// 消息 /// 结果 /// public static DataResult Success(string message, TData data) { return new DataResult(ResultCode.Success, message, data); } /// /// 返回成功的结果 /// /// 结果 /// public static DataResult Success(TData data) { return new DataResult(ResultCode.Success, string.Empty, data); } /// /// /// /// /// /// public static DataResult Failed(string message, ResultCode ResultCode = ResultCode.Error) { return new DataResult(ResultCode, message); } /// /// /// /// /// /// public static DataResult PageList(int count, TData data) { return new DataResult(ResultCode.Success, string.Empty, data, count); } /// /// /// /// /// public static DataResult Error(string message) { return new DataResult(ResultCode.Error, message, default(TData)); } /// /// 异常的DataResult /// public static DataResult Exception() { return new DataResult(ResultCode.Error, "程序异常,请联系管理员!", default(TData)); } }