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.

147 lines
6.2 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.Linq;
  9. namespace EasyBL.WEBAPP.SYS
  10. {
  11. public class DepartmentsMaintain_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 sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  37. var sDepartmentName = _fetchString(i_crm, @"DepartmentName");
  38. var sEffective = _fetchString(i_crm, @"Effective");
  39. var bExcel = _fetchBool(i_crm, @"Excel");
  40. pml.DataList = db.Queryable<OTB_SYS_Departments>()
  41. .Where(x => x.OrgID == i_crm.ORIGID && x.DelStatus != "Y" && x.DepartmentID.Contains(sDepartmentID) && x.DepartmentName.Contains(sDepartmentName) && sEffective.Contains(x.Effective))
  42. .Select(x => new View_SYS_Departments
  43. {
  44. DepartmentID = SqlFunc.GetSelfAndAutoFill(x.DepartmentID),
  45. OrderCount = SqlFunc.Subqueryable<OTB_SYS_Departments>().Where(p => p.DelStatus != "Y" && p.OrgID == x.OrgID).Count()
  46. })
  47. .OrderBy(sSortField, sSortOrder)
  48. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  49. pml.Total = iPageCount;
  50. rm = new SuccessResponseMessage(null, i_crm);
  51. if (bExcel)
  52. {
  53. }
  54. else
  55. {
  56. rm.DATA.Add(BLWording.REL, pml);
  57. }
  58. } while (false);
  59. }
  60. catch (Exception ex)
  61. {
  62. sMsg = Util.GetLastExceptionMsg(ex);
  63. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_QryService), "", "QueryPage(部門管理(分頁查詢))", "", "", "");
  64. }
  65. finally
  66. {
  67. if (null != sMsg)
  68. {
  69. rm = new ErrorResponseMessage(sMsg, i_crm);
  70. }
  71. }
  72. return rm;
  73. }
  74. #endregion 部門管理(分頁查詢)
  75. #region 部門管理(更新排序)
  76. /// <summary>
  77. /// 部門管理(更新排序)
  78. /// </summary>
  79. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  80. /// <returns></returns>
  81. public ResponseMessage UpdateOrderByValue(RequestMessage i_crm)
  82. {
  83. ResponseMessage rm = null;
  84. string sMsg = null;
  85. try
  86. {
  87. rm = SugarBase.ExecTran(db =>
  88. {
  89. do
  90. {
  91. var sId = _fetchString(i_crm, @"Id");
  92. var iOldOrderByValue = _fetchInt(i_crm, @"OldOrderByValue");
  93. var iNewOrderByValue = _fetchInt(i_crm, @"NewOrderByValue");
  94. var oOrderEntity = db.Queryable<OTB_SYS_Departments>().Single(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == sId);
  95. if (iNewOrderByValue > iOldOrderByValue)
  96. {
  97. var iRelUp = db.Updateable<OTB_SYS_Departments>()
  98. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue - 1 })
  99. .Where(x => x.OrgID == oOrderEntity.OrgID && x.OrderByValue <= iNewOrderByValue && x.OrderByValue > iOldOrderByValue)
  100. .ExecuteCommand();
  101. }
  102. else
  103. {
  104. var iRelDown = db.Updateable<OTB_SYS_Departments>()
  105. .UpdateColumns(x => new OTB_SYS_Departments { OrderByValue = x.OrderByValue + 1 })
  106. .Where(x => x.OrgID == oOrderEntity.OrgID && x.OrderByValue >= iNewOrderByValue && x.OrderByValue < iOldOrderByValue)
  107. .ExecuteCommand();
  108. }
  109. var iRelSelf = db.Updateable(new OTB_SYS_Departments { OrderByValue = iNewOrderByValue })
  110. .UpdateColumns(x => x.OrderByValue)
  111. .Where(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == sId)
  112. .ExecuteCommand();
  113. rm = new SuccessResponseMessage(null, i_crm);
  114. rm.DATA.Add(BLWording.REL, iRelSelf);
  115. } while (false);
  116. return rm;
  117. });
  118. }
  119. catch (Exception ex)
  120. {
  121. sMsg = Util.GetLastExceptionMsg(ex);
  122. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(DepartmentsMaintain_QryService), @"部門管理", @"UpdateOrderByValue(部門管理(更新排序))", @"", @"", @"");
  123. }
  124. finally
  125. {
  126. if (null != sMsg)
  127. {
  128. rm = new ErrorResponseMessage(sMsg, i_crm);
  129. }
  130. }
  131. return rm;
  132. }
  133. #endregion 部門管理(更新排序)
  134. }
  135. }