Example #1
0
        public ActionResult CommonImport(string importCode, string fileName)
        {
            var model = new ImportLogInput()
            {
                Code = importCode, ModuleCode = fileName
            };
            var list = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "在线导入", Value = "在线导入"
                },
                new SelectListItem()
                {
                    Text = "离线导入", Value = "离线导入"
                }
            };

            ViewBag.Offline = list;

            // var module = _modulesAppService.GetNavigation(a => a.Code == fileName);
            var format    = "xls,csv";
            var radioList = new List <string>();

            if (fileName != null)
            {
                foreach (var item in format.Split(','))
                {
                    radioList.Add(fileName + "." + item);
                }
                ;
            }
            ViewBag.Radio = radioList;
            return(View("Easyman.FwWeb.Views.Import.CommonImport", model));
        }
Example #2
0
        public void Add(ImportLogInput input)
        {
            //用户ID
            var userId = AbpSession.UserId;
            //获取外导表
            var impTb = _impTbRepository.FirstOrDefault(a => a.Code == input.Code);

            if (impTb == null)
            {
                "找不到表信息".ErrorMsg();
            }
            //获取上传文件
            var file = _filesRepository.FirstOrDefault(a => a.Id == input.FileId.Value);

            if (file == null || (file != null && !File.Exists(file.Path)))
            {
                "文件不存在!".ErrorMsg();
            }
            var dbServer = impTb.DbServer;//得到数据库对像
            //设置实例名称
            string caseTableName = impTb.EnTableName;

            switch (impTb.Rule)
            {
            case "1":
                caseTableName = impTb.EnTableName + "_" + DateTime.Now.ToString("yyMMdd");
                break;

            case "3":
                caseTableName = impTb.EnTableName + "_" + dbServer.User.ToString();
                break;

            case "4":
                caseTableName = impTb.EnTableName + "_" + input.Suffix.ToString();
                break;
            }

            //创建外导表实例
            var impTbCase = new ImpTbCase()
            {
                CaseTableName = caseTableName,
                ImpTbId       = impTb.Id
            };
            var impTbCaseId = _impTbCaseRepository.InsertAndGetId(impTbCase);
            //批次代码和批次名
            var module      = _moduleRepository.FirstOrDefault(a => a.Code == input.ModuleCode);
            var batchName   = module != null ? module.Name : file.Name;
            var batchHeader = DateTime.Now.ToString("yyMMdd");
            var batchNumber = 1;
            var implogNext  = _importLogRepository.GetAll().OrderByDescending(a => a.Id)
                              .FirstOrDefault(a => a.UserId == userId && a.Code.Contains(batchHeader));

            if (implogNext != null)
            {
                var batchArr = implogNext.Code.Split('-');
                if (batchArr.Count() > 0)
                {
                    if (batchArr[0] == batchHeader)
                    {
                        batchNumber = Int32.Parse(batchArr[1]) + 1;
                    }
                }
            }
            batchName += "-" + batchHeader;
            var batchCode = string.Format("{0}-{1}", batchHeader, batchNumber);
            //创建外导信息日志
            var impLog = new ImportLog()
            {
                FileId        = input.FileId.Value,
                FileName      = file.Name,
                ImpMode       = input.ImportMode,
                ImpTbId       = impTb.Id,
                ImpTbCaseId   = impTbCaseId,
                CaseTableName = caseTableName,
                Duration      = 0,
                Code          = batchCode,
                Name          = batchName,
                UserId        = userId.Value
            };
            var result = _importLogRepository.Insert(impLog);

            //数据导入操作
            if (result != null)
            {
                //创建表执行脚本
                var sqlScript = string.Format(impTb.Sql, result.CaseTableName);
                try
                {
                    #region 判断表是否存在
                    string    strSql    = Easyman.Common.DatabaseHelper.GetIsDataBaseTableSql(dbServer.DataCase, caseTableName, dbServer.User);
                    ErrorInfo er        = new ErrorInfo();
                    object    objReValu = _dbServerAppService.ExecuteScalar(dbServer.Id, strSql, ref er);
                    if (Convert.ToInt32(objReValu) <= 0)
                    {
                        _dbServerAppService.Execute(dbServer.Id, sqlScript);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("名称已由现有对象使用") <= -1)
                    {
                        //throw new ArgumentNullException(ex.Message);
                        throw new Exception("sql执行失败:" + ex.Message);
                    }
                }
                if (result.ImpMode == "离线导入") //0.判断导入模式是否离线,离线则进度任务调度中去,否则继续执行
                {
                    //进入离线模式
                    return;
                }
                //输入导入操作
                var importReult = ImportData(file.Path, impTb, batchCode, result.CaseTableName, dbServer.Id);
                if (importReult)
                {
                    File.Delete(file.Path);
                }

                //old code
                //var connectionString = string.Empty;
                //DatabaseType databaseType = DatabaseType.Oracle;
                //switch (dbType)
                //{
                //    default://默认oracle数据库
                //        connectionString = string.Format("Data Source={0}:{1}/{2};User Id={3};Password={4};Connect Timeout =200",
                //  dbServer.Ip, dbServer.Port, dbServer.DataCase, dbServer.User, dbServer.Password);
                //        break;
                //    case "db2":
                //        connectionString = dbServer.Port == null ?
                //            string.Format("Driver={IBM DB2 ODBC DRIVER};Server={0}:{1};DSN={2};UID={3};PWD={4};Protocol=TCPIP",
                //            dbServer.Ip, dbServer.Port, dbServer.DataCase, dbServer.User, dbServer.Password)
                //            : string.Format("Driver={IBM DB2 ODBC DRIVER};Server={0};DSN={1};UID={2};PWD={3};Protocol=TCPIP",
                //            dbServer.Ip, dbServer.DataCase, dbServer.User, dbServer.Password);
                //        databaseType = DatabaseType.Db2;
                //        break;
                //    case "mysql":
                //        connectionString = string.Format("Server={0};Port={1};Database={2];Uid={3};Pwd={4];",
                //            dbServer.Ip, dbServer.Port, dbServer.DataCase, dbServer.User, dbServer.Password);
                //        databaseType = DatabaseType.MySql;
                //        break;
                //    case "sqlserver":
                //        connectionString = dbServer.Port == null ?
                //            string.Format("server={0};uid={2};pwd={3};database={4}", dbServer.Ip, dbServer.User,
                //            dbServer.Password, dbServer.DataCase)
                //            : string.Format("server={0}:{1};uid={2};pwd={3};database={4}", dbServer.Ip, dbServer.Port,
                //            dbServer.User, dbServer.Password, dbServer.DataCase);
                //        databaseType = DatabaseType.SqlServer;
                //        break;
                //}
                //if (!string.IsNullOrEmpty(connectionString))
                //{
                //    //创建表执行脚本
                //    var sqlScript = string.Format(impTb.Sql, result.CaseTableName);
                //    //创建表操作
                //    CreateTable(connectionString, sqlScript, databaseType);
                //    //执行数据导入操作
                //    if (result.ImpMode == "离线导入") //0.判断导入模式是否离线,离线则进度任务调度中去,否则继续执行
                //    {
                //        //进入离线模式
                //        return;
                //    }
                //    //输入导入操作
                //    var importReult = ImportData(file.Path, impTb, batchCode, result.CaseTableName,
                //        connectionString, dbType);
                //    if (importReult) File.Delete(file.Path);
                //}
            }
        }