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.

759 lines
41 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyBL;
  3. using Entity;
  4. using Entity.Sugar;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using SqlSugar;
  8. using SqlSugar.Base;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. namespace EasyBL.WEBAPP.EIP
  15. {
  16. public class Attendance_QryService : ServiceBase
  17. {
  18. #region 查詢資料
  19. /// <summary>
  20. /// 查詢資料
  21. /// </summary>
  22. /// <param name="i_crm">參數</param>
  23. /// <returns></returns>
  24. public ResponseMessage QueryPage(RequestMessage i_crm)
  25. {
  26. ResponseMessage rm = null;
  27. string sMsg = null;
  28. var db = SugarBase.GetIntance();
  29. try
  30. {
  31. do
  32. {
  33. var pml = new PageModel
  34. {
  35. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  36. PageSize = _fetchInt(i_crm, @"pageSize")
  37. };
  38. var iPageCount = 0;
  39. var sSortField = _fetchString(i_crm, @"sortField");
  40. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  41. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  42. var sDateStart = _fetchString(i_crm, @"DateStart");
  43. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  44. if (!string.IsNullOrEmpty(sUserIDs))
  45. {
  46. sUserIDs = @"|" + sUserIDs.Replace(@",", @"|") + @"|";
  47. }
  48. var rDateStart = new DateTime();
  49. var rDateEnd = new DateTime();
  50. if (!string.IsNullOrEmpty(sDateStart))
  51. {
  52. rDateStart = SqlFunc.ToDate(sDateStart);
  53. }
  54. if (!string.IsNullOrEmpty(sDateEnd))
  55. {
  56. rDateEnd = SqlFunc.ToDate(sDateEnd);
  57. }
  58. var saRoles = db.Queryable<OTB_SYS_MembersToRule>()
  59. .Where(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID).Select(it => it.RuleID).ToList();
  60. var sRoles = JsonToString(saRoles);
  61. //var sdb = new SimpleClient<OTB_EIP_Leave>(DBUnit.Instance);
  62. pml.DataList = db.Queryable<OTB_EIP_Attendance>().OrderBy(sSortField, sSortOrder).Where(it => it.OrgID == i_crm.ORIGID)
  63. .WhereIF(!string.IsNullOrEmpty(sUserIDs), it => sUserIDs.Contains(@"|" + it.UserID + @"|"))
  64. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), it => it.CardDate.Date >= rDateStart.Date)
  65. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), it => it.CardDate.Date <= rDateEnd.Date)
  66. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), it => it.CardDate.Date >= rDateStart.Date && it.CardDate.Date <= rDateEnd.Date)
  67. .Where(@" (UserID = @UserID
  68. OR CHARINDEX('EipManager', @Roles)> 0
  69. OR CHARINDEX('EipView', @Roles)> 0
  70. OR CHARINDEX('Admin', @Roles)> 0)", new { UserID = i_crm.USERID, Roles = sRoles }).ToPageList(pml.PageIndex, pml.PageSize, ref iPageCount);
  71. pml.Total = iPageCount;
  72. rm = new SuccessResponseMessage(null, i_crm);
  73. rm.DATA.Add(BLWording.REL, pml);
  74. } while (false);
  75. }
  76. catch (Exception ex)
  77. {
  78. sMsg = Util.GetLastExceptionMsg(ex);
  79. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"QueryPage(查詢資料)", @"", @"", @"");
  80. }
  81. finally
  82. {
  83. if (null != sMsg)
  84. {
  85. rm = new ErrorResponseMessage(sMsg, i_crm);
  86. }
  87. }
  88. return rm;
  89. }
  90. #endregion 查詢資料
  91. #region 匯出資料
  92. /// <summary>
  93. /// 匯出資料
  94. /// </summary>
  95. /// <param name="i_crm">參數</param>
  96. /// <returns></returns>
  97. public ResponseMessage GetExcel(RequestMessage i_crm)
  98. {
  99. ResponseMessage rm = null;
  100. string sMsg = null;
  101. var db = SugarBase.GetIntance();
  102. try
  103. {
  104. do
  105. {
  106. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  107. var sDateStart = _fetchString(i_crm, @"DateStart");
  108. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  109. if (!string.IsNullOrEmpty(sUserIDs))
  110. {
  111. sUserIDs = @"|" + sUserIDs.Replace(@",", @"|") + @"|";
  112. }
  113. var rDateStart = new DateTime();
  114. var rDateEnd = new DateTime();
  115. if (!string.IsNullOrEmpty(sDateStart))
  116. {
  117. rDateStart = SqlFunc.ToDate(sDateStart);
  118. }
  119. if (!string.IsNullOrEmpty(sDateEnd))
  120. {
  121. rDateEnd = SqlFunc.ToDate(sDateEnd);
  122. }
  123. var saRoles = db.Queryable<OTB_SYS_MembersToRule>()
  124. .Where(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID).Select(it => it.RuleID).ToList();
  125. var sRoles = JsonToString(saRoles);
  126. var saAttendance = db.Queryable<OTB_EIP_Attendance>().OrderBy(x => x.CardDate).Where(it => it.OrgID == i_crm.ORIGID)
  127. .WhereIF(!string.IsNullOrEmpty(sUserIDs), it => sUserIDs.Contains(@"|" + it.UserID + @"|"))
  128. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), it => it.CardDate >= rDateStart)
  129. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), it => it.CardDate <= rDateEnd)
  130. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), it => it.CardDate >= rDateStart && it.CardDate <= rDateEnd)
  131. .Where(@" (UserID = @UserID
  132. OR CHARINDEX('EipManager', @Roles)> 0
  133. OR CHARINDEX('EipView', @Roles)> 0
  134. OR CHARINDEX('Admin', @Roles)> 0)", new { UserID = i_crm.USERID, Roles = sRoles }).ToList();
  135. var saExcelData = new List<Dictionary<string, object>>();
  136. var mStatus = new Map() { { @"B", @"審核中" }, { @"E", @"待經辦" }, { @"H-O", @"已經辦" } };
  137. var saStatus = new string[] { @"B", @"E", @"H-O" };
  138. var iIndex = 1;
  139. var saLeave = db.Queryable<OTB_EIP_Leave>()
  140. .Where(x => x.OrgID == i_crm.ORIGID)
  141. .WhereIF(!string.IsNullOrEmpty(sUserIDs), x => sUserIDs.Contains(@"|" + x.AskTheDummy + @"|"))
  142. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), x => x.EndDate >= SqlFunc.ToDate(sDateStart))
  143. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), x => x.StartDate <= rDateEnd)
  144. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), x => x.EndDate >= SqlFunc.ToDate(sDateStart) && x.StartDate <= rDateEnd).ToList();
  145. var saBusinessTravel = db.Queryable<OTB_EIP_BusinessTravel>()
  146. .Where(x => x.OrgID == i_crm.ORIGID)
  147. .WhereIF(!string.IsNullOrEmpty(sUserIDs), x => sUserIDs.Contains(@"|" + x.AskTheDummy + @"|"))
  148. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), x => x.EndDate >= SqlFunc.ToDate(sDateStart))
  149. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), x => x.StartDate <= rDateEnd)
  150. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), x => x.EndDate >= SqlFunc.ToDate(sDateStart) && x.StartDate <= rDateEnd).ToList();
  151. var saAttendanceDiff = db.Queryable<OTB_EIP_AttendanceDiff>()
  152. .Where(x => x.OrgID == i_crm.ORIGID)
  153. .WhereIF(!string.IsNullOrEmpty(sUserIDs), x => sUserIDs.Contains(@"|" + x.AskTheDummy + @"|"))
  154. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), x => x.FillBrushDate >= SqlFunc.ToDate(sDateStart))
  155. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), x => x.FillBrushDate <= rDateEnd)
  156. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), x => x.FillBrushDate >= SqlFunc.ToDate(sDateStart) && x.FillBrushDate <= rDateEnd).ToList();
  157. foreach (OTB_EIP_Attendance attendance in saAttendance)
  158. {
  159. if (string.IsNullOrEmpty(attendance.SignIn))
  160. {
  161. attendance.SignIn = "------";
  162. }
  163. if (string.IsNullOrEmpty(attendance.SignOut))
  164. {
  165. attendance.SignOut = "------";
  166. }
  167. var dicItem = new Dictionary<string, object>
  168. {
  169. { @"RowIndex", iIndex },
  170. { @"feild1", attendance.OrgID },
  171. { @"feild2", attendance.CardUserName },
  172. { @"feild3", Convert.ToDateTime(attendance.CardDate).ToString(@"yyyy/MM/dd") },
  173. { @"feild4", attendance.TimeA != @"" ? attendance.TimeA + @" / " + attendance.SignIn.Substring(0, 5) : @"-----" },
  174. { @"feild5", attendance.TimeP != @"" ? attendance.TimeP + @" / " + attendance.SignOut.Substring(0, 5) : @"-----" },
  175. { @"feild6", attendance.Hours },
  176. { @"feild7", attendance.Memo }
  177. };
  178. var oLeave = saLeave.Find(x => x.AskTheDummy == attendance.UserID && x.StartDate.Value.Date <= attendance.CardDate.Date && x.EndDate.Value.Date >= attendance.CardDate.Date && saStatus.Contains(x.Status));
  179. dicItem.Add(@"feild8", oLeave == null ? @"" : mStatus[oLeave.Status].ToString());
  180. var oBusinessTravel = saBusinessTravel.Find(x => x.AskTheDummy == attendance.UserID && x.StartDate.Value.Date <= attendance.CardDate.Date && x.EndDate.Value.Date >= attendance.CardDate.Date && saStatus.Contains(x.Status));
  181. dicItem.Add(@"feild9", oBusinessTravel == null ? @"" : mStatus[oBusinessTravel.Status].ToString());
  182. var oAttendanceDiff = saAttendanceDiff.Find(x => x.AskTheDummy == attendance.UserID && x.FillBrushDate.Value.Date == attendance.CardDate.Date && saStatus.Contains(x.Status));
  183. dicItem.Add(@"feild10", oAttendanceDiff == null ? @"" : mStatus[oAttendanceDiff.Status].ToString());
  184. saExcelData.Add(dicItem);
  185. iIndex++;
  186. }
  187. const string sFileName = @"考勤記錄資料";
  188. var dicHeader = new Dictionary<string, object>
  189. {
  190. { @"RowIndex", @"項次" },
  191. { @"feild1", @"組織代號" },
  192. { @"feild2", @"姓名" },
  193. { @"feild3", @"日期" },
  194. { @"feild4", @"上班/刷卡" },
  195. { @"feild5", @"下班/刷卡" },
  196. { @"feild6", @"時數" },
  197. { @"feild7", @"備註" },
  198. { @"feild8", @"請假" },
  199. { @"feild9", @"出差" },
  200. { @"feild10", @"加班" }
  201. };
  202. var bOk = new ExcelService().CreateExcel(saExcelData, out string sPath, dicHeader, sFileName);
  203. rm = new SuccessResponseMessage(null, i_crm);
  204. rm.DATA.Add(BLWording.REL, sPath);
  205. } while (false);
  206. }
  207. catch (Exception ex)
  208. {
  209. sMsg = Util.GetLastExceptionMsg(ex);
  210. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"GetExcel(匯出資料)", @"", @"", @"");
  211. }
  212. finally
  213. {
  214. if (null != sMsg)
  215. {
  216. rm = new ErrorResponseMessage(sMsg, i_crm);
  217. }
  218. }
  219. return rm;
  220. }
  221. #endregion 匯出資料
  222. #region 同步打卡資料
  223. /// <summary>
  224. /// 同步打卡資料
  225. /// </summary>
  226. /// <param name="i_crm">參數</param>
  227. /// <returns></returns>
  228. public ResponseMessage TransferEip(RequestMessage i_crm)
  229. {
  230. ResponseMessage rm = null;
  231. string sMsg = null;
  232. try
  233. {
  234. rm = SugarBase.ExecTran(db =>
  235. {
  236. do
  237. {
  238. var sDateStart = _fetchString(i_crm, @"DateStart");
  239. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  240. var bIsReSet = _fetchBool(i_crm, @"IsReSet");
  241. var sWorkTimePMKey = Common.ConfigGetValue(@"", @"WorkTimePMKey");//上班開始時間(系統設定key名稱)
  242. var sWorkTimeAMKey = Common.ConfigGetValue(@"", @"WorkTimeAMKey");//上班結束時間(系統設定key名稱)
  243. var sAttendancePath = Common.ConfigGetValue(@"", @"AttendancePath");//打卡資料存放路徑
  244. var sLatestShiftTimeKey = Common.ConfigGetValue("", "LatestShiftTimeKey");//工作時間(系統設定key名稱)
  245. var sDelayBufferTimeKey = Common.ConfigGetValue("", "DelayBufferTimeKey");//工作時間遲到緩衝key值
  246. var ConfigSettings = new List<string>() { sWorkTimePMKey, sWorkTimeAMKey, sLatestShiftTimeKey, sDelayBufferTimeKey };
  247. var saDates = new List<string>();
  248. var rDateStart = Convert.ToDateTime(sDateStart);
  249. var rDateEnd = Convert.ToDateTime(sDateEnd);
  250. var oWorkTimePM = db.Queryable<OTB_SYS_SystemSetting>().First(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y" && x.SettingItem == sWorkTimePMKey);
  251. var oWorkTimeAM = db.Queryable<OTB_SYS_SystemSetting>().First(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y" && x.SettingItem == sWorkTimeAMKey);
  252. var osLatestShiftTime = db.Queryable<OTB_SYS_SystemSetting>().First(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y" && x.SettingItem == sLatestShiftTimeKey);
  253. var DelayBuffer = db.Queryable<OTB_SYS_SystemSetting>().First(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y" && x.SettingItem == sDelayBufferTimeKey);
  254. var AttendanceMethod = new AttendanceMethods();
  255. var AttendanceRules = AttendanceMethod.GetAttendanceRule(db, ConfigSettings).Where( ar => ar.OrgID == i_crm.ORIGID).ToList();
  256. if (bIsReSet)
  257. {
  258. var i = db.Deleteable<OTB_EIP_Attendance>()
  259. .Where(x => x.OrgID == i_crm.ORIGID && x.CardDate.Date >= rDateStart.Date && x.CardDate.Date <= rDateEnd.Date).ExecuteCommand();
  260. }
  261. while (rDateStart <= rDateEnd)
  262. {
  263. var sCurrentdayStr = rDateStart.ToString(@"yyyyMMdd");
  264. var sCurrentPath = sAttendancePath + sCurrentdayStr + @".Txt";
  265. if (File.Exists(sCurrentPath))
  266. {
  267. var AllLine = File.ReadAllLines(sCurrentPath, Encoding.Default);
  268. var SelectedData = AllLine.Where(d => d.Contains(i_crm.ORIGID)).ToArray();
  269. var saAttendanceInfo = new List<OTB_EIP_Attendance>();
  270. var CardDate = rDateStart.AddHours(9);
  271. saAttendanceInfo = AttendanceMethod.CalculateAttendance(SelectedData, AttendanceRules, CardDate: CardDate);
  272. var saMembers = db.Queryable<OTB_SYS_Members>().Where(it => it.Effective == @"Y" && it.OrgID == i_crm.ORIGID && it.IsAttendance == true).ToList();
  273. var saAttendanceAdd = new List<OTB_EIP_Attendance>();
  274. var saAttendanceUpd = new List<OTB_EIP_Attendance>();
  275. //遍歷所有需要考勤的人,補全打卡信心用於初始化打卡資料
  276. foreach (OTB_SYS_Members member in saMembers)
  277. {
  278. var bAny = db.Queryable<OTB_EIP_Attendance>().Any(it => it.OrgID == member.OrgID && it.UserID == member.MemberID && SqlFunc.DateIsSame(it.CardDate, rDateStart));
  279. if (!bAny)
  280. {//判斷是否已經有了打卡資料
  281. if (saAttendanceInfo.Any(x => (x.CardId == member.CardId && member.OrgID == x.OrgID)))
  282. {
  283. var oAttendanceInfo = saAttendanceInfo.First(x => x.CardId == member.CardId && member.OrgID == x.OrgID);
  284. if (oAttendanceInfo.StatusA != null && oAttendanceInfo.StatusA == true)
  285. {
  286. oAttendanceInfo.Memo = "遲到";
  287. }
  288. if (oAttendanceInfo.StatusP != null && oAttendanceInfo.StatusP == true)
  289. {
  290. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "早退" : oAttendanceInfo.Memo + ",早退";
  291. }
  292. if (string.IsNullOrEmpty(oAttendanceInfo.SignOut))
  293. {
  294. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "(下班)未刷卡" : oAttendanceInfo.Memo + ",(下班)未刷卡";
  295. }
  296. oAttendanceInfo.UserID = member.MemberID;
  297. saAttendanceAdd.Add(oAttendanceInfo);
  298. }
  299. else
  300. {//需要打卡又沒有抓到打卡資料的人默認未打卡
  301. var DefaultCardDate = rDateStart.AddHours(14);
  302. var oAttendanceInfo = new OTB_EIP_Attendance
  303. {
  304. OrgID = member.OrgID,
  305. UserID = member.MemberID,
  306. CardDate = DefaultCardDate,
  307. CardId = member.CardId,
  308. TimeA = @"",
  309. TimeP = @"",
  310. CardUserName = member.MemberName,
  311. Hours = @"0",
  312. SignIn = @"",
  313. SignOut = @"",
  314. StatusP = true,
  315. StatusA = true,
  316. Memo = @"未刷卡",
  317. CreateDate = DateTime.Now
  318. };
  319. saAttendanceAdd.Add(oAttendanceInfo);
  320. }
  321. }
  322. else
  323. {
  324. var oAttendance_Old = db.Queryable<OTB_EIP_Attendance>().First(it => it.OrgID == member.OrgID && it.UserID == member.MemberID && SqlFunc.DateIsSame(it.CardDate, rDateStart));
  325. if (saAttendanceInfo.Any(x => (x.CardId == member.CardId)))
  326. {
  327. var oAttendanceInfo = saAttendanceInfo.First(x => x.CardId == member.CardId);
  328. if (oAttendanceInfo.StatusA != null && oAttendanceInfo.StatusA == true)
  329. {
  330. oAttendanceInfo.Memo = "遲到";
  331. }
  332. if (oAttendanceInfo.StatusP != null && oAttendanceInfo.StatusP == true)
  333. {
  334. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "早退" : oAttendanceInfo.Memo + ",早退";
  335. }
  336. if (string.IsNullOrEmpty(oAttendanceInfo.SignOut))
  337. {
  338. oAttendanceInfo.Memo = oAttendanceInfo.Memo == "" ? "(下班)未刷卡" : oAttendanceInfo.Memo + ",(下班)未刷卡";
  339. }
  340. oAttendance_Old.TimeA = oAttendanceInfo.TimeA;
  341. oAttendance_Old.TimeP = oAttendanceInfo.TimeP;
  342. oAttendance_Old.Hours = oAttendanceInfo.Hours;
  343. oAttendance_Old.SignIn = oAttendanceInfo.SignIn;
  344. oAttendance_Old.SignOut = oAttendanceInfo.SignOut;
  345. oAttendance_Old.StatusP = oAttendanceInfo.StatusP;
  346. oAttendance_Old.StatusA = oAttendanceInfo.StatusA;
  347. oAttendance_Old.Memo = oAttendanceInfo.Memo;
  348. saAttendanceUpd.Add(oAttendance_Old);
  349. }
  350. }
  351. }
  352. if (saAttendanceAdd.Count > 0)
  353. {
  354. db.Insertable(saAttendanceAdd).ExecuteCommand();
  355. //如果沒有打卡的話就產生未打卡提醒
  356. var saClockTips_Add = new List<OTB_SYS_ClockTips>();
  357. foreach (var attendance in saAttendanceAdd)
  358. {
  359. var sCurYear = attendance.CardDate.Year.ToString();
  360. var oHolidays = db.Queryable<OTB_SYS_Holidays>().Single(x => x.OrgID == attendance.OrgID && x.Year == sCurYear);
  361. if (attendance.SignIn == @"" && oHolidays.Holidays.IndexOf(attendance.CardDate.ToString(@"yyyy-MM-dd")) == -1)
  362. {
  363. if (CheckTips(db, attendance.CardDate, attendance.OrgID, attendance.UserID))
  364. {
  365. var oClockTips = new OTB_SYS_ClockTips
  366. {
  367. OrgID = attendance.OrgID,
  368. ParentID = attendance.CardDate.ToString(@"yyyyMMdd"),
  369. Owner = attendance.UserID,
  370. Type = nameof(attendance),
  371. Title = attendance.CardDate.ToString(@"yyyy.MM.dd") + @"日沒有打卡,也沒有對應的行事曆和EIP資訊,請至EIP填寫對應的申請單",
  372. Content = @"",
  373. Url = @"",
  374. CreateUser = @"",
  375. TipsDate = attendance.CardDate,
  376. CreateDate = DateTime.Now
  377. };
  378. saClockTips_Add.Add(oClockTips);
  379. }
  380. }
  381. }
  382. if (saClockTips_Add.Count > 0)
  383. {
  384. db.Insertable(saClockTips_Add).ExecuteCommand();
  385. }
  386. }
  387. if (saAttendanceUpd.Count > 0)
  388. {
  389. db.Updateable(saAttendanceUpd).ExecuteCommand();
  390. }
  391. }
  392. rDateStart = rDateStart.AddDays(1);
  393. };
  394. rm = new SuccessResponseMessage(null, i_crm);
  395. } while (false);
  396. return rm;
  397. });
  398. }
  399. catch (Exception ex)
  400. {
  401. sMsg = Util.GetLastExceptionMsg(ex);
  402. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"TransferEip(同步打卡資料)", @"", @"", @"");
  403. }
  404. finally
  405. {
  406. if (null != sMsg)
  407. {
  408. rm = new ErrorResponseMessage(sMsg, i_crm);
  409. }
  410. }
  411. return rm;
  412. }
  413. #endregion 同步打卡資料
  414. #region 抓去符合的請假單資料
  415. /// <summary>
  416. /// 抓去符合的請假單資料(用於匹配考勤記錄)
  417. /// </summary>
  418. /// <param name="i_crm">參數</param>
  419. /// <returns></returns>
  420. public ResponseMessage GetLeave(RequestMessage i_crm)
  421. {
  422. ResponseMessage rm = null;
  423. string sMsg = null;
  424. var db = SugarBase.GetIntance();
  425. try
  426. {
  427. do
  428. {
  429. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  430. var sDateStart = _fetchString(i_crm, @"DateStart");
  431. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  432. var rDateStart = new DateTime();
  433. var rDateEnd = new DateTime();
  434. if (!string.IsNullOrEmpty(sDateStart))
  435. {
  436. rDateStart = SqlFunc.ToDate(sDateStart);
  437. }
  438. if (!string.IsNullOrEmpty(sDateEnd))
  439. {
  440. rDateEnd = SqlFunc.ToDate(sDateEnd);
  441. }
  442. var saStatus = new string[] { @"B", @"E", @"H-O" };
  443. var saLeave = db.Queryable<OTB_EIP_Leave>().Where(it => saStatus.Contains(it.Status))
  444. .WhereIF(!string.IsNullOrEmpty(sUserIDs), it => sUserIDs.Contains(it.AskTheDummy))
  445. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), it => it.EndDate.Value.Date >= rDateStart.Date)
  446. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), it => it.StartDate.Value.Date <= rDateEnd.Date)
  447. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), it => it.StartDate.Value.Date <= rDateEnd && it.EndDate.Value.Date >= rDateStart.Date).ToList();
  448. rm = new SuccessResponseMessage(null, i_crm);
  449. rm.DATA.Add(BLWording.REL, saLeave);
  450. } while (false);
  451. }
  452. catch (Exception ex)
  453. {
  454. sMsg = Util.GetLastExceptionMsg(ex);
  455. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"GetLeave(抓去符合的請假單資料)", @"", @"", @"");
  456. }
  457. finally
  458. {
  459. if (null != sMsg)
  460. {
  461. rm = new ErrorResponseMessage(sMsg, i_crm);
  462. }
  463. }
  464. return rm;
  465. }
  466. #endregion 抓去符合的請假單資料
  467. #region 抓去符合的出差資料
  468. /// <summary>
  469. /// 抓去符合的出差資料(用於匹配考勤記錄)
  470. /// </summary>
  471. /// <param name="i_crm">參數</param>
  472. /// <returns></returns>
  473. public ResponseMessage GetBusTrip(RequestMessage i_crm)
  474. {
  475. ResponseMessage rm = null;
  476. string sMsg = null;
  477. var db = SugarBase.GetIntance();
  478. try
  479. {
  480. do
  481. {
  482. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  483. var sDateStart = _fetchString(i_crm, @"DateStart");
  484. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  485. var rDateStart = new DateTime();
  486. var rDateEnd = new DateTime();
  487. if (!string.IsNullOrEmpty(sDateStart))
  488. {
  489. rDateStart = SqlFunc.ToDate(sDateStart);
  490. }
  491. if (!string.IsNullOrEmpty(sDateEnd))
  492. {
  493. rDateEnd = SqlFunc.ToDate(sDateEnd);
  494. }
  495. var saStatus = new string[] { @"B", @"E", @"H-O" };
  496. var saBusTrip = db.Queryable<OTB_EIP_BusinessTravel>().Where(it => saStatus.Contains(it.Status))
  497. .WhereIF(!string.IsNullOrEmpty(sUserIDs), it => sUserIDs.Contains(it.AskTheDummy))
  498. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), it => it.EndDate.Value.Date >= rDateStart.Date)
  499. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), it => it.StartDate.Value.Date <= rDateEnd.Date)
  500. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), it => it.StartDate.Value.Date <= rDateEnd.Date && it.EndDate.Value.Date >= rDateStart.Date).ToList();
  501. rm = new SuccessResponseMessage(null, i_crm);
  502. rm.DATA.Add(BLWording.REL, saBusTrip);
  503. } while (false);
  504. }
  505. catch (Exception ex)
  506. {
  507. sMsg = Util.GetLastExceptionMsg(ex);
  508. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"GetBusTrip(抓去符合的出差資料)", @"", @"", @"");
  509. }
  510. finally
  511. {
  512. if (null != sMsg)
  513. {
  514. rm = new ErrorResponseMessage(sMsg, i_crm);
  515. }
  516. }
  517. return rm;
  518. }
  519. #endregion 抓去符合的出差資料
  520. #region 抓去符合的加班資料
  521. /// <summary>
  522. /// 抓去符合的加班資料(用於匹配考勤記錄)
  523. /// </summary>
  524. /// <param name="i_crm">參數</param>
  525. /// <returns></returns>
  526. public ResponseMessage GetOvertime(RequestMessage i_crm)
  527. {
  528. ResponseMessage rm = null;
  529. string sMsg = null;
  530. var db = SugarBase.GetIntance();
  531. try
  532. {
  533. do
  534. {
  535. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  536. var sDateStart = _fetchString(i_crm, @"DateStart");
  537. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  538. var rDateStart = new DateTime();
  539. var rDateEnd = new DateTime();
  540. if (!string.IsNullOrEmpty(sDateStart))
  541. {
  542. rDateStart = SqlFunc.ToDate(sDateStart);
  543. }
  544. if (!string.IsNullOrEmpty(sDateEnd))
  545. {
  546. rDateEnd = SqlFunc.ToDate(sDateEnd);
  547. }
  548. var saStatus = new string[] { @"B", @"E", @"H-O" };
  549. var saOverTime = db.Queryable<OTB_EIP_OverTime>().Where(it => saStatus.Contains(it.Status)).ToList();
  550. var saOvertimeList = new List<Map>();
  551. foreach (OTB_EIP_OverTime overtime in saOverTime)
  552. {
  553. var jaOverTimes = (JArray)JsonConvert.DeserializeObject(overtime.OverTimes);
  554. foreach (JObject jo in jaOverTimes)
  555. {
  556. var sEmployeeCode = jo[@"EmployeeCode"].ToString();
  557. var sStart = jo[@"StartDate"].ToString();
  558. var sEnd = jo[@"EndDate"].ToString();
  559. var rStart = SqlFunc.ToDate(sStart).Date;
  560. var rEnd = SqlFunc.ToDate(sEnd).Date;
  561. if ((!string.IsNullOrEmpty(sUserIDs) && sUserIDs.Contains(sEmployeeCode) || string.IsNullOrEmpty(sUserIDs)) &&
  562. ((!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd) && rStart <= rDateEnd.Date && rEnd >= rDateStart.Date) ||
  563. (!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd) && rEnd >= rDateStart.Date) ||
  564. (string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd) && rStart <= rDateEnd.Date) ||
  565. (string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd))))
  566. {
  567. var map = new Map
  568. {
  569. { @"Guid", overtime.Guid },
  570. { @"AskTheDummy", sEmployeeCode },
  571. { @"Status", overtime.Status },
  572. { @"StartDate", sStart },
  573. { @"EndDate", sEnd }
  574. };
  575. saOvertimeList.Add(map);
  576. }
  577. }
  578. }
  579. rm = new SuccessResponseMessage(null, i_crm);
  580. rm.DATA.Add(BLWording.REL, saOvertimeList);
  581. } while (false);
  582. }
  583. catch (Exception ex)
  584. {
  585. sMsg = Util.GetLastExceptionMsg(ex);
  586. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"GetOvertime(抓去符合的加班資料)", @"", @"", @"");
  587. }
  588. finally
  589. {
  590. if (null != sMsg)
  591. {
  592. rm = new ErrorResponseMessage(sMsg, i_crm);
  593. }
  594. }
  595. return rm;
  596. }
  597. #endregion 抓去符合的加班資料
  598. #region 抓去符合的差勤異常資料
  599. /// <summary>
  600. /// 抓去符合的差勤異常資料(用於匹配考勤記錄)
  601. /// </summary>
  602. /// <param name="i_crm">參數</param>
  603. /// <returns></returns>
  604. public ResponseMessage GetAttendanceDiff(RequestMessage i_crm)
  605. {
  606. ResponseMessage rm = null;
  607. string sMsg = null;
  608. var db = SugarBase.GetIntance();
  609. try
  610. {
  611. do
  612. {
  613. var sUserIDs = _fetchString(i_crm, @"UserIDs");
  614. var sDateStart = _fetchString(i_crm, @"DateStart");
  615. var sDateEnd = _fetchString(i_crm, @"DateEnd");
  616. var rDateStart = new DateTime();
  617. var rDateEnd = new DateTime();
  618. if (!string.IsNullOrEmpty(sDateStart))
  619. {
  620. rDateStart = SqlFunc.ToDate(sDateStart);
  621. }
  622. if (!string.IsNullOrEmpty(sDateEnd))
  623. {
  624. rDateEnd = SqlFunc.ToDate(sDateEnd).AddDays(1);
  625. }
  626. var saStatus = new string[] { @"B", @"E", @"H-O" };
  627. var saAttendanceDiff = db.Queryable<OTB_EIP_AttendanceDiff>().Where(it => saStatus.Contains(it.Status))
  628. .WhereIF(!string.IsNullOrEmpty(sUserIDs), it => sUserIDs.Contains(it.AskTheDummy))
  629. .WhereIF(!string.IsNullOrEmpty(sDateStart) && string.IsNullOrEmpty(sDateEnd), it => it.FillBrushDate >= rDateStart)
  630. .WhereIF(!string.IsNullOrEmpty(sDateEnd) && string.IsNullOrEmpty(sDateStart), it => it.FillBrushDate <= rDateEnd)
  631. .WhereIF(!string.IsNullOrEmpty(sDateStart) && !string.IsNullOrEmpty(sDateEnd), it => it.FillBrushDate <= rDateEnd && it.FillBrushDate >= rDateStart).ToList();
  632. rm = new SuccessResponseMessage(null, i_crm);
  633. rm.DATA.Add(BLWording.REL, saAttendanceDiff);
  634. } while (false);
  635. }
  636. catch (Exception ex)
  637. {
  638. sMsg = Util.GetLastExceptionMsg(ex);
  639. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Attendance_QryService), @"考勤查詢", @"GetAttendanceDiff(抓去符合的差勤異常資料)", @"", @"", @"");
  640. }
  641. finally
  642. {
  643. if (null != sMsg)
  644. {
  645. rm = new ErrorResponseMessage(sMsg, i_crm);
  646. }
  647. }
  648. return rm;
  649. }
  650. #endregion 抓去符合的差勤異常資料
  651. /// <summary>
  652. /// 程序执行时间测试
  653. /// </summary>
  654. /// <param name="dateBegin">开始时间</param>
  655. /// <param name="dateEnd">结束时间</param>
  656. /// <returns>返回(秒)单位,比如: 0.00239秒</returns>
  657. public static double ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
  658. {
  659. var ts1 = new TimeSpan(dateBegin.Ticks);
  660. var ts2 = new TimeSpan(dateEnd.Ticks);
  661. var ts3 = ts1.Subtract(ts2).Duration();
  662. //你想转的格式
  663. return ts3.TotalHours;
  664. }
  665. /// <summary>
  666. /// 檢核EIP申請資料與行事曆判斷是否需要填寫
  667. /// </summary>
  668. /// <param name="userId">结束时间</param>
  669. /// <param name="db">todo: describe db parameter on CheckTips</param>
  670. /// <param name="date">todo: describe date parameter on CheckTips</param>
  671. /// <param name="origId">todo: describe origId parameter on CheckTips</param>
  672. /// <returns>返回(秒)单位,比如: 0.00239秒</returns>
  673. private static bool CheckTips(SqlSugarClient db, DateTime date, string origId, string userId)
  674. {
  675. var bToTips = true;
  676. do
  677. {
  678. 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)));
  679. if (iCount_Calendar > 0)
  680. {
  681. bToTips = false;
  682. break;
  683. }
  684. const string sStatus = @"B,E,H-O";
  685. //差勤異常
  686. var iCount_AttendanceDiff = db.Queryable<OTB_EIP_AttendanceDiff>().Count(x => x.OrgID == origId && x.AskTheDummy == userId && sStatus.Contains(x.Status) && x.FillBrushDate.Value.Date == date.Date)
  687. ;
  688. if (iCount_AttendanceDiff > 0)
  689. {
  690. bToTips = false;
  691. break;
  692. }
  693. //請假單
  694. var iCount_Leave = db.Queryable<OTB_EIP_Leave>().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)));
  695. if (iCount_Leave > 0)
  696. {
  697. bToTips = false;
  698. break;
  699. }
  700. } while (false);
  701. return bToTips;
  702. }
  703. }
  704. }