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.

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