SendLot2Lot3Thread.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using EInk.Dtos;
  2. using EInk.Models;
  3. using EInk.Tools;
  4. using Newtonsoft.Json;
  5. using SqlSugar;
  6. namespace EInk.TaskThread
  7. {
  8. public class SendLot2Lot3Thread
  9. {
  10. public void SendThreadStart()
  11. {
  12. Thread t = new Thread(SendTask);
  13. t.Start();
  14. }
  15. static void SendTask()
  16. {
  17. IConfiguration _configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  18. ISqlSugarClient _db = new SqlSugarClient(new ConnectionConfig()
  19. {
  20. DbType = DbType.PostgreSQL,
  21. ConnectionString = _configuration.GetValue<string>("LocalDbConnectionString"),
  22. IsAutoCloseConnection = true
  23. });
  24. while (true)
  25. {
  26. try
  27. {
  28. List<Lot2Lot3Model> unsend_list = _db.Queryable<Lot2Lot3Model>().Where(x => x.is_send == 0).OrderBy(x => x.id).Take(120).ToList();
  29. if (unsend_list != null && unsend_list.Count > 0)
  30. {
  31. foreach (var unsend in unsend_list)
  32. {
  33. var (rt_bl, rt_str) = HttpHelper.Post(_configuration.GetValue<string>("MesUrl")+ "/MesApi/LotAoiUpLoad", unsend.content, _configuration.GetValue<int>("MesUrlTime"));
  34. if (rt_bl && !string.IsNullOrEmpty(rt_str))
  35. {
  36. ResultDto rt = JsonConvert.DeserializeObject<ResultDto>(rt_str);
  37. if (rt.Code == CodeEnum.Ok)
  38. {
  39. unsend.is_send = 1;
  40. unsend.send_time = DateTime.Now;
  41. _db.Updateable<Lot2Lot3Model>(unsend).WhereColumns(s => s.id).ExecuteCommand();
  42. }
  43. else
  44. {
  45. LogerHelper.RecordLogTxt("SendLotTask, MesApi1, " + rt.Code + "," + rt.Message + "," + unsend.content);
  46. }
  47. }
  48. else
  49. {
  50. LogerHelper.RecordLogTxt("SendLotTask, MesApi2, " + rt_bl+ "," + rt_str + "," + unsend.content);
  51. }
  52. Thread.Sleep(200);
  53. }
  54. }
  55. }
  56. catch(Exception ex)
  57. {
  58. LogerHelper.RecordLogTxt($"SendLotTask, StackTrace:{ex.StackTrace}, Message:{ex.Message}");
  59. }
  60. finally
  61. {
  62. Thread.Sleep(millisecondsTimeout: 10000);
  63. }
  64. }
  65. }
  66. }
  67. }