TraceSupplementLogService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using XYY.Common.Standard;
  7. using XYY.Core.Standard.Data.Infrastructure;
  8. using XYY.Data.Model.Standard.Tracking;
  9. using Dapper;
  10. using XYY.Core.Standard.ExcelHelper.MSExcelHelper;
  11. using System.ComponentModel;
  12. using XYY.Data.Model.Standard.Tracking.TrackingTraces;
  13. using OfficeOpenXml;
  14. using OfficeOpenXml.Style;
  15. using OfficeOpenXml.Table;
  16. using System.IO;
  17. namespace XYY.Service.Standard.TrackingService
  18. {
  19. public interface ITraceSupplementLogService
  20. {
  21. Task<PageResult<Logistics_Trace_SupplementLogView>> QueryData(QueryModel queryModel);
  22. Task<byte[]> DownloadSupplementLog(QueryModel queryModel);
  23. Task<byte[]> DownloadLogs(QueryModel queryModel);
  24. }
  25. public class TraceSupplementLogService : ITraceSupplementLogService
  26. {
  27. private readonly IUnitOfWork _unitOfWork;
  28. public TraceSupplementLogService(IUnitOfWork unitOfWork)
  29. {
  30. this._unitOfWork = unitOfWork;
  31. }
  32. public async Task<PageResult<Logistics_Trace_SupplementLogView>> QueryData(QueryModel queryModel)
  33. {
  34. string sql = "select * from Logistics_Trace_SupplementLog(nolock)";
  35. return await _unitOfWork.GetPagingListAsync<Logistics_Trace_SupplementLogView>(queryModel, sql);
  36. }
  37. public async Task<byte[]> DownloadSupplementLog(QueryModel queryModel)
  38. {
  39. string sql = "select * from Logistics_Trace_SupplementLog(nolock) where 1=1" + _unitOfWork.GetWhere(queryModel.QueryParamer);
  40. var list = await _unitOfWork.QueryBySqlAsync<Logistics_Trace_SupplementLogView>(sql);
  41. MSExcelHelper excelHelper = new MSExcelHelper();
  42. var data = excelHelper.OutDataAsByte<D_SupplementLogView, Logistics_Trace_SupplementLogView>(list.ToList());
  43. return data;
  44. }
  45. private class D_SupplementLogView : MSExcelClassMapping<Logistics_Trace_SupplementLogView>
  46. {
  47. public D_SupplementLogView()
  48. {
  49. Map(x => x.TrackingNumber).Name("跟踪单号");
  50. Map(x => x.JudgmentNodeStr).Name("补充节点");
  51. Map(x => x.TrackSupplementLocationStr).Name("补充位置");
  52. Map(x => x.TrackSupplementTimeStr).Name("补充时间");
  53. Map(x => x.TrackSupplementText).Name("补充内容");
  54. Map(x => x.IsSuccessStr).Name("执行结果");
  55. Map(x => x.ErrorMsg).Name("错误原因");
  56. }
  57. }
  58. #region 下载轨迹日志
  59. public async Task<byte[]> DownloadLogs(QueryModel queryModel)
  60. {
  61. string excelPath = System.IO.Path.Combine(
  62. System.AppContext.BaseDirectory, DateTime.Now.ToString("yyyyMMdd") + ".xlsx");
  63. var orders = await GetOrderInfo(queryModel);
  64. //文件保存到相应的地址
  65. await GenerateLogsFile(orders, excelPath);
  66. var bytes = File.ReadAllBytes(excelPath);
  67. File.Delete(excelPath);
  68. return bytes;
  69. }
  70. private async Task<List<dynamic>> GetOrderInfo(QueryModel queryModel)
  71. {
  72. string where = _unitOfWork.GetWhere(queryModel.QueryParamer);
  73. string sql = $@"select a.CustomerOrderNo,a.TrackingNumber,a.SystemNo,c.LadingBillNumber
  74. from order_order(nolock)a
  75. join logistics_boxDetail(nolock)b on a.id=b.orderId
  76. join logistics_box(nolock)c on c.Id=b.BoxId
  77. where a.CreateOrderChannelId=109 " + where;
  78. List<dynamic> orders = (await _unitOfWork.QueryBySqlAsync<dynamic>(sql)).ToList();
  79. if (orders != null && orders.Count > 0) { return orders; } else { throw new Exception("没有订单数据可被执行!"); }
  80. }
  81. private async Task GenerateLogsFile(List<dynamic> orders, string excelPath)
  82. {
  83. ESRequest request = new ESRequest("http://120.24.149.148:9526", "l_log", "log", "mget");
  84. string strData = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.InStock + "\"")) + "]}");
  85. var list = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData);
  86. List<TrackingMonitor> logs = new List<TrackingMonitor>();
  87. Dictionary<TrackingEventType, List<int>> ErrorRowIndexs = new Dictionary<TrackingEventType, List<int>>();
  88. int index = 1;
  89. ErrorRowIndexs.Add(TrackingEventType.InStock, new List<int>());
  90. foreach (var item in list.docs)
  91. {
  92. if (item.found)
  93. {
  94. string customerOrderNO = item._id.Split('_')[0];
  95. TrackingMonitor trackingMonitor = new TrackingMonitor();
  96. trackingMonitor.InStockGetTime = item._source.GetTime;
  97. trackingMonitor.InStockSendTime = item._source.SendTime;
  98. trackingMonitor.InStockTargetTime = item._source.EventTime;
  99. trackingMonitor.InStockSendRetMsg = item._source.SendMessage;
  100. trackingMonitor.CustomerOrderNO = customerOrderNO;
  101. trackingMonitor.RowIndex = index;
  102. var s = orders.FirstOrDefault(x => x.CustomerOrderNo == customerOrderNO);
  103. if (s != null)
  104. {
  105. trackingMonitor.SystemNo = s.SystemNo;
  106. trackingMonitor.TrackingNumber = s.TrackingNumber;
  107. trackingMonitor.LadingBillNumber = s.LadingBillNumber;
  108. }
  109. if (trackingMonitor.InStockTargetTime != null
  110. && trackingMonitor.InStockSendTime != null
  111. && (trackingMonitor.InStockSendTime - trackingMonitor.InStockTargetTime).Value.TotalHours > 24
  112. )
  113. {
  114. ErrorRowIndexs[TrackingEventType.InStock].Add(trackingMonitor.RowIndex);
  115. }
  116. logs.Add(trackingMonitor);
  117. index++;
  118. }
  119. }
  120. ErrorRowIndexs.Add(TrackingEventType.OutStock, new List<int>());
  121. string strData2 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.OutStock + "\"")) + "]}");
  122. var list2 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData2);
  123. foreach (var item in list2.docs)
  124. {
  125. if (item.found)
  126. {
  127. string customerOrderNO = item._id.Split('_')[0];
  128. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  129. if (trackingMonitor != null)
  130. {
  131. trackingMonitor.OutStockGetTime = item._source.GetTime;
  132. trackingMonitor.OutStockSendTime = item._source.SendTime;
  133. trackingMonitor.OutStockTargetTime = item._source.EventTime;
  134. trackingMonitor.OutStockSendRetMsg = item._source.SendMessage;
  135. if (trackingMonitor.OutStockTargetTime != null
  136. && trackingMonitor.OutStockSendTime != null
  137. && (trackingMonitor.OutStockSendTime - trackingMonitor.OutStockTargetTime).Value.TotalHours > 24
  138. )
  139. {
  140. ErrorRowIndexs[TrackingEventType.OutStock].Add(trackingMonitor.RowIndex);
  141. }
  142. }
  143. }
  144. }
  145. ErrorRowIndexs.Add(TrackingEventType.Traffic, new List<int>());
  146. string strData3 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Traffic + "\"")) + "]}");
  147. var list3 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData3);
  148. foreach (var item in list3.docs)
  149. {
  150. if (item.found)
  151. {
  152. string customerOrderNO = item._id.Split('_')[0];
  153. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  154. if (trackingMonitor != null)
  155. {
  156. trackingMonitor.TrafficGetTime = item._source.GetTime;
  157. trackingMonitor.TrafficSendTime = item._source.SendTime;
  158. trackingMonitor.TrafficTargetTime = item._source.EventTime;
  159. trackingMonitor.TrafficSendRetMsg = item._source.SendMessage;
  160. }
  161. if (trackingMonitor.TrafficTargetTime != null
  162. && trackingMonitor.TrafficSendTime != null
  163. && (trackingMonitor.TrafficSendTime - trackingMonitor.TrafficTargetTime).Value.TotalHours > 24
  164. )
  165. {
  166. ErrorRowIndexs[TrackingEventType.Traffic].Add(trackingMonitor.RowIndex);
  167. }
  168. }
  169. }
  170. ErrorRowIndexs.Add(TrackingEventType.Departure, new List<int>());
  171. string strData4 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Departure + "\"")) + "]}");
  172. var list4 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData4);
  173. foreach (var item in list4.docs)
  174. {
  175. if (item.found)
  176. {
  177. string customerOrderNO = item._id.Split('_')[0];
  178. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  179. if (trackingMonitor != null)
  180. {
  181. trackingMonitor.DepartureGetTime = item._source.GetTime;
  182. trackingMonitor.DepartureSendTime = item._source.SendTime;
  183. trackingMonitor.DepartureTargetTime = item._source.EventTime;
  184. trackingMonitor.DepartureSendRetMsg = item._source.SendMessage;
  185. }
  186. if (trackingMonitor.DepartureTargetTime != null
  187. && trackingMonitor.DepartureSendTime != null
  188. && (trackingMonitor.DepartureSendTime - trackingMonitor.DepartureTargetTime).Value.TotalHours > 24
  189. )
  190. {
  191. ErrorRowIndexs[TrackingEventType.Departure].Add(trackingMonitor.RowIndex);
  192. }
  193. }
  194. }
  195. ErrorRowIndexs.Add(TrackingEventType.Arrive, new List<int>());
  196. string strData5 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Arrive + "\"")) + "]}");
  197. var list5 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData5);
  198. foreach (var item in list5.docs)
  199. {
  200. if (item.found)
  201. {
  202. string customerOrderNO = item._id.Split('_')[0];
  203. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  204. if (trackingMonitor != null)
  205. {
  206. trackingMonitor.ArriveGetTime = item._source.GetTime;
  207. trackingMonitor.ArriveSendTime = item._source.SendTime;
  208. trackingMonitor.ArriveTargetTime = item._source.EventTime;
  209. trackingMonitor.ArriveSendRetMsg = item._source.SendMessage;
  210. }
  211. if (trackingMonitor.ArriveTargetTime != null
  212. && trackingMonitor.ArriveSendTime != null
  213. && (trackingMonitor.ArriveSendTime - trackingMonitor.ArriveTargetTime).Value.TotalHours > 24
  214. )
  215. {
  216. ErrorRowIndexs[TrackingEventType.Arrive].Add(trackingMonitor.RowIndex);
  217. }
  218. }
  219. }
  220. ErrorRowIndexs.Add(TrackingEventType.Clearance, new List<int>());
  221. string strData6 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Take(100).Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Clearance + "\"")) + "]}");
  222. var list6 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData6);
  223. foreach (var item in list6.docs)
  224. {
  225. if (item.found)
  226. {
  227. string customerOrderNO = item._id.Split('_')[0];
  228. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  229. if (trackingMonitor != null)
  230. {
  231. trackingMonitor.ClearanceGetTime = item._source.GetTime;
  232. trackingMonitor.ClearanceSendTime = item._source.SendTime;
  233. trackingMonitor.ClearanceTargetTime = item._source.EventTime;
  234. trackingMonitor.ClearanceSendRetMsg = item._source.SendMessage;
  235. }
  236. if (trackingMonitor.ClearanceTargetTime != null
  237. && trackingMonitor.ClearanceSendTime != null
  238. && (trackingMonitor.ClearanceSendTime - trackingMonitor.ClearanceTargetTime).Value.TotalHours > 24
  239. )
  240. {
  241. ErrorRowIndexs[TrackingEventType.Clearance].Add(trackingMonitor.RowIndex);
  242. }
  243. }
  244. }
  245. ErrorRowIndexs.Add(TrackingEventType.Online, new List<int>());
  246. string strData7 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Online + "\"")) + "]}");
  247. var list7 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData7);
  248. foreach (var item in list7.docs)
  249. {
  250. if (item.found)
  251. {
  252. string customerOrderNO = item._id.Split('_')[0];
  253. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  254. if (trackingMonitor != null)
  255. {
  256. trackingMonitor.OnlineGetTime = item._source.GetTime;
  257. trackingMonitor.OnlineSendTime = item._source.SendTime;
  258. trackingMonitor.OnlineTargetTime = item._source.EventTime;
  259. trackingMonitor.OnlineSendRetMsg = item._source.SendMessage;
  260. }
  261. if (trackingMonitor?.OnlineTargetTime != null
  262. && trackingMonitor?.OnlineSendTime != null
  263. && (trackingMonitor?.OnlineSendTime - trackingMonitor?.OnlineTargetTime).Value.TotalHours > 24
  264. )
  265. {
  266. ErrorRowIndexs[TrackingEventType.Online].Add(trackingMonitor.RowIndex);
  267. }
  268. }
  269. }
  270. ErrorRowIndexs.Add(TrackingEventType.Sign, new List<int>());
  271. string strData8 = request.ExecuteQeury("{\"ids\":[" + string.Join(",", orders.Select(x => "\"" + x.CustomerOrderNo + "_" + TrackingEventType.Sign + "\"")) + "]}");
  272. var list8 = Newtonsoft.Json.JsonConvert.DeserializeObject<TrackingTraceDoc>(strData8);
  273. foreach (var item in list8.docs)
  274. {
  275. if (item.found)
  276. {
  277. string customerOrderNO = item._id.Split('_')[0];
  278. TrackingMonitor trackingMonitor = logs.FirstOrDefault(x => x.CustomerOrderNO == customerOrderNO);
  279. if (trackingMonitor != null)
  280. {
  281. trackingMonitor.SignGetTime = item._source.GetTime;
  282. trackingMonitor.SignSendTime = item._source.SendTime;
  283. trackingMonitor.SignTargetTime = item._source.EventTime;
  284. trackingMonitor.SignSendRetMsg = item._source.SendMessage;
  285. }
  286. if (trackingMonitor.SignTargetTime != null
  287. && trackingMonitor.SignSendTime != null
  288. && (trackingMonitor.SignSendTime - trackingMonitor.SignTargetTime).Value.TotalHours > 24
  289. )
  290. {
  291. ErrorRowIndexs[TrackingEventType.Sign].Add(trackingMonitor.RowIndex);
  292. }
  293. }
  294. }
  295. ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
  296. FileStream existingFile = new FileStream(excelPath, FileMode.Create);
  297. using (ExcelPackage package = new ExcelPackage(existingFile))
  298. {
  299. // 邮编 区
  300. ExcelWorksheet worksheetIn = package.Workbook.Worksheets.Add("Sheet1");
  301. // 第二参数为true 则会把 lstData定义的属性名称作为excel标题
  302. worksheetIn.Cells.LoadFromCollection(logs, true, TableStyles.Medium13);
  303. var color = System.Drawing.ColorTranslator.FromHtml("#FFE699");
  304. int glaStart = 5;
  305. foreach (var item in ErrorRowIndexs)
  306. {
  307. int cStart = glaStart + ((int)item.Key - 1) * 4 + 1;
  308. foreach (var r in item.Value)
  309. {
  310. worksheetIn.Cells[r + 1, cStart].Style.Fill.PatternType = ExcelFillStyle.Solid;
  311. worksheetIn.Cells[r + 1, cStart + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
  312. worksheetIn.Cells[r + 1, cStart + 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
  313. worksheetIn.Cells[r + 1, cStart + 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
  314. worksheetIn.Cells[r + 1, cStart].Style.Fill.BackgroundColor.SetColor(color);
  315. worksheetIn.Cells[r + 1, cStart + 1].Style.Fill.BackgroundColor.SetColor(color);
  316. worksheetIn.Cells[r + 1, cStart + 2].Style.Fill.BackgroundColor.SetColor(color);
  317. worksheetIn.Cells[r + 1, cStart + 3].Style.Fill.BackgroundColor.SetColor(color);
  318. }
  319. }
  320. foreach (var item in ErrorRowIndexs.Keys)
  321. {
  322. int cStart = glaStart + ((int)item - 1) * 4 + 1;
  323. worksheetIn.Column(cStart).Width = 15;
  324. worksheetIn.Column(cStart + 1).Width = 15;
  325. worksheetIn.Column(cStart + 2).Width = 15;
  326. worksheetIn.Column(cStart).Style.Numberformat.Format = "MM-dd HH:mm";
  327. worksheetIn.Column(cStart + 1).Style.Numberformat.Format = "MM-dd HH:mm";
  328. worksheetIn.Column(cStart + 2).Style.Numberformat.Format = "MM-dd HH:mm";
  329. }
  330. worksheetIn.View.FreezePanes(1, 6);
  331. worksheetIn.Column(2).Width = 30;
  332. worksheetIn.Column(3).Width = 20;
  333. worksheetIn.Column(4).Width = 35;
  334. worksheetIn.Column(5).Width = 20;
  335. package.Save(); //Save the workbook.
  336. }
  337. existingFile.Close();
  338. }
  339. #endregion
  340. }
  341. }