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
{
    /// <summary>
    /// 临床科室单元核算表
    /// </summary>
    public class PerSheetDataReadDeptAccounting : 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(PerDataAccountBaisc.RowNumber), r}
                };

                foreach (var item in ExcelReadConfig.AccountBaisc)
                {
                    if (NopiSevice.TryGetPoint(perHeader, item.Description, out int point))
                    {
                        object @object = row.GetCell(point).GetValue();
                        if (item.IsNumber)
                            @object = ConvertHelper.To<decimal?>(@object);

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

                var baisc = JsonHelper.Deserialize<PerDataAccountBaisc>(dic);

                if (!string.IsNullOrEmpty(baisc.UnitType))
                    dataList.Add(baisc);
            }

            return dataList;
        }
    }
}
