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.

135 lines
5.4 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using log4net;
  4. using SqlSugar.Base;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace EasyBL
  9. {
  10. public class LogService : ServiceBase
  11. {
  12. public static readonly ILog mo_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//記錄異常
  13. /// <summary>
  14. /// (會計)取消審核
  15. /// </summary>
  16. /// <param name="i_crm">todo: describe i_crm parameter on ErrorMessage</param>
  17. /// <returns></returns>
  18. public new ResponseMessage ErrorMessage(RequestMessage i_crm)
  19. {
  20. ResponseMessage rm = null;
  21. string sMsg = null;
  22. try
  23. {
  24. var sErrorSource = _fetchString(i_crm, "ErrorSource");
  25. var sErrorlineNO = _fetchString(i_crm, "Errorlineno");
  26. var sErrorcolNO = _fetchString(i_crm, "Errorcolno");
  27. var sErrorMessage = _fetchString(i_crm, nameof(ErrorMessage));
  28. MailSend(sErrorMessage, null, i_crm.ORIGID, i_crm.USERID, "", "", "", sErrorSource, sErrorlineNO, sErrorcolNO);
  29. rm = new SuccessResponseMessage(null, i_crm);
  30. }
  31. catch (Exception ex)
  32. {
  33. sMsg = Util.GetLastExceptionMsg(ex);
  34. }
  35. finally
  36. {
  37. if (null != sMsg)
  38. {
  39. rm = new ErrorResponseMessage(sMsg, i_crm);
  40. }
  41. }
  42. return rm;
  43. }
  44. public static void MailSend(string sErrorMessage, Exception Exception, string sOrgID, string sUserID, string sProgramId, string sProgramName, string sFunctionName, string sErrorSource, string sErrorlineNO, string sErrorcolNO)
  45. {
  46. LogService.mo_Log.Error(sProgramName + sFunctionName + " Error:" + sErrorMessage, Exception);
  47. var db = SugarBase.GetIntance();
  48. string sError = null;
  49. string sUserFromName = null;
  50. var sEmailBody = "";
  51. if (string.IsNullOrWhiteSpace(sProgramId))
  52. {
  53. //拆分JS錯誤來源網址
  54. var saErrorSource = sErrorSource.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
  55. sProgramId = saErrorSource.LastOrDefault();
  56. }
  57. if (string.IsNullOrWhiteSpace(sProgramName))
  58. {
  59. sProgramName = "";
  60. }
  61. if (string.IsNullOrWhiteSpace(sProgramName))
  62. {
  63. sFunctionName = "";
  64. }
  65. if (Exception != null)
  66. {
  67. sErrorMessage += "<br/>" + Exception.ToString();
  68. }
  69. //查詢工程師郵件,拆分維護工程師郵件
  70. var saEmails = Common.GetSystemSetting(db, sOrgID, "ErrorEngineer").Split(new string[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);
  71. //獲取Email郵件格式
  72. var oErrorMessage = db.Queryable<OTB_SYS_Email>().Single(it => it.OrgID == sOrgID && it.EmailID == nameof(ErrorMessage));
  73. if (oErrorMessage != null)
  74. {
  75. if (!string.IsNullOrWhiteSpace(sUserID))
  76. {
  77. var oUserFrom = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.MemberID == sUserID);
  78. if (oUserFrom != null)
  79. {
  80. sUserFromName = oUserFrom.MemberName;
  81. }
  82. }
  83. //寄信開始
  84. foreach (string email in saEmails)
  85. {
  86. //利用郵件獲取工程師ID和名稱
  87. var oUserTo = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.Email == email);
  88. if (oUserTo == null)
  89. {
  90. oUserTo = new OTB_SYS_Members();
  91. }
  92. sEmailBody = oErrorMessage.BodyHtml.Replace("{{:UserName}}", oUserTo.MemberName)
  93. .Replace("{{:UseMember}}", sUserFromName ?? sUserID)
  94. .Replace("{{:FunctionName}}", sFunctionName)
  95. .Replace("{{:ErrorRow}}", sErrorlineNO)
  96. .Replace("{{:ErrorColumn}}", sErrorcolNO)
  97. .Replace("{{:ProgramId}}", sProgramId)
  98. .Replace("{{:ProgramName}}", sProgramName)
  99. .Replace("{{:error}}", sErrorMessage);
  100. var oEmail = new Emails();
  101. var saEmailTo = new List<EmailTo>(); //收件人
  102. var oEmailTo = new EmailTo
  103. {
  104. ToUserID = oUserTo.MemberID,
  105. ToUserName = oUserTo.MemberName,
  106. ToEmail = email,
  107. Type = "to"
  108. };
  109. saEmailTo.Add(oEmailTo);
  110. oEmail.FromUserName = "系統自動發送";//取fonfig
  111. oEmail.Title = "奕達運通管理系統錯誤信息派送";//取fonfig
  112. oEmail.EmailBody = sEmailBody;
  113. oEmail.IsCCSelf = false;
  114. oEmail.Attachments = null;
  115. oEmail.EmailTo = saEmailTo;
  116. var bSend = new MailService(sOrgID, true).MailFactory(oEmail, out sError);
  117. }
  118. }
  119. }
  120. }
  121. }