2025-03-28 09:49:56 +08:00

240 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OracleClient;
using System.Configuration;
using System.Threading;
using System.Diagnostics;
namespace ImportDate
{
public class Program
{
public static string connString = string.Empty;
public static bool Success = false;
public static string LogAddress = string.Empty;
public static string _HostIP = ConfigurationManager.AppSettings["HostIP"].ToString();
public static string _ServerName = ConfigurationManager.AppSettings["ServerName"].ToString();
public static string _UserName = ConfigurationManager.AppSettings["UserName"].ToString();
public static string _Password = ConfigurationManager.AppSettings["Password"].ToString();
public static void InsertDate(string FilePath)
{
string fileName = System.IO.Path.GetFileNameWithoutExtension(FilePath);
connString = GetConnString(_HostIP, _ServerName, _UserName, _Password);
int count = 0; int restult = 0;
using (StreamReader sr = new StreamReader(FilePath, Encoding.UTF8))
{
OracleTransaction myTrans;
using (OracleConnection conn = new OracleConnection(connString))
{
conn.Open();
myTrans = conn.BeginTransaction();
using (OracleCommand cmd = conn.CreateCommand())
{
cmd.Transaction = myTrans;
int i = 0;
string line = null;
try
{
while ((line = sr.ReadLine()) != null)
{
i++;
if (i == 0) { break; }
else if (i > 1)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
else
{
string[] strs = line.Split(new string[] { "," }, StringSplitOptions.None);
string Merchant_code = strs[0];
string Ticket_code = strs[1];
string Ffticket_code = strs[2];
string Centerticket_code = strs[3];
string Oriticket_code = strs[4];
string Transaction_trench = strs[5];
string Transaction_type = strs[6];
float Ticket_amount = float.Parse(strs[7]);
string Currency = strs[8];
float Deduction_rate = float.Parse(strs[9]);
float Taxfee_amount = float.Parse(strs[10]);
DateTime Trade_date = Convert.ToDateTime(strs[11]);
string Mpseparate_desc = strs[12];
string sql = "SELECT Count(1) FROM HIGHWAY_EXCHANGE.T_MPSEPARATE WHERE FFTICKET_CODE='" + Ffticket_code + "'";
OracleCommand oraclecmd = new OracleCommand(sql, conn, myTrans);
decimal number = (decimal)oraclecmd.ExecuteScalar();
if (number >0)
{
//LogManager.MessageLog("文件名" + fileName + "中,付付订单号为:" + Ffticket_code + "的数据重复,该条解析失败");
//Success = false;
continue;
}
cmd.CommandText = "insert into HIGHWAY_EXCHANGE.T_MPSEPARATE(MERCHANT_CODE,TICKET_CODE,FFTICKET_CODE,CENTERTICKET_CODE,ORITICKET_CODE,TRANSACTION_TRENCH,TRANSACTION_TYPE,TICKET_AMOUNT,CURRENCY,DEDUCTION_RATE,TAXFEE_AMOUNT,TRADE_DATE,MPSEPARATE_DESC) values(:Merchant_code,:Ticket_code,:Ffticket_code,:Centerticket_code,:Oriticket_code,:Transaction_trench,:Transaction_type,:Ticket_amount,:Currency,:Deduction_rate,:Taxfee_amount,:Trade_date,:Mpseparate_desc)";
cmd.Parameters.Clear();//清除上一次的参数
cmd.Parameters.Add(new OracleParameter(":MERCHANT_CODE", Merchant_code));
cmd.Parameters.Add(new OracleParameter(":TICKET_CODE", Ticket_code));
cmd.Parameters.Add(new OracleParameter(":FFTICKET_CODE", Ffticket_code));
cmd.Parameters.Add(new OracleParameter(":CENTERTICKET_CODE", Centerticket_code));
cmd.Parameters.Add(new OracleParameter(":ORITICKET_CODE", Oriticket_code));
cmd.Parameters.Add(new OracleParameter(":TRANSACTION_TRENCH", Transaction_trench));
cmd.Parameters.Add(new OracleParameter(":TRANSACTION_TYPE", Transaction_type));
cmd.Parameters.Add(new OracleParameter(":TICKET_AMOUNT", Ticket_amount));
cmd.Parameters.Add(new OracleParameter(":CURRENCY", Currency));
cmd.Parameters.Add(new OracleParameter(":DEDUCTION_RATE", Deduction_rate));
cmd.Parameters.Add(new OracleParameter(":TAXFEE_AMOUNT", Taxfee_amount));
cmd.Parameters.Add(new OracleParameter(":TRADE_DATE", Trade_date));
cmd.Parameters.Add(new OracleParameter(":MPSEPARATE_DESC", Mpseparate_desc));
restult = (int)cmd.ExecuteNonQuery();
if (restult > 0)
{
count++;
Success = true;
}
else
{
Success = false;
}
}
}
}
myTrans.Commit(); //循环外提交事务
}
catch (Exception ex)
{
myTrans.Rollback(); //回滚事务
Success = false;
LogManager.MessageLog("文件名为:" + fileName + "的数据解析失败");
LogManager.ErrorLog(ex, "");
}
finally
{
conn.Close();
}
}
}
}
}
public static void MainMethod()
{
try
{
int SuccessCount = 0;
int FalseCount = 0;
DateTime startTime = DateTime.Now; //定义一个开始时间
string path = System.IO.Directory.GetCurrentDirectory(); //获取exe所在根目录和所在目录下的txt文件
string desDir = path + "\\" + "bak";
if (!IsHaveTxt(path))
{
LogManager.MessageLog("执行程序所在目录下不存在DLS3863_开头的txt文件");
return;
}
if (!Directory.Exists(desDir))
{
Directory.CreateDirectory(desDir);
}
DirectoryInfo folder = new DirectoryInfo(path);
//获取包含DLS3863_开头的所有txt文件*DLS3863_*
foreach (FileInfo file in folder.GetFiles("*DLS3863_*.txt", SearchOption.TopDirectoryOnly))
{
InsertDate(file.FullName);
string desDirPath = desDir + "\\" + file.Name;
if (Success == true)
{
if (File.Exists(desDirPath))
{
desDirPath = desDir + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.FullName) + " - 副本.txt";
}
//if (file.Name.Equals(System.IO.Path.GetFileName(desDirPath)))
MoveDirFile(file.FullName, desDirPath);
SuccessCount++;
}
else if (Success == false)
{
FalseCount++;
if (File.Exists(desDirPath))
{
desDirPath = desDir + "\\" + System.IO.Path.GetFileNameWithoutExtension(file.FullName) + " - 副本.txt";
}
MoveDirFile(file.FullName, desDirPath);
}
}
TimeSpan ts = DateTime.Now - startTime;
if (SuccessCount > 0)
{
LogManager.MessageLog("成功解析" + SuccessCount + "个txt文件,"+ FalseCount + "个txt文件失败," + "共花费时间:" + ts.ToString());
}
}
catch (Exception ex)
{
LogManager.ErrorLog(ex, "");
}
}
/// <summary>
/// 该目录下是否存在txt文件
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public static bool IsHaveTxt(string Path)
{
DirectoryInfo foldinfo = new DirectoryInfo(Path);
foreach (FileInfo info in foldinfo.GetFiles("*.txt"))
{
return true;
}
return false;
}
public static string GetConnString(string _HostIP, string _ServerName, string _UserName, string _Password)
{
string connString = string.Format(@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT=1521))(CONNECT_DATA=(SERVICE_NAME={1})));User Id={2};Password={3};", _HostIP, _ServerName, _UserName, _Password);
return connString;
}
/// <summary>
/// 移动目录内的文件到另一目录
/// </summary>
/// <param name="sorDir">源目录</param>
/// <param name="desDir">目标目录</param>
public static void MoveDirFile(string sorDir, string desDir)
{
try
{
if (Directory.Exists(sorDir))
{
return;
}
File.Move(sorDir, desDir);
if (File.Exists(sorDir))
{
File.Delete(sorDir);
}
}
catch(Exception ex)
{
LogManager.ErrorLog(ex, "");
}
}
static void Main(string[] args)
{
Console.WriteLine("正在执行,请稍后...");
MainMethod();
Console.WriteLine("执行完成...");
//Console.ReadKey();
return;
}
}
}