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.

685 lines
31 KiB

2 years ago
  1. using Entity.Sugar;
  2. using Euro.Transfer.Base;
  3. using Euro.Transfer.Model;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using EasyBL;
  13. using System.Net.Mail;
  14. namespace Euro.Transfer.Jobs.TaskTips
  15. {
  16. enum JobType
  17. {
  18. TaskTips, RunEIPTips, RunAttendanceLists, RunCreateAttendanceTips, RunClearFiles, CheckAnnualleaveAndMail
  19. }
  20. public class Job : ServiceTask
  21. {
  22. /// <summary>
  23. /// 任务开始
  24. /// </summary>
  25. protected override void Start()
  26. {
  27. try
  28. {
  29. //运行中
  30. this.mIsRunning = true;
  31. var db = SugarBase.GetIntance();
  32. var sCurrTime = DateTime.Now.ToString("HH:mm");
  33. var sTaskTipsTimeID = Common.ConfigGetValue("", "TaskTipsTimeID");
  34. var sEIPTipsTimeID = Common.ConfigGetValue("", "EIPTipsTimeID");
  35. var sAttendanceTime = Common.ConfigGetValue("", "ReadAttendanceTime");
  36. var sCreateAttendanceTips = Common.ConfigGetValue("", "CreateAttendanceTipsTime");
  37. var sClearFilesTime = Common.ConfigGetValue("", "ClearFilesTime");
  38. var CheckAnnualleave = Common.ConfigGetValue("", "ReadWenZhongAnnualleave");
  39. var saSetTask = db.Queryable<OTB_SYS_SystemSetting>().Where(it => it.Effective == "Y" && it.SettingItem == sTaskTipsTimeID).ToList();
  40. if (saSetTask.Count > 0)
  41. {
  42. foreach (OTB_SYS_SystemSetting set in saSetTask)
  43. {
  44. if (set.SettingValue.IndexOf(sCurrTime) > -1)
  45. {
  46. //执行工作项
  47. this.RunTaskTips(set.OrgID);
  48. }
  49. }
  50. }
  51. var saSetEIP = db.Queryable<OTB_SYS_SystemSetting>().Where(it => it.Effective == "Y" && it.SettingItem == sEIPTipsTimeID).ToList();
  52. if (saSetEIP.Count > 0)
  53. {
  54. foreach (OTB_SYS_SystemSetting set in saSetEIP)
  55. {
  56. if (set.SettingValue.IndexOf(sCurrTime) > -1)
  57. {
  58. //执行工作项
  59. this.RunEIPTips(set.OrgID);
  60. }
  61. }
  62. }
  63. if (sAttendanceTime.IndexOf(sCurrTime) > -1)
  64. {
  65. WriteTaskLog("Start:" + JobType.RunAttendanceLists.ToString(), JobType.RunAttendanceLists);//記錄所有log
  66. //讀取卡鐘打卡資料
  67. this.RunAttendanceLists();
  68. WriteTaskLog("End:" + JobType.RunAttendanceLists.ToString(), JobType.RunAttendanceLists);//記錄所有log
  69. }
  70. if (sCurrTime == sCreateAttendanceTips)
  71. {
  72. //產生未打卡定時提醒資料
  73. this.RunCreateAttendanceTips();
  74. }
  75. if (sCurrTime == sClearFilesTime)
  76. {
  77. //执行清理廢除文件工作项
  78. this.RunClearFiles();
  79. }
  80. if (sCurrTime == CheckAnnualleave)
  81. {
  82. WriteTaskLog("Start:" + JobType.CheckAnnualleaveAndMail.ToString(), JobType.CheckAnnualleaveAndMail);
  83. //檢查特休
  84. CheckAnnualleaveAndMail();
  85. WriteTaskLog("End:" + JobType.CheckAnnualleaveAndMail.ToString(), JobType.CheckAnnualleaveAndMail);
  86. }
  87. }
  88. catch (Exception error)
  89. {
  90. ErrorLog(error);
  91. }
  92. finally
  93. {
  94. //空闲
  95. this.mIsRunning = false;
  96. }
  97. }
  98. /// <summary>
  99. /// 任务停止
  100. /// </summary>
  101. protected override void Stop()
  102. {
  103. this.mIsRunning = false;
  104. }
  105. #region 执行代辦提醒邏輯
  106. /// <summary>
  107. /// 执行代辦提醒邏輯
  108. /// </summary>
  109. /// <param name="sOrgID">todo: describe sOrgID parameter on RunTaskTips</param>
  110. protected void RunTaskTips(string sOrgID)
  111. {
  112. try
  113. {
  114. do
  115. {
  116. var db = SugarBase.GetIntance();
  117. var saTasks = db.Queryable<OTB_SYS_Task, OTB_SYS_Members>((t, m) => t.OrgID == m.OrgID && t.CreateUser == m.MemberID)
  118. .Select((t, m) => new OTB_SYS_Task
  119. {
  120. OrgID = t.OrgID,
  121. EventID = t.EventID,
  122. EventName = t.EventName,
  123. Owner = t.Owner,
  124. StartDate = t.StartDate,
  125. EndDate = t.EndDate,
  126. Important = SqlFunc.IIF(t.Important == "M", "普通", "重要"),
  127. CreateUser = m.MemberName,
  128. TaskDescription = t.TaskDescription,
  129. SourceFrom = t.SourceFrom,
  130. Params = t.Params
  131. })
  132. .Where(t => t.OrgID == sOrgID &&
  133. ((t.Status == "U" && t.AlertTime == null) || (t.Status != "O" && t.Status != "D" && t.AlertTime >= DateTime.Now))).ToList();
  134. if (saTasks.Count > 0)
  135. {
  136. var listMessages = new List<Message>();
  137. foreach (OTB_SYS_Task task in saTasks)
  138. {
  139. var msg = new Message
  140. {
  141. Type = MessageType.SendToUser,
  142. Memo = "tips",
  143. ToOrgId = task.OrgID,
  144. ToUserId = task.Owner
  145. };
  146. if (!listMessages.Any(x => (x.ToOrgId == msg.ToOrgId && x.ToUserId == msg.ToUserId)))
  147. {
  148. listMessages.Add(msg);
  149. }
  150. }
  151. foreach (Message message in listMessages)
  152. {
  153. if (hubClient.OnlineUsers.Any(x => (x.OrgId == message.ToOrgId && x.UserId == message.ToUserId)))
  154. {
  155. var listTaskInfos = new List<TaskInfo>();
  156. foreach (OTB_SYS_Task _task in saTasks)
  157. {
  158. var sOrgId = _task.OrgID;
  159. var sUserId = _task.Owner;
  160. if (message.ToOrgId == sOrgId && message.ToUserId == sUserId)
  161. {
  162. var task = new TaskInfo
  163. {
  164. OrgID = sOrgId,
  165. Owner = sUserId,
  166. StartDate = _task.StartDate,
  167. EndDate = _task.EndDate == null ? _task.StartDate : _task.EndDate,
  168. EventID = _task.EventID,
  169. EventName = _task.EventName,
  170. Important = _task.Important,
  171. CreateUser = _task.CreateUser,
  172. SourceFrom = _task.SourceFrom,
  173. Params = _task.Params
  174. };
  175. listTaskInfos.Add(task);
  176. }
  177. }
  178. message.Content = ServiceBase.JsonToString(listTaskInfos);
  179. hubClient.msgProxy.Invoke("Send", message);
  180. }
  181. }
  182. }
  183. } while (false);
  184. }
  185. catch (Exception error)
  186. {
  187. ErrorLog(error);
  188. }
  189. }
  190. #endregion 执行代辦提醒邏輯
  191. #region 执行考勤未打卡提醒邏輯
  192. /// <summary>
  193. /// 执行考勤未打卡提醒邏輯
  194. /// </summary>
  195. /// <param name="sOrgID">todo: describe sOrgID parameter on RunEIPTips</param>
  196. protected void RunEIPTips(string sOrgID)
  197. {
  198. try
  199. {
  200. do
  201. {
  202. var db = SugarBase.GetIntance();
  203. var rNow = DateTime.Now;
  204. var rStart = DateTime.Now;
  205. var rEnd = DateTime.Now;
  206. if (rNow.Day <= 5)
  207. {
  208. rStart = rStart.AddMonths(-1);
  209. rStart = new DateTime(rStart.Year, rStart.Month, 1);
  210. }
  211. else
  212. {
  213. rStart = new DateTime(rNow.Year, rNow.Month, 1);
  214. }
  215. var saClockTips = db.Queryable<OTB_SYS_ClockTips>().Where(x => x.OrgID == sOrgID && x.TipsDate >= rStart && x.TipsDate <= rEnd).ToList();
  216. if (saClockTips.Count > 0)
  217. {
  218. var listMessages = new List<Message>();
  219. foreach (OTB_SYS_ClockTips tips in saClockTips)
  220. {
  221. var msg = new Message
  222. {
  223. Type = MessageType.SendToUser,
  224. Memo = "attendance",
  225. ToOrgId = tips.OrgID,
  226. ToUserId = tips.Owner
  227. };
  228. if (!listMessages.Any(x => (x.ToOrgId == msg.ToOrgId && x.ToUserId == msg.ToUserId)))
  229. {
  230. listMessages.Add(msg);
  231. }
  232. }
  233. foreach (Message message in listMessages)
  234. {
  235. if (hubClient.OnlineUsers.Any(x => (x.OrgId == message.ToOrgId && x.UserId == message.ToUserId)))
  236. {
  237. var listClockTips = new List<ClockTipsInfo>();
  238. foreach (OTB_SYS_ClockTips _tips in saClockTips)
  239. {
  240. var sOrgId = _tips.OrgID;
  241. var sUserId = _tips.Owner;
  242. if (message.ToOrgId == sOrgId && message.ToUserId == sUserId)
  243. {
  244. var tips = new ClockTipsInfo
  245. {
  246. OrgID = sOrgId,
  247. Owner = sUserId,
  248. NO = _tips.NO,
  249. Type = _tips.Type,
  250. Title = _tips.Title,
  251. Content = _tips.Content,
  252. Url = _tips.Url,
  253. CreateUser = _tips.CreateUser,
  254. CreateDate = _tips.CreateDate,
  255. Parm = ""
  256. };
  257. listClockTips.Add(tips);
  258. }
  259. }
  260. message.Content = ServiceBase.JsonToString(listClockTips);
  261. hubClient.msgProxy.Invoke("Send", message);
  262. }
  263. }
  264. }
  265. } while (false);
  266. }
  267. catch (Exception error)
  268. {
  269. ErrorLog(error);
  270. }
  271. }
  272. #endregion 执行考勤未打卡提醒邏輯
  273. #region 轉入文字檔案
  274. /// <summary>
  275. /// 轉入文字檔案
  276. /// </summary>
  277. protected void RunAttendanceLists()
  278. {
  279. try
  280. {
  281. do
  282. {
  283. var db = SugarBase.GetIntance();
  284. var ConfigSettings = new List<string>() { Common.ConfigGetValue("", "WorkTimePMKey"), Common.ConfigGetValue("", "WorkTimeAMKey")
  285. ,Common.ConfigGetValue("", "LatestShiftTimeKey"), Common.ConfigGetValue("", "DelayBufferTimeKey") };
  286. var sAttendanceFilePath = Common.ConfigGetValue("", "AttendancePath");//打卡資料存放路徑
  287. var AttendanceMethod = new AttendanceMethods();
  288. var AttendanceRules = AttendanceMethod.GetAttendanceRule(db, ConfigSettings);
  289. //取得昨天、今日打卡名稱。
  290. var LoadingDates = new DateTime[] { DateTime.Now.Date.AddDays(-1), DateTime.Now.Date };
  291. var DeleteStartDate = LoadingDates[0].Date;
  292. var DeleteEndDate = LoadingDates[1].Date.AddSeconds(24 * 60 * 60 - 1);
  293. var i = db.Deleteable<OTB_EIP_Attendance>().Where(x => x.CardDate.Date >= DeleteStartDate && x.CardDate.Date <= DeleteEndDate).ExecuteCommand();
  294. WriteTaskLog("已刪除:" + i.ToString() + "筆,從" + DeleteStartDate + "到" + DeleteEndDate, JobType.RunAttendanceLists);
  295. var saAttendanceAdd = new List<OTB_EIP_Attendance>();
  296. foreach (var date in LoadingDates)
  297. {
  298. var CurrentDay = date.ToString("yyyyMMdd");
  299. var LoadingFilesPath = sAttendanceFilePath + CurrentDay + ".Txt";
  300. if (File.Exists(LoadingFilesPath))
  301. {
  302. var AllLineDatas = File.ReadAllLines(LoadingFilesPath, Encoding.Default).Distinct().ToArray();
  303. //固定時間為13點。
  304. var CardDate = date.AddHours(13);
  305. var saCardAttendanceInfo = AttendanceMethod.CalculateAttendance(AllLineDatas, AttendanceRules, CardDate).OrderBy(c => c.CardId).ToList();
  306. var saMembers = db.Queryable<OTB_SYS_Members>().Where(it => it.Effective == "Y" && it.IsAttendance == true).ToList();
  307. //遍歷所有需要考勤的人,補全打卡信息用於初始化打卡資料
  308. foreach (OTB_SYS_Members member in saMembers)
  309. {
  310. if (saCardAttendanceInfo.Any(x => (x.CardId == member.CardId && member.OrgID == x.OrgID)))
  311. {
  312. var oAttendanceInfo = saCardAttendanceInfo.First(x => x.CardId == member.CardId && member.OrgID == x.OrgID);
  313. if (oAttendanceInfo.StatusA != null && oAttendanceInfo.StatusA == true)
  314. {
  315. oAttendanceInfo.Memo = "遲到";
  316. }
  317. if (oAttendanceInfo.StatusP != null && oAttendanceInfo.StatusP == true)
  318. {
  319. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "早退" : oAttendanceInfo.Memo + ",早退";
  320. }
  321. if (string.IsNullOrEmpty(oAttendanceInfo.SignOut))
  322. {
  323. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "(下班)未刷卡" : oAttendanceInfo.Memo + ",(下班)未刷卡";
  324. }
  325. oAttendanceInfo.UserID = member.MemberID;
  326. saAttendanceAdd.Add(oAttendanceInfo);
  327. }
  328. else
  329. {
  330. //需要打卡又沒有抓到打卡資料的人默認未打卡
  331. var oAttendanceInfo = new OTB_EIP_Attendance
  332. {
  333. OrgID = member.OrgID,
  334. UserID = member.MemberID,
  335. CardDate = CardDate,
  336. CardId = member.CardId,
  337. TimeA = "",
  338. TimeP = "",
  339. CardUserName = member.MemberName,
  340. Hours = "0",
  341. SignIn = "",
  342. SignOut = "",
  343. StatusP = true,
  344. StatusA = true,
  345. Memo = "未刷卡",
  346. CreateDate = DateTime.Now
  347. };
  348. saAttendanceAdd.Add(oAttendanceInfo);
  349. }
  350. }
  351. }
  352. }
  353. if (saAttendanceAdd.Count > 0)
  354. {
  355. db.Insertable(saAttendanceAdd).ExecuteCommand();
  356. WriteTaskLog("已新增:" + saAttendanceAdd.Count + "筆,從" + LoadingDates[0] + " 到 " + LoadingDates[1], JobType.RunAttendanceLists);
  357. }
  358. } while (false);
  359. }
  360. catch (Exception error)
  361. {
  362. ErrorLog(error);
  363. }
  364. }
  365. #endregion 轉入文字檔案
  366. #region 执行清理後台產生的廢除文件
  367. /// <summary>
  368. /// 执行清理後台產生的廢除文件
  369. /// </summary>
  370. protected void RunClearFiles()
  371. {
  372. try
  373. {
  374. do
  375. {
  376. var sClearFilesPath = Common.ConfigGetValue("", "ClearFilesPath");
  377. var rCurDate = DateTime.Now.AddDays(-7);
  378. if (Directory.Exists(sClearFilesPath))
  379. {
  380. var filePathArr = Directory.GetFiles(sClearFilesPath);
  381. var fileCreateDate = new Dictionary<string, DateTime>();
  382. foreach (string file in filePathArr)
  383. {
  384. var fi = new FileInfo(file);
  385. fileCreateDate[file] = fi.CreationTime;
  386. }
  387. fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
  388. foreach (KeyValuePair<string, DateTime> item in fileCreateDate)
  389. {
  390. if (item.Value <= rCurDate && File.Exists(item.Key))
  391. {
  392. File.Delete(item.Key);
  393. }
  394. }
  395. }
  396. } while (false);
  397. }
  398. catch (Exception error)
  399. {
  400. ErrorLog(error);
  401. }
  402. }
  403. #endregion 执行清理後台產生的廢除文件
  404. #region 產生未打卡定時提醒資料
  405. /// <summary>
  406. /// 產生未打卡定時提醒資料
  407. /// </summary>
  408. protected void RunCreateAttendanceTips()
  409. {
  410. try
  411. {
  412. do
  413. {
  414. var db = SugarBase.GetIntance();
  415. var sOrigID = Common.ConfigGetValue("", "TransferOrgID");
  416. var rNow = DateTime.Now;
  417. var rCurDate = rNow.AddDays(-1);
  418. var saClockTips_Add = new List<OTB_SYS_ClockTips>();
  419. var saAttendance = db.Queryable<OTB_EIP_Attendance>().Where(x => x.OrgID == sOrigID && x.CardDate.Date == rCurDate.Date).ToList();
  420. var sCurYear = rCurDate.Year.ToString();
  421. var oHolidays = db.Queryable<OTB_SYS_Holidays>().Single(x => x.OrgID == sOrigID && x.Year == sCurYear);
  422. foreach (var attendance in saAttendance)
  423. {
  424. if (attendance.SignIn == "" && oHolidays.Holidays.IndexOf(rCurDate.ToString("yyyy-MM-dd")) == -1)
  425. {
  426. if (CheckTips(db, rCurDate, attendance.OrgID, attendance.UserID))
  427. {
  428. var oClockTips = new OTB_SYS_ClockTips
  429. {
  430. OrgID = attendance.OrgID,
  431. ParentID = rCurDate.ToString("yyyyMMdd"),
  432. Owner = attendance.UserID,
  433. Type = nameof(attendance),
  434. Title = rCurDate.ToString("yyyy.MM.dd") + "日沒有打卡,也沒有對應的行事曆和EIP資訊,請至EIP填寫對應的申請單",
  435. Content = "",
  436. Url = "",
  437. CreateUser = "",
  438. TipsDate = rCurDate,
  439. CreateDate = rNow
  440. };
  441. saClockTips_Add.Add(oClockTips);
  442. }
  443. }
  444. }
  445. if (saClockTips_Add.Count > 0)
  446. {
  447. db.Insertable(saClockTips_Add).ExecuteCommand();
  448. }
  449. } while (false);
  450. }
  451. catch (Exception error)
  452. {
  453. ErrorLog(error);
  454. }
  455. }
  456. #endregion 產生未打卡定時提醒資料
  457. /// <summary>
  458. /// 檢核EIP申請資料與行事曆判斷是否需要填寫
  459. /// </summary>
  460. /// <param name="userId">结束时间</param>
  461. /// <param name="db">todo: describe db parameter on CheckTips</param>
  462. /// <param name="date">todo: describe date parameter on CheckTips</param>
  463. /// <param name="origId">todo: describe origId parameter on CheckTips</param>
  464. /// <returns>返回(秒)单位,比如: 0.00239秒</returns>
  465. private static bool CheckTips(SqlSugarClient db, DateTime date, string origId, string userId)
  466. {
  467. var bToTips = true;
  468. do
  469. {
  470. var iCount_Calendar = db.Queryable<OTB_SYS_Calendar>().Count(x => x.OrgID == origId && x.UserID == userId && !x.DelStatus && (x.StartDate.Date == date.Date || x.EndDate.Date == date.Date || (x.StartDate <= date && x.EndDate >= date)));
  471. if (iCount_Calendar > 0)
  472. {
  473. bToTips = false;
  474. break;
  475. }
  476. var sStatus = "B,E,H-O";
  477. //差勤異常
  478. var iCount_AttendanceDiff = db.Queryable<OTB_EIP_AttendanceDiff>()
  479. .Count(x => x.OrgID == origId && x.AskTheDummy == userId && sStatus.Contains(x.Status) && x.FillBrushDate.Value.Date == date.Date);
  480. if (iCount_AttendanceDiff > 0)
  481. {
  482. bToTips = false;
  483. break;
  484. }
  485. //請假單
  486. var iCount_Leave = db.Queryable<OTB_EIP_Leave>()
  487. .Count(x => x.OrgID == origId && x.AskTheDummy == userId && sStatus.Contains(x.Status) && (x.StartDate.Value.Date == date.Date || x.EndDate.Value.Date == date.Date || (x.StartDate <= date && x.EndDate >= date)));
  488. if (iCount_Leave > 0)
  489. {
  490. bToTips = false;
  491. break;
  492. }
  493. } while (false);
  494. return bToTips;
  495. }
  496. /// <summary>
  497. /// 程序执行时间测试
  498. /// </summary>
  499. /// <param name="dateBegin">开始时间</param>
  500. /// <param name="dateEnd">结束时间</param>
  501. /// <returns>返回(秒)单位,比如: 0.00239秒</returns>
  502. private static double ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
  503. {
  504. var ts1 = new TimeSpan(dateBegin.Ticks);
  505. var ts2 = new TimeSpan(dateEnd.Ticks);
  506. var ts3 = ts1.Subtract(ts2).Duration();
  507. //你想转的格式
  508. return ts3.TotalHours;
  509. }
  510. /// <summary>
  511. /// 檢查是否在時間區間內
  512. /// </summary>
  513. /// <param name="dateTime"></param>
  514. /// <param name="StartDT"></param>
  515. /// <param name="EndDT"></param>
  516. /// <returns></returns>
  517. public static bool CheckDateRange(DateTime dateTime, DateTime StartDT, DateTime EndDT)
  518. {
  519. var DataTicks = dateTime.Ticks;
  520. var StartTicks = StartDT.Ticks;
  521. var EndTicks = EndDT.Ticks;
  522. if (DataTicks >= StartTicks && DataTicks <= EndTicks)
  523. return true;
  524. else
  525. return false;
  526. }
  527. /// <summary>
  528. /// 特休提醒
  529. /// </summary>
  530. public static void CheckAnnualleaveAndMail()
  531. {
  532. var Job = JobType.CheckAnnualleaveAndMail;
  533. var TitleTemp = "<系統自動發信>{UserName}特休假即將失效通知";
  534. var ContentTemp = "您於{EnableDate}-{ExpirationDate}剩餘的特休假即將在{ExpirationDate}失效,如需遞延最晚請於失效日前1個月向管理部提出書面申請,逾期未申請屆期將直接換算薪資發放。";
  535. var ReplaceKeys = new { EnableDate = "{EnableDate}", ExpiredDate = "{ExpirationDate}", UserName = "{UserName}" };
  536. var db = SugarBase.DB;
  537. try
  538. {
  539. do
  540. {
  541. var StartDT = DateTime.Now.Date;
  542. var EndDT = StartDT.AddDays(45);
  543. var AvailableAnnualleaves = db.Queryable<OTB_EIP_WenZhong>()
  544. .Where(it => it.DelFlag == false && it.Notice == false && it.RemainHours > 0 && it.ExpirationDate.HasValue).ToList();
  545. var ToBeExpiredAL = AvailableAnnualleaves.Where(c => CheckDateRange(c.ExpirationDate.Value, StartDT, EndDT)).OrderBy(c => c.OrgID).ToList();
  546. if (ToBeExpiredAL.Count > 0)
  547. {
  548. var UpdateWenZhongs = new List<OTB_EIP_WenZhong>();
  549. var Users = db.Queryable<OTB_SYS_Members>().Where(it => it.Effective == "Y").ToList();
  550. var mailServices = new List<MailService>();
  551. var AllOrgIDs = ToBeExpiredAL.Select(c => c.OrgID).ToList().Distinct();
  552. foreach (var OrgID in AllOrgIDs)
  553. {
  554. mailServices.Add(new MailService(OrgID, false));
  555. }
  556. string LogMsg = @"";
  557. foreach (var Al in ToBeExpiredAL)
  558. {
  559. var FoundUser = Users.FirstOrDefault(c => c.Effective == "Y" && c.MemberID == Al.UserID && c.OrgID == Al.OrgID);
  560. if (FoundUser != null)
  561. {
  562. var OrgID = FoundUser.OrgID;
  563. var MailTitle = TitleTemp.Replace(ReplaceKeys.UserName, FoundUser.MemberName);
  564. var MailBody = ContentTemp.Replace(ReplaceKeys.EnableDate, Al.EnableDate.Value.ToString("yyyy/MM/dd"))
  565. .Replace(ReplaceKeys.ExpiredDate, Al.ExpirationDate.Value.ToString("yyyy/MM/dd"));
  566. using (MailMessage mail = new MailMessage())
  567. {
  568. var UseStmpEmailServer = mailServices.Where(c => c.OrgID == FoundUser.OrgID).First();
  569. mail.To.Add(FoundUser.Email);
  570. mail.From = new MailAddress(UseStmpEmailServer.mailSetting["FromEmail"].ToString(), UseStmpEmailServer.mailSetting["FromName"].ToString(), System.Text.Encoding.UTF8);
  571. mail.Subject = MailTitle;
  572. mail.SubjectEncoding = Encoding.UTF8;//郵件標題編碼
  573. mail.Body = MailBody; //郵件內容
  574. mail.BodyEncoding = Encoding.UTF8;//郵件內容編碼
  575. mail.IsBodyHtml = true;//是否是HTML郵件
  576. var bSend = UseStmpEmailServer.SendMailNET_NoSelf(mail);
  577. if (bSend)
  578. {
  579. LogMsg += "寄送特休通知給: " + Al.UserName + ",特休期間從" + Al.EnableDate.Value.ToString("yyyy/MM/dd") + " 到 " + Al.ExpirationDate.Value.ToString("yyyy/MM/dd") + Environment.NewLine;
  580. Al.Notice = true;
  581. UpdateWenZhongs.Add(Al);
  582. }
  583. else
  584. {
  585. LogMsg += UseStmpEmailServer.ErrorMessages;
  586. }
  587. }
  588. }
  589. }
  590. if (LogMsg.Length > 0)
  591. {
  592. WriteTaskLog(LogMsg, Job);
  593. }
  594. if (UpdateWenZhongs.Count > 0)
  595. {
  596. db.Updateable(UpdateWenZhongs).UpdateColumns(it => new { it.Notice }).ExecuteCommand();
  597. }
  598. }
  599. } while (false);
  600. }
  601. catch (Exception error)
  602. {
  603. ErrorLog(error);
  604. }
  605. }
  606. /// <summary>
  607. /// 輸出錯誤資訊
  608. /// </summary>
  609. /// <param name="error"></param>
  610. private static void ErrorLog(Exception error)
  611. {
  612. ServiceTools.WriteLog("", "Error:" + error.ToString() + "StackTrace:" + Environment.NewLine + error.StackTrace, true);
  613. }
  614. private static void WriteTaskLog(string cont, JobType job)
  615. {
  616. var FolderName = job.ToString();
  617. var path = System.Windows.Forms.Application.StartupPath.ToString() + @"\Tasks\" + FolderName + "\\";
  618. if (!Directory.Exists(path))
  619. {
  620. Directory.CreateDirectory(path);
  621. }
  622. path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  623. using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
  624. {
  625. sw.WriteLine(DateTime.Now);
  626. sw.WriteLine(cont);
  627. sw.WriteLine("");
  628. sw.Close();
  629. }
  630. }
  631. }
  632. }