Exemple #1
0
        public virtual async Task <UserInfoModel> PrepareUserInfoModelAsync(TblUsers user)
        {
            var result = user.Adapt <UserInfoModel>();

            result.ShowUserSubscriptionInfo = (await _userGroupsService.GetAsEnumerableAsync()).Any();

            if (user.UserCountry != null)
            {
                result.Country = user.UserCountry.GetLocalized(x => x.CountryName);
            }

            if (user.UserGroup != null && user.SubscriptionExpireDate > DateTime.Now)
            {
                var textColor       = string.IsNullOrWhiteSpace(user.UserGroup.GetLocalized(x => x.GroupTextColor)) ? "inherit" : user.UserGroup.GetLocalized(x => x.GroupTextColor);
                var backgroundColor = string.IsNullOrWhiteSpace(user.UserGroup.GetLocalized(x => x.GroupBackgroundColor)) ? "inherit" : user.UserGroup.GetLocalized(x => x.GroupBackgroundColor);
                var groupImage      = string.IsNullOrWhiteSpace(user.UserGroup.GetLocalized(x => x.GroupSmallIcon))
                    ? ""
                    : $@"<img style=""max-height: 18px;"" src=""{
                            user.UserGroup.GetLocalized(x => x.GroupSmallIcon)
                        }""/>";

                result.UserGroup =
                    $@"<span class=""badge"" style=""color: {textColor}; background-color:{backgroundColor}"">{
                            groupImage
                        } {user.UserGroup.GetLocalized(x => x.GroupName)}</span>";

                var highestGroup = await _userGroupsService.GetHighestUserGroupAsync();

                result.UserSubscribedToHighestPlan   = user.UserGroup.GroupPriority >= highestGroup.GroupPriority;
                result.WhenExtendCurrentPlanBtnShown = user.UserGroup.WhenExtendCurrentPlanBtnShown;

                if (user.UserGroup.MaxDownloadCount > 0)
                {
                    var maxDownloadPeriodTypeStr = _localizationService.GetResource(user.UserGroup.MaxDownloadPeriodType.ToString());
                    result.UserGroupDownloadLimit = user.UserGroup.MaxDownloadCount + _localizationService.GetResource("Per") + maxDownloadPeriodTypeStr;
                }
                else
                {
                    result.UserGroupDownloadLimit = _localizationService.GetResource("Unlimited");
                }
            }
            else
            {
                result.UserGroup              = $"<small>({_localizationService.GetResource("YouNotSubscribedToPlan")})</small>";
                result.SubscriptionDate       = null;
                result.SubscriptionExpireDate = null;
            }

            if (user.MaxDownloadCount > 0)
            {
                var maxDownloadPeriodTypeStr = _localizationService.GetResource(user.MaxDownloadPeriodType.ToString());
                result.DownloadLimit = user.MaxDownloadCount + _localizationService.GetResource("Per") + maxDownloadPeriodTypeStr;
            }
            else
            {
                result.DownloadLimit = _localizationService.GetResource("Unlimited");
            }

            return(result);
        }
        public virtual async Task <ActionResult> UsersChart(DateTime?FromDate, DateTime?ToDate, TimePeriodType PeriodType = TimePeriodType.Day)
        {
            if (FromDate == null || ToDate == null || FromDate >= ToDate)
            {
                FromDate = DateTime.Now.AddDays(-6);
                FromDate = new DateTime(FromDate.Value.Year, FromDate.Value.Month, FromDate.Value.Day, 0, 0, 0);
                ToDate   = DateTime.Now;
                ToDate   = new DateTime(ToDate.Value.Year, ToDate.Value.Month, ToDate.Value.Day, 23, 59, 59);
            }

            var datetimeToStringFormat = "g";

            switch (PeriodType)
            {
            case TimePeriodType.Hour:
                datetimeToStringFormat = "yyyy/MM/dd HH:mm";
                break;

            case TimePeriodType.Day:
                datetimeToStringFormat = "yyyy/MM/dd";
                break;

            case TimePeriodType.Month:
                datetimeToStringFormat = "yyyy/MM";
                break;

            case TimePeriodType.Year:
                datetimeToStringFormat = "yyyy";
                break;
            }

            var allUsers = await _usersService.UsersReportAsync(FromDate.Value, ToDate.Value, PeriodType, false, 0);

            var verifiedUsers = await _usersService.UsersReportAsync(FromDate.Value, ToDate.Value, PeriodType, true, 0);

            var allUsersCount = allUsers.Sum(p => p.Value);

            var chartDatas = new List <ChartData>
            {
                new ChartData()
                {
                    ChartItems = allUsers.Select(p => new ChartPoint()
                    {
                        Y = p.Value, X = p.Key.ToString(datetimeToStringFormat)
                    }).ToList(),
                    Name  = $"{_localizationService.GetResource("Total")} ({allUsersCount})",
                    Color = "#5e35b1"
                },
                new ChartData()
                {
                    ChartItems = verifiedUsers.Select(p => new ChartPoint()
                    {
                        Y = p.Value, X = p.Key.ToString(datetimeToStringFormat)
                    }).ToList(),
                    Name  = $"{_localizationService.GetResource("EmailConfirmed")} ({verifiedUsers.Sum(p=> p.Value)} - %{(allUsersCount > 0 ? (verifiedUsers.Sum(p=> p.Value) * 100) / allUsersCount : 0)})",
                    Color = "#bd30ab"
                }
            };

            //By UserGroup
            foreach (var userGroup in (await _userGroupsService.GetAsEnumerableAsync()).OrderByDescending(p => p.GroupPriority))
            {
                var userGroupReport = await _usersService.UsersReportAsync(FromDate.Value, ToDate.Value, PeriodType, false, userGroup.Id);

                chartDatas.Add(new ChartData()
                {
                    ChartItems = userGroupReport.Select(p => new ChartPoint()
                    {
                        Y = p.Value, X = p.Key.ToString(datetimeToStringFormat)
                    }).ToList(),
                    Name  = $"{userGroup.GetLocalized(x=> x.GroupName)} ({userGroupReport.Sum(p => p.Value)} - %{(allUsersCount > 0 ? (userGroupReport.Sum(p => p.Value) * 100) / allUsersCount : 0)})",
                    Color = userGroup.GetLocalized(x => x.GroupBackgroundColor)
                });
            }

            return(View("Partials/_Chart", new ChartModel()
            {
                ChartDatas = chartDatas,
                ToDate = ToDate.Value,
                FromDate = FromDate.Value,
                PeriodType = PeriodType,
                XAxisTitle = _localizationService.GetResource("Date"),
                YAxisTitle = _localizationService.GetResource("NumberOfUsers"),
                ChartName = "NumberUsersChart",
                ControllerName = "UsersChart",
                UpdateTargetId = "usersChartHolder"
            }));
        }