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.

246 lines
11 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.Text;
  11. namespace EasyBL.WEBAPP.WSM
  12. {
  13. public class TrackingLog_QryService : ServiceBase
  14. {
  15. #region 查詢貨況查詢記錄分組明細
  16. /// <summary>
  17. /// 查詢貨況查詢記錄分組明細
  18. /// </summary>
  19. /// <param name="i_crm"></param>
  20. /// <returns></returns>
  21. public ResponseMessage GetGroupInfo(RequestMessage i_crm)
  22. {
  23. ResponseMessage rm = null;
  24. string sMsg = null;
  25. var db = SugarBase.DB;
  26. try
  27. {
  28. do
  29. {
  30. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  31. var sQueryNumber = _fetchString(i_crm, @"QueryNumber");
  32. var sQueryTimeStart = _fetchString(i_crm, @"QueryTimeStart");
  33. var sQueryTimeEnd = _fetchString(i_crm, @"QueryTimeEnd");
  34. var sbSql = new StringBuilder();
  35. var spm = new List<SugarParameter>();
  36. sbSql.Append("select count(1) Count,QueryIp,IPInfo,QueryNumber from OTB_WSM_TrackingLog where 1=1 ");
  37. if (!string.IsNullOrEmpty(sDepartmentID))
  38. {
  39. sbSql.Append(" and DepartmentIDs like '%@DepartmentIDs%' ");
  40. spm.Add(new SugarParameter("@DepartmentIDs", sDepartmentID));
  41. }
  42. if (!string.IsNullOrEmpty(sQueryTimeStart))
  43. {
  44. sbSql.Append(" and querytime>= @QueryTimeStart ");
  45. spm.Add(new SugarParameter("@QueryTimeStart", sQueryTimeStart));
  46. }
  47. if (!string.IsNullOrEmpty(sQueryTimeEnd))
  48. {
  49. sbSql.Append(" and querytime<= @QueryTimeEnd ");
  50. spm.Add(new SugarParameter("@QueryTimeEnd", sQueryTimeEnd));
  51. }
  52. sbSql.Append(" group by QueryIp,IPInfo,QueryNumber ");
  53. var list = db.SqlQueryable<View_WSM_GroupTrackingLog>(sbSql.ToString()).AddParameters(spm).Where(it => it.QueryNumber == sQueryNumber).ToList();
  54. rm = new SuccessResponseMessage(null, i_crm);
  55. rm.DATA.Add(BLWording.REL, list);
  56. } while (false);
  57. }
  58. catch (Exception ex)
  59. {
  60. sMsg = Util.GetLastExceptionMsg(ex);
  61. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TrackingLog_QryService), @"貨況查詢", @"GetGroupInfo(查詢貨況查詢記錄分組明細)", @"", @"", @"");
  62. }
  63. finally
  64. {
  65. if (null != sMsg)
  66. {
  67. rm = new ErrorResponseMessage(sMsg, i_crm);
  68. }
  69. }
  70. return rm;
  71. }
  72. #endregion 查詢貨況查詢記錄分組明細
  73. #region Tracking(Log記錄)分頁查詢
  74. /// <summary>
  75. /// Tracking(Log記錄)分頁查詢
  76. /// </summary>
  77. /// <param name="i_crm"></param>
  78. /// <returns></returns>
  79. public ResponseMessage QueryPage(RequestMessage i_crm)
  80. {
  81. ResponseMessage rm = null;
  82. string sMsg = null;
  83. var db = SugarBase.GetIntance();
  84. try
  85. {
  86. do
  87. {
  88. var pml = new PageModel
  89. {
  90. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  91. PageSize = _fetchInt(i_crm, @"pageSize")
  92. };
  93. var iPageCount = 0;
  94. var sSortField = _fetchString(i_crm, @"sortField");
  95. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  96. var sQueryNumber = _fetchString(i_crm, @"QueryNumber");
  97. var sExhibitioName = _fetchString(i_crm, @"ExhibitioName");
  98. var sDepartmentID = _fetchString(i_crm, @"DepartmentID");
  99. var sQueryTimeStart = _fetchString(i_crm, @"QueryTimeStart");
  100. var sQueryTimeEnd = _fetchString(i_crm, @"QueryTimeEnd");
  101. var bExcel = _fetchBool(i_crm, @"Excel");
  102. var rQueryTimeStart = new DateTime();
  103. var rQueryTimeEnd = new DateTime();
  104. if (!string.IsNullOrEmpty(sQueryTimeStart))
  105. {
  106. rQueryTimeStart = SqlFunc.ToDate(sQueryTimeStart);
  107. }
  108. if (!string.IsNullOrEmpty(sQueryTimeEnd))
  109. {
  110. rQueryTimeEnd = SqlFunc.ToDate(sQueryTimeEnd).AddDays(1);
  111. }
  112. var q1 = db.Queryable<OTB_WSM_TrackingLog, OTB_OPM_ImportExhibition, OTB_OPM_ExportExhibition>
  113. ((t1, t2, t3) =>
  114. new object[] {
  115. JoinType.Left, t1.OrgID == t2.OrgID && t1.ParentId == t2.ImportBillNO,
  116. JoinType.Left, t1.OrgID == t3.OrgID && t1.ParentId == t3.ExportBillNO
  117. }
  118. )
  119. .Select((t1, t2, t3) => new View_WSM_TrackingLog
  120. {
  121. NO = SqlFunc.GetSelfAndAutoFill(t1.NO),
  122. ExhibitionNO = SqlFunc.IsNull(t2.ExhibitionNO, t3.ExhibitionNO),
  123. ResponsiblePerson = SqlFunc.IsNull(t2.ResponsiblePerson, t3.ResponsiblePerson)
  124. });
  125. var q2 = db.Queryable<OTB_OPM_Exhibition>();
  126. pml.DataList = db.Queryable
  127. (q1, q2, (v1, v2) => v1.OrgID == v2.OrgID && v1.ExhibitionNO == v2.SN.ToString())
  128. .Where((v1, v2) => v1.DepartmentIDs.Contains(sDepartmentID) && v1.QueryNumber.Contains(sQueryNumber) && (v2.Exhibitioname_TW.Contains(sExhibitioName) || v2.Exhibitioname_EN.Contains(sExhibitioName)))
  129. .WhereIF(!string.IsNullOrEmpty(sQueryTimeStart), (v1, v2) => v1.QueryTime >= rQueryTimeStart.Date)
  130. .WhereIF(!string.IsNullOrEmpty(sQueryTimeEnd), (v1, v2) => v1.QueryTime <= rQueryTimeEnd.Date)
  131. .Select((v1, v2) => new View_WSM_TrackingLog
  132. {
  133. NO = SqlFunc.GetSelfAndAutoFill(v1.NO),
  134. Exhibitioname_TW = v2.Exhibitioname_TW,
  135. Exhibitioname_EN = v2.Exhibitioname_EN
  136. })
  137. .MergeTable()
  138. .OrderBy(sSortField, sSortOrder)
  139. .ToPageList(pml.PageIndex, bExcel ? 100000 : pml.PageSize, ref iPageCount);
  140. pml.Total = iPageCount;
  141. rm = new SuccessResponseMessage(null, i_crm);
  142. if (bExcel)
  143. {
  144. const string sFileName = "Tracking查詢記錄";
  145. var oHeader = new Dictionary<string, string>
  146. {
  147. { "RowIndex", "項次" },
  148. { "QueryNumber", "查詢碼" },
  149. { "Exhibitioname_TW", "活動/展覽名稱" },
  150. { "Exhibitioname_EN", "英文展名" },
  151. { "AgentName", "國外代理" },
  152. { "CustomerName", "客戶/參展廠商" },
  153. { "QueryIp", "IP" },
  154. { "QueryInfo", "IP地址信息" },
  155. { "QueryTime", "查詢時間 " }
  156. };
  157. var saViewTrackingLog = pml.DataList as List<View_WSM_TrackingLog>;
  158. foreach (var item in saViewTrackingLog)
  159. {
  160. var joIPInfo = (JObject)JsonConvert.DeserializeObject(item.IPInfo);
  161. item.IPInfo = joIPInfo["country"] + " " + joIPInfo["area"] + " " + joIPInfo["region"] + " " + joIPInfo["city"];
  162. item.IPInfo = ChineseStringUtility.ToTraditional(item.IPInfo);
  163. }
  164. var dicAlain = ExcelService.GetExportAlain(oHeader, "QueryTime");
  165. var bOk = new ExcelService().CreateExcelByList(saViewTrackingLog, out string sPath, oHeader, dicAlain, sFileName);
  166. rm.DATA.Add(BLWording.REL, sPath);
  167. }
  168. else
  169. {
  170. rm.DATA.Add(BLWording.REL, pml);
  171. }
  172. } while (false);
  173. }
  174. catch (Exception ex)
  175. {
  176. sMsg = Util.GetLastExceptionMsg(ex);
  177. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WEBAPP.WSM.TrackingLog_QryService", "", "QueryPage(Tracking(Log記錄)分頁查詢)", "", "", "");
  178. }
  179. finally
  180. {
  181. if (null != sMsg)
  182. {
  183. rm = new ErrorResponseMessage(sMsg, i_crm);
  184. }
  185. }
  186. return rm;
  187. }
  188. #endregion Tracking(Log記錄)分頁查詢
  189. #region Tracking(Log記錄)(刪除)
  190. /// <summary>
  191. /// Tracking(Log記錄)(刪除)
  192. /// </summary>
  193. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  194. /// <returns></returns>
  195. public ResponseMessage GridDelete(RequestMessage i_crm)
  196. {
  197. ResponseMessage rm = null;
  198. string sMsg = null;
  199. try
  200. {
  201. rm = SugarBase.ExecTran(db =>
  202. {
  203. do
  204. {
  205. var iNO = _fetchInt(i_crm, @"NO");
  206. var iRel = db.Deleteable<OTB_WSM_TrackingLog>()
  207. .Where(x => x.OrgID == i_crm.ORIGID && x.NO == iNO).ExecuteCommand();
  208. rm = new SuccessResponseMessage(null, i_crm);
  209. rm.DATA.Add(BLWording.REL, iRel);
  210. } while (false);
  211. return rm;
  212. });
  213. }
  214. catch (Exception ex)
  215. {
  216. sMsg = Util.GetLastExceptionMsg(ex);
  217. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(TrackingLog_QryService), @"多語系管理", @"GridDelete(Tracking(Log記錄)(刪除))", @"", @"", @"");
  218. }
  219. finally
  220. {
  221. if (null != sMsg)
  222. {
  223. rm = new ErrorResponseMessage(sMsg, i_crm);
  224. }
  225. }
  226. return rm;
  227. }
  228. #endregion Tracking(Log記錄)(刪除)
  229. }
  230. }