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.

223 lines
14 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.OPM
  12. {
  13. public class OtherExhibitionTG_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 sExhibitionName = _fetchString(i_crm, @"ExhibitionName");
  39. var sBillNO = _fetchString(i_crm, @"BillNO");
  40. var sAgent = _fetchString(i_crm, @"Agent");
  41. var sResponsiblePerson = _fetchString(i_crm, @"ResponsiblePerson");
  42. var sDeclarationClass = _fetchString(i_crm, @"DeclarationClass");
  43. var sIsIncludeVoid = _fetchString(i_crm, @"IsIncludeVoid");
  44. var sCustomer = _fetchString(i_crm, @"Customer");
  45. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  46. var bExcel = _fetchBool(i_crm, @"Excel");
  47. var sExcelType = _fetchString(i_crm, @"ExcelType");
  48. var spOrgID = new SugarParameter("@OrgID", i_crm.ORIGID);
  49. var spUserID = new SugarParameter("@UserID", i_crm.USERID);
  50. var spDepartmentID = new SugarParameter("@DepartmentID", sDepartmentID);
  51. string[] saIsIncludeVoid = null;
  52. if (!string.IsNullOrEmpty(sIsIncludeVoid))
  53. {
  54. saIsIncludeVoid = sIsIncludeVoid.Split(',');
  55. }
  56. 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();
  57. var saChildDeptIds = db.Ado.SqlQuery<string>("select DepartmentId from [dbo].[OFN_SYS_GetChilDepartmentIdByDepartmentId](@OrgID,@DepartmentID)", spOrgID, spDepartmentID).ToArray();
  58. var saDeptIdsByUser = db.Ado.SqlQuery<string>("SELECT DepartmentId FROM [dbo].[OFN_SYS_GetChilDepartmentIdByUserID](@OrgID,@UserID)", spOrgID, spUserID).ToArray();
  59. var saChildUserIds = db.Ado.SqlQuery<string>("SELECT MemberID FROM [dbo].[OFN_SYS_GetMemberIDDownByChief](@OrgID,@UserID)", spOrgID, spUserID).ToArray();
  60. pml.DataList = db.Queryable<OTB_OPM_OtherExhibitionTG, OTB_SYS_Members, OTB_OPM_Exhibition, OTB_SYS_Arguments, OTB_CRM_Customers>
  61. ((t1, t2, t3, t4, t5) =>
  62. new object[] {
  63. JoinType.Inner, t1.OrgID == t2.OrgID && t1.ResponsiblePerson == t2.MemberID,
  64. JoinType.Left, t1.OrgID == t3.OrgID && t1.ExhibitionNO == t3.SN.ToString(),
  65. JoinType.Left, t1.OrgID == t4.OrgID && t1.TransportationMode == t4.ArgumentID && t4.ArgumentClassID == "DeclClass",
  66. JoinType.Left, t1.OrgID == t5.OrgID && t1.Agent == t5.guid
  67. }
  68. )
  69. .Where((t1) => t1.OrgID == i_crm.ORIGID)
  70. .WhereIF(!string.IsNullOrEmpty(sBillNO), (t1) => t1.Bills.Contains("\"BillNO\":%\"" + sBillNO))
  71. .WhereIF(!string.IsNullOrEmpty(sExhibitionName), (t1, t2, t3) => (t3.Exhibitioname_TW + t3.Exhibitioname_EN + t3.ExhibitioShotName_TW).Contains(sExhibitionName))
  72. .WhereIF(!string.IsNullOrEmpty(sResponsiblePerson), (t1) => t1.ResponsiblePerson == sResponsiblePerson)
  73. .WhereIF(!string.IsNullOrEmpty(sDeclarationClass), (t1) => t1.DeclarationClass == sDeclarationClass)
  74. .WhereIF(!string.IsNullOrEmpty(sAgent), (t1, t2, t3, t4, t5) => (t5.CustomerCName + t5.CustomerEName).Contains(sAgent))
  75. .WhereIF(!string.IsNullOrEmpty(sCustomer), (t1) => t1.Exhibitors.Contains(sCustomer))
  76. .WhereIF(!string.IsNullOrEmpty(sDepartmentID), (t1) => SqlFunc.ContainsArray(saChildDeptIds, t1.DepartmentID))
  77. .WhereIF(!string.IsNullOrEmpty(sIsIncludeVoid), (t1) => SqlFunc.ContainsArray(saIsIncludeVoid, t1.IsVoid))
  78. .Where((t1) => t1.CreateUser == i_crm.USERID || t1.ResponsiblePerson == i_crm.USERID || SqlFunc.ContainsArray(saDeptIdsByUser, t1.DepartmentID) ||
  79. SqlFunc.Subqueryable<OTB_SYS_Members>().Where(c => c.MemberID == t1.CreateUser && c.OrgID == t1.OrgID).Select(c => c.ImmediateSupervisor) == i_crm.USERID ||
  80. SqlFunc.Subqueryable<OTB_SYS_Members>().Where(p => p.MemberID == t1.ResponsiblePerson && p.OrgID == t1.OrgID).Select(c => c.ImmediateSupervisor) == i_crm.USERID || SqlFunc.ContainsArray(saChildUserIds, t1.CreateUser) || SqlFunc.ContainsArray(saChildUserIds, t1.ResponsiblePerson) || SqlFunc.ContainsArray(saRoles, "Account") || SqlFunc.ContainsArray(saRoles, "CDD") || SqlFunc.ContainsArray(saRoles, "Admin") || SqlFunc.ContainsArray(saRoles, "Manager"))
  81. .Select((t1, t2, t3, t4, t5) => new View_OPM_OtherExhibitionTG
  82. {
  83. OrgID = t1.OrgID,
  84. Guid = t1.Guid,
  85. ExhibitionNO = t1.ExhibitionNO,
  86. ExhibitionDateStart = t1.ExhibitionDateStart,
  87. ExhibitionName = t3.Exhibitioname_TW,
  88. Exhibitioname_EN = t3.Exhibitioname_EN,
  89. ExhibitioShotName_TW = t3.ExhibitioShotName_TW,
  90. ResponsiblePersonName = t2.MemberName,
  91. DeclarationClassName = t4.ArgumentValue,
  92. AgentName = SqlFunc.IIF(SqlFunc.HasValue(t5.CustomerCName), t5.CustomerCName, t5.CustomerEName),
  93. ArrivalTimeShow = SqlFunc.MappingColumn(t1.Guid, "CONVERT(VARCHAR(16),ArrivalTime, 120)"),
  94. FreePeriodShow = SqlFunc.MappingColumn(t1.Guid, "CONVERT(VARCHAR(16),FreePeriod, 23)"),
  95. ResponsiblePerson = t1.ResponsiblePerson,
  96. Agent = t1.Agent,
  97. DeclarationClass = t1.DeclarationClass,
  98. ImportDeclarationNO = t1.ImportDeclarationNO,
  99. ContainerNumber = t1.ContainerNumber,
  100. BillLadNO = t1.BillLadNO,
  101. BillLadNOSub = t1.BillLadNOSub,
  102. BoxNo = t1.BoxNo,
  103. Unit = t1.Unit,
  104. Weight = t1.Weight,
  105. ShipmentPort = t1.ShipmentPort,
  106. DestinationPort = t1.DestinationPort,
  107. ArrivalTime = t1.ArrivalTime,
  108. StoragePlace = t1.StoragePlace,
  109. GoodsType = t1.GoodsType,
  110. Size = t1.Size,
  111. VolumeWeight = t1.VolumeWeight,
  112. FreePeriod = t1.FreePeriod,
  113. ExchangeRate = t1.ExchangeRate,
  114. IsVoid = t1.IsVoid,
  115. Volume = t1.Volume,
  116. Price = t1.Price,
  117. ShipmentPortCode = t1.ShipmentPortCode,
  118. DestinationPortCode = t1.DestinationPortCode,
  119. VoidReason = t1.VoidReason,
  120. Bills = t1.Bills,
  121. DepartmentID = t1.DepartmentID,
  122. Memo = t1.Memo,
  123. Exhibitors = t1.Exhibitors,
  124. CreateDate = t1.CreateDate
  125. })
  126. .MergeTable()
  127. .OrderByIF(string.IsNullOrEmpty(sSortField), "isnull(ExhibitionDateStart,CreateDate) desc,ExhibitionName")
  128. .OrderByIF(!string.IsNullOrEmpty(sSortField), sSortField + " " + sSortOrder)
  129. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  130. pml.Total = iPageCount;
  131. rm = new SuccessResponseMessage(null, i_crm);
  132. if (bExcel)
  133. {
  134. var sFileName = "";
  135. var oHeader = new Dictionary<string, string>();
  136. var listMerge = new List<Dictionary<string, int>>();
  137. var dicAlain = new Dictionary<string, string>();
  138. var saCustomers1 = pml.DataList;
  139. var saOtherExhibition = pml.DataList as List<View_OPM_OtherExhibitionTG>;
  140. var dt_new = saOtherExhibition.ListToDataTable<View_OPM_OtherExhibitionTG>();
  141. switch (sExcelType)
  142. {
  143. case "OtherBusiness_BasicInformation":
  144. sFileName = "其他(駒驛)業務基本資料";
  145. oHeader = new Dictionary<string, string>
  146. {
  147. { "RowIndex", "項次" },
  148. { "Guid", "帳單號碼" },
  149. { "ExhibitionName", "活動/展覽名稱" },
  150. { "ResponsiblePersonName", "負責業務" },
  151. { "AgentName", "國外代理" },
  152. { "DeclarationClassName", "報關類別" },
  153. { "ImportDeclarationNO", "報單號碼" },
  154. { "ContainerNumber", "貨櫃號碼" },
  155. { "BillLadNO", "提單號碼" },
  156. { "BillLadNOSub", "分提單號碼" },
  157. { "BoxNo", "件數" },
  158. { "Unit", "單位" },
  159. { "Weight", "毛重(G.W.)" },
  160. { "ShipmentPort", "起運地" },
  161. { "DestinationPort", "目的地" },
  162. { "ArrivalTimeShow", "抵達時間" },
  163. { "StoragePlace", "貨物儲放倉庫" },
  164. { "GoodsType", "貨型" },
  165. { "Size", "尺寸" },
  166. { "Volume", "材積(CBM)" },
  167. { "VolumeWeight", "材積重(C.W.)" },
  168. { "Price", "貨價" },
  169. { "FreePeriodShow", "免費期" },
  170. { "Memo", "特別注意事項" }
  171. };
  172. foreach (DataRow row in dt_new.Rows)
  173. {
  174. var sBills = "";
  175. var JaBills = (JArray)JsonConvert.DeserializeObject(row["Bills"].ToString());
  176. foreach (JObject bill in JaBills)
  177. {
  178. sBills += bill["BillNO"].ToString() + ",";
  179. }
  180. row["Guid"] = sBills;
  181. }
  182. break;
  183. default:
  184. {
  185. break;
  186. }
  187. }
  188. var bOk = new ExcelService().CreateExcelByTb(dt_new, out string sPath, oHeader, dicAlain, listMerge, sFileName);
  189. rm.DATA.Add(BLWording.REL, sPath);
  190. }
  191. else
  192. {
  193. rm.DATA.Add(BLWording.REL, pml);
  194. }
  195. } while (false);
  196. }
  197. catch (Exception ex)
  198. {
  199. sMsg = Util.GetLastExceptionMsg(ex);
  200. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherBusiness_QryService), "", "QueryPage(其他(分頁查詢))", "", "", "");
  201. }
  202. finally
  203. {
  204. if (null != sMsg)
  205. {
  206. rm = new ErrorResponseMessage(sMsg, i_crm);
  207. }
  208. }
  209. return rm;
  210. }
  211. #endregion 其他(分頁查詢)
  212. }
  213. }