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.

248 lines
13 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity;
  3. using Entity.Sugar;
  4. using Entity.ViewModels;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using SqlSugar;
  8. using SqlSugar.Base;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. namespace EasyBL.WEBAPP.WSM
  13. {
  14. public class PackingOrder_QryService : ServiceBase
  15. {
  16. #region 預約記錄分頁查詢
  17. /// <summary>
  18. /// 預約記錄分頁查詢
  19. /// </summary>
  20. /// <param name="i_crm"></param>
  21. /// <returns></returns>
  22. public ResponseMessage QueryPage(RequestMessage i_crm)
  23. {
  24. ResponseMessage rm = null;
  25. string sMsg = null;
  26. var db = SugarBase.GetIntance();
  27. try
  28. {
  29. do
  30. {
  31. var pml = new PageModel
  32. {
  33. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  34. PageSize = _fetchInt(i_crm, @"pageSize")
  35. };
  36. var iPageCount = 0;
  37. var sSortField = _fetchString(i_crm, @"sortField");
  38. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  39. var sMuseumMumber = _fetchString(i_crm, @"MuseumMumber");
  40. var sSearchWords = _fetchString(i_crm, @"SearchWords");
  41. var sAppointDateTimeStart = _fetchString(i_crm, @"AppointDateTimeStart");
  42. var sAppointDateTimeEnd = _fetchString(i_crm, @"AppointDateTimeEnd");
  43. var sApproachTime = _fetchString(i_crm, @"ApproachTime");
  44. var sExitTime = _fetchString(i_crm, @"ExitTime");
  45. var bExcel = _fetchBool(i_crm, @"Excel");
  46. var rAppointDateTimeStart = new DateTime();
  47. var rAppointDateTimeEnd = new DateTime();
  48. var rApproachTime = new DateTime();
  49. var rExitTime = new DateTime();
  50. if (!string.IsNullOrEmpty(sAppointDateTimeStart))
  51. {
  52. rAppointDateTimeStart = SqlFunc.ToDate(sAppointDateTimeStart);
  53. }
  54. if (!string.IsNullOrEmpty(sAppointDateTimeEnd))
  55. {
  56. rAppointDateTimeEnd = SqlFunc.ToDate(sAppointDateTimeEnd).AddDays(1);
  57. }
  58. if (!string.IsNullOrEmpty(sApproachTime))
  59. {
  60. rApproachTime = SqlFunc.ToDate(sApproachTime);
  61. }
  62. if (!string.IsNullOrEmpty(sExitTime))
  63. {
  64. rExitTime = SqlFunc.ToDate(sExitTime).AddDays(1);
  65. }
  66. pml.DataList = db.Queryable<OTB_WSM_PackingOrder, OTB_OPM_Exhibition, OTB_CRM_ImportCustomers>
  67. ((t1, t2, t3) =>
  68. new object[] {
  69. JoinType.Left, t1.OrgID == t2.OrgID && t1.ExhibitionNO == t2.SN.ToString(),
  70. JoinType.Left, t1.OrgID == t3.OrgID && t1.ExhibitionNO == t3.ExhibitionNO.ToString() && t1.CustomerId == t3.guid
  71. }
  72. )
  73. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.MuseumMumber.Contains(sMuseumMumber))
  74. .Where((t1, t2, t3) => t1.CompName.Contains(sSearchWords) || t1.AppointNO.Contains(sSearchWords) || t1.AppointUser.Contains(sSearchWords) || t1.AppointTel.Contains(sSearchWords) || t1.AppointEmail.Contains(sSearchWords) || t1.Contactor.Contains(sSearchWords) || t1.ContactTel.Contains(sSearchWords) || t1.PackingInfo.Contains(sSearchWords) || t2.Exhibitioname_TW.Contains(sSearchWords) || t2.Exhibitioname_EN.Contains(sSearchWords))
  75. .WhereIF(!string.IsNullOrEmpty(sAppointDateTimeStart), (t1, t2, t3) => t1.CreateDate >= rAppointDateTimeStart.Date)
  76. .WhereIF(!string.IsNullOrEmpty(sAppointDateTimeEnd), (t1, t2, t3) => t1.CreateDate <= rAppointDateTimeEnd.Date)
  77. .WhereIF(!string.IsNullOrEmpty(sApproachTime), (t1, t2, t3) => t1.ExitTime >= rApproachTime.Date)
  78. .WhereIF(!string.IsNullOrEmpty(sExitTime), (t1, t2, t3) => t1.ApproachTime <= rExitTime.Date)
  79. .Select((t1, t2, t3) => new View_WSM_PackingOrder
  80. {
  81. AppointNO = t1.AppointNO,
  82. OrgID = t1.OrgID,
  83. ExhibitionNO = t1.ExhibitionNO,
  84. CompName = t1.CompName,
  85. MuseumMumber = t1.MuseumMumber,
  86. AppointUser = t1.AppointUser,
  87. AppointTel = t1.AppointTel,
  88. AppointEmail = t1.AppointEmail,
  89. Contactor = t1.Contactor,
  90. ContactTel = t1.ContactTel,
  91. ApproachTime = t1.ApproachTime,
  92. ExitTime = t1.ExitTime,
  93. PackingInfo = t1.PackingInfo,
  94. Total = t1.Total,
  95. Memo = t1.Memo,
  96. CreateUser = t1.CreateUser,
  97. CreateDate = t1.CreateDate,
  98. CustomerId = t1.CustomerId,
  99. OtherId = t1.OtherId,
  100. AppointDateTime = t1.AppointDateTime,
  101. IsKeyMode = t1.IsKeyMode,
  102. OtherIdFrom = t1.OtherIdFrom,
  103. Exhibitioname_TW = t2.Exhibitioname_TW,
  104. Exhibitioname_EN = t2.Exhibitioname_EN,
  105. IsFormal = SqlFunc.IIF(t3.IsFormal == null, false, t3.IsFormal)
  106. })
  107. .MergeTable()
  108. .OrderBy(sSortField, sSortOrder)
  109. .ToPageList(pml.PageIndex, pml.PageSize, ref iPageCount);
  110. pml.Total = iPageCount;
  111. rm = new SuccessResponseMessage(null, i_crm);
  112. if (bExcel)
  113. {
  114. const string sFileName = "駒驛預約記錄";
  115. var oHeader = new Dictionary<string, string>
  116. {
  117. { "Exhibitioname_TW", "展覽名稱" },
  118. { "MuseumMumber", "攤位編號" },
  119. { "CompName", "公司名稱" },
  120. { "Field0", "攤位數" },
  121. { "Contactor", "聯絡人1" },
  122. { "ContactTel", "電話" },
  123. { "Field1", "進場日期" },
  124. { "Field2", "進場時間" },
  125. { "Field3", "退場日期" },
  126. { "Field4", "退場日期" },
  127. { "Field5", "件數" },
  128. { "Field6", "總重" },
  129. { "Field7", "長" },
  130. { "Field8", "寬" },
  131. { "Field9", "高" },
  132. { "Field10", "CBM" },
  133. { "Field11", "包裝" },
  134. { "Field12", "備註" },
  135. };
  136. var dt_new = new DataTable();
  137. dt_new.Columns.Add("Exhibitioname_TW");
  138. dt_new.Columns.Add("MuseumMumber");
  139. dt_new.Columns.Add("CompName");
  140. dt_new.Columns.Add("Field0");
  141. dt_new.Columns.Add("Contactor");
  142. dt_new.Columns.Add("ContactTel");
  143. dt_new.Columns.Add("Field1");
  144. dt_new.Columns.Add("Field2");
  145. dt_new.Columns.Add("Field3");
  146. dt_new.Columns.Add("Field4");
  147. dt_new.Columns.Add("Field5");
  148. dt_new.Columns.Add("Field6");
  149. dt_new.Columns.Add("Field7");
  150. dt_new.Columns.Add("Field8");
  151. dt_new.Columns.Add("Field9");
  152. dt_new.Columns.Add("Field10");
  153. dt_new.Columns.Add("Field11");
  154. dt_new.Columns.Add("Field12");
  155. var listMerge = new List<Dictionary<string, int>>();
  156. var oExpoType_TW = new Map { { "01", "裸機" }, { "02", "木箱" }, { "03", "散貨" }, { "04", "打板" }, { "05", "其他" } };
  157. var iPackingChildIndex = 0;
  158. var saPackingOrder = pml.DataList as List<View_WSM_PackingOrder>;
  159. foreach (var item in saPackingOrder)
  160. {
  161. var iPackingIndex = iPackingChildIndex;
  162. var jaPackingInfo = (JArray)JsonConvert.DeserializeObject(item.PackingInfo);
  163. if (jaPackingInfo.Count > 0)
  164. {
  165. foreach (JObject packinfo in jaPackingInfo)
  166. {
  167. var row_new = dt_new.NewRow();
  168. var sExpoType = packinfo["ExpoType"].ToString();
  169. var sExpoLen = packinfo["ExpoLen"].ToString();
  170. var sExpoWidth = packinfo["ExpoWidth"].ToString();
  171. var sExpoHeight = packinfo["ExpoHeight"].ToString();
  172. var sExpoWeight = packinfo["ExpoWeight"].ToString();
  173. var sExpoNumber = packinfo["ExpoNumber"].ToString();
  174. row_new["Exhibitioname_TW"] = item.Exhibitioname_TW;
  175. row_new["CompName"] = item.CompName;
  176. row_new["ContactTel"] = item.ContactTel;
  177. row_new["Field12"] = item.Memo;
  178. row_new["MuseumMumber"] = item.MuseumMumber;
  179. row_new["Field0"] = "";
  180. row_new["Contactor"] = item.Contactor;
  181. row_new["Field1"] = Convert.ToDateTime(item.ApproachTime).ToString("yyyy/MM/dd");
  182. row_new["Field2"] = Convert.ToDateTime(item.ApproachTime).ToString("HH:mm");
  183. row_new["Field3"] = Convert.ToDateTime(item.ExitTime).ToString("yyyy/MM/dd");
  184. row_new["Field4"] = Convert.ToDateTime(item.ExitTime).ToString("HH:mm");
  185. row_new["Field5"] = sExpoNumber;
  186. row_new["Field6"] = sExpoWeight;
  187. row_new["Field7"] = sExpoLen;
  188. row_new["Field8"] = sExpoWidth;
  189. row_new["Field9"] = sExpoHeight;
  190. row_new["Field10"] = Math.Round(Convert.ToDecimal(sExpoLen) * Convert.ToDecimal(sExpoWidth) * Convert.ToDecimal(sExpoHeight) / Convert.ToDecimal("1000000"), 2);
  191. row_new["Field11"] = oExpoType_TW[sExpoType].ToString();
  192. dt_new.Rows.Add(row_new);
  193. iPackingChildIndex++;
  194. }
  195. if (jaPackingInfo.Count > 1)
  196. {
  197. for (var i = 0; i < 10; i++)
  198. {
  199. var dicMerge = new Dictionary<string, int>
  200. {
  201. { "FirstRow", iPackingIndex + 3 },
  202. { "FirstCol", i },
  203. { "RowCount", jaPackingInfo.Count },
  204. { "ColCount", 1 }
  205. };
  206. listMerge.Add(dicMerge);
  207. }
  208. }
  209. }
  210. }
  211. var dicAlain = ExcelService.GetExportAlain(oHeader, "MuseumMumber,Contactor,Field0,Field1,Field2,Field3,Field4,Field5,Field6,Field7,Field8,Field9,Field10,Field11");
  212. var bOk = new ExcelService().CreateExcelByTb(dt_new, out string sPath, oHeader, dicAlain, listMerge, sFileName);
  213. rm.DATA.Add(BLWording.REL, sPath);
  214. }
  215. else
  216. {
  217. rm.DATA.Add(BLWording.REL, pml);
  218. }
  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.WSM.PackingOrder_QryService", "", "QueryPage(預約記錄分頁查詢)", "", "", "");
  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. }