CarOrderRepository.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Entity.WorkCar;
  8. using GxPress.Repository.Interface.WorkCar;
  9. using Microsoft.Extensions.Caching.Distributed;
  10. using Microsoft.Extensions.Options;
  11. namespace GxPress.Repository.Implement.WorkCar
  12. {
  13. public class CarOrderRepository : ICarOrderRepository
  14. {
  15. private readonly Repository<CarOrder> _repository;
  16. public CarOrderRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IDistributedCache cache)
  17. {
  18. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  19. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  20. _repository = new Repository<CarOrder>(database, cache);
  21. }
  22. public IDatabase Database => _repository.Database;
  23. public string TableName => _repository.TableName;
  24. public List<TableColumn> TableColumns => _repository.TableColumns;
  25. public async Task<int> InsertAsync(CarOrder item)
  26. {
  27. return await _repository.InsertAsync(item);
  28. }
  29. public async Task<IEnumerable<CarOrder>> GetListAsync(int carId)
  30. {
  31. return await _repository.GetAllAsync(Q.Where(nameof(CarOrder.CarId), carId));
  32. }
  33. public async Task<IEnumerable<int>> GetOrderedCarIds(int typeId, DateTime startDate, DateTime endDate)
  34. {
  35. var orders = await _repository.GetAllAsync(Q
  36. .Where(nameof(CarOrder.TypeId), typeId)
  37. .Where(nameof(CarOrder.EndDate), ">", endDate)
  38. );
  39. var carIds = new List<int>();
  40. foreach (var order in orders)
  41. {
  42. if (!CheckDate(startDate, endDate, order.StartDate, order.EndDate))
  43. {
  44. carIds.Add(order.CarId);
  45. }
  46. }
  47. return carIds;
  48. }
  49. private bool CheckDate(DateTime a, DateTime b, DateTime x, DateTime y)
  50. {
  51. //讨论取a[B,E]与b1[B,E]不重叠部分:
  52. //2.a)当 (a.E<=b1.B)||(b1.E<=a.B) 时,无重叠,保留原先的a
  53. //时间无重叠                 
  54. if ((b < x) || (y < a))
  55. {
  56. return false;
  57. }
  58. //2.b)否则有重叠,去掉原先的a;
  59. //当 (a.B<b1.B)&&(b1.B<=a.E) 时,留下左边非重复段 a1[a.B,b1.B]
  60. //当 (a.B<=b1.E)&&(b1.E<a.E) 时,留下右边非重复段 a2[b1.E,a.E]
  61. //重叠
  62. if ((a < x) && (x <= b))
  63. {
  64. return true;
  65. }
  66. if ((a <= y) && (y < b))
  67. {
  68. return true;
  69. }
  70. if ((a < y) && (b == x))
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76. public async Task<bool> DeleteAsync(int orderId)
  77. {
  78. return await _repository.DeleteAsync(orderId);
  79. }
  80. public async Task DeleteByCarIdAsync(int carId)
  81. {
  82. await _repository.DeleteAsync(Q.Where(nameof(CarOrder.CarId), carId));
  83. }
  84. }
  85. }