Пример #1
0
        public int RemoveErrorPoints(List <PosInfo> list)
        {
            int             sum            = 0;
            var             bags           = PosInfoListHelper.GetListByCode(list);
            List <Position> allErrorPoints = new List <Position>();

            for (int i = 0; i < bags.Count; i++)
            {
                var            person      = bags[i];
                List <PosInfo> posInfoList = person.Items;//某个人
                Log.Info(LogTags.HisPos, string.Format("删除 {0},{1}({2}/{3})", person.Name, posInfoList.Count, (i + 1), bags.Count));

                var errorPosList = PosDistanceHelper.FilterErrorPoints(posInfoList);
                if (errorPosList.Count > 0)
                {
                    List <int> ids = new List <int>();
                    foreach (var item in errorPosList)
                    {
                        ids.Add(item.Id);
                    }

                    var query       = DbSet.AsNoTracking().Where(item => ids.Contains(item.Id)).OrderBy(item => item.DateTimeStamp);
                    var count       = query.Count();
                    var errorPoints = query.ToList();
                    allErrorPoints.AddRange(errorPoints);
                    Log.Info(LogTags.HisPos, string.Format("删除点 {0}/{1}", count, posInfoList.Count));
                    sum += count;
                    //query.DeleteFromQuery();
                }
            }
            if (sum > 0)
            {
                Log.Info(LogTags.HisPos, string.Format("删除点 {0}/{1}", sum, list.Count));
            }
            SavePositions(allErrorPoints, DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff"));
            return(sum);
        }
Пример #2
0
        public List <PosInfo> GetAllData(string tag, bool useBuffer = false)
        {
            try
            {
                if (useBuffer == true)
                {
                    if (allPoslist != null)//使用缓存
                    {
                        return(allPoslist);
                    }
                }

                allPoslist = new List <PosInfo>();
                GC.Collect();//垃圾收集,降低内存

                //string tag = "GetAllPositionsByDay";// LogTags.Server LogTags.HisPos
                Log.Info(tag, "Start");
                DateTime start = DateTime.Now;

                Bll bll = Bll.NewBllNoRelation();
                if (bll.DbHistory.Database.Exists() == false)
                {
                    return(allPoslist);
                }

                //关联人员
                var personnels = bll.Personnels.ToDictionary();
                Log.Info(tag, string.Format(" personnels count:" + personnels.Count));
                //关联区域
                var areas = bll.Areas.ToDictionary();
                Log.Info(tag, string.Format(" areas count:" + areas.Count));

                //var posCount = bll.Positions.GetCount();
                //Log.Info(tag, string.Format("posCount:{0:N}", posCount));
                ////allPoslist = bll.Positions.ToList();

                //var first = bll.Positions.GetFirst();
                //Log.Info(tag, string.Format("first:" + first.Id));

                //bll.Positions.GetAllPositionsCountByDay((progress) =>
                //{
                //    if (progress.Count > 0)
                //    {
                //        Log.Info(tag, string.Format("date:{0},count:{1},({2}/{3},{4:p})",
                //        progress.Date.ToString("yyyy-MM-dd"), progress.Count, progress.Index, progress.Total, progress.Percent));
                //    }
                //    return true;
                //});

                //var list1 = bll.Positions.GetPositionsOfDay(first.DateTime);

                //var list=bll.Positions.GetPosInfoListOfDay(first.DateTime);

                //allPoslist = list;


                start = DateTime.Now;

                var            totalRemoveCount = 0;
                List <PosInfo> list             = null;

                if (AppContext.HistoryBufferLoadMode == 0)
                {
                    list = bll.Positions.GetAllPosInfoListByDay((progress) =>
                    {
                        if (progress.Count > 0)
                        {
                            int r = 0;
                            string progressText = string.Format("date:{0},count:{1:N},({2}/{3},{4:p})",
                                                                progress.Time, progress.Count, progress.Index, progress.Total, progress.Percent);
                            Log.Info(tag, progressText);
                            var lst = progress.Items as List <PosInfo>;
                            if (lst != null)
                            {
                                if (AppContext.DeleteRepeatPositionsWhenLoad &&
                                    lst.Count > 100000
                                    )
                                {
                                    var list2         = PosInfoListHelper.GetListByCode(lst);
                                    var removeCount   = bll.Positions.RemoveRepeatData(tag, true, list2, progressText + "|");//删除重复数据
                                    totalRemoveCount += removeCount;
                                    if (removeCount > 0)
                                    {
                                        return(1);
                                    }
                                }
                            }
                            return(r);//false的话就中断了,即指获取最后一个
                        }
                        return(0);
                    });
                }
                else if (AppContext.HistoryBufferLoadMode == 1)//数据量很大时使用,按小时处理
                {
                    list = bll.Positions.GetAllPosInfoListByHour((progress) =>
                    {
                        if (progress.Count > 0)
                        {
                            int r = 0;
                            string progressText = string.Format("date:{0},count:{1:N},({2}/{3},{4:p})",
                                                                progress.Time, progress.Count, progress.Index, progress.Total, progress.Percent);
                            Log.Info(tag, progressText);
                            var lst = progress.Items as List <PosInfo>;
                            if (lst != null)
                            {
                                if (AppContext.DeleteRepeatPositionsWhenLoad &&
                                    lst.Count > 100000
                                    )
                                {
                                    var list2         = PosInfoListHelper.GetListByCode(lst);
                                    var removeCount   = bll.Positions.RemoveRepeatData(tag, true, list2, progressText + "|");//删除重复数据
                                    totalRemoveCount += removeCount;
                                    if (removeCount > 0)
                                    {
                                        return(1);
                                    }
                                }
                            }
                            return(r);//false的话就中断了,即指获取最后一个
                        }
                        return(0);
                    });
                }



                //allPoslist = list;
                Log.Info(tag, string.Format(" getlist count:" + list.Count));
                Log.Info(tag, string.Format(" getlist time1:" + (DateTime.Now - start)));

                if (totalRemoveCount > 0)
                {
                    start = DateTime.Now;
                    list  = bll.Positions.GetAllPosInfoListByDay((progress) =>
                    {
                        if (progress.Count > 0)
                        {
                            int r = 0;
                            Log.Info(tag, string.Format("date:{0},count:{1:N},({2}/{3},{4:p})",
                                                        progress.Date.ToString("yyyy-MM-dd"), progress.Count, progress.Index, progress.Total, progress.Percent));
                            var lst = progress.Items as List <PosInfo>;
                            if (lst != null)
                            {
                                //Log.Info(tag, "RemoveRepeatData Start");
                                //var list2 = PosInfoListHelper.GetListByPerson(lst);
                                //var removeCount = bll.Positions.RemoveRepeatData(true, list2);//删除重复数据
                                //totalRemoveCount += removeCount;
                                //Log.Info(tag, "RemoveRepeatData End");
                                //if (removeCount > 0)
                                //{
                                //    return 1;
                                //}
                            }
                            return(r);//false的话就中断了,即指获取最后一个
                        }
                        return(0);
                    });
                    //allPoslist = list;
                    Log.Info(tag, string.Format(" getlist2 count:" + list.Count));
                    Log.Info(tag, string.Format(" getlist2 time1:" + (DateTime.Now - start)));
                }

                start = DateTime.Now;
                foreach (PosInfo item in list)
                {
                    item.ParseTimeStamp();
                }
                Log.Info(tag, string.Format(" ToDateTime time1:" + (DateTime.Now - start)));


                //var nopersonPosList=list.Where(i => i.PersonnelID == null).ToList();

                //var tagRelation = TagRelationBuffer.Instance();

                //SetPersonnalName(tag,personnels, list);
                Log.Info(tag, "SetPersonnalName Start");
                //var count = list.Count;
                for (int i = 0; i < list.Count; i++)
                {
                    //if (i % 100000 == 0)
                    //{
                    //    Log.Info(tag, string.Format("SetPersonnalName {0}/{1}",(i+1), count));
                    //}
                    PosInfo pos = list[i];
                    var     pid = pos.PersonnelID;

                    //if (pid == null)
                    //{
                    //    var pos2 = bll.Positions.FindById(pos.Id);
                    //    tagRelation.SetPositionInfo(pos2);
                    //}

                    if (pid != null && personnels.ContainsKey((int)pid))
                    {
                        var p = personnels[(int)pid];
                        pos.PersonnelName = string.Format("{0}({1})", p.Name, pos.Code);
                    }
                    else
                    {
                        pos.PersonnelName = string.Format("{0}({1})", pos.Code, pos.Code);//有些卡对应的人员不存在
                    }
                }
                Log.Info(tag, "SetPersonnalName End");

                //var noAreaList = SetAreaPath(tag, areas, ref list);//放到子方法中确实会导致内存占用增加 ????
                Log.Info(tag, "SetAreaPath Start");
                var noAreaList   = new List <PosInfo>();
                var noAreaIdList = new List <int>();
                var count        = list.Count;
                for (int i = 0; i < count; i++)
                {
                    //if (i % 100000 == 0)
                    //{
                    //    Log.Info(tag, string.Format("SetAreaPath {0}/{1}", (i + 1), count));
                    //}
                    PosInfo pos    = list[i];
                    var     areaId = pos.AreaId;
                    if (areaId != null && areas.ContainsKey((int)areaId))
                    {
                        var area = areas[(int)areaId];
                        //pos.Area = area;
                        pos.AreaPath = area.GetToBuilding(">");
                        //allPoslist.Add(pos);//只有有区域数据的会被添加
                    }
                    else
                    {
                        noAreaList.Add(pos);//todo:没有区域数据的测试为什么找不到区域用
                        noAreaIdList.Add(pos.Id);
                    }
                }
                Log.Info(tag, "SetAreaPath End");
                Log.Info(tag, string.Format(" noAreaList count:" + noAreaList.Count));

                //if (noAreaIdList.Count > 0)
                //{
                //    var tagRelation = TagRelationBuffer.Instance();
                //    var posList2 = bll.Positions.Where(i => noAreaIdList.Contains(i.Id));
                //    List<int?> newAreas=new List<int?>();
                //    foreach (DbModel.LocationHistory.Data.Position position in posList2)
                //    {
                //        //tagRelation.SetArea(position);//设置区域
                //        //if (!newAreas.Contains(position.AreaId))
                //        //{
                //        //    newAreas.Add(position.AreaId);
                //        //}
                //        position.AreaId = tagRelation.GetParkArea().Id;//直接设置
                //    }
                //    //bll.Positions.EditRange(posList2);//保存到数据库
                //    Log.Info(tag, "处理找不到定位区域的数据 newAreas:"+ newAreas.Count);
                //}

                allPoslist = list;
                RefreshBuffer();

                //return allPoslist;
                Log.Info(tag, "End");
                //list.Clear();
                //list = null;
                GC.Collect();//垃圾收集,降低内存
            }
            catch (Exception ex)
            {
                string strError = ex.Message;

                Log.Info(tag, "Error:" + ex);
            }

            return(allPoslist);
        }