protected void list_ItemCreated(object sender, RepeaterItemEventArgs e) { AreaInOutTime areaTime = (AreaInOutTime)e.Item.DataItem; if (areaTime != null) { Img icon = e.Item.FindControl("icon") as Img; Anchor tagName = e.Item.FindControl("tagName") as Anchor; DateTimeLabel inTime = e.Item.FindControl("inTime") as DateTimeLabel; DateTimeLabel outTime = e.Item.FindControl("outTime") as DateTimeLabel; SmartLabel duration = e.Item.FindControl("duration") as SmartLabel; tagName.Text = Convert.ToString(areaTime.hostName); if (areaTime.inTime != DateTime.MinValue) { inTime.DisplayValue = areaTime.inTime; } if (areaTime.outTime != DateTime.MinValue) { outTime.DisplayValue = areaTime.outTime; } duration.Text = ""; if (tagName.Text == "--- ---") { duration.Text = "("; } if (areaTime.stayTime.Ticks == 0) { duration.Text += "0"; } else { if (areaTime.stayTime.Days > 0) { duration.Text += areaTime.stayTime.Days.ToString() + "天"; } if (areaTime.stayTime.Hours > 0) { duration.Text += areaTime.stayTime.Hours.ToString() + "时"; } if (areaTime.stayTime.Minutes > 0) { duration.Text += areaTime.stayTime.Minutes.ToString() + "分"; } if (areaTime.stayTime.Seconds > 0) { duration.Text += areaTime.stayTime.Seconds.ToString() + "秒"; } } if (tagName.Text == "--- ---") { duration.Text += ")"; } } }
private void LoadRepeater(List <int> listAreaCoordinates, int[] TagIdArray, DateTime dtFrom, DateTime dtTo, string SearchType) { try { IList <AreaInOutTime> lTime = new List <AreaInOutTime>(); int totalCount = 0; using (AppDataContext db = new AppDataContext()) { for (int i = 0; i < TagIdArray.Length; i++) { long totalTicks = 0; int entryCount = 0; DateTime firstInTime = DateTime.MinValue; DateTime lastOutTime = DateTime.MinValue; DateTime endTime = DateTime.Now; if (dtTo < endTime) { endTime = dtTo; } int hostId = TagIdArray[i]; HostTag oHost = HostTag.GetById(hostId); if (oHost == null) { lblMessage.Text += String.Format("ID={0}不存在", hostId); continue; } int tagId = oHost.TagId; string hostName = oHost.HostName; IList <TagPositionLog> lLog = db.TagPositionLogs .Where(x => x.HostId == hostId && x.WriteTime >= dtFrom && x.WriteTime <= endTime) .OrderBy(x => x.WriteTime).ToList(); var firstQuery = db.TagPositionLogs.Where(x => x.WriteTime < dtFrom && x.HostId == hostId) .OrderByDescending(x => x.WriteTime).Take(1); TagPositionLog prevLog = null; try { if (firstQuery != null) { prevLog = firstQuery.First(); } } catch { } AreaInOutTime areaTime = new AreaInOutTime(); areaTime.hostName = hostName; areaTime.hostId = hostId; //previous log if (prevLog != null) { if (prevLog.X > 0) { if (listAreaCoordinates.Contains(prevLog.CoordinatesId)) { areaTime.inTime = dtFrom; firstInTime = areaTime.inTime; } } } bool bFirst = true; if (lLog != null && lLog.Count > 0) { foreach (TagPositionLog log in lLog) { DateTime dtWriteTime = new DateTime(log.WriteTime.Year, log.WriteTime.Month, log.WriteTime.Day, log.WriteTime.Hour, log.WriteTime.Minute, log.WriteTime.Second); if (bFirst) { bFirst = false; //first log if (log.X > 0 && listAreaCoordinates.Contains(log.CoordinatesId)) { if (areaTime.inTime == DateTime.MinValue) { areaTime.inTime = dtWriteTime; firstInTime = areaTime.inTime; } } else { areaTime.outTime = dtWriteTime; if (areaTime.inTime < dtWriteTime) { lastOutTime = areaTime.outTime; } } } else { if (log.X > 0 && listAreaCoordinates.Contains(log.CoordinatesId)) { if (areaTime.inTime == DateTime.MinValue) { areaTime.inTime = dtWriteTime; firstInTime = areaTime.inTime; if (areaTime.outTime != DateTime.MinValue) { areaTime.outTime = DateTime.MinValue; } } else { if (areaTime.outTime == DateTime.MinValue) { } else { double disappearedTime = (dtWriteTime - areaTime.outTime).Duration().TotalSeconds; if (disappearedTime > 60) { areaTime.stayTime = (areaTime.outTime - areaTime.inTime).Duration(); totalTicks += areaTime.stayTime.Ticks; if (SearchType == "2") { lTime.Add(areaTime); } entryCount++; areaTime = new AreaInOutTime(); areaTime.inTime = dtWriteTime; } else { areaTime.outTime = DateTime.MinValue; } } } } else { if (areaTime.outTime == DateTime.MinValue) { areaTime.outTime = dtWriteTime; lastOutTime = areaTime.outTime; } } } } } if (areaTime.inTime != DateTime.MinValue) { if (areaTime.outTime != DateTime.MinValue) { double disappearedTime = (endTime - areaTime.outTime).Duration().TotalSeconds; if (disappearedTime > 60) { areaTime.stayTime = (areaTime.outTime - areaTime.inTime).Duration(); totalTicks += areaTime.stayTime.Ticks; if (SearchType == "2") { lTime.Add(areaTime); } entryCount++; } else { areaTime.stayTime = (endTime - areaTime.inTime).Duration(); totalTicks += areaTime.stayTime.Ticks; if (SearchType == "2") { lTime.Add(areaTime); } entryCount++; } lastOutTime = areaTime.outTime; } else { areaTime.stayTime = (endTime - areaTime.inTime).Duration(); totalTicks += areaTime.stayTime.Ticks; if (SearchType == "2") { lTime.Add(areaTime); } entryCount++; } } if (totalTicks > 0) { lastOutTime = areaTime.outTime; //record the last out time areaTime = new AreaInOutTime(); areaTime.hostId = -1; if (SearchType == "1") { areaTime.hostName = hostName; areaTime.inTime = firstInTime; areaTime.outTime = lastOutTime; //assign last out time //if (areaTime.inTime < lastOutTime) // areaTime.outTime = lastOutTime; } else if (SearchType == "2") { areaTime.hostName = "--- ---"; areaTime.inTime = DateTime.MinValue; areaTime.outTime = DateTime.MinValue; } areaTime.stayTime = new TimeSpan(totalTicks); lTime.Add(areaTime); totalCount++; } else { areaTime = new AreaInOutTime(); areaTime.hostName = hostName; areaTime.inTime = DateTime.MinValue; areaTime.outTime = DateTime.MinValue; areaTime.stayTime = new TimeSpan(0); lTime.Add(areaTime); totalCount++; } } } DataTable dt = new DataTable(); dt.Columns.Add("hostName", typeof(string)); dt.Columns.Add("inTime", typeof(string)); dt.Columns.Add("outTime", typeof(string)); dt.Columns.Add("stayTime", typeof(string)); for (int i = 0; i < lTime.Count(); i++) { string inTimeValue = (lTime.ElementAt(i).inTime == DateTime.MinValue) ? "" : lTime.ElementAt(i).inTime.ToString(); string outTimeValue = (lTime.ElementAt(i).outTime == DateTime.MinValue) ? "" : lTime.ElementAt(i).outTime.ToString(); dt.Rows.Add( lTime.ElementAt(i).hostName, inTimeValue, outTimeValue, lTime.ElementAt(i).stayTime.ToString() ); } ViewState["dt"] = dt; list.DataSource = lTime; list.DataBind(); } catch (Exception err) { lblMessage.Text += err.ToString(); } lbltime.Text = " 从[" + dtFrom.ToString("yyyy年MM月dd日HH时mm分", DateTimeFormatInfo.InvariantInfo) + "] 到 [" + dtTo.ToString("yyyy年MM月dd日HH时mm分", DateTimeFormatInfo.InvariantInfo) + "]"; }