OrderRepository.cs 6.8 KB

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