예제 #1
0
        public List <UserRankDTO> GetUserRankClosedPL()
        {
            var tryGetAuthUser = TryGetAuthUser();

            int balanceTypeId = 1;

            if (tryGetAuthUser != null)
            {
                balanceTypeId = db.Balances.FirstOrDefault(o => o.Id == tryGetAuthUser.ActiveBalanceId).TypeId;
            }

            var beginTimeUtc = DateTimes.GetRankingBeginTimeUTC();

            var userDTOs = db.Positions.Where(o => o.ClosedAt != null && o.ClosedAt >= beginTimeUtc && o.BalanceTypeId == balanceTypeId)
                           .GroupBy(o => o.UserId)
                           .Join(db.Users, g => g.Key, u => u.Id, (g, u) => new UserRankDTO()
            {
                id = g.Key.Value,

                nickname = u.Nickname,
                picUrl   = u.PicUrl,

                //posCount = g.Count(),
                winRate = (decimal)g.Count(p => p.PL > 0) / g.Count(),
                roi     = g.Sum(p => p.PL.Value) / g.Sum(p => p.Invest.Value),
            }).OrderByDescending(o => o.roi).Take(YJYGlobal.DEFAULT_PAGE_SIZE).ToList();

            //var tryGetAuthUser = TryGetAuthUser();
            if (tryGetAuthUser != null)
            {
                ////move myself to the top
                //var findIndex = userDTOs.FindIndex(o => o.id == tryGetAuthUser.Id);
                //if (findIndex > 0)
                //{
                //    var me = userDTOs.First(o => o.id == tryGetAuthUser.Id);
                //    userDTOs.RemoveAt(findIndex);
                //    userDTOs.Insert(0, me);
                //}
                //else if (findIndex == 0)
                //{
                //    //do nothing
                //}
                //else
                //{
                //    userDTOs.Insert(0, new UserRankDTO()
                //    {
                //        id = tryGetAuthUser.Id,
                //        nickname = tryGetAuthUser.Nickname,
                //        picUrl = tryGetAuthUser.PicUrl,

                //        //posCount = 0,
                //        roi = 0,
                //        winRate = 0,
                //    });
                //}

                //add myself to the top
                var me = userDTOs.FirstOrDefault(o => o.id == tryGetAuthUser.Id);
                if (me != null)
                {
                    userDTOs.Insert(0,
                                    new UserRankDTO()
                    {
                        id       = me.id,
                        nickname = me.nickname,
                        picUrl   = me.picUrl,
                        roi      = me.roi,
                        winRate  = me.winRate
                    });
                }
                else
                {
                    userDTOs.Insert(0,
                                    new UserRankDTO()
                    {
                        id       = tryGetAuthUser.Id,
                        nickname = tryGetAuthUser.Nickname,
                        picUrl   = tryGetAuthUser.PicUrl,

                        roi     = 0,
                        winRate = 0,
                    });
                }
            }

            return(userDTOs);
        }
예제 #2
0
        public List <FeedDTO> GetDefaultFeeds(int count = 50, DateTime?newerThan = null, DateTime?olderThan = null)
        {
            var tryGetAuthUser = TryGetAuthUser();

            if (tryGetAuthUser == null)
            {
                //不再区分中英文
                //get system feed
                //var language = Thread.CurrentThread.CurrentUICulture.Name;
                var systemFeedsWhereClause = db.Headlines.AsQueryable();
                if (olderThan != null)
                {
                    systemFeedsWhereClause = systemFeedsWhereClause.Where(o => o.CreateAt < olderThan);
                }
                if (newerThan != null)
                {
                    systemFeedsWhereClause = systemFeedsWhereClause.Where(o => o.CreateAt > newerThan);
                }
                var systemFeeds = systemFeedsWhereClause
                                  .OrderByDescending(o => o.CreateAt).Take(count)
                                  .Select(o => new FeedDTO()
                {
                    user =
                        new UserBaseDTO()
                    {
                        picUrl = Blob.USER_DEFAULT_PIC_FOLDER_URL + Blob.SYSTEM_FEED_PIC_FILENAME,
                        //nickname = "system",
                    },
                    type  = "system",
                    time  = o.CreateAt.Value,
                    body  = o.Body,
                    title = o.Header,
                })
                                  .ToList();

                return(systemFeeds);
            }

            var feedUserIds = new List <int>();

            feedUserIds.Add(tryGetAuthUser.Id);

            if (tryGetAuthUser.ShowFollowingFeed ?? true)
            {
                //following user ids
                var followingUserIds =
                    db.UserFollows.Where(o => o.UserId == tryGetAuthUser.Id).Select(o => o.FollowingId).ToList();

                feedUserIds = feedUserIds.Concat(followingUserIds).ToList();
            }

            if (tryGetAuthUser.ShowTradeFollowingFeed ?? true)
            {
                //trade following user ids
                var tradeFollowingUserIds =
                    db.UserTradeFollows.Where(o => o.UserId == tryGetAuthUser.Id).Select(o => o.FollowingId).ToList();

                feedUserIds = feedUserIds.Concat(tradeFollowingUserIds).ToList();
            }

            //-------------------balance type -------------------
            int balanceTypeId = 1;

            if (tryGetAuthUser != null)
            {
                balanceTypeId = db.Balances.FirstOrDefault(o => o.Id == tryGetAuthUser.ActiveBalanceId).TypeId;
            }

            //get open feeds
            var openFeedsWhereClause = db.Positions.Where(o => o.FollowPosId == null && feedUserIds.Contains(o.UserId.Value) && o.BalanceTypeId == balanceTypeId);

            if (olderThan != null)
            {
                openFeedsWhereClause = openFeedsWhereClause.Where(o => o.CreateTime < olderThan);
            }
            var openFeeds = openFeedsWhereClause
                            .OrderByDescending(o => o.CreateTime).Take(count)
                            .Select(o => new FeedDTO()
            {
                user = new UserBaseDTO()
                {
                    id = o.UserId.Value
                },
                type     = "open",
                time     = o.CreateTime.Value,
                position =
                    new PositionBaseDTO()
                {
                    id = o.Id, invest = o.Invest, leverage = o.Leverage, isLong = o.Side
                },
                security = new SecurityBaseDTO()
                {
                    id = o.SecurityId.Value
                },
            })
                            .ToList();

            //get close feeds
            var closeFeedsWhereClause = db.Positions.Where(o => o.FollowPosId == null && feedUserIds.Contains(o.UserId.Value) && o.ClosedAt != null && o.BalanceTypeId == balanceTypeId);

            if (olderThan != null)
            {
                closeFeedsWhereClause = closeFeedsWhereClause.Where(o => o.ClosedAt < olderThan);
            }
            var closeFeeds = closeFeedsWhereClause
                             .OrderByDescending(o => o.ClosedAt).Take(count)
                             .Select(o => new FeedDTO()
            {
                user = new UserBaseDTO()
                {
                    id = o.UserId.Value
                },
                type     = "close",
                time     = o.ClosedAt.Value,
                position = new PositionBaseDTO()
                {
                    id = o.Id, roi = o.PL / o.Invest, isLong = o.Side
                },
                security = new SecurityBaseDTO()
                {
                    id = o.SecurityId.Value
                },
            })
                             .ToList();

            //get status feeds
            var statusFeedsWhereClause = db.Status.Where(o => feedUserIds.Contains(o.UserId.Value));

            if (olderThan != null)
            {
                statusFeedsWhereClause = statusFeedsWhereClause.Where(o => o.Time < olderThan);
            }
            var statusFeed = statusFeedsWhereClause
                             .OrderByDescending(o => o.Time).Take(count)
                             .Select(o => new FeedDTO()
            {
                user = new UserBaseDTO()
                {
                    id = o.UserId.Value
                },
                type   = "status",
                time   = o.Time.Value,
                status = o.Text,
            })
                             .ToList();

            //concat results
            var @resultEnumerable = openFeeds.Concat(closeFeeds).Concat(statusFeed);

            //get system feed
            if (tryGetAuthUser.ShowHeadlineFeed ?? true)
            {
                //get system feed
                var systemFeedsWhereClause = db.Headlines.AsQueryable();
                if (olderThan != null)
                {
                    systemFeedsWhereClause = systemFeedsWhereClause.Where(o => o.CreateAt < olderThan);
                }
                if (newerThan != null)
                {
                    systemFeedsWhereClause = systemFeedsWhereClause.Where(o => o.CreateAt > newerThan);
                }
                var systemFeeds = systemFeedsWhereClause
                                  .OrderByDescending(o => o.CreateAt).Take(count)
                                  .Select(o => new FeedDTO()
                {
                    user =
                        new UserBaseDTO()
                    {
                        picUrl = Blob.USER_DEFAULT_PIC_FOLDER_URL + Blob.SYSTEM_FEED_PIC_FILENAME,
                        //nickname = "system",
                    },
                    type  = "system",
                    time  = o.CreateAt.Value,
                    body  = o.Body,
                    title = o.Header,
                })
                                  .ToList();

                //add to results
                @resultEnumerable = @resultEnumerable.Concat(systemFeeds);
            }

            //filter by param 'newerThan'
            if (newerThan != null)
            {
                @resultEnumerable = @resultEnumerable.Where(o => o.time > newerThan.Value);
            }

            var result = @resultEnumerable.OrderByDescending(o => o.time).Take(count).ToList();

            //get ranked users
            var beginTimeUtc = DateTimes.GetRankingBeginTimeUTC();
            var rankedUsers  =
                db.Positions.Where(o => o.ClosedAt != null && o.ClosedAt >= beginTimeUtc && o.BalanceTypeId == balanceTypeId)
                .GroupBy(o => o.UserId)
                .Select(g => new
            {
                id  = g.Key.Value,
                roi = g.Sum(p => p.PL.Value) / g.Sum(p => p.Invest.Value),
            })
                .OrderByDescending(o => o.roi)
                .Take(YJYGlobal.DEFAULT_PAGE_SIZE)
                .ToList();

            //populate user/security info
            var users = db.Users.Where(o => feedUserIds.Contains(o.Id)).ToList();
            var prods = WebCache.Instance.ProdDefs;

            foreach (var feedDto in result)
            {
                if (feedDto.user != null && feedDto.user.id != null)
                {
                    var user = users.FirstOrDefault(o => o.Id == feedDto.user.id);
                    feedDto.user.nickname = user.Nickname;
                    feedDto.user.picUrl   = user.PicUrl;

                    feedDto.isRankedUser = rankedUsers.Any(o => o.id == feedDto.user.id);
                }

                if (feedDto.security != null)
                {
                    feedDto.security.name =
                        Translator.GetProductNameByThreadCulture(
                            prods.FirstOrDefault(o => o.Id == feedDto.security.id).Name);
                }
            }

            return(result);
        }