using NPOI.SS.UserModel;
using System;
using System.ComponentModel;
using DeviceRepairAndOptimization.Common.NpoiExtend;
using System.Reflection;

namespace DeviceRepairAndOptimization.Biz.AM.Templates
{
    public class FM_P0075_17
    {

        /// <summary>
        /// 设备名称Equipment name:
        /// </summary>
        [Description("设备名称Equipment name:")]
        public string EquipmentName = "[A,1]";

        /// <summary>
        /// 设备编号Equipment number:
        /// </summary>
        [Description("设备编号Equipment number:")]
        public string EquipmentID = "[A,2]";

        /// <summary>
        /// 安装地点Installation site：
        /// </summary>
        [Description("安装地点Installation site：")]
        public string InstallationSite = "[I,1]";

        /// <summary>
        /// 设备型号Equipment model：
        /// </summary>
        [Description("设备型号Equipment model：")]
        public string EquipmentTs = "[I,2]";

        /// <summary>
        /// 序号
        /// </summary>
        [Description("序号")]
        public string OrderNumber = "[A,4]";

        /// <summary>
        /// 异常备注：
        /// </summary>
        [Description("异常备注：")]
        public string ExceptionContent = "[A,14]";

        /// <summary>
        /// 保养人签字
        /// </summary>
        [Description("保养人签字")]
        public string Signature = "[K,14]";

        /// <summary>
        /// 保养日期
        /// </summary>
        [Description("保养日期")]
        public string MaintenanceDate = "[K,15]";

        public ArgumentException CheckCurrentSheet(ISheet sheet)
        {
            try
            {
                var type = this.GetType();
                FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (var item in fields)
                {
                    string Description = string.Empty;
                    object[] attrs = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (null != attrs && attrs.Length > 0)
                    {
                        DescriptionAttribute description = (DescriptionAttribute)attrs[0];
                        Description = description.Description;
                    }

                    string field = item.GetValue(this)?.ToString();
                    if (item.Name == "ExceptionContent" || item.Name == "Signature" || item.Name == "MaintenanceDate")
                    {
                        ICell cell = sheet.LocateCellByAddress(field);
                        if (cell.CellType != CellType.String)
                        {
                            int RowIndex = cell.RowIndex;
                            bool HasError = true;
                            while (RowIndex < sheet.LastRowNum)
                            {
                                RowIndex++;
                                if (sheet.GetRow(RowIndex).Cells[cell.ColumnIndex].CellType == CellType.String
                                    && Description.Equals(sheet.GetRow(RowIndex).Cells[cell.ColumnIndex].StringCellValue, StringComparison.CurrentCultureIgnoreCase)
                                    )
                                {
                                    HasError = false;
                                    break;
                                }
                            }

                            if (HasError)
                            {
                                return new ArgumentException($"当前点检表格式不正确，操作失败！");
                            }
                        }
                    }
                    else
                    {
                        string CellValue = sheet.LocateCellByAddress(field).StringCellValue;
                        if (!Description.Equals(CellValue, StringComparison.CurrentCultureIgnoreCase))
                            return new ArgumentException($"当前点检表格式不正确，操作失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                return new ArgumentException(ex.Message);
            }

            return null;
        }
    }
}
