using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Business.StructuredDB { public class ResultHelper { public static ResultViewModel CreateResult(int row, string msg) { ResultCode code = ResultCode.Fail, subCode = ResultCode.BusinessError; if (row > 0) { code = ResultCode.Success; } else { if (msg == "") { subCode = ResultCode.ExistsError; } } return CreateResult(code, null, subCode, msg); } public static ResultViewModel CreateResult(ResultCode code = ResultCode.Fail, dynamic data = null, ResultCode subCode = ResultCode.Fail, string subMsg = "") { string Msg = GetCustomAttributes(code); if (code == ResultCode.Success) { subCode = code; subMsg = string.IsNullOrEmpty(subMsg) ? Msg : subMsg; } else { subMsg = string.IsNullOrEmpty(subMsg) ? GetCustomAttributes(subCode) : subMsg; } return new ResultViewModel() { Code = Convert.ToInt32(code).ToString(), Msg = Msg, SubCode = Convert.ToInt32(subCode).ToString(), SubMsg = subMsg, Data = data }; } private static string GetCustomAttributes(ResultCode code) { Type type = code.GetType(); FieldInfo fieldInfo = type.GetField(code.ToString()); var customAttributes = fieldInfo.GetCustomAttributesData(); if (customAttributes.Count > 0) { return customAttributes[0].NamedArguments[0].TypedValue.Value.ToString(); } return ""; } } }