277 lines
14 KiB
C#
277 lines
14 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.ServiceProcess;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using HZQR.Common;
|
||
using ICSharpCode.SharpZipLib.Zip;
|
||
using Newtonsoft.Json.Linq;
|
||
using SocketTransfer.SDK;
|
||
|
||
namespace Unzip
|
||
{
|
||
partial class Service1 : ServiceBase
|
||
{
|
||
string _OracleConnStr = ConfigurationManager.AppSettings["OracleConnStr"].ToString();//数据库连接字符串
|
||
string Master_Table = ConfigurationManager.AppSettings["Master_Table"].ToString();//销售流水主表名
|
||
string Details_Table = ConfigurationManager.AppSettings["Details_Table"].ToString();//销售流水详情表名
|
||
string ZipUrl = ConfigurationManager.AppSettings["ZipUrl"].ToString();//待解压文件绝对路径
|
||
string ExecTime = ConfigurationManager.AppSettings["ExecTime"].ToString();//服务执行间隔时间,小时为单位
|
||
System.Timers.Timer _timer = new System.Timers.Timer();//定时器
|
||
public Service1()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
#region 方法 -> 开启服务
|
||
protected override void OnStart(string[] args)
|
||
{
|
||
// TODO: 在此处添加代码以启动服务。
|
||
int ExecHour = Convert.ToInt32(ExecTime == "" ? "1" : ExecTime);
|
||
int _interval = ExecHour * 60 * 60 * 1000;
|
||
_timer.Interval = _interval;
|
||
_timer.AutoReset = true;
|
||
_timer.Enabled = true;
|
||
_timer.Elapsed += new System.Timers.ElapsedEventHandler(ActionRun);
|
||
_timer.Start();
|
||
LogUtil.WriteLog(null, "文件解压服务启动", "文件解压服务");
|
||
RunProccess();
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 通知服务
|
||
protected override void OnStop()
|
||
{
|
||
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
|
||
_timer.AutoReset = false;
|
||
_timer.Enabled = false;
|
||
_timer.Stop();
|
||
LogUtil.WriteLog(null, "文件解压服务停止", "文件解压服务");
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 执行方法
|
||
private void ActionRun(object sender, System.Timers.ElapsedEventArgs e)
|
||
{
|
||
RunProccess();
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 执行解压操作,并插入数据库
|
||
private void RunProccess()
|
||
{
|
||
try
|
||
{
|
||
if (ZipUrl == "")
|
||
{
|
||
LogUtil.WriteLog(null, "文件解压失败:文件夹路径不能为空", "文件解压服务");
|
||
}
|
||
else
|
||
{
|
||
string fileName = ZipUrl;
|
||
OracleHelper _OracleHelper = new OracleHelper(_OracleConnStr.Split(',')[0],
|
||
_OracleConnStr.Split(',')[1], _OracleConnStr.Split(',')[2], _OracleConnStr.Split(',')[3]);
|
||
//获取给定路径下-所有匹配文件
|
||
DirectoryInfo dic = new DirectoryInfo(@fileName.TrimEnd('\\'));
|
||
//获取指定路径下所有zip文件
|
||
FileInfo[] filesDic = dic.GetFiles("*.zip", SearchOption.TopDirectoryOnly).Distinct().ToArray();
|
||
//获取指定路径下所有文件夹
|
||
DirectoryInfo[] dicInfo = dic.GetDirectories("*", SearchOption.TopDirectoryOnly).Distinct().ToArray();
|
||
if (filesDic.Length > 0)
|
||
{
|
||
ForFile(filesDic, fileName, _OracleHelper);
|
||
}
|
||
if (dicInfo.Length > 0)
|
||
{
|
||
//循环文件夹,找出所有zip文件并解压
|
||
foreach (DirectoryInfo item in dicInfo)
|
||
{
|
||
if (item.FullName.Contains("SystemLog"))
|
||
{
|
||
continue;
|
||
}
|
||
FileInfo[] filesInfo = item.GetFiles("*.zip", SearchOption.TopDirectoryOnly).Distinct().ToArray();
|
||
if (filesInfo.Length > 0)
|
||
{
|
||
ForFile(filesInfo, item.FullName, _OracleHelper);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogUtil.WriteLog(null, "文件解压失败:" + ex.Message, "文件解压服务");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 循环解压文件,并将数据插入数据库
|
||
private void ForFile(FileInfo[] filesInfo, string fileName, OracleHelper _OracleHelper)
|
||
{
|
||
string error = "";
|
||
//已解压的zip文件移动到该文件夹下
|
||
string SavaZipUrl = fileName + "\\" + "ZIP";
|
||
if (!Directory.Exists(SavaZipUrl))
|
||
{
|
||
Directory.CreateDirectory(SavaZipUrl);
|
||
}
|
||
foreach (FileInfo item in filesInfo)
|
||
{
|
||
if (UnZip(item, fileName, ref error))
|
||
{
|
||
//将已解压zip文件移动到ZIP文件夹中
|
||
if (!File.Exists(SavaZipUrl + "\\" + item.Name))
|
||
{
|
||
File.Move(item.FullName, SavaZipUrl + "\\" + item.Name);
|
||
}
|
||
LogUtil.WriteLog(null, "文件【" + item.FullName + "】解压成功", "文件解压服务");
|
||
|
||
#region 向表T_SELLMASTER、T_SELLDETAILS插入数据
|
||
string sql;
|
||
string DirectoryPath = fileName + "\\" + item.Name.Replace(item.Extension, "");
|
||
//表T_SELLMASTER
|
||
if (File.Exists(DirectoryPath + "\\T_SELLMASTER.txt"))
|
||
{
|
||
string masterfile = File.ReadAllText(DirectoryPath + "\\T_SELLMASTER.txt", Encoding.Default);
|
||
var jar = JArray.Parse(masterfile);
|
||
foreach (JObject data in jar)
|
||
{
|
||
DataTable dt = _OracleHelper.ExcuteSqlGetDataSet(@"SELECT COUNT(1) FROM " + Master_Table +
|
||
" WHERE SELLMASTER_CODE='" + data["SELLMASTER_CODE"] + "'").Tables[0];
|
||
if (Convert.ToInt32(dt.Rows[0][0]) == 0)
|
||
{
|
||
sql = string.Format(@"INSERT INTO {28}
|
||
(SELLMASTER_ID,SELLMASTER_CODE,SERVERPARTCODE,SERVERPART_NAME,
|
||
SHOPCODE,SHOPNAME,MACHINECODE,SELLWORKER_CODE,SELLWORKER_NAME,
|
||
SELLMASTER_DATE,TICKET_CODE,SELLMASTER_COUNT,SELLMASTER_OFFPRICE,
|
||
SELLMASTER_AMOUNT,CASHPAY,BANKPAY,MEMBERPAY,COUPONPAY,MOBILEPAY,
|
||
INTERNALPAY,OTHERPAY,PAYMENT_TYPE,MOBILEPAY_CODE,MERCHANT_ORDER,
|
||
SELLMASTER_DESC,TRANSFER_STATE,PAY_AMOUNT,COUPON_TYPE,SELLMASTER_STATE)
|
||
VALUES ({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',
|
||
TO_TIMESTAMP('{9}','YYYY-MM-DD HH24:MI:SS'),'{10}',{11},{12},
|
||
{13},{14},{15},{16},{17},{18},{19},{20},'{21}','{22}','{23}',
|
||
'{24}',{25},{26},'{27}',{29})",
|
||
data["SELLMASTER_ID"], data["SELLMASTER_CODE"], data["SERVERPARTCODE"], data["SERVERPART_NAME"],
|
||
data["SHOPCODE"], data["SHOPNAME"], data["MACHINECODE"], data["SELLWORKER_CODE"], data["SELLWORKER_NAME"],
|
||
data["SELLMASTER_DATE"], data["TICKET_CODE"], data["SELLMASTER_COUNT"], data["SELLMASTER_OFFPRICE"],
|
||
data["SELLMASTER_AMOUNT"], data["CASHPAY"], data["BANKPAY"], data["MEMBERPAY"], data["COUPONPAY"],
|
||
data["MOBILEPAY"], data["INTERNALPAY"], data["OTHERPAY"], data["PAYMENT_TYPE"],
|
||
data["MOBILEPAY_CODE"], data["MERCHANT_ORDER"], data["SELLMASTER_DESC"], 0,
|
||
data["PAY_AMOUNT"], data["COUPON_TYPE"], Master_Table,
|
||
data["SELLMASTER_STATE"] == null ? "9" : data["SELLMASTER_STATE"].ToString());
|
||
|
||
_OracleHelper.ExcuteSql(sql);
|
||
}
|
||
}
|
||
|
||
File.Delete(DirectoryPath + "\\T_SELLMASTER.txt");
|
||
}
|
||
//表T_SELLDETAILS
|
||
if (File.Exists(DirectoryPath + "\\T_SELLDETAILS.txt"))
|
||
{
|
||
string detailFile = File.ReadAllText(DirectoryPath + "\\T_SELLDETAILS.txt", Encoding.Default);
|
||
var jar = JArray.Parse(detailFile);
|
||
foreach (JObject data in jar)
|
||
{
|
||
DataTable dt = _OracleHelper.ExcuteSqlGetDataSet(@"SELECT COUNT(1) FROM " + Details_Table +
|
||
" WHERE SELLDETAILS_ID=" + data["SELLDETAILS_ID"] + " AND SELLMASTER_CODE = '" +
|
||
data["SELLMASTER_CODE"] + "'").Tables[0];
|
||
if (Convert.ToInt32(dt.Rows[0][0]) == 0)
|
||
{
|
||
sql = string.Format(@"INSERT INTO {16}
|
||
(SELLDETAILS_ID,SELLMASTER_CODE,COMMODITY_CODE,COMMODITY_BARCODE,
|
||
COMMODITY_NAME,SELLDETAILS_COUNT,SELLDETAILS_PRICE,SELLDETAILS_OFFPRICE,
|
||
SELLDETAILS_AMOUNT,LINENUM,PAYMENT_TYPE,CREATE_DATE,
|
||
SELLDETAILS_DESC,TRANSFER_STATE,COMMODITY_SYMBOL,COMMODITY_TYPE)
|
||
VALUES ({0},'{1}','{2}','{3}','{4}',{5},{6},{7},
|
||
{8},{9},'{10}',TO_TIMESTAMP('{11}','YYYY-MM-DD HH24:MI:SS'),
|
||
'{12}',{13},'{14}','{15}')",
|
||
data["SELLDETAILS_ID"], data["SELLMASTER_CODE"], data["COMMODITY_CODE"], data["COMMODITY_BARCODE"],
|
||
data["COMMODITY_NAME"], data["SELLDETAILS_COUNT"], data["SELLDETAILS_PRICE"], data["SELLDETAILS_OFFPRICE"],
|
||
data["SELLDETAILS_AMOUNT"], data["LINENUM"], data["PAYMENT_TYPE"], data["CREATE_DATE"],
|
||
data["SELLDETAILS_DESC"], 0, data["COMMODITY_SYMBOL"], data["COMMODITY_TYPE"], Details_Table);
|
||
|
||
_OracleHelper.ExcuteSql(sql);
|
||
}
|
||
}
|
||
|
||
File.Delete(DirectoryPath + "\\T_SELLDETAILS.txt");
|
||
}
|
||
#endregion
|
||
|
||
Directory.Delete(DirectoryPath);
|
||
|
||
LogUtil.WriteLog(null, "文件夹【" + DirectoryPath + "】删除成功", "文件夹删除");
|
||
}
|
||
else
|
||
{
|
||
LogUtil.WriteLog(null, "文件【" + item.FullName + "】解压失败:" + error, "文件解压服务");
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 解压Zip文件
|
||
private bool UnZip(FileInfo zipFile, string url, ref string error)
|
||
{
|
||
ZipEntry zipEntry;
|
||
string fileUrl = url + "\\" + zipFile.Name.Replace(zipFile.Extension, "");
|
||
try
|
||
{
|
||
using (ZipInputStream zipInput = new ZipInputStream(File.OpenRead(zipFile.FullName)))
|
||
{
|
||
//判断存储解压数据的文件夹是否存在
|
||
if (!Directory.Exists(fileUrl))
|
||
{
|
||
Directory.CreateDirectory(fileUrl);
|
||
}
|
||
while ((zipEntry = zipInput.GetNextEntry()) != null)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(zipEntry.Name))
|
||
{
|
||
//文件是否为目录路径
|
||
if (zipEntry.IsDirectory)
|
||
{
|
||
if (!Directory.Exists(fileUrl + "\\" + zipEntry.Name))
|
||
{
|
||
Directory.CreateDirectory(fileUrl + "\\" + zipEntry.Name);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!File.Exists(fileUrl + "\\" + zipEntry.Name))
|
||
{
|
||
FileStream stream = File.Create(fileUrl + "\\" + zipEntry.Name);
|
||
byte[] data = new byte[zipEntry.Size];
|
||
int size = Convert.ToInt32(zipEntry.Size);
|
||
//读取文件数据
|
||
size = zipInput.Read(data, 0, data.Length);
|
||
if (size == 0)
|
||
break;
|
||
stream.Write(data, 0, size);
|
||
stream.Close();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
zipInput.Close();
|
||
}
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
error = ex.Message;
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|