| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- using Procurement.Enums;
- using Procurement.ViewModel;
- namespace Procurement.Helpers
- {
- 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 "";
- }
- }
- }
|