AppCarController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using GxPress.Auth;
  5. using GxPress.Entity.WorkCar;
  6. using GxPress.Repository.Interface.WorkCar;
  7. using GxPress.Request.App.WorkCar;
  8. using GxPress.Result;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Mvc;
  11. namespace GxPress.Api.AppControllers
  12. {
  13. /// <summary>
  14. /// 车辆
  15. /// </summary>
  16. [Route("api/app/car")]
  17. [ApiController]
  18. [Authorize]
  19. public class AppCarController : ControllerBase
  20. {
  21. private readonly ILoginContext _loginContext;
  22. private readonly ICarTypeRepository _carTypeRepository;
  23. private readonly ICarOrderRepository _carOrderRepository;
  24. private readonly ICarRepository _carRepository;
  25. public AppCarController(ILoginContext loginContext, ICarTypeRepository carTypeRepository, ICarOrderRepository carOrderRepository, ICarRepository carRepository)
  26. {
  27. _loginContext = loginContext;
  28. _carTypeRepository = carTypeRepository;
  29. _carOrderRepository = carOrderRepository;
  30. _carRepository = carRepository;
  31. }
  32. /// <summary>
  33. /// 获取车辆类型列表
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpGet("type")]
  37. public async Task<IEnumerable<CarType>> TypeList()
  38. {
  39. return await _carTypeRepository.GetListAsync();
  40. }
  41. /// <summary>
  42. /// 预定车辆
  43. /// </summary>
  44. /// <returns></returns>
  45. [HttpPost("actions/order")]
  46. public async Task<DefaultResult> Order([FromBody] CarRequest request)
  47. {
  48. var orderedCarIds =
  49. await _carOrderRepository.GetOrderedCarIds(request.TypeId, request.StartDate, request.EndDate);
  50. var carId = await _carRepository.GetAvailableCarId(request.TypeId, orderedCarIds);
  51. if (carId == 0)
  52. {
  53. throw new Exception("本时段车辆已预订,请更换预订时间或车辆类型");
  54. }
  55. var order = new CarOrder
  56. {
  57. UserId = _loginContext.AccountId,
  58. CarId = carId,
  59. Reasons = request.Reasons,
  60. Destination = request.Destination,
  61. TypeId = request.TypeId,
  62. StartDate = request.StartDate,
  63. EndDate = request.EndDate,
  64. Hour = request.Hour
  65. };
  66. await _carOrderRepository.InsertAsync(order);
  67. return new DefaultResult
  68. {
  69. Value = true
  70. };
  71. }
  72. }
  73. }