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.

271 lines
11 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Entity.ViewModels;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. namespace EasyBL.WEBAPP.EIP
  14. {
  15. public class LeaveRequest_QryService : ServiceBase
  16. {
  17. #region 請假區間設定(分頁查詢)
  18. /// <summary>
  19. /// 客戶管理(分頁查詢)
  20. /// </summary>
  21. /// <param name="i_crm"></param>
  22. /// <returns></returns>
  23. public ResponseMessage QueryPage(RequestMessage i_crm)
  24. {
  25. ResponseMessage rm = null;
  26. string sMsg = null;
  27. var db = SugarBase.GetIntance();
  28. try
  29. {
  30. do
  31. {
  32. var pml = new PageModel
  33. {
  34. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  35. PageSize = _fetchInt(i_crm, @"pageSize")
  36. };
  37. var iPageCount = 0;
  38. var sSortField = _fetchString(i_crm, @"sortField");
  39. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  40. var sHolidayCategory = _fetchString(i_crm, @"HolidayCategory");
  41. var sUserID = _fetchString(i_crm, @"UserID");
  42. var sLeaveDateStart = _fetchString(i_crm, @"LeaveDateStart");
  43. var sLeaveDateEnd = _fetchString(i_crm, @"LeaveDateEnd");
  44. var rLeaveDateStart = new DateTime();
  45. var rLeaveDateEnd = new DateTime();
  46. if (!string.IsNullOrEmpty(sLeaveDateStart))
  47. {
  48. rLeaveDateStart = SqlFunc.ToDate(sLeaveDateStart);
  49. }
  50. if (!string.IsNullOrEmpty(sLeaveDateEnd))
  51. {
  52. rLeaveDateEnd = SqlFunc.ToDate(sLeaveDateEnd);
  53. }
  54. var saRoles = db.Queryable<OTB_SYS_MembersToRule>().Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID).Select(x => x.RuleID).ToList().ToArray();
  55. pml.DataList = db.Queryable<OTB_EIP_LeaveRequest, OTB_SYS_Members, OTB_SYS_Arguments>
  56. ((t1, t2, t3) =>
  57. new object[] {
  58. JoinType.Inner, t1.OrgID == t2.OrgID && t1.MemberID == t2.MemberID,
  59. JoinType.Inner, t1.OrgID == t3.OrgID && t1.Leave == t3.ArgumentID && t3.ArgumentClassID=="LeaveType"
  60. }
  61. )
  62. .Where(t1 => t1.OrgID == i_crm.ORIGID)
  63. .WhereIF(!string.IsNullOrEmpty(sHolidayCategory), (t1) => t1.Leave == sHolidayCategory)
  64. .WhereIF(!string.IsNullOrEmpty(sUserID), (t1) => t1.MemberID == sUserID)
  65. .WhereIF(!string.IsNullOrEmpty(sLeaveDateStart), (t1) => t1.EnableDate >= rLeaveDateStart.Date)
  66. .WhereIF(!string.IsNullOrEmpty(sLeaveDateEnd), (t1) => t1.ExpirationDate <= rLeaveDateEnd.Date)
  67. .Where((t1) => t1.MemberID == i_crm.USERID
  68. || SqlFunc.ContainsArray(saRoles, "EipManager") || SqlFunc.ContainsArray(saRoles, "Admin") || SqlFunc.ContainsArray(saRoles, "Manager"))
  69. .Select((t1, t2, t3) => new View_EIP_LeaveRequest
  70. {
  71. guid = SqlFunc.GetSelfAndAutoFill(t1.guid),
  72. WenZhongAcount = t2.WenZhongAcount,
  73. HolidayCategoryName = t3.ArgumentValue,
  74. MemberName = t2.MemberName,
  75. })
  76. .MergeTable()
  77. .OrderBy(sSortField, sSortOrder)
  78. .ToPageList(pml.PageIndex, pml.PageSize, ref iPageCount);
  79. pml.Total = iPageCount;
  80. rm = new SuccessResponseMessage(null, i_crm);
  81. rm.DATA.Add(BLWording.REL, pml);
  82. } while (false);
  83. }
  84. catch (Exception ex)
  85. {
  86. sMsg = Util.GetLastExceptionMsg(ex);
  87. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "QueryPage(客戶管理(分頁查詢))", "", "", "");
  88. }
  89. finally
  90. {
  91. if (null != sMsg)
  92. {
  93. rm = new ErrorResponseMessage(sMsg, i_crm);
  94. }
  95. }
  96. return rm;
  97. }
  98. #endregion 請假區間設定(分頁查詢)
  99. #region 請假區間設定(單筆查詢)
  100. /// <summary>
  101. /// 客戶管理(單筆查詢)
  102. /// </summary>
  103. /// <param name="i_crm"></param>
  104. /// <returns></returns>
  105. public ResponseMessage QueryOne(RequestMessage i_crm)
  106. {
  107. ResponseMessage rm = null;
  108. string sMsg = null;
  109. var db = SugarBase.GetIntance();
  110. try
  111. {
  112. do
  113. {
  114. var sId = _fetchString(i_crm, @"Guid");
  115. var oEntity = db.Queryable<OTB_EIP_LeaveRequest>().Single(x => x.OrgID == i_crm.ORIGID && x.guid == sId);
  116. rm = new SuccessResponseMessage(null, i_crm);
  117. rm.DATA.Add(BLWording.REL, oEntity);
  118. } while (false);
  119. }
  120. catch (Exception ex)
  121. {
  122. sMsg = Util.GetLastExceptionMsg(ex);
  123. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "QueryOne(請假區間設定(單筆查詢))", "", "", "");
  124. }
  125. finally
  126. {
  127. if (null != sMsg)
  128. {
  129. rm = new ErrorResponseMessage(sMsg, i_crm);
  130. }
  131. }
  132. return rm;
  133. }
  134. #endregion 請假區間設定(單筆查詢)
  135. #region 取得有效的請假(多筆查詢)
  136. /// <summary>
  137. /// 取得有效的請假
  138. /// </summary>
  139. /// <param name="i_crm"></param>
  140. /// <returns></returns>
  141. public ResponseMessage GetAvailableHLeaveHours(RequestMessage i_crm)
  142. {
  143. ResponseMessage rm = null;
  144. string sMsg = null;
  145. var db = SugarBase.GetIntance();
  146. try
  147. {
  148. do
  149. {
  150. //
  151. var sUserID = _fetchString(i_crm, @"UserID");
  152. var sOrgID = i_crm.ORIGID;
  153. var sLeaveDateStart = _fetchString(i_crm, @"LeaveDateStart");
  154. var sLeaveDateEnd = _fetchString(i_crm, @"LeaveDateEnd");
  155. //var sHolidayCategory = _fetchString(i_crm, @"HolidayCategory");
  156. var rLeaveDateStart = new DateTime();
  157. var rLeaveDateEnd = new DateTime();
  158. if (!string.IsNullOrEmpty(sLeaveDateStart))
  159. {
  160. rLeaveDateStart = SqlFunc.ToDate(sLeaveDateStart);
  161. }
  162. if (!string.IsNullOrEmpty(sLeaveDateEnd))
  163. {
  164. rLeaveDateEnd = SqlFunc.ToDate(sLeaveDateEnd);
  165. }
  166. var leaveRequests = db.Queryable<OTB_EIP_LeaveRequest>()
  167. .Where(t1 => t1.RemainHours > 0 && t1.OrgID == sOrgID)
  168. .WhereIF(!string.IsNullOrEmpty(sUserID), (t1) => t1.MemberID == sUserID)
  169. .WhereIF(!string.IsNullOrEmpty(sLeaveDateStart), (t1) => t1.EnableDate <= rLeaveDateStart.Date && t1.ExpirationDate >= rLeaveDateStart.Date)
  170. .WhereIF(!string.IsNullOrEmpty(sLeaveDateStart), (t1) => t1.EnableDate <= rLeaveDateEnd.Date && t1.ExpirationDate >= rLeaveDateEnd.Date)
  171. .OrderBy(t1 => t1.EnableDate)
  172. .OrderBy(t1 => t1.ExpirationDate)
  173. .ToList();
  174. rm = new SuccessResponseMessage(null, i_crm);
  175. rm.DATA.Add(BLWording.REL, leaveRequests);
  176. } while (false);
  177. }
  178. catch (Exception ex)
  179. {
  180. sMsg = Util.GetLastExceptionMsg(ex);
  181. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "GetAvailableHLeaveHours(取得有效的請假(多筆查詢))", "", "", "");
  182. }
  183. finally
  184. {
  185. if (null != sMsg)
  186. {
  187. rm = new ErrorResponseMessage(sMsg, i_crm);
  188. }
  189. }
  190. return rm;
  191. }
  192. #endregion 請假區間設定(單筆查詢)
  193. #region
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. /// <param name="i_crm"></param>
  198. /// <returns></returns>
  199. public ResponseMessage GetAllLeaveRequest(RequestMessage i_crm)
  200. {
  201. ResponseMessage rm = null;
  202. string sMsg = null;
  203. var db = SugarBase.GetIntance();
  204. try
  205. {
  206. do
  207. {
  208. var sUserID = _fetchString(i_crm, @"UserID");
  209. var sOrgID = i_crm.ORIGID;
  210. var iCurrentYear = _fetchInt(i_crm, @"CurrentYear");
  211. var leaveRequests = db.Queryable<OTB_EIP_LeaveRequest>()
  212. .Where(t1 => t1.OrgID == sOrgID)
  213. .WhereIF(!string.IsNullOrEmpty(sUserID), (t1) => t1.MemberID == sUserID)
  214. .OrderBy(t1 => t1.EnableDate).ToList();
  215. leaveRequests = leaveRequests.Where(t1 =>
  216. {
  217. if (t1.ExpirationDate.HasValue && t1.EnableDate.HasValue)
  218. {
  219. if (t1.ExpirationDate?.Year == iCurrentYear || t1.EnableDate?.Year == iCurrentYear)
  220. return true;
  221. }
  222. return false;
  223. }).ToList();
  224. rm = new SuccessResponseMessage(null, i_crm);
  225. rm.DATA.Add(BLWording.REL, leaveRequests);
  226. } while (false);
  227. }
  228. catch (Exception ex)
  229. {
  230. sMsg = Util.GetLastExceptionMsg(ex);
  231. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "GetAllLeaveRequest(請假區間設定(單筆查詢))", "", "", "");
  232. }
  233. finally
  234. {
  235. if (null != sMsg)
  236. {
  237. rm = new ErrorResponseMessage(sMsg, i_crm);
  238. }
  239. }
  240. return rm;
  241. }
  242. #endregion
  243. }
  244. }