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.

215 lines
9.9 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. namespace EasyBL.WEBAPP.SYS
  9. {
  10. public class ArgumentMaintain_QryService : ServiceBase
  11. {
  12. #region 參數值(分頁資料)
  13. /// <summary>
  14. /// 參數值(分頁資料)
  15. /// </summary>
  16. /// <param name="i_crm">todo: describe i_crm parameter on QueryPage</param>
  17. /// <returns></returns>
  18. public ResponseMessage QueryPage(RequestMessage i_crm)
  19. {
  20. ResponseMessage rm = null;
  21. string sMsg = null;
  22. var db = SugarBase.DB;
  23. try
  24. {
  25. do
  26. {
  27. var pml = new PageModel
  28. {
  29. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  30. PageSize = _fetchInt(i_crm, @"pageSize")
  31. };
  32. var iPageCount = 0;
  33. var sSortField = _fetchString(i_crm, @"sortField");
  34. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  35. var sArgumentClassID = _fetchString(i_crm, @"ArgumentClassID");
  36. var sArgumentID = _fetchString(i_crm, @"ArgumentID");
  37. var sArgumentValue = _fetchString(i_crm, @"ArgumentValue");
  38. var sEffective = _fetchString(i_crm, @"Effective");
  39. var bExcel = _fetchBool(i_crm, @"Excel");
  40. pml.DataList = db.Queryable<OTB_SYS_Arguments, OTB_SYS_ArgumentClass, OTB_SYS_Arguments>
  41. ((t1, t2, t3) =>
  42. new object[] {
  43. JoinType.Left, t1.OrgID == t2.OrgID && t1.ArgumentClassID == t2.ArgumentClassID,
  44. JoinType.Left, t1.OrgID == t3.OrgID && t1.ArgumentClassID == t3.ArgumentClassID && t1.ParentArgument==t3.ArgumentID
  45. }
  46. )
  47. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.DelStatus == "N" && t2.DelStatus == "N" && t1.ArgumentID.Contains(sArgumentID) && t1.ArgumentValue.Contains(sArgumentValue) && sEffective.Contains(t1.Effective))
  48. .WhereIF(!string.IsNullOrEmpty(sArgumentClassID), (t1, t2, t3) => t1.ArgumentClassID == sArgumentClassID)
  49. .Select((t1, t2, t3) => new View_SYS_Arguments
  50. {
  51. ArgumentID = SqlFunc.GetSelfAndAutoFill(t1.ArgumentID),
  52. ArgumentClassName = t2.ArgumentClassName,
  53. ParentArgumentName = t2.ArgumentClassName + "-" + t3.ArgumentValue,
  54. OrderCount = SqlFunc.Subqueryable<OTB_SYS_Arguments>().Where(p => p.ArgumentClassID == t1.ArgumentClassID && p.OrgID == t1.OrgID && p
  55. .DelStatus != "Y").Count()
  56. })
  57. .MergeTable()
  58. .OrderBy(sSortField, sSortOrder)
  59. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  60. pml.Total = iPageCount;
  61. rm = new SuccessResponseMessage(null, i_crm);
  62. if (bExcel)
  63. {
  64. const string sFileName = "參數值";
  65. var oHeader = new Dictionary<string, string>
  66. {
  67. { "RowIndex", "項次" },
  68. { "ArgumentClassName", "參數類別" },
  69. { "ParentArgumentName", "父層" },
  70. { "ArgumentID", "參數值" },
  71. { "ArgumentValue", "參數值說明" },
  72. { "Effective", "狀態(Y:有效;N:無效)" }
  73. };
  74. var dicAlain = ExcelService.GetExportAlain(oHeader, "Effective");
  75. var saArguments = pml.DataList as List<View_SYS_Arguments>;
  76. var bOk = new ExcelService().CreateExcelByList(saArguments, out string sPath, oHeader, dicAlain, sFileName);
  77. rm.DATA.Add(BLWording.REL, sPath);
  78. }
  79. else
  80. {
  81. rm.DATA.Add(BLWording.REL, pml);
  82. }
  83. } while (false);
  84. }
  85. catch (Exception ex)
  86. {
  87. sMsg = Util.GetLastExceptionMsg(ex);
  88. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ArgumentMaintain_QryService), @"參數值", @"QueryPage(參數值(分頁資料))", @"", @"", @"");
  89. }
  90. finally
  91. {
  92. if (null != sMsg)
  93. {
  94. rm = new ErrorResponseMessage(sMsg, i_crm);
  95. }
  96. }
  97. return rm;
  98. }
  99. #endregion 參數值(分頁資料)
  100. #region 參數類別(多筆)
  101. /// <summary>
  102. /// 參數類別(多筆)
  103. /// </summary>
  104. /// <param name="i_crm"></param>
  105. /// <returns></returns>
  106. public ResponseMessage QueryList(RequestMessage i_crm)
  107. {
  108. ResponseMessage rm = null;
  109. string sMsg = null;
  110. var db = SugarBase.GetIntance();
  111. try
  112. {
  113. do
  114. {
  115. var saArgumentClass = db.Queryable<OTB_SYS_ArgumentClass>()
  116. .Where(x => x.OrgID == i_crm.ORIGID && x.Effective == "Y" && x.DelStatus == "N")
  117. .OrderBy(x => x.OrderByValue)
  118. .ToList();
  119. rm = new SuccessResponseMessage(null, i_crm);
  120. rm.DATA.Add(BLWording.REL, saArgumentClass);
  121. } while (false);
  122. }
  123. catch (Exception ex)
  124. {
  125. sMsg = Util.GetLastExceptionMsg(ex);
  126. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ArgumentMaintain_QryService), "", "QueryList(參數類別(多筆))", "", "", "");
  127. }
  128. finally
  129. {
  130. if (null != sMsg)
  131. {
  132. rm = new ErrorResponseMessage(sMsg, i_crm);
  133. }
  134. }
  135. return rm;
  136. }
  137. #endregion 參數類別(多筆)
  138. #region 參數值(更新排序)
  139. /// <summary>
  140. /// 參數值(更新排序)
  141. /// </summary>
  142. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  143. /// <returns></returns>
  144. public ResponseMessage UpdateOrderByValue(RequestMessage i_crm)
  145. {
  146. ResponseMessage rm = null;
  147. string sMsg = null;
  148. try
  149. {
  150. rm = SugarBase.ExecTran(db =>
  151. {
  152. do
  153. {
  154. var sParentId = _fetchString(i_crm, @"ParentId");
  155. var sId = _fetchString(i_crm, @"Id");
  156. var iOldOrderByValue = _fetchInt(i_crm, @"OldOrderByValue");
  157. var iNewOrderByValue = _fetchInt(i_crm, @"NewOrderByValue");
  158. var oOrderEntity = db.Queryable<OTB_SYS_Arguments>()
  159. .Single(x => x.OrgID == i_crm.ORIGID && x.ArgumentClassID == sParentId && x.ArgumentID == sId);
  160. if (iNewOrderByValue > iOldOrderByValue)
  161. {
  162. var iRelUp = db.Updateable<OTB_SYS_Arguments>()
  163. .UpdateColumns(x => new OTB_SYS_Arguments { OrderByValue = x.OrderByValue - 1 })
  164. .Where(x => x.OrgID == oOrderEntity.OrgID && x.ArgumentClassID == sParentId && x.DelStatus == "N" && x.OrderByValue <= iNewOrderByValue && x.OrderByValue > iOldOrderByValue)
  165. .ExecuteCommand();
  166. }
  167. else
  168. {
  169. var iRelDown = db.Updateable<OTB_SYS_Arguments>()
  170. .UpdateColumns(x => new OTB_SYS_Arguments { OrderByValue = x.OrderByValue + 1 })
  171. .Where(x => x.OrgID == oOrderEntity.OrgID && x.ArgumentClassID == sParentId && x.DelStatus == "N" && x.OrderByValue >= iNewOrderByValue && x.OrderByValue < iOldOrderByValue)
  172. .ExecuteCommand();
  173. }
  174. var iRelSelf = db.Updateable(new OTB_SYS_Arguments { OrderByValue = iNewOrderByValue })
  175. .UpdateColumns(x => x.OrderByValue)
  176. .Where(x => x.OrgID == i_crm.ORIGID && x.ArgumentClassID == sParentId && x.ArgumentID == sId)
  177. .ExecuteCommand();
  178. rm = new SuccessResponseMessage(null, i_crm);
  179. rm.DATA.Add(BLWording.REL, iRelSelf);
  180. } while (false);
  181. return rm;
  182. });
  183. }
  184. catch (Exception ex)
  185. {
  186. sMsg = Util.GetLastExceptionMsg(ex);
  187. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ArgumentMaintain_QryService), @"參數值管理", @"UpdateOrderByValue(參數值管理(更新排序))", @"", @"", @"");
  188. }
  189. finally
  190. {
  191. if (null != sMsg)
  192. {
  193. rm = new ErrorResponseMessage(sMsg, i_crm);
  194. }
  195. }
  196. return rm;
  197. }
  198. #endregion 參數值(更新排序)
  199. }
  200. }