예제 #1
0
        public MogoMigrateView GetMogoMigrateView(long id)
        {
            if (id.ConvertToLong() == 10000)
            {
                var view = new MogoMigrateView {
                    UserTable = "User_User",
                    Key       = RuntimeContext.Current.WebsiteConfig.OpenApiSetting.Key
                                //  ProjectId = project.ProjectId.ToString()
                };
                return(view);
            }

            return(new MogoMigrateView());
        }
예제 #2
0
        public ServiceResult Migrate(MogoMigrateView view)
        {
            if (!HttpWeb.UserName.Equal("admin"))
            {
                return(ServiceResult.Failure("当前操作用户名非admin,不能进行该操作"));
            }

            if (view.MongoTableName.IsNullOrEmpty())
            {
                return(ServiceResult.Failure("Mongodb数据库不能为空"));
            }

            if (view.MongoConnectionString.IsNullOrEmpty())
            {
                return(ServiceResult.Failure("Mongodb链接字符串不能为空"));
            }

            var user = Resolve <IUserService>().GetSingle(HttpWeb.UserId);

            if (user.Status != Status.Normal)
            {
                return(ServiceResult.Failure("用户状态不正常,不能进行该操作"));
            }

            if (!user.UserName.Equal("admin"))
            {
                return(ServiceResult.Failure("当前操作用户名非admin,不能进行该操作"));
            }

            if (!view.UserTable.Equals("User_User"))
            {
                return(ServiceResult.Failure("用户数据表填写出错,不能进行该操作"));
            }

            //if (!view.ProjectId.Equals(HttpWeb.Token.ProjectId.ToString())) {
            //    return ServiceResult.Failure("项目Id填写错误,不能进行该操作");
            //}

            if (!view.Key.Equals(RuntimeContext.Current.WebsiteConfig.OpenApiSetting.Key))
            {
                return(ServiceResult.Failure("秘钥填写错误,不能进行该操作"));
            }

            var connection = new MongoDbConnection {
                ConnectionString = view.MongoConnectionString,
                Database         = view.MongoTableName
            };
            var types = GetMongoEntityTypes();

            foreach (var type in types)
            {
                //// 使用Mongodb的上下文链接字符串
                //MongoRepositoryConnection.MongoDbConnectionContext = connection;
                //// 读取需要迁移数据库中所有的数据
                //var sourceResult = DynamicService.ResolveMethod(type.Name, "GetList");
                //if (!sourceResult.Item1.Succeeded) {
                //    return ServiceResult.Failure($"表{type.Name}数据获取失败:" + sourceResult.Item2.ToString());
                //}

                //// 切换数据库,当前配置数据库
                //MongoRepositoryConnection.MongoDbConnectionContext = null;
                //// 批量插入数据库中
                //var addResult = DynamicService.ResolveMethod(type.Name, "AddMany", sourceResult.Item2);
                //if (!addResult.Item1.Succeeded) {
                //    return ServiceResult.Failure($"表{type.Name}数据添加失败:" + sourceResult.Item2.ToString());
                //}
            }

            Resolve <ITableService>().Log("数据迁移成功");
            Resolve <ISmsService>().SendRaw(Resolve <ISmsService>().OpenMobile(), "您的数据已迁移成功,请悉知。");
            return(ServiceResult.Success);
        }