using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
namespace NSC
{
///
/// 功能:异步网络服务引擎
/// 作者:王昊昇
/// 时间:2012.11.8
///
public static class ServiceDataBufferEngine
{
///
/// 创建新的服务流水号
///
/// 返回一个新的服务流水号
public static string CreateNewServiceSN()
{
Common.Data.SqlLite.SqlLiteHelper dal = new Common.Data.SqlLite.SqlLiteHelper();
using (System.Data.IDbConnection conn = dal.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
{
string pid = dal.GetSingle(conn,
"INSERT INTO T_SERVICELOG(SERVICEPATH,SUBMITTIME,COMPLETE) VALUES('','" + DateTime.Now.ToString(Common.TextUtil.DateTimeUtil.DATETIME_YYYYMMDD_HHMMSS) + "','0');"
+ Common.Data.SqlLite.SqlLiteHelper.SELECT_IDENTITY_ID).ToString();
return pid;
}
}
///
/// 写入服务数据文件
///
/// 服务代码
/// 要写入的服务数据
public static void WriteServiceFile(string serviceSN, NetServiceStruct data)
{
string configPath = Common.Config.ConfigSetting.GetSetting("0001");
string fileName = configPath == "" ? System.Windows.Forms.Application.StartupPath + @"\ServiceBuffer\" + serviceSN : configPath + @"\" + serviceSN;
using (System.IO.MemoryStream ms = PacketsEncoder.ConvertToStream(data))
using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read))
{
fs.Write(ms.ToArray(), 0, (int)ms.Length);
fs.Flush();
}
Common.Data.SqlLite.SqlLiteHelper dal = new Common.Data.SqlLite.SqlLiteHelper();
using (System.Data.IDbConnection conn = dal.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
{
dal.ExecuteSql(conn, string.Format("UPDATE T_SERVICELOG SET SERVICEPATH={0},COMPLETE='1' WHERE PID={1}", fileName, serviceSN));
}
}
///
/// 获取执行结果
///
/// 服务代码
/// 结果数据
/// 返回是否执行成功
public static bool GetServiceResult(string serviceSN, out NetServiceStruct data)
{
Common.Data.SqlLite.SqlLiteHelper dal = new Common.Data.SqlLite.SqlLiteHelper();
using (System.Data.IDbConnection conn = dal.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
using (System.Data.DataTable table = dal.QueryReturnDataTable(conn, string.Format("SELECT VERSIONNO FROM T_UPDATE")))
{
if (table.Rows.Count > 0)
{
using (System.IO.FileStream fs = new System.IO.FileStream(table.Rows[0]["VERSIONNO"].ToString(), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
using (System.IO.MemoryStream ms = new System.IO.MemoryStream((int)fs.Length))
{
fs.CopyTo(ms);
data = PacketsDecoder.ParseNetStream(ms);
}
return true;
}
else
{
data = null;
throw new Common.Exceptions.NoServiceLogException();
}
}
}
}
}