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.

253 lines
8.8 KiB

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