private void RecordDailyInfo()
        {
            if (CurrentEntities.Count <= 0)
            {
                return;
            }
            if (!CheckTable <DBTStkDailyEntity>())
            {
                return;
            }

            CurrentEntities.Values.ToList().ForEach(info =>
            {
                var entity       = new DBTStkDailyEntity(info.Code, info.Time.Date, accessor);
                entity.Open      = info.TodayOpen;
                entity.Close     = info.Current;
                entity.High      = info.High;
                entity.Low       = info.Low;
                entity.VolAmount = info.GVolAmount;
                entity.VolMoney  = info.GVolMoney;

                entity.GenerateID();
                entity.Save();
            });
            CurrentEntities.Clear();
        }
        private bool SaveEntity(DBTStkDailyEntity entity, string code)
        {
            if (entity == null)
            {
                return(false);
            }

            entity.Code = code;
            if (entity.IsDataValid && CheckTable <DBTStkDailyEntity>())
            {
                var stk_daily = new DBTStkDailyEntity(entity.Code, entity.Date, accessor);
                if (stk_daily.Fresh() > 0 && entity.Equals(stk_daily))
                {
                    return(false);
                }
                stk_daily.Copy(entity, "ID");
                stk_daily.GenerateID();
                if (stk_daily.Save() > 0)
                {
                    //logger.Write(TYPE.INFO, string.Format("import data([CD:{0}][TDDT:{1}]) in (STK_DAILY_TD)", stk_daily.Code, stk_daily.Date.ToString("yyyy-MM-dd")));
                    return(true);
                }
            }
            return(false);
        }
 private void ProcessStockInfo(TengxunStockInfoEntity info)
 {
     if (info == null || !info.IsTodayData || info.TradeList.Count <= 0)
     {
         return;
     }
     if (!LastDayEntities.ContainsKey(info.Code))
     {
         var entity = accessor.RetrieveEntity <DBTStkDailyEntity>(DBTStkDailyEntity.LastDailyEntityCommand(info.Code, accessor)).FirstOrDefault();
         if (entity != null)
         {
             if (entity.Close != info.YesterdayClose)
             {
                 entity.Close = info.YesterdayClose;
                 entity.Save();
             }
             LastDayEntities.Add(entity.Code, entity);
         }
     }
     if (!CurrentEntities.ContainsKey(info.Code))
     {
         CurrentEntities.Add(info.Code, info);
     }
     else
     {
         CurrentEntities[info.Code] = info;
     }
     if (!lines.ContainsKey(info.Code))
     {
         lines.Add(info.Code, new TengxunMinuteLine(SaveEntity));
     }
     foreach (ItemInfoEntity item in info.TradeList)
     {
         if (!lines[info.Code].IsReady)
         {
             lines[info.Code].Initialize(item.Time);
         }
         if (filter4stk.Add(item))
         {
             lines[info.Code].Add(item);
         }
     }
 }
        private T ExportEntity <T>(string line, Template <TemplateUnit> template) where T : TableEntity
        {
            if (string.IsNullOrWhiteSpace(line) || template == null)
            {
                return(default(T));
            }
            var separator = Convert.ToChar(9);

            var values = line.Trim().Split(new char[] { separator });

            if (values == null || values.Length <= 0)
            {
                return(default(T));
            }

            object      data   = null;
            TableEntity entity = null;

            if (typeof(T) == typeof(DBTStkGeneralEntity))
            {
                entity = new DBTStkGeneralEntity();
            }
            if (typeof(T) == typeof(DBTStkDailyEntity))
            {
                entity = new DBTStkDailyEntity();
            }
            if (typeof(T) == typeof(DBTStkMinuteEntity))
            {
                entity = new DBTStkMinuteEntity();
            }
            if (typeof(T) == typeof(DBTIdxGeneralEntity))
            {
                entity = new DBTIdxGeneralEntity();
            }
            if (typeof(T) == typeof(DBTStkFavoriteEntity))
            {
                entity = new DBTStkFavoriteEntity();
            }


            if (entity != null)
            {
                try
                {
                    var temp = new Dictionary <string, string>();
                    template.FindAll(t => t.ColumnIndex >= 0).ForEach(itm =>
                    {
                        var value = (itm.ColumnIndex < values.Length) ? values[itm.ColumnIndex] : string.Empty;
                        if (temp.ContainsKey(itm.DBColumn))
                        {
                            temp[itm.DBColumn] = (string.IsNullOrEmpty(temp[itm.DBColumn])) ? value : temp[itm.DBColumn] + value;
                        }
                        else
                        {
                            temp.Add(itm.DBColumn, value);
                        }
                    });

                    foreach (string col in temp.Keys)
                    {
                        var column = entity.GetColumn(col);
                        if (column == null)
                        {
                            continue;
                        }
                        column.Value = TemplateUnit.ConvertVal(column.DataType, temp[col]);
                        entity.SetColumn(column);
                    }
                }
                catch (Exception err)
                {
                    logger.Write(TYPE.ERROR, string.Format("Template:{0} Line:{1}", template.Table.ToString(), line));
                    logger.Write(err);
                }

                data = entity;
            }

            return((data == null) ? default(T) : (T)data);
        }
        protected override RESULT Process(StringBuilder messager)
        {
            var dir = Config.Instance.INFO.ImportSetting.DirInfo;

            if (dir == null || !Directory.Exists(dir.FullName))
            {
                return(RESULT.OK);
            }
            var files = Directory.GetFiles(dir.FullName).Where(f => f.Trim().ToLower().EndsWith(".txt"));

            if (files == null || files.Count() <= 0)
            {
                return(RESULT.OK);
            }

            //Files Loading...
            var del = true; var count = 0; var total = 0;
            var file = new FileInfo(files.First());

            logger.Write(TYPE.INFO, string.Format("1.importing file({0})...", file.Name));
            try
            {
                using (var templates = Config.Instance.INFO.ImportSetting.Templates)
                {
                    Template <TemplateUnit> template = null;
                    using (var reader = new StreamReader(File.OpenRead(file.FullName), EncodingType.GetType(file.FullName)))
                    {
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if ((template = templates.MatchedTemplate) == null)
                            {
                                templates.Match(line);
                            }
                            else
                            {
                                if (template.Table == TABLES.NONE)
                                {
                                    logger.Write(TYPE.WARNING, string.Format("template({0}) is invalid.", template.Name));
                                    break;
                                }

                                switch (template.Table)
                                {
                                case TABLES.STK_GENERAL_M:
                                    if (SaveEntity(ExportEntity <DBTStkGeneralEntity>(line, template)))
                                    {
                                        count++;
                                    }
                                    total++;
                                    break;

                                case TABLES.STK_DAILY_TD:
                                    var code1 = DBTStkDailyEntity.FetchCode(file.Name);
                                    if (count <= 0)
                                    {
                                        if (!ClearDaily(code1))
                                        {
                                            continue;
                                        }
                                    }
                                    if (SaveEntity(ExportEntity <DBTStkDailyEntity>(line, template), code1))
                                    {
                                        count++;
                                    }
                                    total++;
                                    break;

                                case TABLES.STK_MINUTE_TD:
                                    var code2 = DBTStkMinuteEntity.FetchCode(file.Name);
                                    if (count <= 0)
                                    {
                                        if (!ClearMinute(code2))
                                        {
                                            continue;
                                        }
                                    }
                                    if (SaveEntity(ExportEntity <DBTStkMinuteEntity>(line, template), code2))
                                    {
                                        count++;
                                    }
                                    total++;
                                    break;

                                case TABLES.IDX_GENERAL_M:
                                    if (SaveEntity(ExportEntity <DBTIdxGeneralEntity>(line, template)))
                                    {
                                        count++;
                                    }
                                    total++;
                                    break;

                                case TABLES.STK_FAVORITE_M:
                                    if (SaveEntity(ExportEntity <DBTStkFavoriteEntity>(line, template)))
                                    {
                                        count++;
                                    }
                                    total++;
                                    break;
                                }
                            }
                        }
                    }

                    if (template != null)
                    {
                        switch (template.Table)
                        {
                        case TABLES.STK_DAILY_TD:
                            if (count > 0)
                            {
                                CalculateDaily(DBTStkDailyEntity.FetchCode(file.Name));
                            }
                            break;
                        }
                    }
                }
            }
            catch (IOException err)
            {
                logger.Write(TYPE.ERROR, string.Format("import file({0}) failed(IOException:{1}).", file.Name, err.Message));
                del = false;
            }
            catch (UnauthorizedAccessException err)
            {
                logger.Write(TYPE.ERROR, string.Format("import file({0}) failed(UnauthorizedAccessException:{1}).", file.Name, err.Message));
                del = false;
            }
            catch (Exception err)
            {
                logger.Write(err);
            }
            finally
            {
                logger.Write(TYPE.INFO, string.Format("2.imported data rows({0}/{1})", count, total));
                if (del)
                {
                    File.Delete(file.FullName);
                }
            }

            return(RESULT.OK);
        }