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.

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