GetAS400DataJob.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /// <summary>
  20. /// 正式接口
  21. /// </summary>
  22. /// <returns></returns>
  23. public bool GetAS400(string specifyVIN = "")
  24. {
  25. if (string.IsNullOrEmpty(specifyVIN) && string.IsNullOrEmpty(SysEnvironment.shareUp_PI_NO)) return true;
  26. try
  27. {
  28. AS400Service.GETMAINR ws = new AS400Service.GETMAINR();
  29. AS400Service.mainInput mainInput = new AS400Service.mainInput();
  30. mainInput.PI_LINE = "02";
  31. mainInput.PI_TYPE = "PAOFF";
  32. mainInput.PI_NO = !string.IsNullOrEmpty(specifyVIN) ? specifyVIN : SysEnvironment.shareUp_PI_NO;
  33. AS400Service.mainResult mainRes = ws.main(mainInput);
  34. string[] strs = mainRes.PI_LIST;
  35. // string[] strs = new string[] { "LHGCV1679N8081023 NTVGHB600 NH904M A 2022/02/1608:38:03" };
  36. if (!CheckPostResult(strs))
  37. {
  38. LogHelper.log.Info($"无法在AS400中查到车号 {mainInput.PI_NO} 或格式异常");
  39. return false;
  40. }
  41. string sVin = "";
  42. int iPLCCode = 0;
  43. int iPaintLine = 0;
  44. char[] chs = { ' ', '\t' };
  45. LogHelper.log.Debug("【接收数据start】");
  46. foreach (string s in strs)
  47. {
  48. LogHelper.log.Debug($"接收数据行:{s}");
  49. string[] resSplit = s.Split(chs, StringSplitOptions.RemoveEmptyEntries);
  50. //获取VIN
  51. sVin = resSplit[0];
  52. if (sVin.Length != 17)
  53. {
  54. LogHelper.log.Error($"车号字段长度错误!{resSplit[0]}");
  55. SysEnvironment.warn_AS400 = true;
  56. return false;
  57. }
  58. //根据VIN替重
  59. if (CheckArrayContain(SysEnvironment.LastPostResult, s))
  60. {
  61. LogHelper.log.Debug($"查询到重复!{sVin}");
  62. continue;
  63. }
  64. //获取车型
  65. iPLCCode = BLLFactory<BlModelContrast>.Instance.GetPLCCodebyModelCode(sVin.Substring(3, 2));
  66. //获取颜色
  67. iPaintLine = BLLFactory<BlColorContrast>.Instance.GetPaintLinebyColorCard(resSplit[2]);
  68. //存如MTOC表
  69. MoMtocData model = new MoMtocData();
  70. model.Vin = sVin;
  71. model.Model = iPLCCode;
  72. model.Color = iPaintLine;
  73. model.Fetch_Time = DateTime.Now;
  74. using (DbTransaction trans = BLLFactory<BlMtocData>.Instance.CreateTransaction())
  75. {
  76. try
  77. {
  78. BLLFactory<BlMtocData>.Instance.Insert(model, trans);
  79. trans.Commit();
  80. }
  81. catch (Exception ex)
  82. {
  83. trans.Rollback();
  84. LogHelper.log.Error($"存入数据库MTOC表异常:{ex.Message}");
  85. LogHelper.log.Error($"当前缓存的POST结果:");
  86. foreach (string str in SysEnvironment.LastPostResult)
  87. {
  88. LogHelper.log.Error($"缓存: {str}");
  89. }
  90. }
  91. }
  92. }
  93. SysEnvironment.shareUp_PI_NO = strs[strs.Length - 1].Substring(0, 17);//更新下次查询起始车号
  94. SysEnvironment.LastPostResult = null;
  95. SysEnvironment.LastPostResult = (string[])strs.Clone(); //更新本次post结果, 不是车号!
  96. return true;
  97. }
  98. catch (Exception ex)
  99. {
  100. SysEnvironment.warn_AS400 = true;
  101. throw ex;
  102. //LogHelper.log.Error($"获取AS400数据出错:{ex.Message}");
  103. //return false;
  104. }
  105. }
  106. private bool CheckPostResult(string[] strs)
  107. {
  108. //长度不足1
  109. //所有元素都是空字符串/每个元素解析出的字段少于5个
  110. if (strs.Length < 1) return false;
  111. char[] chs = { ' ', '\t' };
  112. foreach (string ss in strs)
  113. {
  114. if (ss.Split(chs, StringSplitOptions.RemoveEmptyEntries).Length < 5)
  115. return false;
  116. }
  117. return true;
  118. }
  119. /// <summary>
  120. /// 检查数组是否包含了指定的字符串
  121. /// </summary>
  122. /// <param name="strs"></param>
  123. /// <param name="strMatch"></param>
  124. /// <returns></returns>
  125. public bool CheckArrayContain(string[] strs, string strMatch)
  126. {
  127. for (int i = 0; i < strs.Length; i++)
  128. {
  129. // LogHelper.log.Debug($"校验重复行:{strMatch}/n{strs[i]}");
  130. if (string.Equals(strs[i], strMatch))
  131. return true;
  132. }
  133. return false;
  134. }
  135. }
  136. }