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,string multiCode = "") : this(resultType, message, null,multiCode) { } /// /// 初始化一个类型的新实例 /// public DataResult(ResultCode type, string message, object data,string multiCode = "") : base(type, message, data,multiCode) { } /// /// 获取 成功的操作结果 /// 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,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); } /// /// 其他操作 /// /// /// /// public static DataResult Other(string message,string multiCode = "") { return new DataResult(ResultCode.Other, message, null,multiCode); } /// /// 返回失败的结果 /// /// 消息 /// /// public static DataResult Failed(string message,string multiCode = "") { return new DataResult(ResultCode.Error, message,multiCode); } } 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,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; } /// /// 结果码 /// public ResultCode Code { get; set; } private string _message; /// /// 多语言代码 /// public string MultiCode{ get; set; } /// /// 总记录数 /// 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,string multiCode ="") { return new DataResult(ResultCode.Success, message, data,multiCode); } /// /// 返回成功的结果 /// /// 结果 /// /// public static DataResult Success(TData data,string multiCode ="") { return new DataResult(ResultCode.Success, string.Empty, data,multiCode); } /// /// /// /// /// /// /// public static DataResult Failed(string message, ResultCode ResultCode = ResultCode.Error,string multiCode ="") { return new DataResult(ResultCode, message,multiCode); } /// /// /// /// /// /// 多语言代码 /// public static DataResult PageList(int count, TData data,string multiCode ="") { return new DataResult(ResultCode.Success, string.Empty, data,multiCode, count); } /// /// /// /// /// /// public static DataResult Error(string message,string multiCode ="") { return new DataResult(ResultCode.Error, message, default(TData),multiCode); } /// /// 异常的DataResult /// public static DataResult Exception() { return new DataResult(ResultCode.Error, "程序异常,请联系管理员!", MultiLanguageConst.ProgramException); } }