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.

251 lines
13 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity.Sugar;
  3. using Entity.ViewModels;
  4. using SqlSugar;
  5. using SqlSugar.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. namespace EasyBL.WEBAPP.EIP
  10. {
  11. public class Leave_QryService : ServiceBase
  12. {
  13. #region 請假單分頁查詢
  14. /// <summary>
  15. /// 請假單分頁查詢
  16. /// </summary>
  17. /// <param name="i_crm"></param>
  18. /// <returns></returns>
  19. public ResponseMessage QueryPage(RequestMessage i_crm)
  20. {
  21. ResponseMessage rm = null;
  22. string sMsg = null;
  23. var db = SugarBase.GetIntance();
  24. try
  25. {
  26. do
  27. {
  28. var pml = new PageModel
  29. {
  30. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  31. PageSize = _fetchInt(i_crm, @"pageSize")
  32. };
  33. var iPageCount = 0;
  34. var sSortField = _fetchString(i_crm, @"sortField");
  35. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  36. var sKeyNote = _fetchString(i_crm, @"KeyNote") ?? "";
  37. var sAskTheDummy = _fetchString(i_crm, @"AskTheDummy");
  38. var sImportant = _fetchString(i_crm, @"Important") ?? "";
  39. var sStatus = _fetchString(i_crm, @"Status");
  40. var sRoles = _fetchString(i_crm, @"Roles") ?? "Admin";
  41. var sLeaveDateStart = _fetchString(i_crm, @"LeaveDateStart");
  42. var sLeaveDateEnd = _fetchString(i_crm, @"LeaveDateEnd");
  43. var sHolidayCategory = _fetchString(i_crm, @"HolidayCategory");
  44. var bExcel = _fetchBool(i_crm, @"Excel");
  45. var rLeaveDateStart = new DateTime();
  46. var rLeaveDateEnd = new DateTime();
  47. if (!string.IsNullOrEmpty(sLeaveDateStart))
  48. {
  49. rLeaveDateStart = SqlFunc.ToDate(sLeaveDateStart);
  50. }
  51. if (!string.IsNullOrEmpty(sLeaveDateEnd))
  52. {
  53. rLeaveDateEnd = SqlFunc.ToDate(sLeaveDateEnd).AddDays(1);
  54. }
  55. pml.DataList = db.Queryable<OTB_EIP_Leave, OTB_SYS_Members, OTB_SYS_Members, OTB_SYS_Members, OTB_SYS_Arguments>
  56. ((t1, t2, t3, t4, t5) =>
  57. new object[] {
  58. JoinType.Inner, t1.OrgID == t2.OrgID && t1.AskTheDummy == t2.MemberID,
  59. JoinType.Inner, t1.OrgID == t3.OrgID && t1.Agent_Person == t3.MemberID,
  60. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Handle_Person == t4.MemberID,
  61. JoinType.Inner, t1.OrgID == t5.OrgID && t1.HolidayCategory == t5.ArgumentID && t5.ArgumentClassID=="LeaveType"
  62. }
  63. )
  64. .Where((t1, t2, t3, t4) => t1.OrgID == i_crm.ORIGID && t1.KeyNote.Contains(sKeyNote))
  65. .WhereIF(!string.IsNullOrEmpty(sImportant), (t1, t2, t3, t4) => sImportant.Contains(t1.Important))
  66. .WhereIF(!string.IsNullOrEmpty(sAskTheDummy), (t1, t2, t3, t4) => t1.AskTheDummy == sAskTheDummy)
  67. .WhereIF(!string.IsNullOrEmpty(sStatus), (t1, t2, t3, t4) => sStatus.Contains(t1.Status))
  68. .WhereIF(!(sRoles.Contains("Admin") || sRoles.Contains("EipManager") || sRoles.Contains("EipView")), (t1, t2, t3, t4) => (t1.AskTheDummy == i_crm.USERID || t1.Handle_Person == i_crm.USERID || t1.Agent_Person == i_crm.USERID || t1.CheckFlows.Contains(i_crm.USERID)))
  69. .WhereIF(!string.IsNullOrEmpty(sLeaveDateStart), (t1, t2, t3, t4) => t1.EndDate >= rLeaveDateStart.Date)
  70. .WhereIF(!string.IsNullOrEmpty(sLeaveDateEnd), (t1, t2, t3, t4) => t1.StartDate <= rLeaveDateEnd.Date)
  71. .WhereIF(!string.IsNullOrEmpty(sHolidayCategory), (t1, t2, t3, t4) => t1.HolidayCategory == sHolidayCategory)
  72. .Select((t1, t2, t3, t4, t5) => new View_EIP_Leave
  73. {
  74. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  75. WenZhongAcount = t2.WenZhongAcount,
  76. AskTheDummyName = t2.MemberName,
  77. Agent_PersonName = t3.MemberName,
  78. Handle_PersonName = t4.MemberName,
  79. HolidayCategoryName = t5.ArgumentValue
  80. })
  81. .MergeTable()
  82. .OrderBy(sSortField, sSortOrder)
  83. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  84. pml.Total = iPageCount;
  85. rm = new SuccessResponseMessage(null, i_crm);
  86. if (bExcel)
  87. {
  88. const string sFileName = "請假單";
  89. var oHeader = new Dictionary<string, string>
  90. {
  91. { "RowIndex", "項次" },
  92. { "WenZhongAcount", "員工代號" },
  93. { "AskTheDummyName", "員工姓名" },
  94. { "HolidayCategory", "假別" },
  95. { "AttendanceDate", "出勤日期" },
  96. { "DateStart", "開始日期" },
  97. { "TimeStart", "開始時間" },
  98. { "DateEnd", "結束日期" },
  99. { "TimeEnd", "結束時間" },
  100. { "TotalTime", "請假時數 " },
  101. { "Field1", "扣伙食費 " },
  102. { "Field2", "扣交通津貼 " },
  103. { "LeaveReason", "事由" },
  104. { "Memo", "備註" },
  105. { "Field3", "工作門市代號" },
  106. { "Field4", "職務技能代號" }
  107. };
  108. var dt_new = new DataTable();
  109. dt_new.Columns.Add("RowIndex");
  110. dt_new.Columns.Add("WenZhongAcount");
  111. dt_new.Columns.Add("AskTheDummyName");
  112. dt_new.Columns.Add("HolidayCategory");
  113. dt_new.Columns.Add("AttendanceDate");
  114. dt_new.Columns.Add("DateStart");
  115. dt_new.Columns.Add("TimeStart");
  116. dt_new.Columns.Add("DateEnd");
  117. dt_new.Columns.Add("TimeEnd");
  118. dt_new.Columns.Add("TotalTime");
  119. dt_new.Columns.Add("Field1");
  120. dt_new.Columns.Add("Field2");
  121. dt_new.Columns.Add("LeaveReason");
  122. dt_new.Columns.Add("Memo");
  123. dt_new.Columns.Add("Field3");
  124. dt_new.Columns.Add("Field4");
  125. var listMerge = new List<Dictionary<string, int>>();
  126. var saLeave = pml.DataList as List<View_EIP_Leave>;
  127. foreach (var item in saLeave)
  128. {
  129. var row_new = dt_new.NewRow();
  130. row_new["RowIndex"] = item.RowIndex;
  131. row_new["WenZhongAcount"] = item.WenZhongAcount;
  132. row_new["AskTheDummyName"] = item.AskTheDummyName;
  133. row_new["HolidayCategory"] = item.HolidayCategory;
  134. row_new["DateStart"] = Convert.ToDateTime(item.StartDate).ToString("yyyy/MM/dd");
  135. row_new["TimeStart"] = Convert.ToDateTime(item.StartDate).ToString("HH:mm");
  136. row_new["DateEnd"] = Convert.ToDateTime(item.EndDate).ToString("yyyy/MM/dd");
  137. row_new["TimeEnd"] = Convert.ToDateTime(item.EndDate).ToString("HH:mm");
  138. row_new["AttendanceDate"] = row_new["DateStart"];
  139. row_new["TotalTime"] = item.TotalTime;
  140. row_new["LeaveReason"] = Common.CutByteString(item.LeaveReason, 40);
  141. row_new["Field1"] = "";
  142. row_new["Field2"] = "";
  143. row_new["Field3"] = "";
  144. row_new["Field4"] = "";
  145. row_new["Memo"] = item.Memo;
  146. dt_new.Rows.Add(row_new);
  147. }
  148. var dicAlain = ExcelService.GetExportAlain(oHeader, "WenZhongAcount,AskTheDummyName,HolidayCategory,TotalTime,DateStart,TimeStart,DateEnd,TimeEnd,AttendanceDate");
  149. var bOk = new ExcelService().CreateExcelByTb(dt_new, out string sPath, oHeader, dicAlain, listMerge, sFileName);
  150. rm.DATA.Add(BLWording.REL, sPath);
  151. }
  152. else
  153. {
  154. rm.DATA.Add(BLWording.REL, pml);
  155. }
  156. } while (false);
  157. }
  158. catch (Exception ex)
  159. {
  160. sMsg = Util.GetLastExceptionMsg(ex);
  161. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.Leave_QryService", "", "QueryPage(請假單分頁查詢)", "", "", "");
  162. }
  163. finally
  164. {
  165. if (null != sMsg)
  166. {
  167. rm = new ErrorResponseMessage(sMsg, i_crm);
  168. }
  169. }
  170. return rm;
  171. }
  172. #endregion 請假單分頁查詢
  173. #region 請假單單筆查詢
  174. /// <summary>
  175. /// 請假單單筆查詢
  176. /// </summary>
  177. /// <param name="i_crm"></param>
  178. /// <returns></returns>
  179. public ResponseMessage QueryOne(RequestMessage i_crm)
  180. {
  181. ResponseMessage rm = null;
  182. string sMsg = null;
  183. var db = SugarBase.GetIntance();
  184. try
  185. {
  186. do
  187. {
  188. var sId = _fetchString(i_crm, @"Guid");
  189. var oEntity = db.Queryable<OTB_EIP_Leave, OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Members, OTB_SYS_Members, OTB_SYS_Arguments>
  190. ((t1, t2, t3, t4, t5, t6) =>
  191. new object[] {
  192. JoinType.Inner, t1.OrgID == t2.OrgID && t1.AskTheDummy == t2.MemberID,
  193. JoinType.Inner, t2.OrgID == t3.OrgID && t2.DepartmentID == t3.DepartmentID,
  194. JoinType.Inner, t1.OrgID == t4.OrgID && t1.Agent_Person == t4.MemberID,
  195. JoinType.Inner, t1.OrgID == t5.OrgID && t1.Handle_Person == t5.MemberID,
  196. JoinType.Inner, t1.OrgID == t6.OrgID && t1.HolidayCategory == t6.ArgumentID && t6.ArgumentClassID=="LeaveType"
  197. }
  198. )
  199. .Where((t1, t2, t3, t4, t5, t6) => t1.OrgID == i_crm.ORIGID && t1.Guid == sId)
  200. .Select((t1, t2, t3, t4, t5, t6) => new View_EIP_Leave
  201. {
  202. Guid = SqlFunc.GetSelfAndAutoFill(t1.Guid),
  203. WenZhongAcount = t2.WenZhongAcount,
  204. AskTheDummyName = t2.MemberName,
  205. DeptName = t3.DepartmentName,
  206. Agent_PersonName = t4.MemberName,
  207. Handle_PersonName = t5.MemberName,
  208. HolidayCategoryName = t6.ArgumentValue
  209. }).Single();
  210. if (!string.IsNullOrEmpty(oEntity.RelationId))
  211. {
  212. var oRelation = db.Queryable<OTB_EIP_Leave>().Single(x => x.OrgID == i_crm.ORIGID && x.Guid == oEntity.RelationId);
  213. if (oRelation != null)
  214. {
  215. oEntity.ExFeild1 = oRelation.KeyNote;
  216. }
  217. }
  218. rm = new SuccessResponseMessage(null, i_crm);
  219. rm.DATA.Add(BLWording.REL, oEntity);
  220. } while (false);
  221. }
  222. catch (Exception ex)
  223. {
  224. sMsg = Util.GetLastExceptionMsg(ex);
  225. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.EIP.Leave_QryService", "", "QueryOne(請假單單筆查詢)", "", "", "");
  226. }
  227. finally
  228. {
  229. if (null != sMsg)
  230. {
  231. rm = new ErrorResponseMessage(sMsg, i_crm);
  232. }
  233. }
  234. return rm;
  235. }
  236. #endregion 請假單單筆查詢
  237. }
  238. }