Ejemplo n.º 1
0
        public void FindIncomeGrowthTest()
        {
            var compRepo = new MongoDBRepository <Company>(nameof(Company)).DbCollection;
            var histColl = new MongoDBRepository <Financial>(nameof(Financial)).DbCollection;
            var results  = histColl.Aggregate().Group(key => key.companyId,
                                                      value => new {
                CompanyId   = value.Select(x => x.companyId).First(),
                TotalIncome = value.Sum(x => x.netIncome).Value,
                FirstYear   = value.First().netIncome.Value,
                LastYear    = value.Last().netIncome.Value,
                YearCount   = value.Count()
            }).ToList().Distinct();

            foreach (var result in results)
            {
                double growthRate = ((double)(result.FirstYear - result.LastYear) / result.LastYear) * 100 / result.YearCount;
                var    filter     = Builders <Company> .Filter.Eq("_id", result.CompanyId);

                var comp = compRepo.Find(filter).ToListAsync().Result;

                if (growthRate > 10 && comp.Count > 0)
                {
                    Console.WriteLine($"{comp.First().symbol}, {result.TotalIncome}, {result.FirstYear}, {result.LastYear}, {growthRate}");
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            initRepository();

            StreamWriter sw = null;

            #region 统计用户浏览老房子
            QueryObject <Event> findAllViewAHouseEvent = new QueryObject <Event>(EventDb);
            findAllViewAHouseEvent.AppendQuery(QueryOperator.Equal, "Type", "ViewAHouseEvent", QueryLogic.And);
            var allViewAHouseEvent = EventDb.Find(findAllViewAHouseEvent);
            //用来统计每个用户共浏览了多少次,key为用户id
            Dictionary <Guid, NameAndCount> userViewHouse = new Dictionary <Guid, NameAndCount>();
            //用来统计每个老房子被浏览了多少次,key为house id
            Dictionary <Guid, NameAndCount> houseHasViewed = new Dictionary <Guid, NameAndCount>();
            foreach (var viewAHouseEvent in allViewAHouseEvent)
            {
                #region userViewHouse
                Guid userId = (Guid)viewAHouseEvent.Sender;
                if (userViewHouse.ContainsKey(userId))
                {
                    userViewHouse[userId].Count++;
                }
                else
                {
                    string userName;
                    if (userId == Guid.Empty)
                    {
                        userName = "******";
                    }
                    else
                    {
                        userName = UserDb.FindOneById(userId).NickName;
                    }
                    userViewHouse.Add(userId, new NameAndCount(userName, 1));
                }
                #endregion

                #region houseHasViewed
                Guid houseId = (Guid)viewAHouseEvent.Subject;
                if (houseHasViewed.ContainsKey(houseId))
                {
                    houseHasViewed[houseId].Count++;
                }
                else
                {
                    string houseName;
                    if (HouseDb.FindOneById(houseId) == null)
                    {
                        houseName = "未知";
                    }
                    else
                    {
                        houseName = HouseDb.FindOneById(houseId).Name;
                    }
                    houseHasViewed.Add(houseId, new NameAndCount(houseName, 1));
                }
                #endregion
            }
            //统计用户浏览
            sw = new StreamWriter(@"C:\用户浏览老房子.csv", false, System.Text.Encoding.UTF8);
            sw.WriteLine("用户昵称,浏览次数");
            sw.WriteLine("总数," + allViewAHouseEvent.Count());
            foreach (KeyValuePair <Guid, NameAndCount> userViewHouseKeyValuePair in userViewHouse)
            {
                sw.WriteLine(userViewHouseKeyValuePair.Value.Name + "," + userViewHouseKeyValuePair.Value.Count);
            }
            sw.Dispose();
            //统计老房子被浏览
            sw = new StreamWriter(@"C:\老房子被浏览.csv", false, System.Text.Encoding.UTF8);
            sw.WriteLine("house名,浏览次数");
            sw.WriteLine("总数," + allViewAHouseEvent.Count());
            foreach (KeyValuePair <Guid, NameAndCount> houseHasViewedKeyValuePair in houseHasViewed)
            {
                sw.WriteLine(houseHasViewedKeyValuePair.Value.Name + "," + houseHasViewedKeyValuePair.Value.Count);
            }
            sw.Dispose();

            #endregion

            #region 统计用户like
            QueryObject <Event> findAllLikeEvent = new QueryObject <Event>(EventDb);
            findAllLikeEvent.AppendQuery(QueryOperator.Equal, "Type", "LikeEvent", QueryLogic.And);
            var allLikeEvent = EventDb.Find(findAllLikeEvent);
            //用来统计用户点赞次数
            Dictionary <Guid, NameAndCount> userLike = new Dictionary <Guid, NameAndCount>();
            foreach (var likeEvent in allLikeEvent)
            {
                Guid userId = (Guid)likeEvent.Sender;
                if (userLike.ContainsKey(userId))
                {
                    userLike[userId].Count++;
                }
                else
                {
                    string userName = UserDb.FindOneById(userId).NickName;
                    userLike.Add(userId, new NameAndCount(userName, 1));
                }
            }
            sw = new StreamWriter(@"C:\用户点赞次数.csv", false, System.Text.Encoding.UTF8);
            sw.WriteLine("用户昵称,点赞次数");
            sw.WriteLine("总数," + allLikeEvent.Count());
            foreach (KeyValuePair <Guid, NameAndCount> userLikeKeyValuePair in userLike)
            {
                sw.WriteLine(userLikeKeyValuePair.Value.Name + "," + userLikeKeyValuePair.Value.Count);
            }
            sw.Dispose();
            #endregion
        }