using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace DllEapDal { public class TrackHelperDal { private IDatabase CurrDb = null; public TrackHelperDal(IDatabase db) { CurrDb = db; } public DateTime? GetTrackIn(Machine mac, ref string errorinfo) { try { DateTime currtime = DateTime.Now; //读入小于当前时间且不超过60秒的机台记录 string condition = $@" and a.fdate>STR_TO_DATE('{currtime.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s') and a.fdateSTR_TO_DATE('{currtime.AddMinutes(-2).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s') // and a.fdate maccount = CurrDb.FindListForCondition(condition, ref errorinfo).OrderByDescending(t => t.FDate).ToList(); if (maccount.Count <= 0)//前面没有操作记录,则以当前时间为开始 return DateTime.Now; if (maccount.Count > 2) { //判断最后2条是否重复,如果重复则要删除1条 if (maccount[0].EventTypeID == maccount[1].EventTypeID && maccount[0].FCount == maccount[1].FCount) maccount.Remove(maccount[0]); } //如果最后条记录是开始事件,则以该条开始时间为trackin时间 if (maccount[0].EventTypeID == -1) return maccount[0].FDate; if (maccount[0].EventTypeID == 1 && maccount.Count == 1) return DateTime.Now; //如果最后记录是结束事件,且结束事件前面是开始事件,则以此开始事件为trackin时间 if (maccount[0].EventTypeID == 1 && maccount.Count > 1) return maccount[1].FDate; return DateTime.Now; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public DateTime? GetTrackOut(Machine mac, ref string errorinfo) { try { //读入小于当前时间且不超过60秒的机台记录 DateTime currtime = DateTime.Now; string condition = $@" and a.fdate>STR_TO_DATE('{currtime.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")}', '%Y-%m-%d %H:%i:%s') and a.fdate maccount = CurrDb.FindListForCondition(condition, ref errorinfo).OrderByDescending(t => t.FDate).ToList(); if (maccount.Count <= 0)//前面没有操作记录,则以当前时间为开始 return DateTime.Now; if (maccount.Count > 2) { //判断最后2条是否重复,如果重复则要删除1条 if (maccount[0].EventTypeID == maccount[1].EventTypeID && maccount[0].FCount == maccount[1].FCount) maccount.Remove(maccount[0]); } //如果最后条记录是开始事件,则以该条开始时间为trackout时间 if (maccount[0].EventTypeID == -1) return maccount[0].FDate; if (maccount[0].EventTypeID == 1 && maccount.Count == 1) return maccount[0].FDate.AddSeconds(1); //如果最后记录是结束事件,且结束事件前面是开始事件,则以此开始事件为trackin时间 if (maccount[0].EventTypeID == 1 && maccount.Count > 1) { var endTypes = maccount.Where(c => c.EventTypeID == -1).OrderByDescending(c => c.FDate); if (endTypes == null || endTypes.Count() == 0) { return maccount[0].FDate.AddSeconds(1); } return endTypes.ElementAt(0).FDate; } return DateTime.Now; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } } }