You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
9.8 KiB

  1. using CounsellorBL;
  2. using Newtonsoft.Json;
  3. using OT.COM.LogisticsUtil;
  4. using OT.COM.SignalerMessage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.Data;
  9. using System.Linq;
  10. using System.Net.Http;
  11. using System.Reflection;
  12. using System.Web;
  13. using System.Web.Http;
  14. namespace DefenseWeb.Models
  15. {
  16. public partial class CmdService : AApiServiceBase
  17. {
  18. static public string DecodeParm(string i_sEncodedData)
  19. {
  20. i_sEncodedData = i_sEncodedData.Replace(" ", "+");
  21. byte[] encodedDataAsBytes = System.Convert.FromBase64String(i_sEncodedData);
  22. string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
  23. return HttpUtility.UrlDecode(returnValue);
  24. }
  25. public CResponseMessage GetDataJsonString(bool i_bDecode, HttpRequestBase i_rRequest)
  26. {
  27. CResponseMessage sRes = null;
  28. try
  29. {
  30. string value = (true == i_bDecode) ? DecodeParm(i_rRequest.Form[""]) : null;
  31. sRes = ProgessJson2(value, i_rRequest);
  32. }
  33. catch (Exception ex)
  34. {
  35. Exception exCur = ex;
  36. while (null != exCur.InnerException)
  37. {
  38. exCur = exCur.InnerException;
  39. }
  40. sRes = new CErrorResponseMessage(exCur.Message);
  41. }
  42. return sRes;
  43. }
  44. public HttpResponseMessage GetData([FromBody]dynamic i_value, bool i_bDecode, HttpRequestMessage i_rRequest)
  45. {
  46. string sRes = null;
  47. try
  48. {
  49. string value = (true == i_bDecode) ? DecodeParm(i_value) : null;
  50. sRes = ProgessJson(value, i_rRequest);
  51. }
  52. catch (Exception ex)
  53. {
  54. Exception exCur = ex;
  55. while (null != exCur.InnerException)
  56. {
  57. exCur = exCur.InnerException;
  58. }
  59. sRes = JsonConvert.SerializeObject(new CErrorResponseMessage(exCur.Message));
  60. }
  61. return new HttpResponseMessage()
  62. {
  63. Content = new StringContent(sRes,
  64. System.Text.Encoding.UTF8, "application/json")
  65. };
  66. }
  67. protected override CResponseMessage HandleRequest2(CRequestMessage i_joRequest, HttpRequestMessage i_rRequest)
  68. {
  69. CResponseMessage sRes = null;
  70. try
  71. {
  72. do
  73. {
  74. if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
  75. {
  76. //sRes = makeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
  77. sRes = new CErrorResponseMessage(string.Format(BLWording.REQUEST_IS_NULL), i_joRequest);
  78. break;
  79. }
  80. string sModuleName = i_joRequest.MODULE + "Service";
  81. ClassHelper ch = new ClassHelper();
  82. object oModule = null;
  83. string sCreateError = ch.GetInstByClassName(sModuleName, out oModule);
  84. if (sCreateError != null || oModule == null)
  85. {
  86. // sRes = makeErrorReturn(i_joRequest, sCreateError);
  87. sRes = new CErrorResponseMessage(sCreateError, i_joRequest);
  88. break;
  89. }
  90. ServiceBase bls = oModule as ServiceBase;
  91. if (bls == null)
  92. {
  93. //sRes = makeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
  94. sRes = new CErrorResponseMessage(BLWording.COVERT_FAIL, i_joRequest);
  95. break;
  96. }
  97. //sRes = makeMessage(bls.Entry(i_joRequest));
  98. sRes = bls.Entry(i_joRequest);
  99. }
  100. while (false);
  101. }
  102. catch (Exception e)
  103. {
  104. // sRes = makeErrorReturn(i_joRequest, e.Message);
  105. sRes = new CErrorResponseMessage(e.Message, i_joRequest);
  106. }
  107. //if (true == string.IsNullOrWhiteSpace(sRes))
  108. //{
  109. // sRes = makeErrorReturn(i_joRequest, "Unknow Error");
  110. //}
  111. return sRes;
  112. }
  113. protected override string HandleRequest(CRequestMessage i_joRequest, HttpRequestMessage i_rRequest)
  114. {
  115. string sRes = "";
  116. try
  117. {
  118. do
  119. {
  120. if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
  121. {
  122. sRes = makeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
  123. break;
  124. }
  125. string sModuleType = i_joRequest.CUSTOMDATA.ContainsKey("module_id") ? i_joRequest.CUSTOMDATA["module_id"] : "";
  126. string sModuleName = i_joRequest.MODULE + "Service";
  127. // ClassHelper ch = new ClassHelper();
  128. object oModule = null;
  129. string sCreateError = this.GetInstByClassName(sModuleName, sModuleType, out oModule);
  130. if (sCreateError != null || oModule == null)
  131. {
  132. sRes = makeErrorReturn(i_joRequest, sCreateError);
  133. break;
  134. }
  135. ServiceBase bls = oModule as ServiceBase;
  136. if (bls == null)
  137. {
  138. sRes = makeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
  139. break;
  140. }
  141. sRes = makeMessage(bls.Entry(i_joRequest));
  142. }
  143. while (false);
  144. }
  145. catch (Exception e)
  146. {
  147. sRes = makeErrorReturn(i_joRequest, e.Message);
  148. }
  149. if (true == string.IsNullOrWhiteSpace(sRes))
  150. {
  151. sRes = makeErrorReturn(i_joRequest, "Unknow Error");
  152. }
  153. return sRes;
  154. }
  155. public string GetInstByClassName(string i_sTypeName, string i_sModuleId, out object o_oRes)
  156. {
  157. object obj2 = null;
  158. string str = null;
  159. Type typeByTypeName = this.GetTypeByTypeName(i_sTypeName, i_sModuleId);
  160. if (typeByTypeName == null)
  161. {
  162. str = "NO THIS ENTITY";
  163. }
  164. else
  165. {
  166. obj2 = Activator.CreateInstance(typeByTypeName);
  167. if (obj2 == null)
  168. {
  169. str = "ENTITY CREATE FAIL";
  170. }
  171. }
  172. o_oRes = obj2;
  173. return str;
  174. }
  175. public Type GetTypeByTypeName(string i_sTypeName, string i_sModuleId)//
  176. {
  177. Type type = null;
  178. string codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
  179. codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
  180. Assembly[] assemblyArray = (from f in AppDomain.CurrentDomain.GetAssemblies()
  181. where !f.IsDynamic
  182. && f.CodeBase != null
  183. && f.CodeBase.StartsWith(codeBase)
  184. && f.IsDefined(typeof(AssemblyCompanyAttribute), false) == true
  185. && f.IsDefined(typeof(AssemblyDescriptionAttribute), false) == true
  186. && f.IsDefined(typeof(AssemblyProductAttribute), false) == true
  187. && f.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false).OfType<AssemblyCompanyAttribute>().FirstOrDefault().Company == "Origtek"
  188. && f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault().Description == "Service"
  189. && ((f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Equals("CounsellorBL"))
  190. || (f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Contains("CounsellorBL." + i_sModuleId.ToUpper()) == true))
  191. orderby f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product descending
  192. select f).ToArray<Assembly>();
  193. // && f.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false).OfType<AssemblyCompanyAttribute>().FirstOrDefault() != null
  194. // && f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault() != null
  195. // && f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault() != null
  196. foreach (Assembly assembly2 in assemblyArray)
  197. {
  198. var typeArray = assembly2.GetTypes().Where(x => x.IsSubclassOf(typeof(ServiceBase))).ToArray<Type>();
  199. foreach (Type classType in typeArray)
  200. {
  201. if (classType.IsClass && (classType.Name == i_sTypeName))
  202. {
  203. type = classType;
  204. return type;
  205. }
  206. }
  207. }
  208. return type;
  209. }
  210. }
  211. }