CSVProgramReader.cs 4.7 KB

using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
 

namespace OnlineStore.LoadCSVLibrary
{
    public class CSVProgramReader<T> : CSVReaderBase where T : DBLineProgram
    {
        /// <summary>
        /// 所有的位置集合,key=位置
        /// </summary>
        public static Dictionary<string, T> allProgramMap = new Dictionary<string, T>();

        /// <summary>
        /// 添加一个csv文件的数据到位置集合中
        /// </summary>
        /// <param name="filePath">cvs文件路径+文件名</param>  
        /// <returns></returns>
        public static Dictionary<String, T> AddCSVFile(string filePath)
        {
            allProgramMap = new Dictionary<string, T>();
            
            Type type = typeof(T);

            Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(T));
            if (proTitleMap.Count <= 4)
            {
                LOGGER.Error(typeof(T).ToString() + "只读取到" + proTitleMap.Count + "个属性");
            }
            List<string> cvsTitleList = new List<string>(proTitleMap.Values);
            List<string> propertyList = new List<string>(proTitleMap.Keys);

            Dictionary<String, T> result = new Dictionary<String, T>();

            string[] lines = ReadCSVFile(filePath);
            int index = 0;
            Dictionary<string, int> titleIndex = new Dictionary<string, int>();
            foreach (var line in lines)
            {
                var array = line.Split(Spilt_Char);
                if (index == 0)
                {
                    titleIndex = GetTitleIndex(line, cvsTitleList);
                }
                else
                {
                    try
                    {
                        if (array.Length >= titleIndex.Count)
                        {
                            if (array.Length > 0 && array[0].Equals(""))
                            {
                                continue;
                            }
                            var bllIns = type.Assembly.CreateInstance(type.FullName);
                            //取得属性集合
                            PropertyInfo[] props = type.GetProperties();

                            int listIndex = 0;
                            string programName = "";
                            foreach (string key in cvsTitleList)
                            {
                                int titIndex = titleIndex[key];
                                string value = array[titIndex];
                                string proName = propertyList[listIndex];
                                PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
                                if (prop != null)
                                {//如果属性存在
                                    prop.SetValue(bllIns, Convert.ChangeType(value, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
                                }
                                if (proName.Equals("Programname"))
                                {
                                    programName = value;
                                }
                                listIndex++;
                            }
                            result.Add(programName, (T)bllIns);
                            if (allProgramMap.ContainsKey(programName))
                            {
                                throw new PositionAlreadyExistingExection("仓位:" + programName + "已存在!");
                            }
                            allProgramMap.Add(programName, (T)bllIns);
                        }
                        else
                        {
                            LOGGER.Error("读取csv,index=" + index + ",数据格式不匹配!,line=" + line);
                        }
                    }
                    catch (Exception ex)
                    {
                        LOGGER.Debug("CSV 读取行【" + line + "】行转换失败");
                    }
                }
                index++;
            }
            return result;
        }
        /// <summary>
        /// 根据Key获得一个位置信息
        /// </summary> 
        public static T GetProgram(string programName)
        {
            if (programName == null)
            {
                return null;
            }
            T result = null;
            if (allProgramMap.ContainsKey(programName))
            {
                result = allProgramMap[programName];
                return (T)result;
            }
            else
            {
                return null;
            }
        }
    }
}