using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Authentication.Standard;
using XYY.Common.Standard;
using XYY.Core.Standard.ExcelHelper.MSExcelHelper;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard.Order;
using XYY.Service.Standard.Order;

namespace XYY.Api.Order.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderStatisticsController : ApiControllerBase
    {
        private readonly ICustomerOrderStatisticsService _customerOrderStatisticsService;
        public OrderStatisticsController(ICustomerOrderStatisticsService customerOrderStatisticsService)
        {
            this._customerOrderStatisticsService = customerOrderStatisticsService;
        }
        [AllowAnonymous]
        public async Task<IActionResult> GetdeliverySignCollectList(QueryModel queryModel)
        {
            var u = User.GetUserContent();
            var dto = await _customerOrderStatisticsService.GetDeliverySignCollectPager(queryModel, u.CustomerId);
            return Ok(dto);
        }
        [AllowAnonymous]
        public async Task<IActionResult> GetDeliverySignDetailList(QueryModel queryModel)
        {
            var u = User.GetUserContent();
            var dto = await _customerOrderStatisticsService.GetDeliverySignDetailPager(queryModel, u.CustomerId);
            return Ok(dto);
        }
        [AllowAnonymous]
        public async Task<IActionResult> OutData(QueryModel queryModel)
        {
            var u = User.GetUserContent();
            var list = await _customerOrderStatisticsService.GetDeliverySignDetailList(queryModel, u.CustomerId);
            var ms = new MSExcelHelper();
            byte[] data = null;
            if (await _customerOrderStatisticsService.IsShowOnlineTime(u.CustomerId))
            {
                data = ms.OutDataAsByte<OutDeliverySignDetailInfoOnlineTime, deliverySignDetail>(list.ToList());
            }
            else { data = ms.OutDataAsByte<OutDeliverySignDetailInfo, deliverySignDetail>(list.ToList()); }
            var utc = DateTime.Now.ToFileTimeUtc();
            return File($"{utc}.xlsx", data);
        }
        [AllowAnonymous]
        public async Task<bool> IsShowOnlineTime()
        {
            var u = User.GetUserContent();
            return await _customerOrderStatisticsService.IsShowOnlineTime(u.CustomerId);
        }
    }
}
