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.

273 lines
11 KiB

3 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(sLeaveDateEnd), (t1) => t1.EnableDate <= rLeaveDateEnd.Date && t1.ExpirationDate >= rLeaveDateEnd.Date)
  171. .Where(t1 => t1.MemberID == sUserID)
  172. .Where(t1 => (t1.EnableDate <= rLeaveDateStart.Date && t1.ExpirationDate >= rLeaveDateStart.Date) || (t1.EnableDate <= rLeaveDateEnd.Date && t1.ExpirationDate >= rLeaveDateEnd.Date))
  173. .OrderBy(t1 => t1.EnableDate)
  174. .OrderBy(t1 => t1.ExpirationDate)
  175. .ToList();
  176. rm = new SuccessResponseMessage(null, i_crm);
  177. rm.DATA.Add(BLWording.REL, leaveRequests);
  178. } while (false);
  179. }
  180. catch (Exception ex)
  181. {
  182. sMsg = Util.GetLastExceptionMsg(ex);
  183. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "GetAvailableHLeaveHours(取得有效的請假(多筆查詢))", "", "", "");
  184. }
  185. finally
  186. {
  187. if (null != sMsg)
  188. {
  189. rm = new ErrorResponseMessage(sMsg, i_crm);
  190. }
  191. }
  192. return rm;
  193. }
  194. #endregion 請假區間設定(單筆查詢)
  195. #region
  196. /// <summary>
  197. ///
  198. /// </summary>
  199. /// <param name="i_crm"></param>
  200. /// <returns></returns>
  201. public ResponseMessage GetAllLeaveRequest(RequestMessage i_crm)
  202. {
  203. ResponseMessage rm = null;
  204. string sMsg = null;
  205. var db = SugarBase.GetIntance();
  206. try
  207. {
  208. do
  209. {
  210. var sUserID = _fetchString(i_crm, @"UserID");
  211. var sOrgID = i_crm.ORIGID;
  212. var iCurrentYear = _fetchInt(i_crm, @"CurrentYear");
  213. var leaveRequests = db.Queryable<OTB_EIP_LeaveRequest>()
  214. .Where(t1 => t1.OrgID == sOrgID)
  215. .WhereIF(!string.IsNullOrEmpty(sUserID), (t1) => t1.MemberID == sUserID)
  216. .OrderBy(t1 => t1.EnableDate).ToList();
  217. leaveRequests = leaveRequests.Where(t1 =>
  218. {
  219. if (t1.ExpirationDate.HasValue && t1.EnableDate.HasValue)
  220. {
  221. if (t1.ExpirationDate?.Year == iCurrentYear || t1.EnableDate?.Year == iCurrentYear)
  222. return true;
  223. }
  224. return false;
  225. }).ToList();
  226. rm = new SuccessResponseMessage(null, i_crm);
  227. rm.DATA.Add(BLWording.REL, leaveRequests);
  228. } while (false);
  229. }
  230. catch (Exception ex)
  231. {
  232. sMsg = Util.GetLastExceptionMsg(ex);
  233. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(LeaveRequest_QryService), "", "GetAllLeaveRequest(請假區間設定(單筆查詢))", "", "", "");
  234. }
  235. finally
  236. {
  237. if (null != sMsg)
  238. {
  239. rm = new ErrorResponseMessage(sMsg, i_crm);
  240. }
  241. }
  242. return rm;
  243. }
  244. #endregion
  245. }
  246. }