using SLH.SSDMS.Common; using SLH.SSDMS.Model.DO; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace SLH.SSDMS.Services.serviceImpl { public class RfidJudge : IRfidJudge { private static string lastRfidCode = ""; private static Base_ReadRecord lastReadRecord = new Base_ReadRecord(); private static IBaseServices baseServices = new BaseServices(); private static IBaseServices runInfoServices = new BaseServices(); private static IBaseServices readRecordServices = new BaseServices(); private static IBaseServices beltInfoServices = new BaseServices(); public async Task JudgeDirection(string rfidCode) { Expression> exp = s1 => true; exp = exp.And(x => x.updateFlag == 0); Expression> order = (x) => x.rfidCode; List rfidInfos = await baseServices.Query(exp, order,true); List rfidList = new List(); rfidInfos.ForEach(x => rfidList.Add(x.rfidCode)); if (StringExtension.IsNotBlank(lastRfidCode)) { int presentIndex = rfidList.IndexOf(rfidCode); int lastIndex = rfidList.IndexOf(lastRfidCode); if (lastIndex == 0 && presentIndex > lastIndex && presentIndex == 1) { lastRfidCode = rfidCode; return 1; } if (lastIndex == 0 && presentIndex > lastIndex && presentIndex == rfidList.Count - 1) { lastRfidCode = rfidCode; return 0; } if (lastIndex > 0 && presentIndex > 0 && lastIndex < presentIndex) { lastRfidCode = rfidCode; return 1; } if (lastIndex > 0 && presentIndex > 0 && lastIndex > presentIndex) { lastRfidCode = rfidCode; return 0; } if (presentIndex == 0 && lastIndex > presentIndex && lastIndex == rfidList.Count - 1) { lastRfidCode = rfidCode; return 1; } if (presentIndex == 0 && lastIndex > presentIndex && lastIndex == 1) { lastRfidCode = rfidCode; return 0; } } lastRfidCode = rfidCode; return 3; } public async Task JudgeRunSpeed(Base_ReadRecord readRecord) { //计算时间差 try { TimeSpan readTime = new TimeSpan(readRecord.readTime.Ticks); if (lastReadRecord == null) { Console.WriteLine("lastReadRecord为空"); return new decimal(); } TimeSpan lastTime = new TimeSpan(lastReadRecord.readTime.Ticks); decimal interval = (decimal)readTime.Subtract(lastTime).Duration().TotalSeconds; Console.WriteLine("时差:" + interval); //计算速度m/s decimal result = readRecord.preDistance / interval; var speed = Math.Round(result, 3); lastReadRecord = readRecord; return speed; } catch(Exception ex) { LogHelper.Error("速度计算异常", ex); return 0; } } public async Task JudgeRunDistance(Base_ReadRecord readRecord) { try { //查询已经生成好的里程数据 Expression> exp = s1 => true; exp = exp.And(x => x.lotNum == readRecord.lotNum); List runInfos = await runInfoServices.Query(exp); var distance = runInfos.Sum(x=>x.runDistance); //查询当天的里程数据 Expression> readExp = s1 => true; readExp = readExp.And(x => x.readTime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")); List readRecords = await readRecordServices.Query(readExp); var info = readRecords.Sum(x => x.preDistance); return Math.Round((info + distance) / 1000,3); } catch (Exception ex) { LogHelper.Error("里程计算异常", ex); return 0; } } public async Task JudgeRunTime(Base_ReadRecord readRecord) { try { //查询已经生成好的里程数据 Expression> exp = s1 => true; exp = exp.And(x => x.lotNum == readRecord.lotNum); List runInfos = await runInfoServices.Query(exp); var runTime = runInfos.Sum(x=>x.runTime); //查询当天的里程数据 Expression> readExp = s1 => true; readExp = readExp.And(x => x.readTime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")); List readRecords = await readRecordServices.Query(readExp); Base_ReadRecord todayReadRecord = readRecords.OrderBy(x => x.readTime).First(); TimeSpan readTime = new TimeSpan(DateTime.Now.Ticks); TimeSpan lastTime = new TimeSpan(todayReadRecord.readTime.Ticks); decimal interval = (decimal)readTime.Subtract(lastTime).Duration().TotalSeconds; return Math.Round((interval + runTime) / 60 / 60,2); } catch (Exception ex) { LogHelper.Error("时长计算异常", ex); return 0; } } public async Task JudgeFirstTime(string lotCode) { try { Expression> exp = s1 => true; exp = exp.And(x => x.lotNum == lotCode); var info = await beltInfoServices.Query(exp); return info.FirstOrDefault().firstTime; }catch(Exception ex) { LogHelper.Error("首次上机时间查询异常"+ lotCode, ex); return DateTime.Now.ToString("yyyy-MM-dd"); } } } }