李昊 4 years ago
parent
commit
d4cdfb25e4

+ 4 - 4
gx_api/GxPress/Api/GxPress.Api/AdminControllers/AdminPlatformDataController.cs

@@ -29,10 +29,10 @@ namespace GxPress.Api.AdminControllers
         {
             var result = new PlatformDataResult();
             result.PlatformOperationDataResult = await platformDataService.GetPlatformDataAsync();
-            result.UserIncreaseResult = new UserIncreaseResult();
-            result.UserAreaDistributingResults = new List<UserAreaDistributingResult>();
-            result.UserVipProportionResult = new UserVipProportionResult();
-            result.OnlineUserResult = new OnlineUserResult();
+            result.UserIncreaseResult = await platformDataService.GetUserIncreaseResult();
+            result.UserAreaDistributingResults = await platformDataService.GetUserAreaDistributingResults();
+            result.UserVipProportionResult = await platformDataService.GetUserVipProportionResult();
+            result.OnlineUserResult = await platformDataService.GetOnlineUserResult();
             result.PlatformContentDataResult = new PlatformContentDataResult();
             result.ContentTypeDistributingResult = new List<ContentTypeDistributingResult>();
             result.ContentOlogyDistributingResult = new List<ContentOlogyDistributingResult>();

+ 3 - 3
gx_api/GxPress/Model/GxPress.Result/DataCenter/PlatformDataResult.cs

@@ -127,12 +127,12 @@ namespace GxPress.Result.DataCenter
         /// 城市
         /// </summary>
         /// <value></value>
-        public string City { get; set; }
+        public string Name { get; set; }
         /// <summary>
-        /// 城市数量
+        /// 数量
         /// </summary>
         /// <value></value>
-        public int CityUserCount { get; set; }
+        public int Value { get; set; }
     }
     /// <summary>
     /// 会员用户占比

+ 4 - 0
gx_api/GxPress/Repository/GxPress.Repository.Implement/UserRepository.cs

@@ -99,6 +99,10 @@ namespace GxPress.Repository.Implement
             return userDetail;
         }
 
+        public async Task<int> GetUserCount(string beginTime, string endTime)
+        {
+            return await _repository.CountAsync(Q.WhereDate(nameof(User.CreatedDate), ">=", beginTime).WhereDate(nameof(User.CreatedDate), "<=", endTime));
+        }
         public async Task<User> GetAsync(int id)
         {
             var user = await _repository.GetAsync(id);

+ 1 - 0
gx_api/GxPress/Repository/GxPress.Repository.Interface/IUserRepository.cs

@@ -238,5 +238,6 @@ namespace GxPress.Repository.Interface
         /// <returns></returns>
         Task<PagedList<UserDetail>> GetUserVipDetail(UserSearchVipRequest request);
         Task<bool> UpdateDisableAsync(UserUpdateDisableRequest request);
+        Task<int> GetUserCount(string beginTime, string endTime);
     }
 }

+ 70 - 0
gx_api/GxPress/Service/GxPress.Service.Implement/PlatformData/PlatformDataService.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Threading.Tasks;
 using Dapper;
 using Datory;
@@ -63,6 +65,74 @@ namespace GxPress.Service.Implement.PlatformData
         public async Task<UserIncreaseResult> GetUserIncreaseResult()
         {
             var result = new UserIncreaseResult();
+            result.MonthData = new List<string>();
+            result.ExpectedData = new List<int>();
+            var nowTime = DateTime.Now;
+            for (int i = 0; i < 12; i++)
+            {
+                result.MonthData.Add(nowTime.AddMonths(-i).Month + "月");
+                result.ExpectedData.Add(await userRepository.GetUserCount(nowTime.AddMonths(-i).ToString("yyyy-MM-01"), nowTime.AddMonths(-i + 1).ToString("yyyy-MM-01")));
+            }
+            return result;
+        }
+        /// <summary>
+        /// 用户地区分布
+        /// </summary>
+        /// <value></value>
+        public async Task<List<UserAreaDistributingResult>> GetUserAreaDistributingResults()
+        {
+            var sql = "SELECT province as Name,count(1) as Value FROM ccpph_tede.tede_user group by province";
+            var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
+            var database = new Database(DatabaseType.MySql, connectionString);
+            var connection = database.GetConnection();
+            var list = await connection.QueryAsync<UserAreaDistributingResult>(sql);
+            foreach (var item in list)
+            {
+                if (string.IsNullOrEmpty(item.Name))
+                    item.Name = "未知";
+            }
+            return list.ToList();
+        }
+        /// <summary>
+        /// 会员用户占比
+        /// </summary>
+        /// <returns></returns>
+        public async Task<UserVipProportionResult> GetUserVipProportionResult()
+        {
+            var sql = "SELECT (select count(1) from tede_user where IsVip=1)/(select count(1) from tede_user) as value";
+            var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
+            var database = new Database(DatabaseType.MySql, connectionString);
+            var connection = database.GetConnection();
+            var result = await connection.QueryFirstAsync<UserVipProportionResult>(sql);
+            return result;
+        }
+        /// <summary>
+        /// 实时在线人数
+        /// </summary>
+        /// <returns></returns>
+        public async Task<OnlineUserResult> GetOnlineUserResult()
+        {
+            var result = new OnlineUserResult();
+            result.ActualData = new List<int>();
+            result.ExpectedData = new List<int>();
+            //时间
+            result.MonthData = new List<string>();
+            var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
+            var database = new Database(DatabaseType.MySql, connectionString);
+            var connection = database.GetConnection();
+            var nowTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
+            for (int i = 0; i <= 6; i++)
+            {
+                var monthData = "";
+                if ((i * 4).ToString().Length < 2)
+                    monthData = "0";
+                monthData += (i * 4).ToString() + ":00";
+                result.MonthData.Add(monthData);
+                var sql = $"SELECT count(1) FROM tede_user_login where CreatedDate>='{nowTime.AddHours(i - 4).ToString("yyyy-MM-dd hh:mm:ss")}' and CreatedDate<='{nowTime.AddHours(i).ToString("yyyy-MM-dd hh:mm:ss")}'";
+                result.ActualData.Add(await connection.ExecuteScalarAsync<int>(sql));
+                sql = $"SELECT count(1) FROM tede_user_login where CreatedDate>='{nowTime.AddDays(-1).AddHours(i - 4).ToString("yyyy-MM-dd hh:mm:ss")}' and CreatedDate<='{nowTime.AddDays(-1).AddHours(i).ToString("yyyy-MM-dd hh:mm:ss")}'";
+                result.ExpectedData.Add(await connection.ExecuteScalarAsync<int>(sql));
+            }
             return result;
         }
     }

+ 22 - 1
gx_api/GxPress/Service/GxPress.Service.Interface/PlatformData/IPlatformDataService.cs

@@ -1,14 +1,35 @@
+using System.Collections.Generic;
 using System.Threading.Tasks;
 using GxPress.Result.DataCenter;
 
 namespace GxPress.Service.Interface.PlatformData
 {
-    public interface IPlatformDataService:IService
+    public interface IPlatformDataService : IService
     {
         /// <summary>
         /// 平台运营数据
         /// </summary>
         /// <returns></returns>
         Task<PlatformOperationDataResult> GetPlatformDataAsync();
+        /// <summary>
+        /// 用户总数增长趋势
+        /// </summary>
+        /// <returns></returns>
+        Task<UserIncreaseResult> GetUserIncreaseResult();
+        /// <summary>
+        /// 用户地区分布
+        /// </summary>
+        /// <value></value>
+        Task<List<UserAreaDistributingResult>> GetUserAreaDistributingResults();
+         /// <summary>
+        /// 会员用户占比
+        /// </summary>
+        /// <returns></returns>
+        Task<UserVipProportionResult> GetUserVipProportionResult();
+        /// <summary>
+        /// 实时在线人数
+        /// </summary>
+        /// <returns></returns>
+       Task<OnlineUserResult> GetOnlineUserResult();
     }
 }