OrderRepository.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. query.WhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%");
  45. if (request.PayWay > 0)
  46. query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
  47. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  48. {
  49. if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
  50. {
  51. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
  52. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
  53. }
  54. }
  55. if (request.UserId > 0)
  56. query.Where(nameof(Entity.Order.Order.UserId), request.UserId);
  57. result.Total = await _repository.CountAsync(query);
  58. query.OrderByDesc(nameof(Entity.Order.Order.CreatedDate));
  59. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  60. return result;
  61. }
  62. /// <summary>
  63. /// 财务中心
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. public async Task<OrderFinanceResult<Entity.Order.Order>> GetAllOrderFinnceAsync(OrderFinanceSearchRequest request)
  68. {
  69. var result = new OrderFinanceResult<Entity.Order.Order>();
  70. var query = Q.NewQuery();
  71. if (!string.IsNullOrEmpty(request.KeyWord))
  72. query.WhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%");
  73. if (request.PayWay > 0)
  74. query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
  75. if (request.OrderType > 0)
  76. query.Where(nameof(Entity.Order.Order.OrderType), request.OrderType);
  77. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  78. {
  79. if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
  80. {
  81. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
  82. query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
  83. }
  84. }
  85. result.Total = await _repository.CountAsync(query);
  86. //显示价格
  87. result.ShowPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price), query);
  88. //集合
  89. result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  90. //所有价格
  91. result.AllPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price));
  92. //订单数
  93. result.AllOrderCount = await _repository.CountAsync(Q.Where(nameof(Entity.Order.Order.Id), ">", 0));
  94. //VIP人数
  95. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  96. var database = new Database(databaseType, _connectionString);
  97. var connection = database.GetConnection();
  98. var sql = $"select count(1) from tede_user where IsVip=1 and id in(select userid from tede_order)";
  99. result.VipUserCount = await connection.ExecuteScalarAsync<int>(sql);
  100. // Vip总额
  101. sql = $" select Sum(Price) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
  102. result.VipAllPrice = await connection.ExecuteScalarAsync<int>(sql);
  103. //VIP订单数
  104. sql = $" select count(1) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
  105. result.VipAllOrderCount = await connection.ExecuteScalarAsync<int>(sql);
  106. return result;
  107. }
  108. /// <summary>
  109. /// 添加
  110. /// </summary>
  111. /// <param name="order"></param>
  112. /// <returns></returns>
  113. public async Task<int> InsertAsync(Entity.Order.Order order)
  114. {
  115. return await _repository.InsertAsync(order);
  116. }
  117. /// <summary>
  118. ///获取订单
  119. /// </summary>
  120. /// <param name="orderNumber"></param>
  121. /// <returns></returns>
  122. public async Task<Entity.Order.Order> GetOrderAsync(string orderNumber)
  123. {
  124. return await _repository.GetAsync(Q.Where(nameof(Entity.Order.Order.OrderNumber), orderNumber));
  125. }
  126. public async Task<bool> UpdateAsync(Entity.Order.Order order)
  127. {
  128. return await _repository.UpdateAsync(order);
  129. }
  130. public async Task<bool> GetExistsAsync(int userId, int mediaId)
  131. {
  132. 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));
  133. }
  134. }
  135. }