using EasyBL.WebApi.Message;
using Entity.Sugar;
using log4net;
using SqlSugar.Base;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EasyBL
{
public class LogService : ServiceBase
{
public static readonly ILog mo_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//記錄異常
///
/// (會計)取消審核
///
/// todo: describe i_crm parameter on ErrorMessage
///
public new ResponseMessage ErrorMessage(RequestMessage i_crm)
{
ResponseMessage rm = null;
string sMsg = null;
try
{
var sErrorSource = _fetchString(i_crm, "ErrorSource");
var sErrorlineNO = _fetchString(i_crm, "Errorlineno");
var sErrorcolNO = _fetchString(i_crm, "Errorcolno");
var sErrorMessage = _fetchString(i_crm, nameof(ErrorMessage));
MailSend(sErrorMessage, null, i_crm.ORIGID, i_crm.USERID, "", "", "", sErrorSource, sErrorlineNO, sErrorcolNO);
rm = new SuccessResponseMessage(null, i_crm);
}
catch (Exception ex)
{
sMsg = Util.GetLastExceptionMsg(ex);
}
finally
{
if (null != sMsg)
{
rm = new ErrorResponseMessage(sMsg, i_crm);
}
}
return rm;
}
public static void MailSend(string sErrorMessage, Exception Exception, string sOrgID, string sUserID, string sProgramId, string sProgramName, string sFunctionName, string sErrorSource, string sErrorlineNO, string sErrorcolNO)
{
LogService.mo_Log.Error(sProgramName + sFunctionName + " Error:" + sErrorMessage, Exception);
var db = SugarBase.GetIntance();
string sError = null;
string sUserFromName = null;
var sEmailBody = "";
if (string.IsNullOrWhiteSpace(sProgramId))
{
//拆分JS錯誤來源網址
var saErrorSource = sErrorSource.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
sProgramId = saErrorSource.LastOrDefault();
}
if (string.IsNullOrWhiteSpace(sProgramName))
{
sProgramName = "";
}
if (string.IsNullOrWhiteSpace(sProgramName))
{
sFunctionName = "";
}
if (Exception != null)
{
sErrorMessage += "
" + Exception.ToString();
}
//查詢工程師郵件,拆分維護工程師郵件
var saEmails = Common.GetSystemSetting(db, sOrgID, "ErrorEngineer").Split(new string[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);
//獲取Email郵件格式
var oErrorMessage = db.Queryable().Single(it => it.OrgID == sOrgID && it.EmailID == nameof(ErrorMessage));
if (oErrorMessage != null)
{
if (!string.IsNullOrWhiteSpace(sUserID))
{
var oUserFrom = db.Queryable().Single(it => it.OrgID == sOrgID && it.MemberID == sUserID);
if (oUserFrom != null)
{
sUserFromName = oUserFrom.MemberName;
}
}
//寄信開始
foreach (string email in saEmails)
{
//利用郵件獲取工程師ID和名稱
var oUserTo = db.Queryable().Single(it => it.OrgID == sOrgID && it.Email == email);
if (oUserTo == null)
{
oUserTo = new OTB_SYS_Members();
}
sEmailBody = oErrorMessage.BodyHtml.Replace("{{:UserName}}", oUserTo.MemberName)
.Replace("{{:UseMember}}", sUserFromName ?? sUserID)
.Replace("{{:FunctionName}}", sFunctionName)
.Replace("{{:ErrorRow}}", sErrorlineNO)
.Replace("{{:ErrorColumn}}", sErrorcolNO)
.Replace("{{:ProgramId}}", sProgramId)
.Replace("{{:ProgramName}}", sProgramName)
.Replace("{{:error}}", sErrorMessage);
var oEmail = new Emails();
var saEmailTo = new List(); //收件人
var oEmailTo = new EmailTo
{
ToUserID = oUserTo.MemberID,
ToUserName = oUserTo.MemberName,
ToEmail = email,
Type = "to"
};
saEmailTo.Add(oEmailTo);
oEmail.FromUserName = "系統自動發送";//取fonfig
oEmail.Title = "奕達運通管理系統錯誤信息派送";//取fonfig
oEmail.EmailBody = sEmailBody;
oEmail.IsCCSelf = false;
oEmail.Attachments = null;
oEmail.EmailTo = saEmailTo;
var bSend = new MailService(sOrgID, true).MailFactory(oEmail, out sError);
}
}
}
}
}