using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Timers; using System.IO; using System.Data; using System.Data.Common; using System.Data.SqlClient; using WebLib; namespace Education { public class FollowService { private static FollowService _instance = null; private Timer _timer = null; private static int _interval = 30; private static string _doWorkTime = "00:00"; private static bool _enabled = false; private static bool _busy = false; private static 發送郵件 發送郵件 = new 發送郵件(); private DAC_BPSN Dao員工; private DbConnection conn; /// /// 發動時間,預設值為 00:00 /// public static string DoWorkTime { get { return _doWorkTime; } set { _doWorkTime = value; } } /// /// 郵件模版路徑 /// public static string TemplatePath { get; set; } /// /// 設定或取得簽核跟催服務是否啟動 /// public static bool Enabled { get { return _enabled; } set { if (value) { if (_enabled) return; Instance._timer.Interval = _interval * 1000; Instance._timer.AutoReset = true; Instance._timer.Enabled = true; _enabled = true; } else { if (!_enabled) return; _enabled = false; Instance._timer.Enabled = false; } } } private FollowService() { _timer = new Timer(); _timer.Enabled = false; _timer.Elapsed += new ElapsedEventHandler(DoFollowWork); conn = DAC.NewConnection(); Dao員工 = new DAC_BPSN(conn); } public static FollowService Instance { get { if (_instance == null) _instance = new FollowService(); return _instance; } } private void DoFollowWork(object sender, ElapsedEventArgs e) { string nowTime = DateTime.UtcNow.AddHours(8).ToString("HH:mm"); if (nowTime.CompareTo(_doWorkTime) == 0 && !_busy) { _busy = true; try { DAC_表單簽核明細 dao表單簽核明細 = new DAC_表單簽核明細(conn); 表單簽核明細List first = dao表單簽核明細.Select第一次稽催(); 表單簽核明細List second = dao表單簽核明細.Select第二次稽催(); 表單簽核明細List third = dao表單簽核明細.Select第三次稽催(); foreach (表單簽核明細 item in first) { SendFollowMail(item, 3); item.稽催狀態 = 1; dao表單簽核明細.UpdateOne(item); } foreach (表單簽核明細 item in second) { SendFollowMail(item, 7); item.稽催狀態 = 2; dao表單簽核明細.UpdateOne(item); } foreach (表單簽核明細 item in third) { SendFollowMail(item, 14); item.稽催狀態 = 3; dao表單簽核明細.UpdateOne(item); } DoSurveyReminderWork(); } finally { _busy = false; } } } private void SendFollowMail(表單簽核明細 item, int 天數) { string 電子郵件 = Dao員工.Get電子郵件(item.簽核人員); string 員工姓名 = Dao員工.Get員工姓名(item.簽核人員); 發送郵件 發送郵件 = new 發送郵件(); 發送郵件.發送簽核跟催郵件(item.簽核ID, 員工姓名, 電子郵件, 天數, TemplatePath); } private void DoSurveyReminderWork() { if (string.IsNullOrEmpty(PublicVariable.SiteBaseUrl)) return; string 手機版網址 = PublicVariable.SiteBaseUrl + "/MobileSurvey/Login.aspx"; int deadlineDays = PublicVariable.SurveyReminderDeadlineDays; DateTime minDate = PublicVariable.SurveyReminderMinCourseDate; string[] excludeEmps = PublicVariable.SurveyReminderExcludeEmployees .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); DbCommand cmd = conn.CreateCommand(); cmd.CommandText = @"select A.學員編號, A.訓練課程編號, C.BPNME as 學員姓名, C.BPEML as 電子郵件, B.顯示編號, B.訓練名稱, B.訓練日期, B.訓練日期迄 from 課程報名 A inner join 訓練課程企劃 B on A.訓練課程編號 = B.編號 inner join BPSNView C on A.學員編號 = C.BPENO where (A.報名狀態 = 'Y' or A.報名狀態 = 'R') and B.訓練日期 >= @MinCourseDate and (A.滿意度催填狀態 is null or A.滿意度催填狀態 = '') and not exists ( select 1 from 滿意度調查 D where D.訓練課程企劃編號 = A.訓練課程編號 and D.學員編號 = A.學員編號 ) and dateadd(day, @DeadlineDays, B.訓練日期迄) < getdate() order by A.學員編號"; cmd.Parameters.Add(new SqlParameter("@MinCourseDate", minDate)); cmd.Parameters.Add(new SqlParameter("@DeadlineDays", deadlineDays)); DataTable dt = new DataTable(); using (var reader = cmd.ExecuteReader()) { dt.Load(reader); } foreach (DataRow row in dt.Rows) { string 學員編號 = DAC.GetString(row["學員編號"]); if (excludeEmps.Contains(學員編號)) continue; string 學員姓名 = DAC.GetString(row["學員姓名"]); string 電子郵件 = DAC.GetString(row["電子郵件"]); string 訓練名稱 = DAC.GetString(row["訓練名稱"]); DateTime 訓練日期 = DAC.GetDateTime(row["訓練日期"]); DateTime 訓練日期迄 = DAC.GetDateTime(row["訓練日期迄"]); int 訓練課程編號 = DAC.GetInt32(row["訓練課程編號"]); 發送郵件 發送郵件 = new 發送郵件(); 發送郵件.發送滿意度催填郵件(學員姓名, 電子郵件, 訓練名稱, 訓練日期, 訓練日期迄, 手機版網址); DbCommand cmdUpd = conn.CreateCommand(); cmdUpd.CommandText = "update 課程報名 set 滿意度催填狀態 = 'Y'" + " where 訓練課程編號 = @訓練課程編號 and 學員編號 = @學員編號"; cmdUpd.Parameters.Add(new SqlParameter("@訓練課程編號", 訓練課程編號)); cmdUpd.Parameters.Add(new SqlParameter("@學員編號", 學員編號)); cmdUpd.ExecuteNonQuery(); } } } }