Exemple #1
0
 /// <summary>
 /// 修改数据库之后修改redis中相关记录
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="lamb"></param>
 public void UpdateRedis <T>(Expression <Func <T, bool> > lamb) where T : BaseModel, new()
 {
     try
     {
         List <T> list = GetList <T>(lamb);
         foreach (T t in list)
         {
             bool succ = RedisWriteExHelper.SetUpdate(typeof(T).Name, t.GUID, JsonConvert.SerializeObject(t));
             //RabbitMQMessage rabbitMQMessage = new RabbitMQMessage()
             //{
             //    MessageKey = typeof(T).Name,
             //    EntityGuid = t.GUID,
             //    MessageBody = JsonConvert.SerializeObject(t),
             //    OperTime = DateTime.Now,
             //    OperType = (int)OperTypeEnum.UpdateEntryInHash
             //};
             //RabbitMQTasks.MQTask(rabbitMQMessage);
         }
     }
     catch (Exception ex)
     {
         Log4Library.LogHelper.ErrorLog(ex.ToString());
         throw (ex);
     }
 }
        /// <summary>
        /// 将数据库数据插入到redis
        /// </summary>
        /// <param name="databaseTableList">所有基础表</param>
        private void ImportDataToRedis(List <DbTable> databaseTableList)
        {
            Task task = Task.Factory.StartNew(() =>
            {
                //try
                //{
                int dataTableCount = databaseTableList.Count;

                Assembly assembly           = Assembly.Load(@"XYHis.Model");
                Type[] classTableTypes      = assembly.GetTypes();           //所有的model类
                Assembly mapContextAssembly = Assembly.Load(@"XYHis.MapContext");
                Type[] mapContextTypes      = mapContextAssembly.GetTypes(); //所有的映射类
                XYHis.Framework.Services.DBServiceBase db = new XYHis.Framework.Services.DBServiceBase();
                RedisWriteExHelper redisExHelper          = new RedisWriteExHelper();
                int tableIndex  = 0;
                int recordCount = 0;
                DbConfiguration dbConfiguration = db.GetCurrentDBContext().DbConfiguration;//当前数据库上下文
                string tablename  = string.Empty;
                string tablename2 = string.Empty;
                foreach (DbTable dt in databaseTableList)
                {
                    //获取表记录大小
                    string sql  = string.Format("select count(*) from {0}", dt.Name);
                    recordCount = int.Parse(SqlHelper.ExecuteScalar(CommandType.Text, sql).ToString());//数据库记录数量
                    if (recordCount > 40000)
                    {
                        dataTableCount -= 1;
                        //MessageBox.Show("数据大于40000,暂不更新");
                        continue;
                    }
                    if (recordCount == 0)
                    {
                        dataTableCount -= 1;
                        //MessageBox.Show("数据大于40000,暂不更新");
                        continue;
                    }
                    tableIndex += 1;

                    var ss = DbRelationService.Instance.SetParentRelations(dt, databaseTableList);
                    string[] relationColumns = new string[dt.ParentRelations.Count];//表的所有外键
                    int k = 0;
                    foreach (DbRelation relation in dt.ParentRelations)
                    {
                        relationColumns[k] = relation.FK_Relation_Column;
                        k++;
                    }

                    List <Type> classTypeList = classTableTypes.Where(t => t.Name.ToUpper() == dt.Name).ToList();                    //model类
                    List <Type> mapTypeList   = mapContextTypes.Where(t => t.Name.ToUpper().Contains(dt.Name + "MAPPING")).ToList(); //映射类

                    if (classTypeList.Count > 0 && mapTypeList.Count > 0)
                    {                                                     //model类和映射类都有的情况才进行导入
                        Type currentClssType = classTypeList[0];
                        RedisWriteHelper.KeyDelete(currentClssType.Name); //导入前先删除已有的数据
                        try
                        {
                            dbConfiguration.GetClass(currentClssType);
                        }
                        catch
                        {
                            dataTableCount -= 1;
                            continue;
                        }
                        Type dbServiceBaseType = db.GetType();
                        MethodInfo mi          = dbServiceBaseType.GetMethod("GetAllListUseInDataImport", new Type[] { }).MakeGenericMethod(currentClssType);
                        object obj             = mi.Invoke(db, null);
                        Type redisExHelperType = redisExHelper.GetType();
                        mi = redisExHelperType.GetMethod("ImportDataToSet").MakeGenericMethod(currentClssType);
                        mi.Invoke(redisExHelper, new object[] { currentClssType.Name, obj, relationColumns, recordCount });
                        tablename2 = currentClssType.Name;
                        if (tableIndex <= 1)
                        {
                            tablename = currentClssType.Name;
                        }
                    }
                    else
                    {
                        dataTableCount -= 1;
                    }
                    if (tableIndex <= 1)
                    {
                        ThreadPool.QueueUserWorkItem(delegate
                        {
                            SynchronizationContext.SetSynchronizationContext(new
                                                                             DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                            SynchronizationContext.Current.Post(pl =>
                            {
                                //总数
                                Msg.Text += "【更新总数】 :" + dataTableCount.ToString() + " 张表信息" + "\n";
                                //this.allcountText.Text = dataTableCount.ToString() + " ";
                                //更新数
                                Msg.Text += "【已更新数】 :" + tableIndex.ToString() + " 张表信息" + "\n";
                                //this.currcount.Text = tableIndex.ToString() + " ";
                                //成功数
                                Msg.Text += "【状态信息】 :" + tablename2.ToString() + " " + "共 " + recordCount + " 行记录更新" + "\n";
                                //this.succcount.Text = tablename2.ToString() + " " + "" + recordCount;
                                //更新百分比
                                this.Lith.Value = Convert.ToDouble(tableIndex) / Convert.ToDouble(dataTableCount) * 100;
                                //this.Lith.Value = Convert.ToDouble(tableIndex) / Convert.ToDouble(dataTableCount) * 100;
                                //更新百分比数字
                                Msg.Text += "【状态信息】 :" + "已更新 " + Math.Round(this.Lith.Value).ToString() + " %" + "\n";
                                //this.LithText.Text = Math.Round(this.Lith.Value).ToString() + " %";
                                //消息
                                Msg.Text += "【状态信息】 :" + string.Format("成功更新 [{0}] 表", tablename) + "\n";
                                //this.msg.Text = string.Format("成功更新 [{0}] 表", tablename);
                            }, null);
                        });
                    }
                    else if (tableIndex == dataTableCount)
                    {
                    }
                    if (tableIndex != 1)
                    {
                        ThreadPool.QueueUserWorkItem(delegate
                        {
                            SynchronizationContext.SetSynchronizationContext(new
                                                                             DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                            SynchronizationContext.Current.Post(pl =>
                            {
                                //总数
                                Msg.Text += "【更新总数】 :" + dataTableCount.ToString() + " 张表信息" + "\n";
                                //this.allcountText.Text = dataTableCount.ToString() + " ";
                                //更新数
                                Msg.Text += "【已更新数】 :" + tableIndex.ToString() + " 张表信息" + "\n";
                                //this.currcount.Text = tableIndex.ToString() + " ";
                                //成功数
                                Msg.Text += "【状态信息】 :" + tablename2.ToString() + " " + "共 " + recordCount + " 行记录更新" + "\n";
                                //this.succcount.Text = tablename2.ToString() + " " + "" + recordCount;
                                //更新百分比
                                this.Lith.Value = Convert.ToDouble(tableIndex) / Convert.ToDouble(dataTableCount) * 100;
                                //this.Lith.Value = Convert.ToDouble(tableIndex) / Convert.ToDouble(dataTableCount) * 100;
                                //更新百分比数字
                                Msg.Text += "【状态信息】 :" + "已更新 " + Math.Round(this.Lith.Value).ToString() + " %" + "\n";
                                //this.LithText.Text = Math.Round(this.Lith.Value).ToString() + " %";
                                //消息
                                Msg.Text += "【状态信息】 :" + string.Format("成功更新 [{0}] 表", tablename) + "\n";
                                //this.msg.Text = string.Format("成功更新 [{0}] 表", tablename);
                            }, null);
                        });
                    }
                }
            });
        }