GetAS400DataJob.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Quartz;
  2. using System;
  3. using ProjectBase.Data.BaseDAL;
  4. using ProjectBase.Data.Logs;
  5. using SIMDP.Model;
  6. using System.Threading.Tasks;
  7. using ProjectBase.Util;
  8. using System.Data.Common;
  9. namespace SIMDP.BLL
  10. {
  11. public class GetAS400DataJob : IJob
  12. {
  13. public async Task Execute(IJobExecutionContext context)
  14. {
  15. // if (!SysEnvironment.sign_OnlineMode) return;
  16. if (BLLFactory<BlMtocData>.Instance.GetRecordCount() > 1500) return;
  17. await Task.Run(() => GetAS400());
  18. }
  19. public bool GetAS400(string specifyVIN = "")
  20. {
  21. if (string.IsNullOrEmpty(specifyVIN) && string.IsNullOrEmpty(SysEnvironment.shareUp_PI_NO)) return true;
  22. try
  23. {
  24. AS400Service.GETMAINR ws = new AS400Service.GETMAINR();
  25. AS400Service.mainInput mainInput = new AS400Service.mainInput();
  26. mainInput.PI_LINE = "02";
  27. mainInput.PI_TYPE = "PAOFF";
  28. mainInput.PI_NO = !string.IsNullOrEmpty(specifyVIN) ? specifyVIN : SysEnvironment.shareUp_PI_NO;
  29. AS400Service.mainResult mainRes = ws.main(mainInput);
  30. string[] strs = mainRes.PI_LIST;
  31. //string[] strs = new string[] { "LHGCV1679N8081023 NTVGHB600 NH904M A 2022/02/1608:38:03" };
  32. //string[] strs = new string[] { "LHGRV8886P8000183 63F4HA500 B508M L1202211112807:51:31" };
  33. //if (!CheckPostResult(strs))
  34. //{
  35. // LogHelper.log.Info($"无法在AS400中查到车号 {mainInput.PI_NO} 或格式异常");
  36. // return false;
  37. //}
  38. if (strs.Length<1)
  39. LogHelper.log.Info($"获取AS400数据失败,数组长度小于1");
  40. LogHelper.log.Debug("【接收数据start】");
  41. foreach (string s in strs)
  42. {
  43. LogHelper.log.Debug($"接收数据行:{s}");
  44. int iPLCCode = 0;
  45. int iPaintLine = 0;
  46. char[] chs = { ' ', '\t' };
  47. //string sVin = s.Substring(0, 19).Trim();
  48. //string smodel = sVin.Substring(3, 2);
  49. //string[] smtoc = s.Substring(19, 22).Split(chs, StringSplitOptions.RemoveEmptyEntries);
  50. //string scolor = smtoc[1];
  51. string sVin = "";
  52. string smodel = "";
  53. string scolor = "";
  54. string[] smtoc= s.Split(chs, StringSplitOptions.RemoveEmptyEntries);
  55. if (smtoc.Length < 3)
  56. {
  57. LogHelper.log.Info($"AS400数据格式异常,数组长度小于3,原始数据:{s}");
  58. return false;
  59. }
  60. sVin = smtoc[0].Trim();
  61. smodel= sVin.Substring(3, 2);
  62. scolor = smtoc[2].Trim();
  63. if (CheckArrayContain(SysEnvironment.LastPostResult, s))
  64. {
  65. LogHelper.log.Debug($"查询到重复AS400数据!车号 {sVin}");
  66. continue;
  67. }
  68. //获取车型
  69. iPLCCode = BLLFactory<BlModelContrast>.Instance.GetPLCCodebyModelCode(smodel);
  70. if (iPLCCode <= 0) LogHelper.log.Error($"接收车型异常 smodel= {smodel}");
  71. //获取颜色
  72. iPaintLine = BLLFactory<BlColorContrast>.Instance.GetPaintLinebyColorCard(scolor);
  73. if (iPaintLine <= 0) LogHelper.log.Error($"接收颜色异常 scolor= {scolor}");
  74. //存入MTOC表
  75. MoMtocData model = new MoMtocData();
  76. model.Vin = sVin;
  77. model.Model = iPLCCode;
  78. model.Color = iPaintLine;
  79. model.Fetch_Time = DateTime.Now;
  80. using (DbTransaction trans = BLLFactory<BlMtocData>.Instance.CreateTransaction())
  81. {
  82. try
  83. {
  84. BLLFactory<BlMtocData>.Instance.Insert(model, trans);
  85. trans.Commit();
  86. }
  87. catch (Exception ex)
  88. {
  89. SysEnvironment.warn_AS400 = true;
  90. trans.Rollback();
  91. LogHelper.log.Error($"存入数据库MTOC表异常:{ex.Message}");
  92. LogHelper.log.Error($"当前缓存的POST结果:");
  93. foreach (string str in SysEnvironment.LastPostResult)
  94. {
  95. LogHelper.log.Error($"缓存: {str}");
  96. }
  97. }
  98. }
  99. }
  100. // SysEnvironment.shareUp_PI_NO = strs[strs.Length - 1].Substring(0, 17);//更新下次查询起始车号
  101. SysEnvironment.shareUp_PI_NO = strs[strs.Length - 1].Substring(0, 19).Trim();//更新下次查询起始车号
  102. SysEnvironment.LastPostResult = null;
  103. SysEnvironment.LastPostResult = (string[])strs.Clone(); //更新本次post结果, 不是车号!
  104. return true;
  105. }
  106. catch (Exception ex)
  107. {
  108. SysEnvironment.warn_AS400 = true;
  109. LogHelper.log.Error($"获取AS400数据出错:{ex.Message}");
  110. throw ex;
  111. //return false;
  112. }
  113. }
  114. private bool CheckPostResult(string[] strs)
  115. {
  116. if (strs.Length < 1) return false;
  117. //foreach (string ss in strs)
  118. //{
  119. // if (ss.Length != 59)
  120. // return false;
  121. //}
  122. return true;
  123. }
  124. /// <summary>
  125. /// 检查数组是否包含了指定的字符串
  126. /// </summary>
  127. /// <param name="strs"></param>
  128. /// <param name="strMatch"></param>
  129. /// <returns></returns>
  130. public bool CheckArrayContain(string[] strs, string strMatch)
  131. {
  132. for (int i = 0; i < strs.Length; i++)
  133. {
  134. // LogHelper.log.Debug($"校验重复行:{strMatch}/n{strs[i]}");
  135. if (string.Equals(strs[i], strMatch))
  136. return true;
  137. }
  138. return false;
  139. }
  140. }
  141. }