using System; using System.Collections.Generic; using System.Threading.Tasks; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Entity.WorkCar; using GxPress.Repository.Interface.WorkCar; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; namespace GxPress.Repository.Implement.WorkCar { public class CarOrderRepository : ICarOrderRepository { private readonly Repository _repository; public CarOrderRepository(IOptionsMonitor dbOptionsAccessor, IDistributedCache cache) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database, cache); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(CarOrder item) { return await _repository.InsertAsync(item); } public async Task> GetListAsync(int carId) { return await _repository.GetAllAsync(Q.Where(nameof(CarOrder.CarId), carId)); } public async Task> GetOrderedCarIds(int typeId, DateTime startDate, DateTime endDate) { var orders = await _repository.GetAllAsync(Q .Where(nameof(CarOrder.TypeId), typeId) .Where(nameof(CarOrder.EndDate), ">", endDate) ); var carIds = new List(); foreach (var order in orders) { if (!CheckDate(startDate, endDate, order.StartDate, order.EndDate)) { carIds.Add(order.CarId); } } return carIds; } private bool CheckDate(DateTime a, DateTime b, DateTime x, DateTime y) { //讨论取a[B,E]与b1[B,E]不重叠部分: //2.a)当 (a.E<=b1.B)||(b1.E<=a.B) 时,无重叠,保留原先的a //时间无重叠                  if ((b < x) || (y < a)) { return false; } //2.b)否则有重叠,去掉原先的a; //当 (a.B DeleteAsync(int orderId) { return await _repository.DeleteAsync(orderId); } public async Task DeleteByCarIdAsync(int carId) { await _repository.DeleteAsync(Q.Where(nameof(CarOrder.CarId), carId)); } } }