OrderRepository.cs 5.8 KB

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