PlatformDataService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.Tools;
  8. using GxPress.EnumConst;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Repository.Interface.Media;
  11. using GxPress.Result.DataCenter;
  12. using GxPress.Service.Interface.PlatformData;
  13. namespace GxPress.Service.Implement.PlatformData
  14. {
  15. public partial class PlatformDataService : IPlatformDataService
  16. {
  17. private readonly IUserRepository userRepository;
  18. private readonly IMediaRepository mediaRepository;
  19. public PlatformDataService(IUserRepository userRepository, IMediaRepository mediaRepository)
  20. {
  21. this.userRepository = userRepository;
  22. this.mediaRepository = mediaRepository;
  23. }
  24. /// <summary>
  25. /// 平台运营数据
  26. /// </summary>
  27. /// <returns></returns>
  28. public async Task<PlatformOperationDataResult> GetPlatformDataAsync()
  29. {
  30. var newUserTime = DateTime.Now.ToString("yyyy-MM-dd");
  31. var onlineUserTime = DateTime.Now.AddMinutes(-10).ToString("yyyy-MM-dd HH:mm:ss");
  32. var activityUserTime = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd HH:mm:ss");
  33. var sql = $@"SELECT
  34. (SELECT
  35. COUNT(1)
  36. FROM
  37. tede_user) AS SumUserCount,
  38. (SELECT
  39. COUNT(1)
  40. FROM
  41. tede_user
  42. WHERE
  43. CreatedDate > '{newUserTime}') AS NewUserCount,
  44. (SELECT
  45. COUNT(1)
  46. FROM
  47. tede_user
  48. WHERE
  49. LoginTime > '{onlineUserTime}') AS OnlineUserCount,
  50. (SELECT
  51. COUNT(1)
  52. FROM
  53. tede_user
  54. WHERE
  55. LoginTime > '{activityUserTime}') AS ActivityUserCount";
  56. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  57. var database = new Database(DatabaseType.MySql, connectionString);
  58. var connection = database.GetConnection();
  59. var result = await connection.QueryFirstAsync<PlatformOperationDataResult>(sql);
  60. return result;
  61. }
  62. /// <summary>
  63. /// 用户总数增长趋势
  64. /// </summary>
  65. /// <returns></returns>
  66. public async Task<UserIncreaseResult> GetUserIncreaseResult()
  67. {
  68. var result = new UserIncreaseResult();
  69. result.MonthData = new List<string>();
  70. result.ExpectedData = new List<int>();
  71. var nowTime = DateTime.Now;
  72. for (int i = 0; i < 12; i++)
  73. {
  74. result.MonthData.Add(nowTime.AddMonths(-i).Month + "月");
  75. result.ExpectedData.Add(await userRepository.GetUserCount(nowTime.AddMonths(-i).ToString("yyyy-MM-01"), nowTime.AddMonths(-i + 1).ToString("yyyy-MM-01")));
  76. }
  77. return result;
  78. }
  79. /// <summary>
  80. /// 用户地区分布
  81. /// </summary>
  82. /// <value></value>
  83. public async Task<List<UserAreaDistributingResult>> GetUserAreaDistributingResults()
  84. {
  85. var sql = "SELECT province as Name,count(1) as Value FROM ccpph_tede.tede_user group by province";
  86. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  87. var database = new Database(DatabaseType.MySql, connectionString);
  88. var connection = database.GetConnection();
  89. var list = await connection.QueryAsync<UserAreaDistributingResult>(sql);
  90. foreach (var item in list)
  91. {
  92. if (string.IsNullOrEmpty(item.Name))
  93. item.Name = "未知";
  94. }
  95. return list.ToList();
  96. }
  97. /// <summary>
  98. /// 会员用户占比
  99. /// </summary>
  100. /// <returns></returns>
  101. public async Task<UserVipProportionResult> GetUserVipProportionResult()
  102. {
  103. var sql = "SELECT (select count(1) from tede_user where IsVip=1)/(select count(1) from tede_user) as value";
  104. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  105. var database = new Database(DatabaseType.MySql, connectionString);
  106. var connection = database.GetConnection();
  107. var result = await connection.QueryFirstAsync<UserVipProportionResult>(sql);
  108. return result;
  109. }
  110. /// <summary>
  111. /// 实时在线人数
  112. /// </summary>
  113. /// <returns></returns>
  114. public async Task<OnlineUserResult> GetOnlineUserResult()
  115. {
  116. var result = new OnlineUserResult();
  117. result.ActualData = new List<int>();
  118. result.ExpectedData = new List<int>();
  119. //时间
  120. result.MonthData = new List<string>();
  121. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  122. var database = new Database(DatabaseType.MySql, connectionString);
  123. var connection = database.GetConnection();
  124. var nowTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
  125. for (int i = 0; i <= 6; i++)
  126. {
  127. var monthData = "";
  128. if ((i * 4).ToString().Length < 2)
  129. monthData = "0";
  130. monthData += (i * 4).ToString() + ":00";
  131. result.MonthData.Add(monthData);
  132. 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")}'";
  133. result.ActualData.Add(await connection.ExecuteScalarAsync<int>(sql));
  134. 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")}'";
  135. result.ExpectedData.Add(await connection.ExecuteScalarAsync<int>(sql));
  136. }
  137. return result;
  138. }
  139. /// <summary>
  140. /// 平台内容数据
  141. /// </summary>
  142. /// <returns></returns>
  143. public async Task<PlatformContentDataResult> GetPlatformContentDataResult()
  144. {
  145. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  146. var database = new Database(DatabaseType.MySql, connectionString);
  147. var connection = database.GetConnection();
  148. var nowTime = DateTime.Now.ToString("yyyy-MM-dd");
  149. var sql = $@"SELECT
  150. (SELECT
  151. COUNT(1)
  152. FROM
  153. tede_media) AS SumContentCount,
  154. (SELECT
  155. COUNT(1)
  156. FROM
  157. tede_media
  158. WHERE
  159. CreatedDate > '{nowTime}') AS NewContentCount,
  160. (SELECT
  161. COUNT(1)
  162. FROM
  163. tede_media
  164. WHERE
  165. IsChecked=1 and IsDelete=0) AS OnlineContentCount,
  166. (SELECT
  167. COUNT(1)
  168. FROM
  169. tede_media
  170. WHERE
  171. FreeProportion>0) AS PayContentCount";
  172. return await connection.QueryFirstAsync<PlatformContentDataResult>(sql);
  173. }
  174. /// <summary>
  175. /// 内容类型分布图
  176. /// </summary>
  177. /// <returns></returns>
  178. public async Task<List<ContentTypeDistributingResult>> GetContentTypeDistributingResults()
  179. {
  180. var result = new List<ContentTypeDistributingResult>();
  181. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  182. var database = new Database(DatabaseType.MySql, connectionString);
  183. var connection = database.GetConnection();
  184. foreach (ResourceTypeConst item in Enum.GetValues(typeof(ResourceTypeConst)))
  185. {
  186. var model = new ContentTypeDistributingResult();
  187. model.Name = item.GetDescriptionOriginal();
  188. var sql = $"select count(1) from tede_media where MediaType={item.GetHashCode()}";
  189. model.Value = await connection.ExecuteScalarAsync<int>(sql);
  190. result.Add(model);
  191. }
  192. foreach (AttachTypeConst item in Enum.GetValues(typeof(AttachTypeConst)))
  193. {
  194. var model = new ContentTypeDistributingResult();
  195. model.Name = item.GetDescriptionOriginal();
  196. var sql = $"select count(1) from tede_media where AttachId={item.GetHashCode()}";
  197. model.Value = await connection.ExecuteScalarAsync<int>(sql);
  198. result.Add(model);
  199. }
  200. return result;
  201. }
  202. /// <summary>
  203. /// 收费内容统计图
  204. /// </summary>
  205. /// <returns></returns>
  206. public async Task<PayContentstatisticsResult> GetPayContentstatisticsResult()
  207. {
  208. var result = new PayContentstatisticsResult();
  209. result.PageAdata = new List<int>();
  210. result.PageBdata = new List<int>();
  211. result.Title = new List<string>();
  212. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  213. var database = new Database(DatabaseType.MySql, connectionString);
  214. var connection = database.GetConnection();
  215. foreach (ResourceTypeConst item in Enum.GetValues(typeof(ResourceTypeConst)))
  216. {
  217. result.Title.Add(item.GetDescriptionOriginal());
  218. var sql = $"select count(1) from tede_media where MediaType={item.GetHashCode()} and FreeProportion>0";
  219. result.PageAdata.Add(await connection.ExecuteScalarAsync<int>(sql));
  220. sql = $"select count(1) from tede_media where MediaType={item.GetHashCode()} and FreeProportion=0";
  221. result.PageBdata.Add(await connection.ExecuteScalarAsync<int>(sql));
  222. }
  223. foreach (AttachTypeConst item in Enum.GetValues(typeof(AttachTypeConst)))
  224. {
  225. result.Title.Add(item.GetDescriptionOriginal());
  226. var sql = $"select count(1) from tede_media where AttachId={item.GetHashCode()} and FreeProportion>0";
  227. result.PageAdata.Add(await connection.ExecuteScalarAsync<int>(sql));
  228. sql = $"select count(1) from tede_media where MediaType={item.GetHashCode()} and FreeProportion=0";
  229. result.PageBdata.Add(await connection.ExecuteScalarAsync<int>(sql));
  230. }
  231. return result;
  232. }
  233. /// <summary>
  234. /// 内容增长情况图
  235. /// </summary>
  236. /// <returns></returns>
  237. public async Task<ContentIncreaseResult> GetContentIncreaseResult()
  238. {
  239. var result = new ContentIncreaseResult();
  240. result.MonthData = new List<string>();
  241. result.ExpectedData = new List<int>();
  242. var nowTime = DateTime.Now;
  243. for (int i = 0; i < 12; i++)
  244. {
  245. result.MonthData.Add(nowTime.AddMonths(-i).Month + "月");
  246. result.ExpectedData.Add(await mediaRepository.CountAsync(nowTime.AddMonths(-i).ToString("yyyy-MM-01"), nowTime.AddMonths(-i + 1).ToString("yyyy-MM-01")));
  247. }
  248. return result;
  249. }
  250. /// <summary>
  251. /// 平台累计访问数据(次)
  252. /// </summary>
  253. /// <returns></returns>
  254. public async Task<PlatformAccumulativeVisitResult> GetPlatformAccumulativeVisitResult()
  255. {
  256. var sql = @"
  257. SELECT
  258. (SELECT
  259. SUM(ReadCount)
  260. FROM
  261. tede_media) AS VisitCount,
  262. (SELECT
  263. SUM(CommentCount)
  264. FROM
  265. tede_media) AS CommentCount,
  266. (SELECT
  267. SUM(RetransmissionCount)
  268. FROM
  269. tede_media) AS RetransmissionCount,
  270. (SELECT
  271. SUM(CollectCount)
  272. FROM
  273. tede_media) AS CollctionCount";
  274. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  275. var database = new Database(DatabaseType.MySql, connectionString);
  276. var connection = database.GetConnection();
  277. return await connection.QueryFirstAsync<PlatformAccumulativeVisitResult>(sql);
  278. }
  279. /// <summary>
  280. /// 平台商务数据
  281. /// </summary>
  282. /// <returns></returns>
  283. public async Task<PlatformCommerceResult> GetPlatformCommerceResult()
  284. {
  285. var sql = @"SELECT
  286. (SELECT
  287. SUM(Price)
  288. FROM
  289. tede_order) AS Amount,
  290. (SELECT
  291. COUNT(1)
  292. FROM
  293. tede_order) AS OrderCount,
  294. (SELECT
  295. COUNT(1)
  296. FROM
  297. tede_user) AS VipUserCount,
  298. (SELECT
  299. SUM(Price)
  300. FROM
  301. tede_order
  302. WHERE
  303. IsVip = 1) AS VipAmount";
  304. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  305. var database = new Database(DatabaseType.MySql, connectionString);
  306. var connection = database.GetConnection();
  307. return await connection.QueryFirstAsync<PlatformCommerceResult>(sql);
  308. }
  309. /// <summary>
  310. /// 内容销售排行榜
  311. /// </summary>
  312. /// <returns></returns>
  313. public async Task<List<ContentSaleRankingResult>> GetContentSaleRankingResults()
  314. {
  315. var result = new List<ContentSaleRankingResult>();
  316. foreach (ResourceTypeConst item in Enum.GetValues(typeof(ResourceTypeConst)))
  317. {
  318. result.Add(new ContentSaleRankingResult { CategoryName = item.GetDescriptionOriginal(), MediaType = item.GetHashCode() });
  319. }
  320. foreach (AttachTypeConst item in Enum.GetValues(typeof(AttachTypeConst)))
  321. {
  322. result.Add(new ContentSaleRankingResult { CategoryName = item.GetDescriptionOriginal(), MediaType = item.GetHashCode() });
  323. }
  324. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  325. var database = new Database(DatabaseType.MySql, connectionString);
  326. var connection = database.GetConnection();
  327. var sql = @"SELECT
  328. MediaType,
  329. SUM(SellAmount) AS Amount,
  330. (SUM(SellAmount) / (SELECT
  331. SUM(SellAmount)
  332. FROM
  333. tede_media)) AS Proportion
  334. FROM
  335. tede_media
  336. GROUP BY MediaType";
  337. var list = await connection.QueryAsync<ContentSaleRankingResult>(sql);
  338. foreach (var item in list)
  339. {
  340. foreach (var dom in result)
  341. {
  342. if (dom.MediaType == item.MediaType)
  343. {
  344. dom.Amount = dom.Amount;
  345. dom.Proportion = dom.Proportion;
  346. }
  347. }
  348. }
  349. sql = @"SELECT
  350. AttachType as MediaType,
  351. SUM(SellAmount) AS Amount,
  352. (SUM(SellAmount) / (SELECT
  353. SUM(SellAmount)
  354. FROM
  355. tede_media)) AS Proportion
  356. FROM
  357. tede_media where AttachId>0
  358. GROUP BY AttachType";
  359. list = await connection.QueryAsync<ContentSaleRankingResult>(sql);
  360. foreach (var item in list)
  361. {
  362. foreach (var dom in result)
  363. {
  364. if (dom.MediaType == item.MediaType)
  365. {
  366. dom.Amount = dom.Amount;
  367. dom.Proportion = dom.Proportion;
  368. }
  369. }
  370. }
  371. return result;
  372. }
  373. /// <summary>
  374. /// 内容分类销售占比
  375. /// </summary>
  376. /// <returns></returns>
  377. public async Task<List<ContentSaleProportionResult>> GetContentSaleProportionResults()
  378. {
  379. var result = new List<ContentSaleProportionResult>();
  380. foreach (ResourceTypeConst item in Enum.GetValues(typeof(ResourceTypeConst)))
  381. {
  382. result.Add(new ContentSaleProportionResult { Name = item.GetDescriptionOriginal(), MediaType = item.GetHashCode() });
  383. }
  384. foreach (AttachTypeConst item in Enum.GetValues(typeof(AttachTypeConst)))
  385. {
  386. result.Add(new ContentSaleProportionResult { Name = item.GetDescriptionOriginal(), MediaType = item.GetHashCode() });
  387. }
  388. var connectionString = ConfigHelper.GetValue("Database:ConnectionString");
  389. var database = new Database(DatabaseType.MySql, connectionString);
  390. var connection = database.GetConnection();
  391. var sql = @"SELECT
  392. MediaType,
  393. (SUM(SellAmount) / (SELECT
  394. SUM(SellAmount)
  395. FROM
  396. tede_media)) AS Value
  397. FROM
  398. tede_media
  399. GROUP BY MediaType";
  400. var list = await connection.QueryAsync<ContentSaleProportionResult>(sql);
  401. foreach (var item in list)
  402. {
  403. foreach (var dom in result)
  404. {
  405. if (dom.MediaType == item.MediaType)
  406. {
  407. dom.Value = dom.Value;
  408. }
  409. }
  410. }
  411. sql = @"SELECT
  412. AttachType as MediaType,
  413. (SUM(SellAmount) / (SELECT
  414. SUM(SellAmount)
  415. FROM
  416. tede_media)) AS Proportion
  417. FROM
  418. tede_media where AttachId>0
  419. GROUP BY AttachType";
  420. list = await connection.QueryAsync<ContentSaleProportionResult>(sql);
  421. foreach (var item in list)
  422. {
  423. foreach (var dom in result)
  424. {
  425. if (dom.MediaType == item.MediaType)
  426. {
  427. dom.Value = dom.Value;
  428. }
  429. }
  430. }
  431. return result;
  432. }
  433. }
  434. }