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.

1132 lines
60 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyNet;
  3. using Entity.Sugar;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace EasyBL.WEBAPP.EIP
  13. {
  14. public class Leave_UpdService : ServiceBase
  15. {
  16. #region 請假單提交簽核
  17. /// <summary>
  18. /// 請假單提交簽核
  19. /// </summary>
  20. /// <param name="i_crm">todo: describe i_crm parameter on LeaveToAudit</param>
  21. /// <returns></returns>
  22. public ResponseMessage LeaveToAudit(RequestMessage i_crm)
  23. {
  24. ResponseMessage rm = null;
  25. string sMsg = null;
  26. try
  27. {
  28. rm = SugarBase.ExecTran(db =>
  29. {
  30. do
  31. {
  32. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  33. var sLeaveSetGuid = _fetchString(i_crm, @"LeaveSetGuid");
  34. var sTotalTime = _fetchString(i_crm, @"TotalTime");
  35. var sHolidayCategory = _fetchString(i_crm, @"HolidayCategory");
  36. var sLeaveSetInfo = _fetchString(i_crm, @"LeaveSetInfo");
  37. var sOrgID = _fetchString(i_crm, @"OrgID");
  38. var strCreateUser = _fetchString(i_crm, @"CreateUser");
  39. var strStartDate = _fetchString(i_crm, @"StartDate");
  40. var strEndDate = _fetchString(i_crm, @"EndDate");
  41. var sDaysOfLeaves = _fetchString(i_crm, @"DaysOfLeaves");
  42. var sLeaveRequestUsing = _fetchString(i_crm, @"LeaveRequestUsing");
  43. var sRoundToInterger = _fetchString(i_crm, @"RoundToInterger");
  44. var ListDaysOfLeaves = new List<string>();
  45. var UsingDateTimeFormate = "yyyy-MM-dd";
  46. var AnnualLeaveMemoInfo = new List<Tuple<string,decimal>>();
  47. var NormalLeaveMemoInfo = new List<Tuple<string, decimal>>();
  48. var NormalRequestGuid = ",";
  49. var LeaveMemo = "";
  50. //轉換物件
  51. var JaDaysOfLeaves = (JArray)JsonConvert.DeserializeObject(sDaysOfLeaves);
  52. foreach (var bill in JaDaysOfLeaves)
  53. {
  54. var asklof = bill.ToString().Split(new char[] { ':', '|' }, StringSplitOptions.RemoveEmptyEntries);
  55. ListDaysOfLeaves.Add(bill.ToString());
  56. }
  57. var JaLeaveRequestUsings = (JArray)JsonConvert.DeserializeObject(sLeaveRequestUsing);
  58. foreach (var lr in JaLeaveRequestUsings)
  59. {
  60. NormalRequestGuid += "," + lr["Guid"].ToString();
  61. decimal.TryParse(lr["UsedHours"].ToString(), out var Result);
  62. NormalLeaveMemoInfo.Add(new Tuple<string, decimal>(lr["Guid"].ToString(), Result));
  63. }
  64. //檢查傳進的ListDaysOfLeaves,一定會有請假資料。
  65. if (!ListDaysOfLeaves.Any())
  66. {
  67. sMsg = "請求錯誤,請重新整理。";
  68. break;
  69. }
  70. //取得請假天數
  71. var oMonthLeaveDay = db.Queryable<OTB_EIP_Leave>()
  72. .Where(a => a.AskTheDummy == strCreateUser && a.OrgID == sOrgID && a.StartDate >= DateTime.Parse(strStartDate) && a.EndDate <= DateTime.Parse(strEndDate) && (a.Status != "C-O" && a.Status != "D-O" && a.Status != "X" && a.Status != "A"))
  73. .Count();
  74. //判斷請假時間是否已於系統上申請過
  75. if (oMonthLeaveDay >= 1) //因會找到自己本身的資料,所以需額外+1
  76. {
  77. sMsg = "請假人已經在這段時間申請休假,請重新選擇日期";
  78. break;
  79. }
  80. var sdb = new SimpleClient<OTB_EIP_Leave>(db);
  81. var oEip = sdb.GetById(sId);
  82. if (oEip == null)
  83. {
  84. sMsg = @"系統找不到對應的請假資料,請核查!";
  85. break;
  86. }
  87. if (sHolidayCategory == @"09")
  88. {
  89. //取的當年度的特休
  90. var ApplyAnnualleaveInfo = GetAnnualRange(db, i_crm.ORIGID, strCreateUser, strStartDate, strEndDate);
  91. if (!ApplyAnnualleaveInfo.Item1)
  92. {
  93. sMsg = ApplyAnnualleaveInfo.Item2;
  94. break;
  95. }
  96. else
  97. {
  98. decimal? iLeaveHours = Convert.ToDecimal(sTotalTime);
  99. var ApplyWZ = ApplyAnnualleaveInfo.Item3;
  100. var CheckTheSameYear = ApplyWZ.Count;
  101. var UpdateWZData = new List<OTB_EIP_WenZhong>();
  102. switch (CheckTheSameYear)
  103. {
  104. //同年度
  105. case 1:
  106. {
  107. var UsingWZ = ApplyWZ.First();
  108. //檢查時數是否超過,扣除相對應
  109. if (UsingWZ.RemainHours < iLeaveHours)
  110. sMsg = $"{UsingWZ.EnableDate.Value.ToString(UsingDateTimeFormate) + " ~ " +UsingWZ.ExpirationDate.Value.ToString(UsingDateTimeFormate)}" +
  111. $"特休剩餘時數不足夠,無法請假。";
  112. else
  113. {
  114. var oWenZhongUpd = new OTB_EIP_WenZhong()
  115. {
  116. Guid = UsingWZ.Guid,
  117. DelFlag = false,
  118. RemainHours = UsingWZ.RemainHours - iLeaveHours,
  119. UsedHours = UsingWZ.UsedHours + iLeaveHours
  120. };
  121. UpdateWZData.Add(oWenZhongUpd);
  122. AnnualLeaveMemoInfo.Add(new Tuple<string, decimal>(UsingWZ.Guid, iLeaveHours.Value));
  123. }
  124. }
  125. break;
  126. //跨年度
  127. case 2:
  128. {
  129. //檢查兩個時數是否超過,扣除相對應
  130. var TotalALHours = ApplyWZ.Sum(c => c.RemainHours);
  131. if (TotalALHours < iLeaveHours)
  132. sMsg = "上年度與今年度特休剩餘時數不足夠,無法請假。";
  133. else
  134. {
  135. var DefaultWorkingHours = 8.0m;
  136. #region 開始請假~上年度結束時間。
  137. var LastYearWZ = ApplyWZ[0];
  138. var StartDate = Convert.ToDateTime(strStartDate).Date;
  139. var WZExpiredDay = LastYearWZ.ExpirationDate.Value.Date;
  140. var LastYearEstimateUse = 0.0m;
  141. var NowYearEstimateUse = 0.0m;
  142. while (StartDate <= WZExpiredDay)
  143. {
  144. var StartDtDay = StartDate.ToString(UsingDateTimeFormate);
  145. var FoundDayOfLeave = ListDaysOfLeaves.Where(c => c.Contains(StartDtDay));
  146. if (FoundDayOfLeave.Any())
  147. {
  148. var UsedLeaveInfo = FoundDayOfLeave.First().Split(new char[] { ':', '|' });
  149. var UsedDay = Convert.ToDecimal(UsedLeaveInfo[1]) * DefaultWorkingHours;
  150. var UsedHour = Convert.ToDecimal(UsedLeaveInfo[2]);
  151. LastYearEstimateUse += UsedDay + UsedHour;
  152. }
  153. StartDate = StartDate.AddDays(1);
  154. }
  155. if (LastYearWZ.RemainHours < LastYearEstimateUse)
  156. {
  157. sMsg += $"{LastYearWZ.EnableDate.Value.ToString(UsingDateTimeFormate) + " ~ " + LastYearWZ.ExpirationDate.Value.ToString(UsingDateTimeFormate)}" +
  158. "特休剩餘時數不足夠,無法請假。";
  159. }
  160. else
  161. {
  162. var oWenZhongUpd = new OTB_EIP_WenZhong()
  163. {
  164. Guid = LastYearWZ.Guid,
  165. DelFlag = false,
  166. RemainHours = LastYearWZ.RemainHours - LastYearEstimateUse,
  167. UsedHours = LastYearWZ.UsedHours + LastYearEstimateUse
  168. };
  169. AnnualLeaveMemoInfo.Add(new Tuple<string, decimal>(LastYearWZ.Guid, LastYearEstimateUse));
  170. UpdateWZData.Add(oWenZhongUpd);
  171. }
  172. #endregion
  173. #region MyRegion
  174. //上年度開始時間~結束請假時間
  175. var NowYearWZ = ApplyWZ[1];
  176. var EndDate = Convert.ToDateTime(strEndDate).Date;
  177. var WZEnabledDay = NowYearWZ.EnableDate.Value.Date;
  178. while (WZEnabledDay <= EndDate)
  179. {
  180. var EnabledDay = WZEnabledDay.ToString(UsingDateTimeFormate);
  181. var FoundDayOfLeave = ListDaysOfLeaves.Where(c => c.Contains(EnabledDay));
  182. if (FoundDayOfLeave.Any())
  183. {
  184. var UsedLeaveInfo = FoundDayOfLeave.First().Split(new char[] { ':', '|' });
  185. var UsedDay = Convert.ToDecimal(UsedLeaveInfo[1]) * DefaultWorkingHours;
  186. var UsedHour = Convert.ToDecimal(UsedLeaveInfo[2]);
  187. NowYearEstimateUse += UsedDay + UsedHour;
  188. }
  189. WZEnabledDay = WZEnabledDay.AddDays(1);
  190. }
  191. if (NowYearWZ.RemainHours < NowYearEstimateUse)
  192. {
  193. sMsg += $"{NowYearWZ.EnableDate.Value.ToString(UsingDateTimeFormate) + " ~ " + NowYearWZ.ExpirationDate.Value.ToString(UsingDateTimeFormate)}" +
  194. "特休剩餘時數不足夠,無法請假。";
  195. }
  196. else
  197. {
  198. var oWenZhongUpd = new OTB_EIP_WenZhong()
  199. {
  200. Guid = NowYearWZ.Guid,
  201. DelFlag = false,
  202. RemainHours = NowYearWZ.RemainHours - NowYearEstimateUse,
  203. UsedHours = NowYearWZ.UsedHours + NowYearEstimateUse
  204. };
  205. AnnualLeaveMemoInfo.Add(new Tuple<string, decimal>(NowYearWZ.Guid, NowYearEstimateUse));
  206. UpdateWZData.Add(oWenZhongUpd);
  207. }
  208. #endregion
  209. }
  210. }
  211. break;
  212. default:
  213. break;
  214. }
  215. if (string.IsNullOrWhiteSpace(sMsg) && UpdateWZData.Any())
  216. {
  217. db.Updateable(UpdateWZData).UpdateColumns(it => new { it.UsedHours, it.RemainHours, it.DelFlag }).ExecuteCommand();
  218. var AnnualLeaveMemoJson = from d in AnnualLeaveMemoInfo
  219. select new { Guid = d.Item1, Hours = d.Item2 };
  220. LeaveMemo = JsonToString(AnnualLeaveMemoJson);
  221. }
  222. else
  223. {
  224. break;
  225. }
  226. }
  227. }
  228. else
  229. {
  230. var UsingLeaveRequest = db.Queryable<OTB_EIP_LeaveRequest>().Where(t1 => NormalRequestGuid.Contains(t1.guid)).ToList();
  231. foreach (var NLMI in NormalLeaveMemoInfo)
  232. {
  233. var LeaveRequest = UsingLeaveRequest.Where(t1 => t1.guid == NLMI.Item1).Single();
  234. LeaveRequest.UsedHours += NLMI.Item2;
  235. LeaveRequest.RemainHours = LeaveRequest.PaymentHours - LeaveRequest.UsedHours;
  236. }
  237. if(UsingLeaveRequest.Any())
  238. db.Updateable(UsingLeaveRequest).UpdateColumns(it => new { it.PaymentHours, it.RemainHours, it.UsedHours }).ExecuteCommand();
  239. //.Where(it => it.Guid == sLeaveSetGuid).ExecuteCommand();
  240. var oLeaveSetUpd = new OTB_EIP_LeaveSet
  241. {
  242. SetInfo = sLeaveSetInfo
  243. };
  244. db.Updateable(oLeaveSetUpd).UpdateColumns(it => new { it.SetInfo })
  245. .Where(it => it.Guid == sLeaveSetGuid).ExecuteCommand();
  246. var NormalLeaveMemoJson = from d in NormalLeaveMemoInfo
  247. select new { Guid = d.Item1, Hours = d.Item2 };
  248. LeaveMemo = JsonToString(NormalLeaveMemoJson);
  249. }
  250. var oLeaveUpd = new OTB_EIP_Leave
  251. {
  252. AnnualLeaveMemo = LeaveMemo,
  253. Status = @"B",
  254. ModifyUser = i_crm.USERID,
  255. ModifyDate = DateTime.Now
  256. };
  257. db.Updateable(oLeaveUpd).UpdateColumns(it => new { it.Status, it.ModifyDate, it.ModifyUser, it.AnnualLeaveMemo })
  258. .Where(it => it.Guid == sId).ExecuteCommand();
  259. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  260. if (!string.IsNullOrEmpty(oEip.Agent_Person))
  261. {
  262. var oAskTheDummy = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == oEip.AskTheDummy);
  263. var sTitle = oAskTheDummy.MemberName + @"的請假單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  264. if (i_crm.LANG == @"zh")
  265. {
  266. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  267. }
  268. //添加提醒消息
  269. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oEip.Agent_Person, @"Leave_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  270. db.Insertable(oTipsAdd).ExecuteCommand();
  271. //添加代辦
  272. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, oEip.Agent_Person, sTitle, @"Leave_View", @"?Action=Upd&Guid=" + oEip.Guid, @"B");
  273. db.Insertable(oTaskAdd).ExecuteCommand();
  274. }
  275. //檢核未打卡提示中有沒有提醒資料,如果有的話就刪除
  276. var saClockTips = db.Queryable<OTB_SYS_ClockTips>()
  277. .Where(x => x.OrgID == i_crm.ORIGID && x.Owner == oEip.AskTheDummy && x.TipsDate <= oEip.EndDate && x.TipsDate >= oEip.StartDate)
  278. .ToList();
  279. if (saClockTips.Count > 0)
  280. {
  281. db.Deleteable(saClockTips).ExecuteCommand();
  282. }
  283. rm = new SuccessResponseMessage(null, i_crm);
  284. rm.DATA.Add(BLWording.REL, oEip.Agent_Person);
  285. } while (false);
  286. return rm;
  287. });
  288. }
  289. catch (Exception ex)
  290. {
  291. sMsg = Util.GetLastExceptionMsg(ex);
  292. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), @"請假單管理", @"LeaveToAudit(請假單提交簽核)", @"", @"", @"");
  293. }
  294. finally
  295. {
  296. if (null != sMsg)
  297. {
  298. rm = new ErrorResponseMessage(sMsg, i_crm);
  299. }
  300. }
  301. return rm;
  302. }
  303. #endregion 請假單提交簽核
  304. /// <summary>
  305. /// 取得請假當下的區間
  306. /// </summary>
  307. /// <param name="db"></param>
  308. /// <param name="OrgID"></param>
  309. /// <param name="UserID"></param>
  310. /// <param name="strStartDate"></param>
  311. /// <param name="strEndDate"></param>
  312. /// <returns>依序:是否成功、訊息、資料</returns>
  313. private Tuple<bool,string,List<OTB_EIP_WenZhong>> GetAnnualRange( SqlSugarClient db,string OrgID, string UserID ,string strStartDate, string strEndDate)
  314. {
  315. var AllMatchedWenZhong = db.Queryable<OTB_EIP_WenZhong>()
  316. .Where(it => it.OrgID == OrgID && it.UserID == UserID ).OrderBy(it => it.EnableDate).ToList();
  317. if (AllMatchedWenZhong.Count == 0)
  318. {
  319. return new Tuple<bool, string, List<OTB_EIP_WenZhong>>(false, "系統找不到對應的文中特休假設定,請核查!", new List<OTB_EIP_WenZhong>());
  320. }
  321. else
  322. {
  323. var saWZLastYear = AllMatchedWenZhong.Where(it => it.EnableDate <= DateTime.Parse(strStartDate).Date && it.ExpirationDate >= DateTime.Parse(strStartDate).Date).First();
  324. var saWZThisYear = AllMatchedWenZhong.Where(it => it.EnableDate <= DateTime.Parse(strEndDate).Date && it.ExpirationDate >= DateTime.Parse(strEndDate).Date).First();
  325. var CheckEmptyWZ = saWZThisYear == null && saWZLastYear == null;
  326. if (CheckEmptyWZ)
  327. {
  328. return new Tuple<bool, string, List<OTB_EIP_WenZhong>>(false, "系統找不到對應的文中特休假設定,請核查!", new List<OTB_EIP_WenZhong>());
  329. }
  330. //在同個年度內。
  331. else if(saWZLastYear.Equals(saWZThisYear))
  332. {
  333. return new Tuple<bool, string, List<OTB_EIP_WenZhong>>(true, "請假區間在同文中年度內", new List<OTB_EIP_WenZhong>() { saWZLastYear });
  334. }
  335. //在不同年度內
  336. else
  337. {
  338. return new Tuple<bool, string, List<OTB_EIP_WenZhong>>(true, "請假區間在不同文中年度內。", new List<OTB_EIP_WenZhong>() { saWZLastYear, saWZThisYear });
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// 檢查請假是否為小數
  344. /// </summary>
  345. /// <param name="db"></param>
  346. /// <param name="LeaveType"></param>
  347. /// <param name="sLeaveType"></param>
  348. /// <param name="OrgID"></param>
  349. /// <returns></returns>
  350. public bool CheckLeaveHourInterger(SqlSugarClient db, string sLeaveType, string OrgID)
  351. {
  352. var Default = false;
  353. try
  354. {
  355. do
  356. {
  357. //取得出勤設定資料
  358. var oLeaveRuelsSetting = db.Queryable<OTB_SYS_Arguments, OTB_SYS_ArgumentsRelated>
  359. ((t1, t2) =>
  360. new object[] {
  361. JoinType.Left, t1.ArgumentID == t2.ArgumentID
  362. }
  363. )
  364. .Where((t1, t2) => t1.ArgumentClassID == "LeaveType" && t1.ArgumentID == sLeaveType && t1.OrgID == OrgID && t2.OrgID == OrgID)
  365. .Select((t1, t2) => new { t1.Correlation, t2.ExFeild4 })
  366. .Single();
  367. if (oLeaveRuelsSetting.ExFeild4 == "Y")
  368. return true;
  369. else
  370. return Default;
  371. } while (false);
  372. }
  373. catch (Exception ex)
  374. {
  375. LogAndSendEmail(@"Param:" , ex, OrgID, " ", nameof(Leave_UpdService), @"請假單管理", @"CheckInterger(檢查請假是否為小數)", @"", @"", @"");
  376. }
  377. return Default;
  378. }
  379. #region 請假單簽核|签辦
  380. /// <summary>
  381. /// 請假單簽核|签辦
  382. /// </summary>
  383. /// <param name="i_crm">todo: describe i_crm parameter on LeaveAudit</param>
  384. /// <returns></returns>
  385. public ResponseMessage LeaveAudit(RequestMessage i_crm)
  386. {
  387. ResponseMessage rm = null;
  388. string sMsg = null;
  389. try
  390. {
  391. rm = SugarBase.ExecTran(db =>
  392. {
  393. do
  394. {
  395. var sId = _fetchString(i_crm, @"Guid");
  396. var sAction = _fetchString(i_crm, @"Action");
  397. var sGoNext = _fetchString(i_crm, @"GoNext");
  398. var sHandlePerson = _fetchString(i_crm, @"HandlePerson");
  399. var sNextSignedWays = _fetchString(i_crm, @"NextSignedWays");
  400. var sNextUsers = _fetchString(i_crm, @"NextUsers");
  401. var sTipsUsers = _fetchString(i_crm, @"TipsUsers");
  402. var sCheckFlows = _fetchString(i_crm, @"CheckFlows");
  403. var sHandleFlows = _fetchString(i_crm, @"HandleFlows");
  404. var sSignedDecision = _fetchString(i_crm, @"SignedDecision");
  405. var sHandleDecision = _fetchString(i_crm, @"HandleDecision");
  406. var sdb = new SimpleClient<OTB_EIP_Leave>(db);
  407. var oEip = sdb.GetById(sId);
  408. var TipsType = WebAppGlobalConstWord.CHECK;
  409. if (oEip == null)
  410. {
  411. sMsg = @"系統找不到對應的請假資料,請核查!";
  412. break;
  413. }
  414. var saNextSignedWays = (JArray)JsonConvert.DeserializeObject(sNextSignedWays);
  415. var saNextUsers = (JArray)JsonConvert.DeserializeObject(sNextUsers);
  416. var saTipsUsers = (JArray)JsonConvert.DeserializeObject(sTipsUsers);
  417. var oUser_Self = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  418. var oAskTheDummy = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == oEip.AskTheDummy);
  419. var sTitle_Self = @"";
  420. var sTitle_Handle = oUser_Self.MemberName + @"審批了" + oAskTheDummy.MemberName + @"的請假單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  421. var sTitle_Next = oAskTheDummy.MemberName + @"的請假單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  422. var sTitle_Notice = @"";
  423. var sStatus = @"B";
  424. if (sAction == @"Signed")
  425. {
  426. sTitle_Self = oUser_Self.MemberName + @"審批了您的請假單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  427. sTitle_Notice = oAskTheDummy.MemberName + @"的請假單申請「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber + @",請點擊查看...";
  428. switch (sSignedDecision)
  429. {
  430. case @"Y":
  431. sTitle_Self += @",審批結果:同意";
  432. if (sHandlePerson != @"")
  433. {
  434. sStatus = @"E";
  435. }
  436. break;
  437. case @"N":
  438. sTitle_Self += @",審批結果:不同意";
  439. sStatus = @"D-O";
  440. TipsType = WebAppGlobalConstWord.FAIL;
  441. break;
  442. case @"O":
  443. sTitle_Self += @",審批結果:先加簽";
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. else
  450. {
  451. sTitle_Self = oUser_Self.MemberName + @"签辦了您的請假單「" + oEip.KeyNote + @"」簽呈編號:" + oEip.SignedNumber;
  452. if (sHandleDecision == @"Y")
  453. {
  454. sTitle_Self += @"簽辦結果:同意";
  455. sStatus = @"H-O";
  456. }
  457. else if (sHandleDecision == @"O")
  458. {
  459. sTitle_Self += @"簽辦結果:先轉呈其他主管審批";
  460. }
  461. }
  462. if (i_crm.LANG == @"zh")
  463. {
  464. sTitle_Self = ChineseStringUtility.ToSimplified(sTitle_Self);
  465. sTitle_Next = ChineseStringUtility.ToSimplified(sTitle_Next);
  466. sTitle_Notice = ChineseStringUtility.ToSimplified(sTitle_Notice);
  467. }
  468. var oLeaveUpd = new OTB_EIP_Leave
  469. {
  470. Status = sStatus,
  471. CheckFlows = sCheckFlows,
  472. HandleFlows = sHandleFlows,
  473. ModifyUser = i_crm.USERID,
  474. ModifyDate = DateTime.Now
  475. };
  476. db.Updateable(oLeaveUpd)
  477. .UpdateColumns(it => new { it.Status, it.CheckFlows, it.HandleFlows, it.ModifyDate, it.ModifyUser })
  478. .Where(it => it.Guid == sId).ExecuteCommand();
  479. var sOwner = @"";
  480. if (sSignedDecision == @"Y" && sGoNext == @"N")
  481. {
  482. sOwner = i_crm.USERID;
  483. }
  484. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId, sOwner);
  485. var listTips = new List<OTB_SYS_Tips>();
  486. var listTask = new List<OTB_SYS_Task>();
  487. var listToTips = new List<string>();
  488. if (sStatus != @"D-O")
  489. {
  490. foreach (string flow in saNextSignedWays)
  491. {
  492. if (sGoNext == @"Y")
  493. {
  494. if (flow == @"flow4")//添加通知和提醒給下個流程所有要通知的人
  495. {
  496. foreach (string user in saTipsUsers)
  497. {
  498. //添加提醒消息
  499. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Notice, user, @"Leave_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  500. listTips.Add(oTipsAdd);
  501. listToTips.Add(user);
  502. }
  503. }
  504. else if (flow == @"flow5")//添加通知和提醒給經辦人
  505. {
  506. if (sHandlePerson != @"")
  507. {
  508. if (sHandleDecision != @"N")
  509. {
  510. //添加代辦
  511. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, sHandlePerson, sTitle_Handle, @"Leave_View", @"?Action=Upd&Guid=" + oEip.Guid, @"E");
  512. listTask.Add(oTaskAdd);
  513. //添加提醒消息
  514. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Handle, sHandlePerson, @"Leave_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  515. listTips.Add(oTipsAdd);
  516. listToTips.Add(sHandlePerson);
  517. }
  518. }
  519. }
  520. else
  521. {
  522. foreach (string user in saNextUsers)//添加通知和提醒給下一個審核的人
  523. {
  524. if (sSignedDecision == @"Y" || sSignedDecision == @"O")
  525. {
  526. //添加代辦
  527. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, oEip.Guid, user, sTitle_Next, @"Leave_View", @"?Action=Upd&Guid=" + oEip.Guid, @"G");
  528. listTask.Add(oTaskAdd);
  529. //添加提醒消息
  530. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Next, user, @"Leave_View" + @"|?Action=Upd&Guid=" + oEip.Guid, WebAppGlobalConstWord.BELL);
  531. listTips.Add(oTipsAdd);
  532. listToTips.Add(user);
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. //添加提醒消息(給請假的人)
  540. var oTips_AskTheDummy = SYS.Task_QryService.TipsAdd(i_crm, sTitle_Self, oEip.AskTheDummy, @"Leave_View" + @"|?Action=Upd&Guid=" + oEip.Guid, TipsType);
  541. listTips.Add(oTips_AskTheDummy);
  542. listToTips.Add(oEip.AskTheDummy);
  543. if (listTips.Count > 0)
  544. {
  545. db.Insertable(listTips).ExecuteCommand();
  546. }
  547. if (listTask.Count > 0)
  548. {
  549. db.Insertable(listTask).ExecuteCommand();
  550. }
  551. if (sStatus == @"D-O")
  552. {
  553. if (oEip.HolidayCategory == @"09")
  554. {
  555. var iYear = oEip.StartDate.Value.Year;
  556. var saWenZhong = db.Queryable<OTB_EIP_WenZhong>()
  557. .Where(it => it.OrgID == i_crm.ORIGID && it.UserID == oEip.AskTheDummy && (it.EnableDate.Value.Year == iYear || it.ExpirationDate.Value.Year == iYear)).ToList();
  558. if (saWenZhong.Count > 0)
  559. {
  560. foreach (OTB_EIP_WenZhong wz in saWenZhong)
  561. {
  562. if ((DateTime)oEip.StartDate >= (DateTime)wz.EnableDate && (DateTime)oEip.StartDate <= (DateTime)wz.ExpirationDate)
  563. {
  564. var oWenZhongUpd = new OTB_EIP_WenZhong
  565. {
  566. UsedHours = (wz.UsedHours ?? 0) - oEip.TotalTime
  567. };
  568. oWenZhongUpd.RemainHours = (wz.PaymentHours ?? 0) - oWenZhongUpd.UsedHours;
  569. oWenZhongUpd.DelFlag = false;
  570. db.Updateable(oWenZhongUpd).UpdateColumns(it => new { it.UsedHours, it.RemainHours, it.DelFlag })
  571. .Where(it => it.Guid == wz.Guid).ExecuteCommand();
  572. break;
  573. }
  574. }
  575. }
  576. }
  577. else
  578. {
  579. var sYear = ((DateTime)oEip.StartDate).ToString(@"yyyy");
  580. var oLeaveSet = db.Queryable<OTB_EIP_LeaveSet>().Single(it => it.OrgID == i_crm.ORIGID && it.UserID == oEip.AskTheDummy && it.TYear == sYear);
  581. if (oLeaveSet != null)
  582. {
  583. var jaSetInfo = (JArray)JsonConvert.DeserializeObject(oLeaveSet.SetInfo);
  584. foreach (JObject jo in jaSetInfo)
  585. {
  586. if (jo[@"Id"].ToString() == oEip.HolidayCategory)
  587. {
  588. var sUsedHours = jo[@"UsedHours"] == null ? @"0" : jo[@"UsedHours"].ToString();
  589. var sPaymentHours = jo[@"PaymentHours"] == null ? @"0" : jo[@"PaymentHours"].ToString();
  590. jo[@"UsedHours"] = Convert.ToDecimal(sUsedHours) - oEip.TotalTime;
  591. if (sPaymentHours != @"")
  592. {
  593. jo[@"RemainHours"] = Convert.ToDecimal(sPaymentHours) - Convert.ToDecimal(jo[@"UsedHours"].ToString());
  594. }
  595. break;
  596. }
  597. }
  598. var oLeaveSetUpd = new OTB_EIP_LeaveSet
  599. {
  600. SetInfo = JsonToString(jaSetInfo)
  601. };
  602. db.Updateable(oLeaveSetUpd).UpdateColumns(it => new { it.SetInfo })
  603. .Where(it => it.Guid == oLeaveSet.Guid).ExecuteCommand();
  604. }
  605. }
  606. }
  607. db.Ado.CommitTran();
  608. rm = new SuccessResponseMessage(null, i_crm);
  609. rm.DATA.Add("Status", sStatus);
  610. rm.DATA.Add(BLWording.REL, listToTips);
  611. } while (false);
  612. return rm;
  613. });
  614. }
  615. catch (Exception ex)
  616. {
  617. sMsg = Util.GetLastExceptionMsg(ex);
  618. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), @"請假單管理", @"LeaveAudit(請假單簽核|签辦)", @"", @"", @"");
  619. }
  620. finally
  621. {
  622. if (null != sMsg)
  623. {
  624. rm = new ErrorResponseMessage(sMsg, i_crm);
  625. }
  626. }
  627. return rm;
  628. }
  629. #endregion 請假單簽核|签辦
  630. #region 作廢
  631. /// <summary>
  632. /// 作廢
  633. /// </summary>
  634. /// <param name="i_crm">todo: describe i_crm parameter on SetVoid</param>
  635. /// <returns></returns>
  636. public ResponseMessage SetVoid(RequestMessage i_crm)
  637. {
  638. ResponseMessage rm = null;
  639. string sMsg = null;
  640. try
  641. {
  642. rm = SugarBase.ExecTran(db =>
  643. {
  644. do
  645. {
  646. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  647. var sVoidReason = _fetchString(i_crm, @"VoidReason");
  648. var sdb = new SimpleClient<OTB_EIP_Leave>(db);
  649. var oEip = sdb.GetById(sId);
  650. var iYear = oEip.StartDate.Value.Year;
  651. if (oEip == null)
  652. {
  653. sMsg = @"系統找不到對應的請假資料,請核查!";
  654. break;
  655. }
  656. if (oEip.HolidayCategory == @"09" && string.IsNullOrWhiteSpace(oEip.AnnualLeaveMemo))
  657. {
  658. sMsg = @"請假的特休資訊遺失,請資訊人員核查!";
  659. break;
  660. }
  661. var AnnualLeaveMemos = (JArray)JsonConvert.DeserializeObject(oEip.AnnualLeaveMemo);
  662. var oLeaveUpd = new OTB_EIP_Leave
  663. {
  664. Status = @"X",
  665. VoidReason = sVoidReason,
  666. ModifyUser = i_crm.USERID,
  667. ModifyDate = DateTime.Now
  668. };
  669. db.Updateable(oLeaveUpd).UpdateColumns(it => new { it.Status, it.VoidReason, it.ModifyDate, it.ModifyUser })
  670. .Where(it => it.Guid == sId).ExecuteCommand();
  671. db.Deleteable<OTB_SYS_Task>()
  672. .Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sId).ExecuteCommand();
  673. new CalendarService().DeleteCalendar(i_crm.ORIGID, "", oEip.Guid);
  674. if (oEip.HolidayCategory == @"09")
  675. {
  676. UpddateWenZhongAnnualLeave(db, i_crm.ORIGID, oEip.AskTheDummy, AnnualLeaveMemos);
  677. }
  678. else
  679. {
  680. var TotalRecoveryHours = oEip.TotalTime;
  681. var RecoverLeaveRequestResult = UpddateNormalRequestLeave(db, i_crm.ORIGID, oEip.AskTheDummy, AnnualLeaveMemos);
  682. TotalRecoveryHours -= RecoverLeaveRequestResult.Item3;
  683. if(TotalRecoveryHours >0)
  684. {
  685. var sYear = iYear.ToString();
  686. var oLeaveSet = db.Queryable<OTB_EIP_LeaveSet>().Single(it => it.OrgID == i_crm.ORIGID && it.UserID == oEip.AskTheDummy && it.TYear == sYear);
  687. if (oLeaveSet != null)
  688. {
  689. var jaSetInfo = (JArray)JsonConvert.DeserializeObject(oLeaveSet.SetInfo);
  690. foreach (JObject jo in jaSetInfo)
  691. {
  692. if (jo[@"Id"].ToString() == oEip.HolidayCategory)
  693. {
  694. jo[@"UsedHours"] = Convert.ToDecimal(jo[@"UsedHours"].ToString()) - oEip.TotalTime;
  695. jo[@"RemainHours"] = Convert.ToDecimal(jo[@"PaymentHours"].ToString()) - Convert.ToDecimal(jo[@"UsedHours"].ToString());
  696. break;
  697. }
  698. }
  699. var oLeaveSetUpd = new OTB_EIP_LeaveSet
  700. {
  701. SetInfo = JsonToString(jaSetInfo)
  702. };
  703. db.Updateable(oLeaveSetUpd).UpdateColumns(it => new { it.SetInfo })
  704. .Where(it => it.Guid == oLeaveSet.Guid).ExecuteCommand();
  705. }
  706. }
  707. }
  708. db.Ado.CommitTran();
  709. rm = new SuccessResponseMessage(null, i_crm);
  710. } while (false);
  711. return rm;
  712. });
  713. }
  714. catch (Exception ex)
  715. {
  716. sMsg = Util.GetLastExceptionMsg(ex);
  717. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), @"請假單管理", @"SetVoid(作廢)", @"", @"", @"");
  718. }
  719. finally
  720. {
  721. if (null != sMsg)
  722. {
  723. rm = new ErrorResponseMessage(sMsg, i_crm);
  724. }
  725. }
  726. return rm;
  727. }
  728. #endregion 作廢
  729. #region 抽單
  730. /// <summary>
  731. /// 抽單動作
  732. /// </summary>
  733. /// <param name="i_crm">todo: describe i_crm parameter on SetReEdit</param>
  734. /// <returns></returns>
  735. public ResponseMessage SetReEdit(RequestMessage i_crm)
  736. {
  737. ResponseMessage rm = null;
  738. string sMsg = null;
  739. try
  740. {
  741. rm = SugarBase.ExecTran(db =>
  742. {
  743. do
  744. {
  745. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  746. var sdb = new SimpleClient<OTB_EIP_Leave>(db);
  747. var oEip = sdb.GetById(sId);
  748. var iYear = oEip.StartDate.Value.Year;
  749. if (oEip == null)
  750. {
  751. sMsg = @"系統找不到對應的請假資料,請核查!";
  752. break;
  753. }
  754. if (oEip.HolidayCategory == @"09" && string.IsNullOrWhiteSpace(oEip.AnnualLeaveMemo) )
  755. {
  756. sMsg = @"請假的特休資訊遺失,請資訊人員核查!";
  757. break;
  758. }
  759. var AnnualLeaveMemos = (JArray)JsonConvert.DeserializeObject(oEip.AnnualLeaveMemo);
  760. var oLeaveUpd = new OTB_EIP_Leave
  761. {
  762. Status = @"C-O",
  763. ModifyUser = i_crm.USERID,
  764. ModifyDate = DateTime.Now
  765. };
  766. db.Updateable(oLeaveUpd).UpdateColumns(it => new { it.Status, it.ModifyDate, it.ModifyUser })
  767. .Where(it => it.Guid == sId).ExecuteCommand();
  768. db.Deleteable<OTB_SYS_Task>()
  769. .Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sId).ExecuteCommand();
  770. new CalendarService().DeleteCalendar(i_crm.ORIGID,"", oEip.Guid);
  771. if (oEip.HolidayCategory == @"09")
  772. {
  773. UpddateWenZhongAnnualLeave(db, i_crm.ORIGID, oEip.AskTheDummy, AnnualLeaveMemos);
  774. }
  775. else
  776. {
  777. var TotalRecoveryHours = oEip.TotalTime;
  778. var RecoverLeaveRequestResult = UpddateNormalRequestLeave(db, i_crm.ORIGID, oEip.AskTheDummy, AnnualLeaveMemos);
  779. TotalRecoveryHours -= RecoverLeaveRequestResult.Item3;
  780. if (TotalRecoveryHours > 0)
  781. {
  782. var sYear = iYear.ToString();
  783. var oLeaveSet = db.Queryable<OTB_EIP_LeaveSet>().Single(it => it.OrgID == i_crm.ORIGID && it.UserID == oEip.AskTheDummy && it.TYear == sYear);
  784. if (oLeaveSet != null)
  785. {
  786. var jaSetInfo = (JArray)JsonConvert.DeserializeObject(oLeaveSet.SetInfo);
  787. foreach (JObject jo in jaSetInfo)
  788. {
  789. if (jo[@"Id"].ToString() == oEip.HolidayCategory)
  790. {
  791. var sPaymentHours = jo[@"PaymentHours"].ToString();
  792. jo[@"UsedHours"] = Convert.ToDecimal(jo[@"UsedHours"].ToString()) - oEip.TotalTime;
  793. if (sPaymentHours != @"")
  794. {
  795. jo[@"RemainHours"] = Convert.ToDecimal(sPaymentHours) - Convert.ToDecimal(jo[@"UsedHours"].ToString());
  796. }
  797. break;
  798. }
  799. }
  800. var oLeaveSetUpd = new OTB_EIP_LeaveSet
  801. {
  802. SetInfo = JsonToString(jaSetInfo)
  803. };
  804. db.Updateable(oLeaveSetUpd).UpdateColumns(it => new { it.SetInfo })
  805. .Where(it => it.Guid == oLeaveSet.Guid).ExecuteCommand();
  806. }
  807. }
  808. }
  809. rm = new SuccessResponseMessage(null, i_crm);
  810. } while (false);
  811. return rm;
  812. });
  813. }
  814. catch (Exception ex)
  815. {
  816. sMsg = Util.GetLastExceptionMsg(ex);
  817. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), @"請假單管理", @"SetReEdit(抽單)", @"", @"", @"");
  818. }
  819. finally
  820. {
  821. if (null != sMsg)
  822. {
  823. rm = new ErrorResponseMessage(sMsg, i_crm);
  824. }
  825. }
  826. return rm;
  827. }
  828. #endregion 抽單
  829. #region 請假單管理(查詢筆數)
  830. /// <summary>
  831. /// 請假單管理(查詢筆數)
  832. /// </summary>
  833. /// <param name="i_crm"></param>
  834. /// <returns></returns>
  835. public ResponseMessage QueryCout(RequestMessage i_crm)
  836. {
  837. ResponseMessage rm = null;
  838. string sMsg = null;
  839. var db = SugarBase.GetIntance();
  840. try
  841. {
  842. do
  843. {
  844. var sEndTime = _fetchString(i_crm, @"EndTime");
  845. var iHours = _fetchInt(i_crm, @"Hours");
  846. var rEndTime = Convert.ToDateTime(sEndTime);
  847. var rStartTime = rEndTime.AddHours(-iHours);
  848. var iCout = db.Queryable<OTB_EIP_Leave>()
  849. .Where(x => x.OrgID == i_crm.ORIGID && x.AskTheDummy == i_crm.USERID && x.EndDate > rStartTime && x.EndDate <= rEndTime)
  850. .Count();
  851. rm = new SuccessResponseMessage(null, i_crm);
  852. rm.DATA.Add(BLWording.REL, iCout);
  853. } while (false);
  854. }
  855. catch (Exception ex)
  856. {
  857. sMsg = Util.GetLastExceptionMsg(ex);
  858. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), "", "QueryCout(請假單管理(查詢筆數))", "", "", "");
  859. }
  860. finally
  861. {
  862. if (null != sMsg)
  863. {
  864. rm = new ErrorResponseMessage(sMsg, i_crm);
  865. }
  866. }
  867. return rm;
  868. }
  869. #endregion 請假單管理(查詢筆數)
  870. #region 請假單管理(檢核當月最大請教時數)
  871. /// <summary>
  872. /// 請假單管理(檢核當月最大請教時數)
  873. /// </summary>
  874. /// <param name="i_crm"></param>
  875. /// <returns></returns>
  876. public ResponseMessage CheckMaxHours(RequestMessage i_crm)
  877. {
  878. ResponseMessage rm = null;
  879. string sMsg = null;
  880. var db = SugarBase.GetIntance();
  881. try
  882. {
  883. do
  884. {
  885. var sDate = _fetchString(i_crm, @"Date");
  886. var iCurHours = decimal.Parse(_fetchString(i_crm, @"CurHours"));
  887. var iMaxHours = _fetchInt(i_crm, @"MaxHours");
  888. var rDate = Convert.ToDateTime(sDate);
  889. var iYear = rDate.Year;
  890. var iNonth = rDate.Month;
  891. var listData = db.Queryable<OTB_EIP_Leave>()
  892. .Where(x => x.OrgID == i_crm.ORIGID && x.AskTheDummy == i_crm.USERID && x.StartDate.HasValue && x.StartDate.Value.Year == iYear)
  893. .Select(x => new { x.StartDate, x.EndDate, x.TotalTime }).ToList();
  894. var iHours = listData.Where(x => x.StartDate.Value.Month == iNonth || x.EndDate.Value.Month == iNonth).Sum(x => x.TotalTime);
  895. //var iHours = db.Queryable<OTB_EIP_Leave>()
  896. // .Where(x => x.OrgID == i_crm.ORIGID && x.AskTheDummy == i_crm.USERID && x.StartDate.Value.Year == iYear && (x.StartDate.Value.Month == iNonth || x.EndDate.Value.Month == iNonth))
  897. // .Sum(x => x.TotalTime);
  898. //.ToSql();
  899. rm = new SuccessResponseMessage(null, i_crm);
  900. rm.DATA.Add(BLWording.REL, iHours + iCurHours > iMaxHours);
  901. } while (false);
  902. }
  903. catch (Exception ex)
  904. {
  905. sMsg = Util.GetLastExceptionMsg(ex);
  906. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Leave_UpdService), "", "QueryCout(請假單管理(檢核當月最大請教時數))", "", "", "");
  907. }
  908. finally
  909. {
  910. if (null != sMsg)
  911. {
  912. rm = new ErrorResponseMessage(sMsg, i_crm);
  913. }
  914. }
  915. return rm;
  916. }
  917. #endregion 請假單管理(檢核當月最大請教時數)
  918. #region 獲取請假規則設定
  919. /// <summary>
  920. /// 檢查假別
  921. /// </summary>
  922. /// <param name="i_crm"></param>
  923. /// <returns></returns>
  924. public ResponseMessage GetLeaveSetting(RequestMessage i_crm)
  925. {
  926. ResponseMessage rm = null;
  927. string sMsg = null;
  928. var db = SugarBase.DB;
  929. try
  930. {
  931. do
  932. {
  933. var sArgumentID = _fetchString(i_crm, @"ArgumentID");
  934. var sOrgID = _fetchString(i_crm, @"OrgID");
  935. //取得出勤設定資料
  936. var oLeaveRuelsSetting = db.Queryable<OTB_SYS_Arguments, OTB_SYS_ArgumentsRelated>
  937. ((t1, t2) =>
  938. new object[] {
  939. JoinType.Left, t1.ArgumentID == t2.ArgumentID
  940. }
  941. )
  942. .Where((t1, t2) => t1.ArgumentClassID == "LeaveType" && t1.ArgumentID == sArgumentID && t1.OrgID == sOrgID && t2.OrgID == sOrgID)
  943. .Select((t1, t2) => new { t1.Correlation, t2.Field1, t2.Field2, t2.Field3 })
  944. .Single();
  945. rm = new SuccessResponseMessage(null, i_crm);
  946. rm.DATA.Add(BLWording.REL, oLeaveRuelsSetting);
  947. } while (false);
  948. }
  949. catch (Exception ex)
  950. {
  951. sMsg = Util.GetLastExceptionMsg(ex);
  952. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveSetService), @"出勤設定", @"GetLeaveSettingByType(依據請假類別獲取請假規則設定)", @"", @"", @"");
  953. }
  954. finally
  955. {
  956. if (null != sMsg)
  957. {
  958. rm = new ErrorResponseMessage(sMsg, i_crm);
  959. }
  960. }
  961. return rm;
  962. }
  963. #endregion 依據請假類別獲取請假規則設定
  964. #region GetUpdateWenZhong
  965. /// <summary>
  966. /// 返回特休假,leave一定要有資料
  967. /// </summary>
  968. /// <param name="db"></param>
  969. /// <param name="leave"></param>
  970. /// <returns></returns>
  971. public Tuple<bool, string> UpddateWenZhongAnnualLeave(SqlSugarClient db,string OrgID, string UserID, JArray AnnualLeaveData)
  972. {
  973. var Result = new List<OTB_EIP_WenZhong>();
  974. if (!AnnualLeaveData.Any())
  975. return new Tuple<bool, string>(false, @"請假的特休資訊遺失,請資訊人員核查!");
  976. if(string.IsNullOrEmpty(OrgID) || string.IsNullOrEmpty(UserID))
  977. return new Tuple<bool, string>(false, @"缺少組織或使用者資訊,請重新整理!");
  978. var RelativeWZGuid = new List<string>();
  979. var Dic = new Dictionary<string, decimal>();
  980. foreach (var ALM in AnnualLeaveData)
  981. {
  982. var Guid = ALM["Guid"].ToString();
  983. RelativeWZGuid.Add(ALM["Guid"].ToString());
  984. decimal.TryParse(ALM["Hours"].ToString(), out var Hours);
  985. Dic.Add(Guid, Hours);
  986. }
  987. var saWenZhong = db.Queryable<OTB_EIP_WenZhong>()
  988. .Where(it => it.OrgID == OrgID && it.UserID == UserID && RelativeWZGuid.Contains(it.Guid)).ToList();
  989. if (saWenZhong.Count > 0)
  990. {
  991. var UpdateWZData = new List<OTB_EIP_WenZhong>();
  992. foreach (OTB_EIP_WenZhong wz in saWenZhong)
  993. {
  994. decimal.TryParse(Dic[wz.Guid].ToString(), out var ReturnHours);
  995. var oWenZhong = new OTB_EIP_WenZhong
  996. {
  997. Guid = wz.Guid,
  998. UsedHours = wz.UsedHours - ReturnHours,
  999. RemainHours = wz.RemainHours + ReturnHours,
  1000. DelFlag = false,
  1001. };
  1002. UpdateWZData.Add(oWenZhong);
  1003. }
  1004. if (UpdateWZData.Any())
  1005. {
  1006. db.Updateable(UpdateWZData).UpdateColumns(it => new { it.Guid, it.UsedHours, it.RemainHours, it.DelFlag }).ExecuteCommand();
  1007. }
  1008. }
  1009. return new Tuple<bool, string>(true,"");
  1010. }
  1011. #endregion
  1012. public Tuple<bool, string,decimal> UpddateNormalRequestLeave(SqlSugarClient db, string OrgID, string UserID, JArray NormalLeaveData)
  1013. {
  1014. if (string.IsNullOrEmpty(OrgID) || string.IsNullOrEmpty(UserID))
  1015. return new Tuple<bool, string, decimal>(false, @"缺少組織或使用者資訊,請重新整理!", decimal.Zero);
  1016. if (NormalLeaveData == null || NormalLeaveData.Count == 0 )
  1017. return new Tuple<bool, string, decimal>(false, @"缺少請假資料,請重新整理!", decimal.Zero);
  1018. var RelativeLeaveRequestGuid = new List<string>();
  1019. var Dic = new Dictionary<string, decimal>();
  1020. var RecoverHours = decimal.Zero;
  1021. foreach (var NLD in NormalLeaveData)
  1022. {
  1023. var Guid = NLD["Guid"].ToString();
  1024. RelativeLeaveRequestGuid.Add(NLD["Guid"].ToString());
  1025. decimal.TryParse(NLD["Hours"].ToString(), out var Hours);
  1026. RecoverHours += Hours;
  1027. Dic.Add(Guid, Hours);
  1028. }
  1029. var saLeaveRequests = db.Queryable<OTB_EIP_LeaveRequest>()
  1030. .Where(it => it.OrgID == OrgID && it.MemberID == UserID && RelativeLeaveRequestGuid.Contains(it.guid)).ToList();
  1031. if (saLeaveRequests.Count > 0)
  1032. {
  1033. foreach (OTB_EIP_LeaveRequest wz in saLeaveRequests)
  1034. {
  1035. decimal.TryParse(Dic[wz.guid].ToString(), out var ReturnHours);
  1036. wz.UsedHours = wz.UsedHours - ReturnHours;
  1037. wz.RemainHours = wz.RemainHours + ReturnHours;
  1038. }
  1039. db.Updateable(saLeaveRequests).UpdateColumns(it => new { it.UsedHours, it.RemainHours }).ExecuteCommand();
  1040. }
  1041. return new Tuple<bool, string, decimal>(true, "", RecoverHours);
  1042. }
  1043. }
  1044. }