private bool SaveEntity(DBTStkMinuteEntity entity, string code)
        {
            if (entity == null)
            {
                return(false);
            }

            entity.Code = code;
            entity.CalculateAverage();
            if (entity.IsDataValid && CheckTable <DBTStkMinuteEntity>())
            {
                var stk_minute = new DBTStkMinuteEntity(entity.Code, entity.Time, accessor);
                if (stk_minute.Fresh() > 0 && entity.Equals(stk_minute))
                {
                    return(false);
                }
                stk_minute.Copy(entity, "ID");
                stk_minute.GenerateID();
                if (stk_minute.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 SaveEntity(TimeLine <ItemInfoEntity> line)
        {
            if (CheckTable <DBTStkMinuteEntity>())
            {
                var entity = new DBTStkMinuteEntity(accessor);
                entity.Code = line.Value.Code;

                if (line is TengxunMinuteLine)
                {
                    var tengxunline = line as TengxunMinuteLine;
                    entity.Time      = new DateTime(tengxunline.CloseTime.Year, tengxunline.CloseTime.Month, tengxunline.CloseTime.Day, tengxunline.CloseTime.Hour, tengxunline.CloseTime.Minute, 0);
                    entity.Open      = tengxunline.Open;
                    entity.Close     = tengxunline.Close;
                    entity.High      = tengxunline.High;
                    entity.Low       = tengxunline.Low;
                    entity.Average   = tengxunline.Average;
                    entity.VolAmount = tengxunline.VolAmount;
                    entity.VolMoney  = tengxunline.VolMoney;
                }

                entity.GenerateID();
                entity.Save();
            }
        }
        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);
        }