136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Timers;
|
|
using System.Net.Mail;
|
|
using System.Data;
|
|
using Wellan.Data;
|
|
using System.Text;
|
|
using System.IO;
|
|
|
|
/// <summary>
|
|
/// 定時發送郵件
|
|
///
|
|
/// 1. 代理人代理前一天發送郵件通知
|
|
/// </summary>
|
|
public class MailTask
|
|
{
|
|
private Timer timer1;
|
|
private static MailTask instance = null;
|
|
|
|
private MailTask()
|
|
{
|
|
timer1 = new Timer();
|
|
timer1.Interval = 1000;
|
|
timer1.AutoReset = true;
|
|
timer1.Elapsed += Timer1_Elapsed;
|
|
}
|
|
|
|
private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
|
|
{
|
|
var nowTime = DateTime.UtcNow.AddHours(8).ToString("HHmmss");
|
|
|
|
// 每日定時工作
|
|
if (nowTime == "000000")
|
|
{
|
|
發代理人提醒郵件();
|
|
}
|
|
|
|
// 每小時定時工作
|
|
if (nowTime.EndsWith("0000"))
|
|
{
|
|
|
|
}
|
|
|
|
// 每分鐘定時工作
|
|
if (nowTime.EndsWith("00"))
|
|
{
|
|
|
|
}
|
|
}
|
|
private static void EnsureInstance()
|
|
{
|
|
if (instance == null)
|
|
instance = new MailTask();
|
|
}
|
|
|
|
public static bool Enabled
|
|
{
|
|
get
|
|
{
|
|
EnsureInstance();
|
|
return instance.timer1.Enabled;
|
|
}
|
|
set
|
|
{
|
|
EnsureInstance();
|
|
instance.timer1.Enabled = value;
|
|
}
|
|
}
|
|
|
|
private void 發代理人提醒郵件()
|
|
{
|
|
RparDAC dao = new RparDAC();
|
|
var notofyList = dao.代理人提醒(DateTime.UtcNow.AddHours(8).Date.AddDays(1));
|
|
|
|
// RPPYN 年度接單編號
|
|
// RPREN 單號
|
|
// RPNUM 差假員工編號
|
|
// BPNME 差假員工姓名
|
|
// BPEML 差假員工電郵
|
|
// RPTYP 差假類別代碼
|
|
// BSSCR 差假類別名稱
|
|
// RPAGT 代理人姓名
|
|
// RPAGM 代理人電郵
|
|
// RPRDT 差假開始日期
|
|
// RPRTM 差假開始時間
|
|
// RPEDT 差假結束日期
|
|
// RPETM 差假結束時間
|
|
|
|
foreach (DataRow r in notofyList.Rows)
|
|
{
|
|
var 單號 = WDAC.GetStringValue(r["RPREN"]);
|
|
var 類別 = WDAC.GetStringValue(r["RPTYP"]);
|
|
|
|
switch (類別.Substring(0, 1))
|
|
{
|
|
// 請假
|
|
case RPAR_TYPE.T0_請假:
|
|
類別 = "請假";
|
|
break;
|
|
// 差旅
|
|
case RPAR_TYPE.T3_出差:
|
|
類別 = "差旅";
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
MailMessage mm = new MailMessage();
|
|
mm.Subject = String.Format("{1}代理提醒 [單號:{0}]", 單號, 類別);
|
|
mm.To.Add(new MailAddress(WDAC.GetStringValue(r["RPAGM"])));
|
|
mm.BodyEncoding = Encoding.UTF8;
|
|
mm.IsBodyHtml = true;
|
|
mm.Body = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Template/代理人提醒.html"))
|
|
.Replace("{類別}", 類別)
|
|
.Replace("{單號}", 單號)
|
|
.Replace("{差勤人}", WDAC.GetStringValue(r["BPNME"]))
|
|
.Replace("{事由}", 類別 == "請假" ? WDAC.GetStringValue(r["RPRSN"]) : WDAC.GetStringValue(r["RPDOC"]))
|
|
.Replace("{起日}", String.Format("{0:yyyy-MM-dd}", r["RPRDT"]))
|
|
.Replace("{起時}", String.Format("{0}", r["RPRTM"]))
|
|
.Replace("{迄日}", String.Format("{0:yyyy-MM-dd}", r["RPEDT"]))
|
|
.Replace("{迄時}", String.Format("{0}", r["RPETM"]))
|
|
;
|
|
// 發送郵件
|
|
try
|
|
{
|
|
Wellan.Service.WMailSender.GetInstance().Send(mm, string.Format("代理提醒 單號:{0}", 單號));
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
}
|
|
|
|
} |