public List <UserRankDTO> GetTradeFollowingUsers() { var result = db.UserTradeFollows .Where(o => o.UserId == UserId) .OrderByDescending(o => o.CreateAt) .Join(db.Users, f => f.FollowingId, u => u.Id, (f, u) => new UserRankDTO() { id = u.Id, nickname = u.Nickname, picUrl = u.PicUrl, //showData = o.Following.ShowData ?? CFDUsers.DEFAULT_SHOW_DATA, //rank = o.Following.LiveRank ?? 0, followTrade = new FollowTradeDTO() { createAt = f.CreateAt, investFixed = f.InvestFixed.Value, stopAfterCount = f.StopAfterCount.Value, } }).ToList(); if (result.Count > 0) { var userIds = result.Select(o => o.id).ToList(); var twoWeeksAgo = DateTimes.GetChinaToday().AddDays(-13); var twoWeeksAgoUtc = twoWeeksAgo.AddHours(-8); var user = GetUser(); var balance = db.Balances.FirstOrDefault(o => o.Id == user.ActiveBalanceId); var datas = db.Positions.Where( o => userIds.Contains(o.UserId.Value) && o.ClosedAt != null && o.ClosedAt >= twoWeeksAgoUtc && o.BalanceTypeId == balance.TypeId) .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(); foreach (var userDto in result) { var data = datas.FirstOrDefault(o => o.id == userDto.id); if (data == null) //this guy has no data { userDto.roi = 0; //userDto.posCount = 0; userDto.winRate = 0; } else { userDto.roi = data.roi; //userDto.posCount = data.posCount; userDto.winRate = data.winRate; } } //result = result.OrderByDescending(o => o.roi).ToList(); } return(result); }
public List <PosChartDTO> PLChartClosed(int userId) { var tryGetAuthUser = TryGetAuthUser(); int balanceTypeId = 1; if (tryGetAuthUser != null) { balanceTypeId = db.Balances.FirstOrDefault(o => o.Id == tryGetAuthUser.ActiveBalanceId).TypeId; } //var user = db.Users.FirstOrDefault(o => o.Id == userId); //if (!(user.ShowData ?? true) && userId != UserId)//not showing data && not myself // return new List<PosChartDTO>(); var dbList = db.Positions.Where(o => o.UserId == userId && o.ClosedAt != null && o.BalanceTypeId == balanceTypeId) .OrderBy(o => o.ClosedAt) .ToList(); if (dbList.Count == 0) { return(new List <PosChartDTO>()); } var result = dbList.GroupBy(o => o.ClosedAt.Value.AddHours(8).Date).Select(o => new PosChartDTO { date = o.Key, //Count = o.Count(), pl = o.Sum(p => p.PL.Value) }).ToList(); #region fill-in all data points for client... var beginDate = DateTime.SpecifyKind(result.First().date, DateTimeKind.Utc); var endDate = DateTimes.GetChinaToday(); var newResult = new List <PosChartDTO>(); decimal cumulativePL = 0; for (DateTime d = beginDate; d <= endDate; d = d.AddDays(1)) { if (d.DayOfWeek == DayOfWeek.Sunday) { continue; } var data = result.FirstOrDefault(o => o.date == d); if (data == null) { newResult.Add(new PosChartDTO() { date = d, pl = cumulativePL }); } else { cumulativePL += data.pl; newResult.Add(new PosChartDTO() { date = d, pl = cumulativePL }); } } //if the first data is not zero, add a zero before it as a reference point if (newResult.First().pl != 0) { newResult.Insert(0, new PosChartDTO() { date = newResult.First().date.AddDays(-1), pl = 0 }); } #endregion //var user = db.Users.FirstOrDefault(o => o.Id == userId); //if (!(user.ShowData ?? CFDUsers.DEFAULT_SHOW_DATA) && userId != UserId) //not showing data && not myself //{ // //data obfuscation // var max = newResult.Max(o => o.pl); // var min = newResult.Min(o => o.pl); // var ratio = max - min == 0 ? 0 : 100 / (max - min); // foreach (var dto in newResult) // { // dto.pl = (dto.pl - min) * ratio; // } //} return(newResult); }
public List <UserRankDTO> GetUserRankPL2w() { var tryGetAuthUser = TryGetAuthUser(); int balanceTypeId = 1; if (tryGetAuthUser != null) { balanceTypeId = db.Balances.FirstOrDefault(o => o.Id == tryGetAuthUser.ActiveBalanceId).TypeId; } var twoWeeksAgo = DateTimes.GetChinaToday().AddDays(-13); var twoWeeksAgoUtc = twoWeeksAgo.AddHours(-8); var userDTOs = db.Positions.Where(o => o.ClosedAt != null && o.ClosedAt >= twoWeeksAgoUtc && 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); }