private string GetTableSuffix(XEnum.ImportItemType itemType) { var suffix = itemType.ToString(); var startAt = suffix.LastIndexOf('.'); suffix = suffix.Substring(startAt + 1); return(suffix); }
private string AssignOrgId(int importId, XEnum.ImportItemType itemType) { if (!(itemType == XEnum.ImportItemType.Loan || itemType == XEnum.ImportItemType.Public || itemType == XEnum.ImportItemType.Private)) { return("Invalid import item: " + itemType.ToString()); } try { var dao = new SqlDbHelper(); var sql = new StringBuilder(); int count = 0; var suffix = GetTableSuffix(itemType); logger.Debug("Assigning OrgId column to " + suffix); sql.AppendLine("UPDATE Import" + suffix); if (itemType == XEnum.ImportItemType.Loan) { sql.AppendLine("SET OrgId = dbo.sfGetOrgId(OrgNo)"); } else { sql.AppendLine("SET OrgId = dbo.sfGetOrgId(OrgName2)"); } sql.AppendLine("WHERE ImportId = {0} AND OrgId IS NULL"); count = dao.ExecuteNonQuery(string.Format(sql.ToString(), importId)); logger.DebugFormat("Done ({0} affected)", count); if (itemType == XEnum.ImportItemType.Loan) { logger.Debug("Assigning OrgId4Report column to " + suffix); sql.Clear(); sql.AppendLine("UPDATE ImportLoan"); sql.AppendLine("SET OrgId4Report = CASE WHEN OrgId IN (1, 2) THEN (CASE WHEN LEN(CustomerName) >= 5 THEN 1 ELSE 2 END) ELSE OrgId END"); sql.AppendLine("WHERE ImportId = {0} AND OrgId4Report IS NULL"); count = dao.ExecuteNonQuery(string.Format(sql.ToString(), importId)); logger.DebugFormat("Done ({0} affected)", count); } } catch (Exception ex) { return(ex.Message); } return(string.Empty); }
private string ImportYW(XEnum.ImportItemType itemType, int importId, string importFolder, string sourceFilePath) { logger.DebugFormat("Importing {0} data", itemType.ToString()); var result = ""; var filePathes = sourceFilePath.Split('|'); for (int i = 0; i < filePathes.Length; i++) { var filePath = filePathes[i]; if (string.IsNullOrEmpty(filePath)) { logger.Debug("Source file not provided"); continue; } var orgId = GetOrgId4YW(filePath); // Do this check before any action if (orgId == 0) { var msg = "不能确定业务状况表数据的所属银行:" + filePath; logger.Error(msg); return(msg); } var done = CopyItem(importId, importFolder, filePath, itemType); if (!done) { logger.Debug("Source file not provided"); return(""); // Do nothing if user hasn't select a file for this table } // Import to database string targetFilePath = importFolder + "\\Processed\\" + GetYWTargetFileName(itemType, orgId); var excelColumns = "*"; var dbColumns = "SubjectCode, SubjectName, LastDebitBalance, LastCreditBalance, CurrentDebitChange, CurrentCreditChange, CurrentDebitBalance, CurrentCreditBalance"; result = ImportTable(importId, targetFilePath, itemType, excelColumns, dbColumns, "OrgId", orgId, 1, i + 1); if (!string.IsNullOrEmpty(result)) { return(result); } } return(result); }
protected bool CopyItem(int importId, string importFolder, string sourceFilePath, XEnum.ImportItemType itemType) { int itemTypeId = (int)itemType; if (sourceFilePath.Length == 0 || !File.Exists(sourceFilePath)) { return(false); } string targetFileName = this.targetFileNames[itemTypeId]; if (itemType == XEnum.ImportItemType.YWNei || itemType == XEnum.ImportItemType.YWWai || itemType == XEnum.ImportItemType.Loan) { var orgId = GetOrgId4YW(sourceFilePath); targetFileName = GetYWTargetFileName(itemType, orgId); } //Original var originalFolder = importFolder + @"\Original\"; if (!Directory.Exists(originalFolder)) { Directory.CreateDirectory(originalFolder); } File.Copy(sourceFilePath, originalFolder + @"\" + targetFileName, true); //Processed var processedFolder = importFolder + @"\Processed\"; if (!Directory.Exists(processedFolder)) { Directory.CreateDirectory(processedFolder); } File.Copy(sourceFilePath, processedFolder + @"\" + targetFileName, true); logger.Debug("Process copied item for " + itemType.ToString()); ExcelHelper.ProcessCopiedItem(processedFolder + @"\" + targetFileName, itemType); logger.Debug("Updating ImportItem table"); var dao = new SqlDbHelper(); var sql = new StringBuilder(); sql.AppendFormat("SELECT ISNULL(MAX(Id), 0) FROM ImportItem WHERE ImportId = {0} AND ItemType = {1}", importId, itemTypeId); var importItemId = (int)dao.ExecuteScalar(sql.ToString()); if (importItemId == 0) { sql.Clear(); sql.AppendLine(string.Format("INSERT INTO ImportItem (ImportId, ItemType, FilePath) VALUES ({0}, {1}, '{2}')", importId, itemTypeId, sourceFilePath)); sql.AppendLine("SELECT SCOPE_IDENTITY()"); importItemId = (int)((decimal)dao.ExecuteScalar(sql.ToString())); logger.Debug("New record created. ImportItemId = " + importItemId.ToString()); } else { sql.Clear(); sql.AppendFormat("UPDATE ImportItem SET FilePath = '{0}', ModifyDate = getdate() WHERE Id = {1}", sourceFilePath, importItemId); dao.ExecuteNonQuery(sql.ToString()); logger.Debug("Existing record updated. ImportItemId = " + importItemId.ToString()); } return(true); }