using NPOI.SS.UserModel;
using Performance.DtoModels;
using Performance.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Performance.Services
{
    public class PerSheetDataReadClinicEmployee : IPerSheetDataRead
    {
        public PerSheetPoint Point => new PerSheetPoint
        {
            HeaderFirstRowNum = 1,
            HeaderLastRowNum = 1,
            HeaderFirstCellNum = 0,
            DataFirstRowNum = 2,
        };

        public List<IPerData> ReadData(ISheet sheet, List<PerHeader> perHeader)
        {
            List<IPerData> dataList = new List<IPerData>();

            for (int r = Point.DataFirstRowNum.Value; r < sheet.LastRowNum + 1; r++)
            {
                var row = sheet.GetRow(r);
                if (row == null) continue;

                Dictionary<string, object> dic = new Dictionary<string, object>
                {
                    { nameof(PerDataClinicEmployee.RowNumber), r}
                };

                foreach (var item in ExcelReadConfig.ClinicEmployee)
                {
                    if (NopiSevice.TryGetPoint(perHeader, item.Description, out int point))
                    {
                        //var cell = row.GetCell(point);
                        //if (cell == null) continue;
                        //// 获取单元格格式
                        //var xxs = cell.CellStyle.DataFormat;
                        //var dataFormat = RecognitionDataFormat.GetDataFormat(xxs);

                        object @object = row.GetCell(point).GetValue();
                        if (item.IsNumber)
                            @object = ConvertHelper.ToDecimalOrNull(@object);

                        if (dic.Keys.Contains(item.Field))
                            dic[item.Field] = @object;
                        else
                            dic.Add(item.Field, @object);
                    }
                }

                var employee = JsonHelper.Deserialize<PerDataClinicEmployee>(dic);

                if (!string.IsNullOrEmpty(employee.UnitType) && !string.IsNullOrEmpty(employee.DoctorName))
                    dataList.Add(employee);
            }

            return dataList;
        }
    }
}
