CarRepository.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Datory;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity.WorkCar;
  7. using GxPress.Repository.Interface.WorkCar;
  8. using Microsoft.Extensions.Caching.Distributed;
  9. using Microsoft.Extensions.Options;
  10. namespace GxPress.Repository.Implement.WorkCar
  11. {
  12. public class CarRepository : ICarRepository
  13. {
  14. private readonly Repository<Car> _repository;
  15. public CarRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IDistributedCache cache)
  16. {
  17. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  18. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  19. _repository = new Repository<Car>(database, cache);
  20. }
  21. public IDatabase Database => _repository.Database;
  22. public string TableName => _repository.TableName;
  23. public List<TableColumn> TableColumns => _repository.TableColumns;
  24. public async Task<int> InsertAsync(Car item)
  25. {
  26. return await _repository.InsertAsync(item);
  27. }
  28. public async Task UpdateAsync(Car item)
  29. {
  30. await _repository.UpdateAsync(item);
  31. }
  32. public async Task<Car> GetAsync(int carId)
  33. {
  34. return await _repository.GetAsync(carId);
  35. }
  36. public async Task<IEnumerable<Car>> GetListAsync(int typeId)
  37. {
  38. if (typeId > 0)
  39. {
  40. return await _repository.GetAllAsync(Q.Where(nameof(Car.TypeId), typeId).OrderByDesc(nameof(Car.Taxis)));
  41. }
  42. return await _repository.GetAllAsync(Q.OrderByDesc(nameof(Car.Taxis)));
  43. }
  44. public async Task<bool> DeleteAsync(int carId)
  45. {
  46. return await _repository.DeleteAsync(carId);
  47. }
  48. public async Task<int> GetAvailableCarId(int typeId, IEnumerable<int> orderedCarIds)
  49. {
  50. return await _repository.GetAsync<int>(Q
  51. .Select(nameof(Car.Id))
  52. .Where(nameof(Car.TypeId), typeId)
  53. .WhereNotIn(nameof(Car.Id), orderedCarIds)
  54. );
  55. }
  56. }
  57. }