using System.ComponentModel; using System.Reflection; using DS.Module.Core.Data; using DS.Module.Core.Extensions; using Microsoft.AspNetCore.Http; using SqlSugar; 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 SuccessedWithDesc(string fieldName) { var fieldInfo = typeof(MultiLanguageConst).GetField(fieldName) ?? throw new ArgumentException($"不存在的常量:{fieldName}", fieldName); var attribute = fieldInfo.GetCustomAttribute(); var desc = attribute?.Description ?? string.Empty; var value = fieldInfo.GetValue(null) as string ?? string.Empty; return Successed(desc, value); } /// /// 其他操作 /// /// /// /// 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); } /// /// 返回失败的结果-带占位符 /// /// 数据库db /// IHttpContextAccessor /// 多语言代码 /// 占位符1 /// 占位符2 /// 占位符3 /// 占位符4 /// 占位符5 /// /// public static DataResult FailedWithPlaceholder(ISqlSugarClient db, IHttpContextAccessor accessor, string multiCode, string placeholde1 = "", string placeholde2 = "", string placeholde3 = "", string placeholde4 = "", string placeholde5 = "") { var desc = string.Empty; var language = accessor.HttpContext.Request.Headers["LanguageType"].FirstOrDefault(); if (language.IsNullOrEmpty()) { language = "CN"; } language = language.ToUpper(); var result = string.Empty; var setInfo = db.Queryable().Where(x => x.MultiCode == multiCode && x.LanguageType == language && x.IsPlaceholder == true) .First(); if (setInfo.IsNotNull()) { desc = setInfo.Content; } else { if (language == "CN") { var fieldInfo = typeof(MultiLanguageConst).GetField(nameof(multiCode)); var attribute = fieldInfo.GetCustomAttribute(); desc = attribute?.Description ?? string.Empty; } else { throw new Exception($"未设置多语言:" + language + "下的消息提示代码:" + multiCode); } } return new DataResult(ResultCode.Error, string.Format(desc, placeholde1, placeholde2, placeholde3, placeholde4, placeholde5), multiCode); } /// /// 返回失败的结果 /// /// 中配置的字符串常量名 /// public static DataResult FailedWithDesc(string fieldName) { var fieldInfo = typeof(MultiLanguageConst).GetField(fieldName) ?? throw new ArgumentException($"不存在的常量:{fieldName}", fieldName); var attribute = fieldInfo.GetCustomAttribute(); var desc = attribute?.Description ?? string.Empty; var value = fieldInfo.GetValue(null) as string ?? string.Empty; return Failed(desc, value); } } 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 Dictionary? AdditionalData { get; set; } /// /// 返回成功的结果 /// /// 消息 /// 结果 /// /// public static DataResult Success(string message, TData data, string multiCode = "") { return new DataResult(ResultCode.Success, message, data, multiCode); } /// /// 返回成功的结果 /// /// /// /// public static DataResult SuccessMsg(string message, string multiCode = "") { return new DataResult(ResultCode.Success, message, default(TData), multiCode); } /// /// 返回成功的结果 /// /// 结果 /// /// public static DataResult Success(TData data, string multiCode = "") { return new DataResult(ResultCode.Success, string.Empty, data, multiCode); } /// /// /// /// /// /// public static DataResult Failed(string message, string multiCode = "") { return new DataResult(ResultCode.Error, message, multiCode); } /// /// 返回失败的结果 /// /// 中配置的字符串常量名 /// public static DataResult FailedWithDesc(string fieldName) { var fieldInfo = typeof(MultiLanguageConst).GetField(fieldName) ?? throw new ArgumentException($"不存在的常量:{fieldName}", fieldName); var attribute = fieldInfo.GetCustomAttribute(); var desc = attribute?.Description ?? string.Empty; var value = fieldInfo.GetValue(null) as string ?? string.Empty; return Failed(desc, value); } /// /// /// /// /// /// public static DataResult FailedData(TData data, string message = "", string multiCode = "") { return new DataResult(ResultCode.Error, message, 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); } }