CSVPositionReader.cs 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

using log4net;
using OnlineStore.Common;
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 CSVPositionReader<T> : CSVReaderBase where T : PostionBase
    {
        /// <summary>
        /// 所有的位置集合,key=位置
        /// </summary>
        public static Dictionary<string, Dictionary<string, T>> AllPositionMap = new Dictionary<string, Dictionary<string, T>>();



        private static List<string> hasReadFileList = new List<string>();

        /// <summary>
        /// 重新加载配置,会删除之前的所有信息,重新读取
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Dictionary<String, T> ReloadCSVFile(string filePath)
        {
            AllPositionMap = new Dictionary<string, Dictionary<string, T>>();
            hasReadFileList = new List<string>();

            return AddCSVFile(filePath);
        }
        private static string GetTName()
        {
            Type type = typeof(T);
            return type.Name;
        }
        public static List<T> getPositionList()
        {
            string name = GetTName();
            if (AllPositionMap.ContainsKey(name))
            {
                List<T> list = new List<T>(AllPositionMap[name].Values);
                return list;
            }
            return new List<T>();
        }
        /// <summary>
        /// 添加一个csv文件的数据到位置集合中
        /// </summary>
        /// <param name="filePath">cvs文件路径+文件名</param>  
        /// <returns></returns>
        public static Dictionary<String, T> AddCSVFile(string filePath, int storeId = 0)
        {
            if (hasReadFileList.Contains(filePath))
            {
                LogUtil.error("文件" + filePath + "已经加载过,直接返回null");
                return null;
            }
            Type type = typeof(T);
            string tname = GetTName();
            Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(T));
            if (proTitleMap.Count <= 4)
            {
                LogUtil.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(filePath, 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 PositionNum = "";
                            foreach (string key in cvsTitleList)
                            {
                                int titIndex = titleIndex[key];
                                string value = array[titIndex].Trim();
                                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("PositionNum"))
                                {
                                    PositionNum = value;
                                }
                                listIndex++;
                            }
                            if (storeId > 0 && ((T)bllIns).StoreId <= 0)
                            {
                                ((T)bllIns).StoreId = storeId;
                            }
                            result.Add(PositionNum, (T)bllIns);
                            if (!AllPositionMap.ContainsKey(tname))
                            {
                                AllPositionMap.Add(tname, new Dictionary<string, T>());
                            }
                            if (AllPositionMap[tname].ContainsKey(PositionNum))
                            {
                                throw new PositionAlreadyExistingExection("仓位:" + PositionNum + "已存在!");
                            }

                            AllPositionMap[tname].Add(PositionNum, (T)bllIns);
                        }
                        else
                        {
                            LogUtil.error("读取" + filePath + ",index=" + index + ",数据格式不匹配!,line=" + line);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtil.debug("CSV 读取行【" + line + "】行转换失败");
                    }
                }
                index++;
            }
            return result;
        }
        /// <summary>
        /// 根据Key获得一个位置信息
        /// </summary> 
        public static T GetPositon(string positionNum)
        {
            if (positionNum == null)
            {
                return null;
            }
            PostionBase result = null;
            string tname = GetTName();
            if (AllPositionMap.ContainsKey(tname))
            {
                if (AllPositionMap[tname].ContainsKey(positionNum))
                {
                    result = AllPositionMap[tname][positionNum];
                    return (T)result;
                }
            }

            return null;

        }
        /// <summary>
        /// 获取同尺寸库位的所有位置信息
        /// </summary>
        /// <param name="positionNum"></param>
        /// <returns></returns>
        public static List<T> GetSameSizePositons(string positionNum)
        {
            if (positionNum == null)
            {
                return null;
            }
            string tname = GetTName();
            if (AllPositionMap.ContainsKey(tname))
            {
                if (AllPositionMap[tname].ContainsKey(positionNum))
                {
                    var result = AllPositionMap[tname][positionNum];
                    if (result == null) return null;
                    int idx = AllPositionMap[tname].Values.ToList().FindIndex(s => s.PositionNum.Equals(positionNum) && s.BagWidth == result.BagWidth && s.BagHigh == result.BagHigh);//s => s.BagWidth == result.BagWidth && s.BagHigh == result.BagHigh
                    List<T> allPos = new List<T>();
                    for (int i = idx; i < AllPositionMap[tname].Values.Count; i++)
                    {
                        allPos.Add(AllPositionMap[tname].Values.ToArray()[i]);
                    }
                    return allPos;
                }
            }
            return null;

        }
        public static bool SavePostion(string filePath, T position)
        {
            Type type = typeof(T);
            string tName = GetTName();
            Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(T));
            if (proTitleMap.Count <= 4)
            {
                LogUtil.error(typeof(T).ToString() + "只读取到" + proTitleMap.Count + "个属性");
            }
            List<string> cvsTitleList = new List<string>(proTitleMap.Values);
            List<string> propertyList = new List<string>(proTitleMap.Keys);
            int positionNumIndex = propertyList.IndexOf("PositionNum");
            int csvIndex = -1;
            string[] lines = ReadCSVFile(filePath);
            int index = 0;
            Dictionary<string, int> titleIndex = new Dictionary<string, int>();

            foreach (var line in lines)
            {
                var array = line.Split(',');
                if (index == 0)
                {
                    titleIndex = GetTitleIndex(filePath, line, cvsTitleList);
                }
                else
                {
                    if (array.Length == titleIndex.Count)
                    {
                        if (csvIndex < 0)
                        {
                            csvIndex = titleIndex[cvsTitleList[positionNumIndex]];
                        }
                        string value = array[csvIndex];
                        if (value.Equals(position.PositionNum))
                        {

                            if (AllPositionMap.ContainsKey(tName))
                            {
                                //更新缓存
                                AllPositionMap[tName].Remove(position.PositionNum);
                                AllPositionMap[tName].Add(position.PositionNum, position);
                                string newValue = PostionToString(position, titleIndex, proTitleMap);
                                lines[index] = newValue;
                                return WriteCSVFile(filePath, lines);
                            }
                        }
                    }
                }
                index++;
            }
            return true;
        }

        private static string PostionToString(PostionBase position, Dictionary<string, int> titleIndex, Dictionary<string, string> proTitleMap)
        {
            //取得属性集合
            PropertyInfo[] props = typeof(T).GetProperties();
            List<string> cvsTitleList = new List<string>(proTitleMap.Values);
            List<string> propertyList = new List<string>(proTitleMap.Keys);
            String[] array = new String[titleIndex.Count];
            foreach (string proName in proTitleMap.Keys)
            {
                PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
                if (prop != null)
                {//如果属性存在
                    string value = prop.GetValue(position, null).ToString();
                    int index = titleIndex[proTitleMap[proName]];
                    array[index] = value;
                }
            }

            string newStr = "";
            foreach (string str in array)
            {
                if (newStr.Equals(""))
                {
                    newStr = str;
                }
                else
                {
                    newStr = newStr + Spilt_Char + str;
                }
            }
            return newStr;
        }
        public static bool UpdatePostion(string filePath, int storeIndex)
        {
            Type type = typeof(T);

            Dictionary<string, string> proTitleMap = getProAttributeMap(typeof(T));
            if (proTitleMap.Count <= 4)
            {
                LogUtil.error(typeof(T).ToString() + "只读取到" + proTitleMap.Count + "个属性");
            }
            List<string> cvsTitleList = new List<string>(proTitleMap.Values);
            List<string> propertyList = new List<string>(proTitleMap.Keys);
            int positionNumIndex = propertyList.IndexOf("PositionNum");
            int posIdIndex = -1;

            string[] lines = ReadCSVFile(filePath);
            int index = 0;
            Dictionary<string, int> titleIndex = new Dictionary<string, int>();

            foreach (var line in lines)
            {
                var array = line.Split(',');
                if (index == 0)
                {
                    titleIndex = GetTitleIndex(filePath, line, cvsTitleList);
                }
                else
                {
                    if (array.Length >= titleIndex.Count)
                    {
                        if (posIdIndex < 0)
                        {
                            posIdIndex = titleIndex[cvsTitleList[positionNumIndex]];
                        }

                        string PosId = array[posIdIndex];
                        PostionBase position = (GetPositon(PosId));
                        if (position != null)
                        {
                            try
                            {
                                // 仓位命名: 4D01020304
                                //第1和第2位表示楼层(4D)
                                //第3和第4位表示料仓(01) 01 - 18为流水线料仓, 19 - 24为包装料仓
                                //第5和第6位表示列(02)
                                //第7和第8位表示行(03)
                                //第9和第10位表示隔板位置(04)
                                //例如: 4D12010124 表示4楼12号料仓第1列第1行架子上的第24个隔板位置
                                //4D19050208 表示4楼19号料仓(包装料仓)第5列第2行架子上的第8个隔板位置

                                string oldPosId = position.PositionNum;//08#AC1_18_3_18
                                string newPosId = "";
                                int jIndex = oldPosId.IndexOf("#");
                                string louceng = "4D";
                                string storeId = storeIndex.ToString().PadLeft(2, '0');
                                if (jIndex > 0)
                                {

                                    string[] hengStr = oldPosId.Split('_');
                                    int lie = 0;
                                    int hang = 0;
                                    int p = 0;
                                    if (hengStr.Length == 4)
                                    {
                                        lie = Convert.ToInt32(hengStr[1]);
                                        hang = Convert.ToInt32(hengStr[2]);
                                        p = Convert.ToInt32(hengStr[3]);
                                    }
                                    string hangStr = "AA";
                                    if (hang.Equals(1))
                                    {
                                        hangStr = "AA";
                                    }
                                    else if (hang.Equals(2))
                                    {
                                        hangStr = "BB";
                                    }
                                    else if (hang.Equals(3))
                                    {
                                        hangStr = "CC";
                                    }
                                    else if (hang.Equals(4))
                                    {
                                        hangStr = "DD";
                                    }
                                    else if (hang.Equals(5))
                                    {
                                        hangStr = "EE";
                                    }
                                    else if (hang.Equals(6))
                                    {
                                        hangStr = "FF";
                                    }
                                    lie++;
                                    newPosId = louceng + storeId + lie.ToString().PadLeft(2, '0') + hangStr + p.ToString().PadLeft(4, '0');
                                }
                                else
                                {
                                    newPosId = louceng + storeId + oldPosId.Substring(4, oldPosId.Length - 4);
                                }
                                position.PositionNum = newPosId;
                                //   position.StoreId = storeIndex;
                                string newValue = PostionToString(position, titleIndex, proTitleMap);
                                lines[index] = newValue;
                            }
                            catch (Exception ex)
                            {
                                LogUtil.error("更新【" + filePath + "】位置出错【" + line + "】" + ex.ToString());
                            }
                        }
                    }
                }
                index++;
            }
            return WriteCSVFile(filePath, lines);

        }
    }


}