using Hotline.OrderTranspond;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.OrderTranspond;
using Hotline.Share.Enums.Order;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using XF.Domain.Repository;

namespace Hotline.Api.Controllers
{
    public class TranspondCityController : BaseController
    {

        private readonly IRepository<TranspondCityRawData> _transpondCityRawDataRepository;

        private readonly IMapper _mapper;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="transpondCityRawDataRepository"></param>
        /// <param name="mapper"></param>
        public TranspondCityController(IRepository<TranspondCityRawData> transpondCityRawDataRepository,
           IMapper mapper)
        {
            _transpondCityRawDataRepository = transpondCityRawDataRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 转出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("order_transfer_out_list")]
        public async Task<PagedDto<TranspondCityRawDataDto>> OrderTransferOutList([FromQuery] QueryTranspondCityDto dto)
        {
            RefAsync<int> total = 0;
            var items = await _transpondCityRawDataRepository.Queryable()
                .Where(p => p.Direction == ETranspondDirection.Out)
                .WhereIF(!string.IsNullOrEmpty(dto.OrderCode), p => p.OrderCode.Contains(dto.OrderCode))
                .WhereIF(dto.StartTime.HasValue, p => p.TransferOutTime >= dto.StartTime)
                   .WhereIF(dto.EndTime.HasValue, p => p.TransferOutTime <= dto.EndTime)
                             .WhereIF(!string.IsNullOrEmpty(dto.CiryName), p => p.CityName.Contains(dto.CiryName))
                 .OrderByDescending(d => d.CreationTime)
                 .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            //返回数据
            return new PagedDto<TranspondCityRawDataDto>(total.Value, _mapper.Map<IReadOnlyList<TranspondCityRawDataDto>>(items));
        }

        /// <summary>
        /// 转入
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("order_receive_in_list")]
        public async Task<PagedDto<TranspondCityRawDataDto>> OrderReceiveInList([FromQuery] QueryTranspondCityDto dto)
        {
            RefAsync<int> total = 0;
            var items = await _transpondCityRawDataRepository.Queryable()
                .Where(p => p.Direction == ETranspondDirection.In)
                .WhereIF(!string.IsNullOrEmpty(dto.OrderCode), p => p.OrderCode.Contains(dto.OrderCode))
                .WhereIF(dto.StartTime.HasValue, p => p.TransferOutTime >= dto.StartTime)
                   .WhereIF(dto.EndTime.HasValue, p => p.TransferOutTime <= dto.EndTime)
                             .WhereIF(!string.IsNullOrEmpty(dto.CiryName), p => p.CityName.Contains(dto.CiryName))
                 .OrderByDescending(d => d.CreationTime)
                 .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            //返回数据
            return new PagedDto<TranspondCityRawDataDto>(total.Value, _mapper.Map<IReadOnlyList<TranspondCityRawDataDto>>(items));
        }
    }
}
