using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SSC
{
///
/// 系统服务结果服务
///
public class Z004 : NSC.IService
{
///
/// 提交新版本文件
///
///
///
///
public NSC.NetServiceStruct ServiceFunction(Common.Data.UserAuthority userAuth, NSC.NetServiceStruct input)
{
//验证权限
if (userAuth == Common.Data.UserAuthority.User)
{
throw new Common.Exceptions.NotEnoughPermissionException();
}
double version = input.Params["Version"].GetDouble();
double oldver = 0;
//获取现有版本号
StringBuilder query = new StringBuilder();
query.Append("SELECT VERSIONNO FROM T_UPDATE WHERE DEVICENAME='{DEVICENAME}' ORDER BY CREATETIME DESC LIMIT 1");
query.Replace("{DEVICENAME}", input.FirmwareModel);
Common.Data.SqlLite.SqlLiteHelper sqlite = new Common.Data.SqlLite.SqlLiteHelper();
using (var conn = sqlite.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
using (var table = sqlite.QueryReturnDataTable(conn, query.ToString()))
{
if (table.Rows.Count > 0)
{
oldver = double.Parse(table.Rows[0]["VERSIONNO"].ToString());
}
}
//验证版本号
if (version <= oldver)
{
throw new Exception("新版本号不能比旧版本号低!");
}
//创建版本目录
System.IO.Directory.CreateDirectory(Common.Config.ConfigSetting.GetSetting("0001") + input.FirmwareModel + @"\" + input.Params["Version"].GetString());
//写入文件
using (System.IO.FileStream fs = new System.IO.FileStream(Common.Config.ConfigSetting.GetSetting("0001")
+ input.FirmwareModel + @"\" + input.Params["Version"].GetString() + @"\update.bin"
, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None))
{
byte[] bytes = input.Params["Files"].GetBinary();
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
}
string updateTime = input.Params.ContainsKey("UpdateTime") ? input.Params["UpdateTime"].GetString() : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
query = new StringBuilder();
query.Append("INSERT INTO T_UPDATE(DEVICENAME,VERSIONNO,UPDATETIME,CREATETIME,CREATEUSER) VALUES('{DEVICENAME}','{VERSIONNO}','{UPDATETIME}','{CREATETIME}','{CREATEUSER}')");
query.Replace("{DEVICENAME}", input.FirmwareModel);
query.Replace("{VERSIONNO}", input.Params["Version"].GetString());
query.Replace("{UPDATETIME}", updateTime);
query.Replace("{CREATETIME}", updateTime);
query.Replace("{CREATEUSER}", input.Params.ContainsKey("User") ? input.Params["User"].GetString() : "");
using (var conn = sqlite.OpenConnection(Common.Config.ConfigSetting.GetConnectionString()))
{
sqlite.ExecuteSql(conn, query.ToString());
}
input.ExceptionType = Common.Data.ExceptionType.NoneException;
input.Returns.Add("LatestVersion", new NSC.ParameterStruct(input.Params["Version"].GetString()));
return input;
}
}
}