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.

263 lines
9.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyNet.Common;
  3. using Entity.Sugar;
  4. using log4net;
  5. using Newtonsoft.Json;
  6. using SqlSugar.Base;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. namespace EasyBL
  12. {
  13. public class MessageBase
  14. {
  15. protected static string _getCustomData(RequestMessage i_joRequest, string i_sKey)
  16. {
  17. string sRes = null;
  18. if (i_joRequest.CUSTOMDATA != null && i_joRequest.CUSTOMDATA.ContainsKey(i_sKey))
  19. {
  20. sRes = i_joRequest.CUSTOMDATA[i_sKey];
  21. }
  22. return sRes;
  23. }
  24. public static T _fetchEntity<T>(RequestMessage i_joRequest) where T : new()
  25. {
  26. //var properties = TypeDescriptor.GetProperties(typeof(T));
  27. //var t = Activator.CreateInstance<T>();
  28. var entity = new T();
  29. var properties = ReflectionHelper.GetProperties(entity.GetType());
  30. foreach (PropertyInfo prop in properties)
  31. {
  32. try
  33. {
  34. ReflectionHelper.SetPropertyValue(entity, prop, _fetchObject(i_joRequest.DATA, prop.Name));
  35. }
  36. catch (Exception)
  37. {
  38. prop.SetValue(entity, null);
  39. }
  40. }
  41. return entity;
  42. }
  43. public static List<T> _fetchCusEntity<T>(RequestMessage i_joRequest, string i_sKey) where T : new()
  44. {
  45. var lstT = new List<T>();
  46. var objval = _fetchObject(i_joRequest.DATA, i_sKey).ToString();
  47. if (!string.IsNullOrEmpty(objval) && objval != "[]")
  48. {
  49. lstT = JsonConvert.DeserializeObject<List<T>>(objval.ToString());
  50. }
  51. return lstT;
  52. }
  53. protected static string _fetchString(RequestMessage i_joRequest, string i_sKey)
  54. {
  55. return _fetchString(i_joRequest.DATA, i_sKey);
  56. }
  57. protected static int _fetchInt(RequestMessage i_joRequest, string i_sKey)
  58. {
  59. var sRes = _fetchString(i_joRequest.DATA, i_sKey);
  60. return (sRes != null && sRes != "") ? int.Parse(sRes) : -1;
  61. }
  62. protected static bool _fetchBool(RequestMessage i_joRequest, string i_sKey)
  63. {
  64. var sRes = _fetchString(i_joRequest.DATA, i_sKey);
  65. return sRes != null ? Convert.ToBoolean(sRes) : false;
  66. }
  67. protected static object _fetchObject(Dictionary<string, object> i_dic, string i_sKey)
  68. {
  69. object sRes = null;
  70. if (i_dic.ContainsKey(i_sKey))
  71. {
  72. sRes = i_dic[i_sKey];
  73. }
  74. return sRes;
  75. }
  76. protected static string _fetchString(Dictionary<string, object> i_dic, string i_sKey)
  77. {
  78. string sRes = null;
  79. if (i_dic.ContainsKey(i_sKey))
  80. {
  81. var obj = i_dic[i_sKey];
  82. if (null != obj)
  83. {
  84. sRes = obj.ToString();
  85. }
  86. }
  87. return sRes;
  88. }
  89. protected static string _getKeyStr(Dictionary<string, object> i_dic, string i_sKey)
  90. {
  91. var sRes = "";
  92. if (i_dic.ContainsKey(i_sKey))
  93. {
  94. var obj = i_dic[i_sKey];
  95. if (null != obj)
  96. {
  97. sRes = obj.ToString();
  98. }
  99. }
  100. return sRes;
  101. }
  102. protected static void _setEntityBase<T>(T i_entity, RequestMessage i_joRequest)
  103. {
  104. var properties = ReflectionHelper.GetProperties(i_entity.GetType());
  105. foreach (PropertyInfo prop in properties)
  106. {
  107. if (prop.Name == "OrgID")
  108. {
  109. prop.SetValue(i_entity, i_joRequest.ORIGID);
  110. }
  111. else if ("ModifyUser,CreateUser".Contains(prop.Name))
  112. {
  113. prop.SetValue(i_entity, i_joRequest.USERID);
  114. }
  115. else if ("ModifyDate,CreateDate".Contains(prop.Name))
  116. {
  117. prop.SetValue(i_entity, DateTime.Now);
  118. }
  119. }
  120. }
  121. private ILog _inst = null;
  122. protected ILog Logger
  123. {
  124. get
  125. {
  126. if (_inst == null)
  127. {
  128. _inst = LogManager.GetLogger(this.GetType());
  129. }
  130. return _inst;
  131. }
  132. }
  133. public void LogAndSendEmail(string sErrorMessage, Exception Exception, string sOrgID, string sUserID, string sProgramId, string sProgramName, string sFunctionName, string sErrorSource, string sErrorlineNO, string sErrorcolNO)
  134. {
  135. Logger.Error(sProgramName + sFunctionName + " Error:" + sErrorMessage, Exception);
  136. var db = SugarBase.GetIntance();
  137. string sError = null;
  138. string sUserFromName = null;
  139. var sEmailBody = "";
  140. if (string.IsNullOrWhiteSpace(sProgramId))
  141. {
  142. //拆分JS錯誤來源網址
  143. var saErrorSource = sErrorSource.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
  144. sProgramId = saErrorSource.LastOrDefault();
  145. }
  146. if (string.IsNullOrWhiteSpace(sProgramName))
  147. {
  148. sProgramName = "";
  149. }
  150. if (string.IsNullOrWhiteSpace(sProgramName))
  151. {
  152. sFunctionName = "";
  153. }
  154. if (Exception != null)
  155. {
  156. sErrorMessage += "<br/>" + Exception.ToString();
  157. }
  158. //查詢工程師郵件,拆分維護工程師郵件
  159. var saEmails = Common.GetSystemSetting(db, sOrgID, "ErrorEngineer").Split(new string[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);
  160. //獲取Email郵件格式
  161. var oErrorMessage = db.Queryable<OTB_SYS_Email>().Single(it => it.OrgID == sOrgID && it.EmailID == nameof(ErrorMessage));
  162. if (oErrorMessage != null)
  163. {
  164. if (!string.IsNullOrWhiteSpace(sUserID))
  165. {
  166. var oUserFrom = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.MemberID == sUserID);
  167. if (oUserFrom != null)
  168. {
  169. sUserFromName = oUserFrom.MemberName;
  170. }
  171. }
  172. //寄信開始
  173. foreach (string email in saEmails)
  174. {
  175. //利用郵件獲取工程師ID和名稱
  176. var oUserTo = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.Email == email);
  177. if (oUserTo == null)
  178. {
  179. oUserTo = new OTB_SYS_Members();
  180. }
  181. sEmailBody = oErrorMessage.BodyHtml.Replace("{{:UserName}}", oUserTo.MemberName)
  182. .Replace("{{:UseMember}}", sUserFromName ?? sUserID)
  183. .Replace("{{:FunctionName}}", sFunctionName)
  184. .Replace("{{:ErrorRow}}", sErrorlineNO)
  185. .Replace("{{:ErrorColumn}}", sErrorcolNO)
  186. .Replace("{{:ProgramId}}", sProgramId)
  187. .Replace("{{:ProgramName}}", sProgramName)
  188. .Replace("{{:error}}", sErrorMessage);
  189. var oEmail = new Emails();
  190. var saEmailTo = new List<EmailTo>(); //收件人
  191. var oEmailTo = new EmailTo
  192. {
  193. ToUserID = oUserTo.MemberID,
  194. ToUserName = oUserTo.MemberName,
  195. ToEmail = email,
  196. Type = "to"
  197. };
  198. saEmailTo.Add(oEmailTo);
  199. oEmail.FromUserName = "系統自動發送";//取fonfig
  200. oEmail.Title = "奕達運通管理系統錯誤信息派送";//取fonfig
  201. oEmail.EmailBody = sEmailBody;
  202. oEmail.IsCCSelf = false;
  203. oEmail.Attachments = null;
  204. oEmail.EmailTo = saEmailTo;
  205. var bSend = new MailService(sOrgID, true).MailFactory(oEmail, out sError);
  206. }
  207. }
  208. }
  209. public ResponseMessage ErrorMessage(RequestMessage i_crm)
  210. {
  211. ResponseMessage rm = null;
  212. string sMsg = null;
  213. try
  214. {
  215. var sErrorSource = _fetchString(i_crm, "ErrorSource");
  216. var sErrorlineNO = _fetchString(i_crm, "Errorlineno");
  217. var sErrorcolNO = _fetchString(i_crm, "Errorcolno");
  218. var sErrorMessage = _fetchString(i_crm, nameof(ErrorMessage));
  219. LogAndSendEmail(sErrorMessage, null, i_crm.ORIGID, i_crm.USERID, "", "", "", sErrorSource, sErrorlineNO, sErrorcolNO);
  220. rm = new SuccessResponseMessage(null, i_crm);
  221. }
  222. catch (Exception ex)
  223. {
  224. sMsg = Util.GetLastExceptionMsg(ex);
  225. }
  226. finally
  227. {
  228. if (null != sMsg)
  229. {
  230. rm = new ErrorResponseMessage(sMsg, i_crm);
  231. }
  232. }
  233. return rm;
  234. }
  235. }
  236. }