SendLot2Lot3Thread.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. private readonly ISqlSugarClient _db;
  11. public SendLot2Lot3Thread(ISqlSugarClient db)
  12. {
  13. _db = db;
  14. }
  15. public void SendThreadStart()
  16. {
  17. Thread t = new Thread(SendTask);
  18. t.Start(_db);
  19. }
  20. static void SendTask(Object db)
  21. {
  22. ISqlSugarClient _db = (ISqlSugarClient)db;
  23. IConfiguration _configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  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(60).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(1000);
  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(10000);
  63. }
  64. }
  65. }
  66. }
  67. }