using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Entity.WorkCar;
using GxPress.Repository.Interface.WorkCar;
using GxPress.Request.App.WorkCar;
using GxPress.Result;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace GxPress.Api.AppControllers
{
///
/// 车辆
///
[Route("api/app/car")]
[ApiController]
[Authorize]
public class AppCarController : ControllerBase
{
private readonly ILoginContext _loginContext;
private readonly ICarTypeRepository _carTypeRepository;
private readonly ICarOrderRepository _carOrderRepository;
private readonly ICarRepository _carRepository;
public AppCarController(ILoginContext loginContext, ICarTypeRepository carTypeRepository, ICarOrderRepository carOrderRepository, ICarRepository carRepository)
{
_loginContext = loginContext;
_carTypeRepository = carTypeRepository;
_carOrderRepository = carOrderRepository;
_carRepository = carRepository;
}
///
/// 获取车辆类型列表
///
///
[HttpGet("type")]
public async Task> TypeList()
{
return await _carTypeRepository.GetListAsync();
}
///
/// 预定车辆
///
///
[HttpPost("actions/order")]
public async Task Order([FromBody] CarRequest request)
{
var orderedCarIds =
await _carOrderRepository.GetOrderedCarIds(request.TypeId, request.StartDate, request.EndDate);
var carId = await _carRepository.GetAvailableCarId(request.TypeId, orderedCarIds);
if (carId == 0)
{
throw new Exception("本时段车辆已预订,请更换预订时间或车辆类型");
}
var order = new CarOrder
{
UserId = _loginContext.AccountId,
CarId = carId,
Reasons = request.Reasons,
Destination = request.Destination,
TypeId = request.TypeId,
StartDate = request.StartDate,
EndDate = request.EndDate,
Hour = request.Hour
};
await _carOrderRepository.InsertAsync(order);
return new DefaultResult
{
Value = true
};
}
}
}