ColumnMapping.cs 3.06 KB
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HH.Data.Excel.ColumnMapConfig
{
    /// <summary>
    /// 字段映射定义
    /// </summary>
    public class ColumnMapping
    {
        /// <summary>
        /// IP地址表 excel 文件配置 对应的实体类名称
        /// </summary>
        public static readonly Dictionary<string, string> ColumnMapIpExcel = new()
        {
            ["类型"] = "equipmentTypeId",
            ["IP地址"] = "iP",
            ["区域"] = "destinationArea",
            ["设备名称"] = "name",
            ["文件名称"] = "fileName",
        };

        // 2. 转换规则注册表
        public static readonly Dictionary<string, Func<object, object>> ValueConverters = new()
        {
            ["equipmentTypeId"] = v => GenderConverter(v.ToString()),
        };

        // 3. 具体转换方法实现
        private static object GenderConverter(string value)
        {
            value = value.Trim();
            if (Program._mainWindow == null) throw new ArgumentException("_mainWindow未初始化!");
            if (Program._mainWindow.EqTypeDataSource == null) throw new ArgumentException("设备类型的数据源值[EqTypeDataSource]为空!");
            //输送线类型返回  Program._mainWindow._eventHandlers.ccTypeId
            if (value == Program._mainWindow._eventHandlers.ccTypeCode)
            {
                return Program._mainWindow._eventHandlers.ccTypeId;
            }
            //只会有一个
            var currentKeyList = Program._mainWindow.EqTypeDataSource.Where(x => x.Value == value).ToList();
            if (currentKeyList.Count == 0)
            {
                throw new ArgumentException($"IP地址表.xlsx文件,列【类型】的值【{value}】设备类型解析转换失败,不存在于设备类型所选的复选框值!");
            }
            return currentKeyList[0].Key;
        }


        /// <summary>
        /// 输送线 excel文件配置 对应的实体类名称
        /// </summary>
        public static readonly Dictionary<string, string> ColumnMapCC = new()
        {
            ["变量名称"] = "Code",
            ["Name"] = "Name",
            ["PropType"] = "PropType",
            ["MonitorCompareValue"] = "MonitorCompareValue",

            ["数据类型"] = "DataType",
            ["DB块编号"] = "Address",

            ["注释"] = "MonitorFailure",

            ["Created"] = "Created",
            ["CreatedBy"] = "CreatedBy",

        };


        /// <summary>
        /// Display和输送线状态地址  excel文件配置 对应的实体类名称
        /// </summary>
        public static readonly Dictionary<string, string> ColumnMapDisplayAndLineCC = new()
        {
            ["Code"] = "Code",
            ["Name"] = "Name",
            ["DataType"] = "DataType",
            ["Address"] = "Address",

            ["PropType"] = "PropType",

            ["Created"] = "Created",
            ["CreatedBy"] = "CreatedBy",

        };
    }
}