using Quartz; using System; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Logs; using SIMDP.Model; using System.Threading.Tasks; using ProjectBase.Util; using System.Data.Common; namespace SIMDP.BLL { public class GetAS400DataJob : IJob { public async Task Execute(IJobExecutionContext context) { // if (!SysEnvironment.sign_OnlineMode) return; if (BLLFactory.Instance.GetRecordCount() > 1500) return; await Task.Run(() => GetAS400()); } /// /// 正式接口 /// /// public bool GetAS400(string specifyVIN = "") { if (string.IsNullOrEmpty(specifyVIN) && string.IsNullOrEmpty(SysEnvironment.shareUp_PI_NO)) return true; try { AS400Service.GETMAINR ws = new AS400Service.GETMAINR(); AS400Service.mainInput mainInput = new AS400Service.mainInput(); mainInput.PI_LINE = "02"; mainInput.PI_TYPE = "PAOFF"; mainInput.PI_NO = !string.IsNullOrEmpty(specifyVIN) ? specifyVIN : SysEnvironment.shareUp_PI_NO; AS400Service.mainResult mainRes = ws.main(mainInput); string[] strs = mainRes.PI_LIST; // string[] strs = new string[] { "LHGCV1679N8081023 NTVGHB600 NH904M A 2022/02/1608:38:03" }; if (!CheckPostResult(strs)) { LogHelper.log.Info($"无法在AS400中查到车号 {mainInput.PI_NO} 或格式异常"); return false; } string sVin = ""; int iPLCCode = 0; int iPaintLine = 0; char[] chs = { ' ', '\t' }; LogHelper.log.Debug("【接收数据start】"); foreach (string s in strs) { LogHelper.log.Debug($"接收数据行:{s}"); string[] resSplit = s.Split(chs, StringSplitOptions.RemoveEmptyEntries); //获取VIN sVin = resSplit[0]; if (sVin.Length != 17) { LogHelper.log.Error($"车号字段长度错误!{resSplit[0]}"); SysEnvironment.warn_AS400 = true; return false; } //根据VIN替重 if (CheckArrayContain(SysEnvironment.LastPostResult, s)) { LogHelper.log.Debug($"查询到重复!{sVin}"); continue; } //获取车型 iPLCCode = BLLFactory.Instance.GetPLCCodebyModelCode(sVin.Substring(3, 2)); //获取颜色 iPaintLine = BLLFactory.Instance.GetPaintLinebyColorCard(resSplit[2]); //存如MTOC表 MoMtocData model = new MoMtocData(); model.Vin = sVin; model.Model = iPLCCode; model.Color = iPaintLine; model.Fetch_Time = DateTime.Now; using (DbTransaction trans = BLLFactory.Instance.CreateTransaction()) { try { BLLFactory.Instance.Insert(model, trans); trans.Commit(); } catch (Exception ex) { trans.Rollback(); LogHelper.log.Error($"存入数据库MTOC表异常:{ex.Message}"); LogHelper.log.Error($"当前缓存的POST结果:"); foreach (string str in SysEnvironment.LastPostResult) { LogHelper.log.Error($"缓存: {str}"); } } } } SysEnvironment.shareUp_PI_NO = strs[strs.Length - 1].Substring(0, 17);//更新下次查询起始车号 SysEnvironment.LastPostResult = null; SysEnvironment.LastPostResult = (string[])strs.Clone(); //更新本次post结果, 不是车号! return true; } catch (Exception ex) { SysEnvironment.warn_AS400 = true; throw ex; //LogHelper.log.Error($"获取AS400数据出错:{ex.Message}"); //return false; } } private bool CheckPostResult(string[] strs) { //长度不足1 //所有元素都是空字符串/每个元素解析出的字段少于5个 if (strs.Length < 1) return false; char[] chs = { ' ', '\t' }; foreach (string ss in strs) { if (ss.Split(chs, StringSplitOptions.RemoveEmptyEntries).Length < 5) return false; } return true; } /// /// 检查数组是否包含了指定的字符串 /// /// /// /// public bool CheckArrayContain(string[] strs, string strMatch) { for (int i = 0; i < strs.Length; i++) { // LogHelper.log.Debug($"校验重复行:{strMatch}/n{strs[i]}"); if (string.Equals(strs[i], strMatch)) return true; } return false; } } }