OrderRepository.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Page;
  9. using GxPress.Common.Tools;
  10. using GxPress.Repository.Interface.Order;
  11. using GxPress.Request.Order;
  12. using GxPress.Result.Order;
  13. using Microsoft.Extensions.Options;
  14. namespace GxPress.Repository.Implement.Order
  15. {
  16. public class OrderRepository : IOrderRepository
  17. {
  18. private readonly Repository<Entity.Order.Order> _repository;
  19. private readonly IMapper _mapper;
  20. private readonly string _connectionString;
  21. private readonly string _databaseTypestr;
  22. public OrderRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  23. {
  24. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  25. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  26. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Entity.Order.Order>(database);
  29. _mapper = mapper;
  30. }
  31. public IDatabase Database => _repository.Database;
  32. public string TableName => _repository.TableName;
  33. public List<TableColumn> TableColumns => _repository.TableColumns;
  34. /// <summary>
  35. /// 查询订单
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. public async Task<PagedList<Entity.Order.Order>> GetAllOrderAsync(OrderSearchRequest request)
  40. {
  41. var result = new PagedList<Entity.Order.Order>();
  42. var query = Q.NewQuery();
  43. if (!string.IsNullOrEmpty(request.KeyWord))
  44. {
  45. query.OrWhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%");
  46. query.OrWhereLike(nameof(Entity.Order.Order.Explain), $"%{request.KeyWord}%");
  47. }
  48. if (request.PayWay > 0)
  49. query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
  50. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  51. {
  52. if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
  53. {
  54. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
  55. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
  56. }
  57. }
  58. if (request.UserId > 0)
  59. query.Where(nameof(Entity.Order.Order.UserId), request.UserId);
  60. result.Total = await _repository.CountAsync(query);
  61. query.OrderByDesc(nameof(Entity.Order.Order.CreatedDate));
  62. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  63. return result;
  64. }
  65. /// <summary>
  66. /// 财务中心
  67. /// </summary>
  68. /// <param name="request"></param>
  69. /// <returns></returns>
  70. public async Task<OrderFinanceResult<Entity.Order.Order>> GetAllOrderFinnceAsync(OrderFinanceSearchRequest request)
  71. {
  72. var result = new OrderFinanceResult<Entity.Order.Order>();
  73. var query = Q.NewQuery();
  74. if (!string.IsNullOrEmpty(request.KeyWord))
  75. query.WhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%");
  76. if (request.PayWay > 0)
  77. query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
  78. if (request.OrderType > 0)
  79. query.Where(nameof(Entity.Order.Order.OrderType), request.OrderType);
  80. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  81. {
  82. if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
  83. {
  84. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
  85. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
  86. }
  87. }
  88. result.Total = await _repository.CountAsync(query);
  89. //显示价格
  90. result.ShowPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price), query);
  91. //集合
  92. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  93. //所有价格
  94. result.AllPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price));
  95. //订单数
  96. result.AllOrderCount = await _repository.CountAsync(Q.Where(nameof(Entity.Order.Order.Id), ">", 0));
  97. //VIP人数
  98. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  99. var database = new Database(databaseType, _connectionString);
  100. var connection = database.GetConnection();
  101. var sql = $"select count(1) from tede_user where IsVip=1 and id in(select userid from tede_order)";
  102. result.VipUserCount = await connection.ExecuteScalarAsync<int>(sql);
  103. // Vip总额
  104. sql = $" select Sum(Price) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
  105. result.VipAllPrice = await connection.ExecuteScalarAsync<int>(sql);
  106. //VIP订单数
  107. sql = $" select count(1) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
  108. result.VipAllOrderCount = await connection.ExecuteScalarAsync<int>(sql);
  109. return result;
  110. }
  111. /// <summary>
  112. /// 添加
  113. /// </summary>
  114. /// <param name="order"></param>
  115. /// <returns></returns>
  116. public async Task<int> InsertAsync(Entity.Order.Order order)
  117. {
  118. return await _repository.InsertAsync(order);
  119. }
  120. /// <summary>
  121. ///获取订单
  122. /// </summary>
  123. /// <param name="orderNumber"></param>
  124. /// <returns></returns>
  125. public async Task<Entity.Order.Order> GetOrderAsync(string orderNumber)
  126. {
  127. return await _repository.GetAsync(Q.Where(nameof(Entity.Order.Order.OrderNumber), orderNumber));
  128. }
  129. public async Task<bool> UpdateAsync(Entity.Order.Order order)
  130. {
  131. return await _repository.UpdateAsync(order);
  132. }
  133. public async Task<bool> GetExistsAsync(int userId, int mediaId)
  134. {
  135. return await _repository.ExistsAsync(Q.Where(nameof(Entity.Order.Order.UserId), userId).Where(nameof(Entity.Order.Order.OrderType), 1).Where(nameof(Entity.Order.Order.MediaId), mediaId));
  136. }
  137. }
  138. }