TrackingLogController.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using iTextSharp.text;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using XYY.Core.Standard.Mvc;
  9. using XYY.Data.Model.Standard.Tracking;
  10. using XYY.Service.Standard.First.DB;
  11. using XYY.TaskTrack.Standard;
  12. namespace XYY.Api.First.Controllers
  13. {
  14. [Route("api/[controller]/[action]")]
  15. [ApiController]
  16. public class TrackingLogController
  17. {
  18. ITrackingLogDB trackingLogDB;
  19. private IMQManager _MQManager;
  20. public TrackingLogController(ITrackingLogDB trackingLogDB, IMQManager mQManager)
  21. {
  22. this.trackingLogDB = trackingLogDB;
  23. _MQManager = mQManager;
  24. }
  25. [AllowAnonymous]
  26. public async Task<ApiJsonModel> RetryOnlie()
  27. {
  28. return ApiJsonModel.OK(trackingLogDB.GetOnlineErrors().Select(x => x.CustomerNo));
  29. }
  30. [AllowAnonymous]
  31. public async Task<ApiJsonModel> AddLog(List<TrackingTraceLog> trackingTraceLogs)
  32. {
  33. ///写入日志是否成功
  34. bool IsInsert = trackingLogDB.ManyInsert(trackingTraceLogs, out var IsRepeat);
  35. if (IsInsert)
  36. {
  37. //写入待推送消息
  38. var message = trackingTraceLogs.Select(x => new XikeTraceMessage
  39. {
  40. CustomerNo = x.CustomerNo,
  41. EventLoaction = x.EventLoaction,
  42. EventContent = x.EventContent,
  43. EventTime = x.EventTime,
  44. Key = x.Key,
  45. TrackingEventType = x.TrackingEventType,
  46. }).ToList();
  47. try
  48. {
  49. await _MQManager.Publish(message);
  50. }
  51. catch (Exception ex)
  52. {
  53. trackingTraceLogs.ForEach(x =>
  54. {
  55. x.StepName = StepName.InsertMQError;
  56. x.SendMessage = "推送同步消息失败" + ex.Message;
  57. });
  58. var IsUpdate = trackingLogDB.ManyUpdate(trackingTraceLogs);
  59. if (!IsUpdate)
  60. {
  61. throw new Exception("更新ES日志状态失败,推送同步消息异常" + ex.Message);
  62. }
  63. else
  64. {
  65. throw new Exception("推送同步消息失败" + ex.Message);
  66. }
  67. }
  68. return ApiJsonModel.OK(null);
  69. }
  70. else
  71. {
  72. if (IsRepeat)
  73. return ApiJsonModel.OK(null);
  74. throw new Exception("写入ES日志失败");
  75. }
  76. }
  77. }
  78. }