using Edge.Core.Domain.FccOrderInfo.Input;
using Edge.Core.Domain.FccOrderInfo.Output;
using FccLite.Web.Repositories.FccOrderInfo;
using FccLite.Web.Repositories.FccStationInfo;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;

namespace FccLite.Web.Services.FccOrderInfo
{
    public class OrderInfoService : IOrderInfoService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private readonly IOrderInfoReposity _orderInfoReposity;
        private readonly IStationRepository _stationRepository;

        private readonly string[] tableTitle = { "流水号","油站", "油机号", "油枪", "油品", "单价", "升数", "金额", "挂枪时间", "交易时间", "支付方式", "支付状态", "支付用户" };
        private readonly Dictionary<int, string> _afterPayType = new Dictionary<int, string>
        {
            {0,"未支付" },
            {1,"已支付" }
        };

        public OrderInfoService(IOrderInfoReposity orderInfoReposity,IStationRepository stationRepository) 
        {
            _orderInfoReposity = orderInfoReposity;
            _stationRepository = stationRepository;
        }

        

        public async Task<OrderInfoOutput> GetPage(OrderInfoPageInput orderInfoPageInput)
        {
            IEnumerable<OrderInfo> enumerable = await _orderInfoReposity.GetPage(orderInfoPageInput);
            List<OrderInfo> orderInfos = enumerable.ToList();
            return new OrderInfoOutput()
            {
                Total = orderInfos.Count,
                List = orderInfos
            };
        }


        public async Task<byte[]> DownloadAfterPayOrderExcel(OrderInfoPageInput orderInfoPageInput)
        {
            IEnumerable<OrderInfo> enumerable = await _orderInfoReposity.GetOrderFilter(orderInfoPageInput);
            try
            {
                var package = new ExcelPackage();
                ExcelWorksheet excelWorksheet = package.Workbook.Worksheets.Add("Sheet1");

                for (int index = 0; index < tableTitle.Length; index++)
                {
                    excelWorksheet.Cells[1, index+1].Value = tableTitle[index];
                }

                using (var range = excelWorksheet.Cells[1, 1, 1, tableTitle.Length])
                {
                    range.Style.Font.Bold = true;
                }

                int row = 2;
                decimal allVolume = 0;
                decimal allAmount = 0;
                foreach (var order in enumerable)
                {
                    excelWorksheet.Cells[row, 1].Value = order.Ttc;
                    excelWorksheet.Cells[row, 2].Value = order.StationName;
                    excelWorksheet.Cells[row, 3].Value = order.MachineName;
                    excelWorksheet.Cells[row, 4].Value = order.NozzleNum;
                    excelWorksheet.Cells[row, 5].Value = order.OilName;
                    excelWorksheet.Cells[row, 6].Value = order.Price;
                    excelWorksheet.Cells[row, 7].Value = order.Volume;
                    excelWorksheet.Cells[row, 8].Value = order.Amount;
                    excelWorksheet.Cells[row, 9].Value = order.EndTime != null ? order.EndTime?.ToString("yyyy-MM-dd HH:mm:ss") : "";
                    excelWorksheet.Cells[row, 10].Value = order.PaymentTime != null ? order.PaymentTime?.ToString("yyyy-MM-dd HH:mm:ss") : "";
                    excelWorksheet.Cells[row, 11].Value = order.PaymentName;

                    string payTypeStr = "未知状态";
                    _afterPayType.TryGetValue(order.PaymentStatus, out payTypeStr);
                    excelWorksheet.Cells[row, 12].Value = payTypeStr;

                    excelWorksheet.Cells[row, 13].Value = order.UserName;

                    allVolume += order.Volume;
                    allAmount += order.Amount;
                    row++;
                }
                excelWorksheet.Cells[row, 7].Value = allVolume;
                excelWorksheet.Cells[row, 8].Value = allAmount;

                return package.GetAsByteArray();
            } catch(Exception e)
            {
                Console.WriteLine(e);
                Logger.Error(e.Message);
            }

            return new byte[0];
        }
    }
}
