using Hotline.Application.OrderApp;
using Hotline.Caching.Interfaces;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Order.CarbonCopy;
using Mapster;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.OrderApi
{
    /// <summary>
    /// 工单抄送
    /// </summary>
    public class OrderCarbonCopyController : BaseController
    {
        private readonly IOrderCarbonCopyApplication _orderCarbonCopyApplication;
        private readonly ISystemDicDataCacheManager _sysDicDataCacheManager;
        private readonly IMapper _mapper;

        public OrderCarbonCopyController(
            IOrderCarbonCopyApplication orderCarbonCopyApplication,
            ISystemDicDataCacheManager sysDicDataCacheManager,
            IMapper mapper
        )
        {
            _orderCarbonCopyApplication = orderCarbonCopyApplication;
            _sysDicDataCacheManager = sysDicDataCacheManager;
            _mapper = mapper;
        }

        [HttpGet]
        public async Task<IReadOnlyList<OrderDto>> Query([FromQuery] QueryCarbonCopyRequest dto)
        {
            var orderccs = await _orderCarbonCopyApplication
                .Query(dto)
                .ToPageListWithoutTotalAsync(dto, HttpContext.RequestAborted);

            var orderdtos = orderccs.Select(d => d.Order.Adapt<OrderDto>()).ToList();
            return orderdtos;
        }

        [HttpGet("count")]
        public async Task<int> Count([FromQuery] QueryCarbonCopyRequest dto)
        {
            return await _orderCarbonCopyApplication
                .Query(dto)
                .CountAsync(HttpContext.RequestAborted);
        }

        /// <summary>
        /// 列表页面基础数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("base-data")]
        public async Task<object> BaseData()
        {
            var rsp = new
            {
                AcceptTypeOptions = _sysDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.AcceptType),
            };
            return rsp;
        }
    }
}
