Пример #1
0
        public bool DataDownEventStart(string loginKey, ref ErrorInfo err, int downID)
        {
            if (!UserCheckFunctioAuthority(loginKey, ref err, MethodBase.GetCurrentMethod()))
            {
                return(false);
            }
            GlobalUser gu = Global.GetUser(loginKey);

            using (DBEntities db = new DBEntities())
            {
                var down = db.YL_DATA_DOWN.SingleOrDefault(x => x.ID == downID);


                // string s = GetSelectScript(down.SELECT_SCRIPT);
                //获得相对应的参数
                //GetMonthPath(ref belongMonth, ref path, down.CREATE_TYPE, down.CREATE_TABLE_NAME);

                string tableName = Fun.ReplaceDataTime(down.CREATE_TABLE_NAME, DateTime.Now, loginKey);

                var eventEnt = new YL_DATA_DOWN_EVENT();
                eventEnt.ID             = Fun.GetSeqID <YL_DATA_DOWN_EVENT>();
                eventEnt.TARGET_NAME    = tableName;
                eventEnt.ALL_NUM        = 0;
                eventEnt.LAST_MONTH_NUM = 0;
                eventEnt.PATH           = down.TO_PATH;
                eventEnt.DATA_DOWN_ID   = downID;
                eventEnt.START_TIME     = DateTime.Now;
                eventEnt.USER_ID        = gu.UserId;
                db.YL_DATA_DOWN_EVENT.Add(eventEnt);
                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    err.IsError = true;
                    err.Message = Fun.GetDbEntityErrMess(dbEx);
                    return(false);
                }


                string createScript = "";
                if (!string.IsNullOrEmpty(down.CREATE_SCRIPT))
                {
                    createScript = down.CREATE_SCRIPT.Replace("{@TABLE_NAME}", eventEnt.TARGET_NAME);
                }

                foreach (var to in down.YL_DATA_DOWN_TO.ToList())
                {
                    var toServer = to.YL_DB_SERVER;
                    #region 在目标服务器上创建表
                    try
                    {
                        try
                        {
                            DbServerNonQuery(toServer.ID, "drop table " + eventEnt.TARGET_NAME);
                        }
                        catch (Exception e)
                        {
                        }
                        DbServerNonQuery(toServer.ID, createScript);
                    }
                    catch (Exception e)
                    {
                        err.IsError = true;
                        err.Message = "在服务器【" + toServer.NICKNAME + "】上建表失败:\r\nSQL:" + createScript + "\r\n" + e.Message;
                        return(false);
                    }
                    #endregion
                    foreach (var from in down.YL_DB_SERVER.ToList())
                    {
                        YL_DATA_DOWN_TASK task = new YL_DATA_DOWN_TASK();
                        task.ID            = Fun.GetSeqID <YL_DATA_DOWN_TASK>();
                        task.NAME          = eventEnt.TARGET_NAME;
                        task.SELECT_SCRIPT = down.SELECT_SCRIPT;
                        task.EVENT_ID      = eventEnt.ID;
                        #region 替换SELECT_SCRIPT
                        foreach (var replace in JSON.EncodeToEntity <IList <KV> >(to.REPLACE_STR))
                        {
                            task.SELECT_SCRIPT = task.SELECT_SCRIPT.Replace(replace.K, replace.V);
                        }
                        task.SELECT_SCRIPT = Fun.ReplaceDataTime(task.SELECT_SCRIPT, DateTime.Now, loginKey);
                        #endregion

                        #region 生成@[00-99]
                        if (task.SELECT_SCRIPT.IndexOf("@") != -1)
                        {
                            task.SELECT_SCRIPT = Fun.GetSelectScript(task.SELECT_SCRIPT);
                        }
                        #endregion

                        //设置存放路径
                        task.TO_PATH = Fun.ReplaceDataTime(down.TO_PATH, DateTime.Now, loginKey);

                        int thisAllNum = 0;

                        task.ALL_NUM = thisAllNum;
                        int upMonthNum = 0;

                        task.LAST_MONTH_NUM   = upMonthNum;
                        task.SELECT_DB_SERVER = from.ID;
                        task.SELECT_SERVER    = string.Format("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SID={2})))", from.IP, from.PORT, from.DBNAME);
                        task.SELECT_UID       = from.UID;
                        task.SELECT_PWD       = from.PASSWORD;
                        task.EVENT_TYPE       = 1;
                        if (down.SUCC_SCRIPT != null)
                        {
                            task.SUCC_SCRIPT = down.SUCC_SCRIPT.Replace("{@TABLE_NAME}", eventEnt.TARGET_NAME);
                        }
                        task.TO_DB_SERVER  = toServer.ID;
                        task.TO_SERVER     = toServer.DBNAME;
                        task.TO_UID        = toServer.UID;
                        task.TO_PWD        = toServer.PASSWORD;
                        task.CREATE_SCRIPT = createScript;
                        task.ERROR_NUM     = 0;
                        task.STATUS        = "等待";
                        task.PAGE_SIZE     = down.PAGE_SIZE;
                        task.SPLIT_STR     = down.SPLIT_STR;
                        task.IS_CANCEL     = 0;
                        task.ORDER_NUM     = task.ID;
                        db.YL_DATA_DOWN_TASK.Add(task);
                    }
                }
                db.SaveChanges();
                return(true);
            }
        }
Пример #2
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                logger.Info("启动自动下载数据 开始");
                var downID = Convert.ToInt32(context.JobDetail.Key.Name.Replace("DownDataJob_", ""));
                logger.Info(string.Format("启动自动下载数据 数据ID{0}", downID));


                var down = FunSqlToClass.ClassSingle <DATA_DOWN>(
                    ConfigurationManager.AppSettings["dbType"],
                    ConfigurationManager.AppSettings["dbConnSt"],
                    "where ID = " + downID,
                    ConfigurationManager.AppSettings["dbPrefix"]
                    );

                string tableName = ProServer.Fun.ReplaceDataTime(down.CREATE_TABLE_NAME, DateTime.Now);
                var    eventEnt  = new DATA_DOWN_EVENT();
                eventEnt.ID = FunSqlToClass.GetSeqID <DATA_DOWN_EVENT>(
                    ConfigurationManager.AppSettings["dbType"],
                    ConfigurationManager.AppSettings["dbConnSt"],
                    ConfigurationManager.AppSettings["dbPrefix"]
                    );
                eventEnt.TARGET_NAME    = tableName;
                eventEnt.ALL_NUM        = 0;
                eventEnt.LAST_MONTH_NUM = 0;
                eventEnt.PATH           = down.TO_PATH;
                eventEnt.DATA_DOWN_ID   = downID;
                eventEnt.START_TIME     = DateTime.Now;

                FunSqlToClass.Save <DATA_DOWN_EVENT>(
                    ConfigurationManager.AppSettings["dbType"],
                    ConfigurationManager.AppSettings["dbConnSt"],
                    eventEnt,
                    ConfigurationManager.AppSettings["dbPrefix"]
                    );

                string createScript = "";
                if (!string.IsNullOrEmpty(down.CREATE_SCRIPT))
                {
                    createScript = down.CREATE_SCRIPT.Replace("{@TABLE_NAME}", eventEnt.TARGET_NAME);
                }

                var allDataDownTo = FunSqlToClass.SqlToList <ProInterface.Models.DATA_DOWN_TO>(string.Format("select * from YL_DATA_DOWN_TO where DATA_DOWN_ID='{0}'", downID), ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"]);

                foreach (var to in allDataDownTo)
                {
                    var toServer = FunSqlToClass.ClassSingle <DB_SERVER>(
                        ConfigurationManager.AppSettings["dbType"],
                        ConfigurationManager.AppSettings["dbConnSt"],
                        "where ID = " + to.DB_SERVER_ID,
                        ConfigurationManager.AppSettings["dbPrefix"]
                        );

                    #region 在目标服务器上创建表
                    var db = new ProServer.ScriptExt();
                    try
                    {
                        try
                        {
                            db.execute("drop table " + eventEnt.TARGET_NAME, toServer);
                        }
                        catch (Exception e)
                        {
                        }
                        db.execute(createScript, toServer);
                    }
                    catch (Exception e)
                    {
                        logger.Info(string.Format("在服务器【{0}】上建表失败:\r\nSQL:{1}\r\n{2}", toServer.NICKNAME, createScript, e.Message));
                    }
                    #endregion


                    var allDataDownForm = FunSqlToClass.SqlToList <ProInterface.Models.DB_SERVER>(string.Format("select a.* from YL_DB_SERVER a,YL_DATA_DOWN_FORM b where a.ID=b.DB_SERVER_ID and b.DATA_DOWN_ID='{0}'", downID), ConfigurationManager.AppSettings["dbType"], ConfigurationManager.AppSettings["dbConnSt"]);
                    foreach (var from in allDataDownForm)
                    {
                        DATA_DOWN_TASK task = new DATA_DOWN_TASK();
                        task.ID = FunSqlToClass.GetSeqID <DATA_DOWN_EVENT>(
                            ConfigurationManager.AppSettings["dbType"],
                            ConfigurationManager.AppSettings["dbConnSt"],
                            ConfigurationManager.AppSettings["dbPrefix"]
                            );
                        task.NAME          = eventEnt.TARGET_NAME;
                        task.SELECT_SCRIPT = down.SELECT_SCRIPT;
                        task.EVENT_ID      = eventEnt.ID;
                        #region 替换SELECT_SCRIPT
                        foreach (var replace in JSON.EncodeToEntity <IList <KV> >(to.REPLACE_STR))
                        {
                            task.SELECT_SCRIPT = task.SELECT_SCRIPT.Replace(replace.K, replace.V);
                        }
                        task.SELECT_SCRIPT = ProServer.Fun.ReplaceDataTime(task.SELECT_SCRIPT, DateTime.Now);
                        #endregion

                        #region 生成@[00-99]
                        if (task.SELECT_SCRIPT.IndexOf("@") != -1)
                        {
                            task.SELECT_SCRIPT = Fun.GetSelectScript(task.SELECT_SCRIPT);
                        }
                        #endregion

                        //设置存放路径
                        task.TO_PATH = ProServer.Fun.ReplaceDataTime(down.TO_PATH, DateTime.Now);

                        int thisAllNum = 0;

                        task.ALL_NUM = thisAllNum;
                        int upMonthNum = 0;

                        task.LAST_MONTH_NUM   = upMonthNum;
                        task.SELECT_DB_SERVER = from.ID;
                        task.SELECT_SERVER    = string.Format("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1})))(CONNECT_DATA=(SERVER=DEDICATED)(SID={2})))", from.IP, from.PORT, from.DBNAME);
                        task.SELECT_UID       = from.UID;
                        task.SELECT_PWD       = from.PASSWORD;
                        task.EVENT_TYPE       = 1;
                        if (down.SUCC_SCRIPT != null)
                        {
                            task.SUCC_SCRIPT = down.SUCC_SCRIPT.Replace("{@TABLE_NAME}", eventEnt.TARGET_NAME);
                        }
                        task.TO_DB_SERVER  = toServer.ID;
                        task.TO_SERVER     = toServer.DBNAME;
                        task.TO_UID        = toServer.UID;
                        task.TO_PWD        = toServer.PASSWORD;
                        task.CREATE_SCRIPT = createScript;
                        task.ERROR_NUM     = 0;
                        task.STATUS        = "等待";
                        task.PAGE_SIZE     = down.PAGE_SIZE;
                        task.SPLIT_STR     = down.SPLIT_STR;
                        task.IS_CANCEL     = 0;
                        task.ORDER_NUM     = task.ID;

                        FunSqlToClass.Save <DATA_DOWN_TASK>(
                            ConfigurationManager.AppSettings["dbType"],
                            ConfigurationManager.AppSettings["dbConnSt"],
                            task,
                            ConfigurationManager.AppSettings["dbPrefix"]
                            );
                    }
                }

                logger.Info("启动自动下载数据 结束");
            }
            catch (Exception ex)
            {
                logger.Error("启动自动下载数据 运行异常", ex);
            }
        }