using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XYY.Service.Standard.First.ESB
{
    public partial class ESBMappingService
    {
        public static List<ZZESBMapping> GetZZESBMapping()
        {
            ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
            var zones = new List<ZZESBMapping>();
            string excelPath = System.IO.Path.Combine(
                    System.AppContext.BaseDirectory, "ZZESB", "轨迹示范.xlsx");
            if (System.IO.File.Exists(excelPath))
            {
                using (FileStream existingFile = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (ExcelPackage package = new ExcelPackage(existingFile))
                    {
                        if (package.Workbook.Worksheets.Count == 0)
                            throw new Exception("文件不可为空");
                        else
                        {
                            foreach (var work in package.Workbook.Worksheets)
                            {
                                int RowNum = work.Dimension.Rows;
                                for (int row = 2; row <= RowNum; row++)
                                {
                                    zones.Add(new ZZESBMapping
                                    {
                                        // 获取每列数据
                                        CountryCode = work.Cells[row, 1].Value.ToString(),
                                        Key = work.Cells[row, 2].Value.ToString(),
                                        Content = work.Cells[row, 3].Value.ToString(),
                                        Event = work.Cells[row, 4].Value.ToString(),
                                    });
                                }
                            }
                            return zones;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("获取分区明细失败");
            }
        }
    }
}
