代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DEV_JIZHANG.Service;
using DEV_JIZHANG.Models;
using DEV_JIZHANG.Common;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace DEV_JIZHANG.SystemManagement
{
public partial class SysBack : System.Web.UI.Page
{
private string strSplitChar = " | " ; // 分隔符,常量
private string strEncryptKey = " WXTBASIC " ;
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
}
}
#region 创建各维修点目录
// 创建维修点数据备份目录
private void CreateBackupDir()
{
string strSiteCode = PublicFunction.GetUserID( this .Request);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.filesCreatePath = Server.MapPath( " ~ " );
uzipfilescreate.CreateDirList( " \\DataBR\\ " + strSiteCode + " \\Backup " );
}
// 创建维修点数据恢复目录
private void CreateRecoveryDir()
{
string strSiteCode = PublicFunction.GetUserID( this .Request);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.filesCreatePath = Server.MapPath( " ~ " );
uzipfilescreate.CreateDirList( " \\DataBR\\ " + strSiteCode + " \\Recovery " );
}
#endregion
#region 数据 导出到CSV
public void ClassToDataTableToCsv( string strSavePath, string className)
{
CSVHelp csv = new CSVHelp();
strSavePath = Server.MapPath(strSavePath);
string strError = "" ;
string sql = "" ;
sql = " select * from " + className + " where uid= " + PublicFunction.GetUserID( this .Page.Request);
DataTable dt = DBHelper.GetDataSet(sql);
csv.DataTableToCSVFile(dt, strSavePath, strSplitChar, ref strError);
}
#endregion
protected void btnBack_Click( object sender, EventArgs e)
{
CSVHelp csv = new CSVHelp();
this .CreateBackupDir(); // 创建备份目录
string uid = PublicFunction.GetUserID( this .Page.Request);
string strBackupDir = " ~/DataBR/ " + uid + " /Backup/ " ; // 备份目录
#region 将维修点信息和用户信息 放入 csv文件
string strError = "" ;
string strLoginName = PublicFunction.GetUserID( this .Request);
string strEncryptUid = csv.EncryptDES(Server.UrlEncode(uid), strEncryptKey);
string strDate = csv.EncryptDES(Server.UrlEncode(DateTime.Now.ToString()), strEncryptKey);
DataTable dt = new DataTable();
dt.Columns.Add( " Uid " );
dt.Columns.Add( " BackDate " );
dt.Rows.Add( new object [] { strEncryptUid, strDate });
csv.DataTableToCSVFile(dt, Server.MapPath(strBackupDir + " 个人.cat " ), strSplitChar, ref strError);
#endregion
#region 从数据库中取出数据放到csv文件中
this .ClassToDataTableToCsv(strBackupDir + " 入库单.cat " , " InInventory " );
this .ClassToDataTableToCsv(strBackupDir + " 出库单.cat " , " OutInventory " );
this .ClassToDataTableToCsv(strBackupDir + " 库存.cat " , " Inventory " );
this .ClassToDataTableToCsv(strBackupDir + " 维修单.cat " , " InService " );
this .ClassToDataTableToCsv(strBackupDir + " 销售单.cat " , " SalesInfo " );
this .ClassToDataTableToCsv(strBackupDir + " 销售产品设置.cat " , " SalesProductName " );
this .ClassToDataTableToCsv(strBackupDir + " 品牌设置.cat " , " SYSBrand " );
this .ClassToDataTableToCsv(strBackupDir + " 供应商设置.cat " , " SYSSupplier " );
this .ClassToDataTableToCsv(strBackupDir + " 共享数据设置.cat " , " UserShare " );
this .ClassToDataTableToCsv(strBackupDir + " 邮寄单.cat " , " YJ " );
#endregion
#region 打包csv文件到zip文件,并弹出下载保存
MemoryStream ms = new MemoryStream();
byte [] buffer = null ;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom(); // 通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
file.Add(Server.MapPath(strBackupDir + " 个人.cat " ));
file.Add(Server.MapPath(strBackupDir + " 入库单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 出库单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 库存.cat " ));
file.Add(Server.MapPath(strBackupDir + " 维修单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 销售单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 销售产品设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 品牌设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 供应商设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 共享数据设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 邮寄单.cat " ));
file.CommitUpdate();
buffer = new byte [ms.Length];
ms.Position = 0 ;
ms.Read(buffer, 0 , buffer.Length);
}
string strFileName = " WXT_DataBackup_ " + PublicFunction.GetUserID( this .Page.Request) + " _ " + DateTime.Now.ToString( " yyyyMMdd " ) + " .zip " ;
Response.AddHeader( " content-disposition " , " attachment;filename= " + Server.UrlEncode(strFileName));
// Response.AddHeader("Content-Transfer-Encoding", "binary");
// Response.ContentType = "application/octet-stream";
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
#endregion
}
protected void btnUp_Click( object sender, EventArgs e)
{
this .CreateRecoveryDir();
string strSiteCode = PublicFunction.GetUserID( this .Request);
string strRecoveryDir = " ~/DataBR/ " + strSiteCode + " /Recovery/ " ; // 恢复目录
try
{
#region 上传并解压文件
CSVHelp csv = new CSVHelp();
if (FileUpload1.PostedFile.ContentLength == 0 )
{
// Page.RegisterClientScriptBlock("myKey", "<script>alert('请浏览并选择恢复的文件!')</script>");
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('请浏览并选择恢复的文件!')</script> " );
return ;
}
if (FileUpload1.PostedFile.ContentType != " application/x-zip-compressed " )
{
// Page.RegisterClientScriptBlock("myKey", "<script>alert('恢复的文件不正确!')</script>");
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复的文件不正确!')</script> " );
return ;
}
// 上传
string FileName = " RecoveryData.zip " ;
string svrFileName = Server.MapPath(strRecoveryDir) + FileName;
this .FileUpload1.PostedFile.SaveAs(svrFileName);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.ZipFileName = svrFileName; // this.UploadFile.PostedFile.FileName.Trim();
uzipfilescreate.filesCreatePath = Server.MapPath(strRecoveryDir);
try
{
// uzipfilescreate.UZipCreateFiles();
ZipEntry entry;
// this.lblError1.Text = uzipfilescreate.ZipFileName.ToString();
ZipInputStream zipinputstream = new ZipInputStream(File.OpenRead(uzipfilescreate.ZipFileName)); // 此句有错误
while ((entry = zipinputstream.GetNextEntry()) != null )
{
// this.CreateDirList(entry.Name);
// string strPath = this.filescreatepath + "\\" + entry.Name;
// this.lblError1.Text = "in";
string strPath = uzipfilescreate.filesCreatePath + " \\ " + entry.Name;
FileStream streamWriter = File.Create(strPath);
byte [] data = new byte [entry.Size];
zipinputstream.Read(data, 0 , data.Length);
streamWriter.Write(data, 0 , data.Length);
streamWriter.Close();
File.SetLastWriteTime(strPath, entry.DateTime);
}
zipinputstream.Close();
}
catch (Exception ex)
{
string strex = ex.Message.ToString();
// this.lblError.Text = strex;
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('您刚刚恢复过,请稍后再试! " + strex.Replace( " ' " , "" ) + " ')</script> " );
return ;
}
#endregion
#region 取出导入文件的维修点和登录用户名,与系统登录的维修点和登录名比较,不匹配则提示错误
string strError = "" ;
DataTable dtTemp = null ;
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 个人.cat " ), ref strError);
if (dtTemp.Rows.Count != 1 )
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('备份源数据文件被破坏,不能进行恢复!')</script> " );
return ;
}
else
{
string strEncryptUid = dtTemp.Rows[ 0 ][ 0 ].ToString().Trim();
string strEncryptDate = dtTemp.Rows[ 0 ][ 1 ].ToString().Trim();
// string strSiteCode = PublicFunction.GetSiteCode(this.Request);
string strLoginUid = PublicFunction.GetUserID( this .Request);
if (csv.EncryptDES(strLoginUid, strEncryptKey) != strEncryptUid)
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('使用的文件非此登录用户的备份文件,不能进行恢复!')</script> " );
return ;
}
}
#endregion
#region csv 导出 datatable,并插入恢复数据
string uid = PublicFunction.GetUserID( this .Page.Request);
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 入库单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete ininventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
InInventory ininventory = new InInventory();
ininventory.InNo = item[ 1 ].ToString();
ininventory.Depot = item[ 2 ].ToString();
ininventory.InGong = item[ 3 ].ToString();
ininventory.InBrand = item[ 4 ].ToString();
ininventory.InModel = item[ 5 ].ToString();
ininventory.InName = item[ 6 ].ToString();
ininventory.InCount = int .Parse(item[ 7 ].ToString());
ininventory.InCost = decimal .Parse(item[ 8 ].ToString());
ininventory.InPeople = item[ 9 ].ToString();
ininventory.InRemark = item[ 10 ].ToString();
ininventory.InDate = DateTime.Parse(item[ 11 ].ToString());
ininventory.Uid = int .Parse(item[ 12 ].ToString());
if ( ! JIZHANGService.CreateInInventoryBLL().SaveEntity(ininventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 维修单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete inservice where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
InService inService = new InService();
inService.Customer = item[ 1 ].ToString();
inService.Sex = item[ 2 ].ToString();
inService.FixedPhone = item[ 3 ].ToString();
inService.MobilePhone = item[ 4 ].ToString();
inService.IMIE = item[ 5 ].ToString();
inService.IdNum = item[ 6 ].ToString();
inService.PhoneType = item[ 7 ].ToString();
inService.Brand = item[ 8 ].ToString();
inService.Model = item[ 9 ].ToString();
inService.PhoneClor = item[ 10 ].ToString();
inService.Facade = item[ 11 ].ToString();
inService.Symptom = item[ 12 ].ToString();
inService.Addon = item[ 13 ].ToString();
inService.Remark = item[ 14 ].ToString();
inService.WxDate = DateTime.Parse(item[ 15 ].ToString());
inService.WxType = item[ 16 ].ToString();
inService.WxNo = item[ 17 ].ToString();
inService.Uid = int .Parse(item[ 18 ].ToString());
inService.WxModel = item[ 19 ].ToString();
inService.WxPeople = item[ 20 ].ToString();
inService.WxResult = item[ 21 ].ToString();
inService.WxFab1 = item[ 22 ].ToString();
inService.WxFab2 = item[ 23 ].ToString();
inService.WxFab3 = item[ 24 ].ToString();
inService.WxFab4 = item[ 25 ].ToString();
inService.WxFab5 = item[ 26 ].ToString();
inService.WxFab6 = item[ 27 ].ToString();
inService.FabCost = decimal .Parse(item[ 28 ].ToString());
inService.FabPrice = decimal .Parse(item[ 29 ].ToString());
inService.FanRemark = item[ 30 ].ToString();
inService.FanShouType = item[ 31 ].ToString();
inService.FanShiPrice = decimal .Parse(item[ 32 ].ToString());
inService.FanPiaoType = item[ 33 ].ToString();
inService.FanXiaoPiao = item[ 34 ].ToString();
inService.WxwDate = DateTime.Parse(item[ 35 ].ToString());
inService.YnXF = item[ 36 ].ToString();
if ( ! JIZHANGService.CreateInServiceBLL().SaveEntity(inService))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 库存.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete inventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
Inventory inventory = new Inventory();
inventory.Brand = item[ 1 ].ToString();
inventory.Model = item[ 2 ].ToString();
inventory.InCount = int .Parse(item[ 3 ].ToString());
inventory.InCost = decimal .Parse(item[ 4 ].ToString());
inventory.InDate = DateTime.Parse(item[ 5 ].ToString());
inventory.Uid = int .Parse(item[ 6 ].ToString());
inventory.Depot = item[ 7 ].ToString();
inventory.Dname = item[ 8 ].ToString();
if ( ! JIZHANGService.CreateInventoryBLL().SaveEntity(inventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 出库单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete outinventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
OutInventory outInventory = new OutInventory();
outInventory.OutNo = item[ 1 ].ToString();
outInventory.Depot = item[ 2 ].ToString();
outInventory.OutGong = item[ 3 ].ToString();
outInventory.OutBrand = item[ 4 ].ToString();
outInventory.OutModel = item[ 5 ].ToString();
outInventory.OutName = item[ 6 ].ToString();
outInventory.OutCount = int .Parse(item[ 7 ].ToString());
outInventory.OutCost = decimal .Parse(item[ 8 ].ToString());
outInventory.OutPeople = item[ 9 ].ToString();
outInventory.OutRemark = item[ 10 ].ToString();
outInventory.OutDate = DateTime.Parse(item[ 11 ].ToString());
outInventory.Uid = int .Parse(item[ 12 ].ToString());
if ( ! JIZHANGService.CreateOutInventoryBLL().SaveEntity(outInventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 销售单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete salesinfo where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SalesInfo salesInfo = new SalesInfo();
salesInfo.SalesBrand = item[ 1 ].ToString();
salesInfo.SalesItem = item[ 2 ].ToString();
salesInfo.SalesCount = int .Parse(item[ 3 ].ToString());
salesInfo.SalesPrice = decimal .Parse(item[ 4 ].ToString());
salesInfo.SalesCost = decimal .Parse(item[ 5 ].ToString());
salesInfo.SaleNo = item[ 6 ].ToString();
salesInfo.SalesBackType = item[ 7 ].ToString();
salesInfo.SalesBackPrice = decimal .Parse(item[ 8 ].ToString());
salesInfo.Uid = int .Parse(item[ 9 ].ToString());
salesInfo.UserName = item[ 10 ].ToString();
salesInfo.Sex = item[ 11 ].ToString();
salesInfo.UserPhone = item[ 12 ].ToString();
salesInfo.PiaoType = item[ 13 ].ToString();
salesInfo.SmallPiao = item[ 14 ].ToString();
salesInfo.Remark = item[ 15 ].ToString();
salesInfo.SalesPeople = item[ 16 ].ToString();
salesInfo.SalesDate = DateTime.Parse(item[ 17 ].ToString());
salesInfo.SalesType = item[ 18 ].ToString();
salesInfo.SalesBackDate = DateTime.Parse(item[ 19 ].ToString());
if ( ! JIZHANGService.CreateSalesInfoBLL().SaveEntity(salesInfo))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 邮寄单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete yj where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
YJ yj = new YJ();
yj.Brand = item[ 1 ].ToString();
yj.Model = item[ 2 ].ToString();
yj.Number = item[ 3 ].ToString();
yj.Count = int .Parse(item[ 4 ].ToString());
yj.UserName = item[ 5 ].ToString();
yj.Remark = item[ 6 ].ToString();
yj.SDate = DateTime.Parse(item[ 7 ].ToString());
yj.YJNO = item[ 8 ].ToString();
yj.YJUser = item[ 9 ].ToString();
yj.YJbao = item[ 10 ].ToString();
yj.Yname = item[ 11 ].ToString();
yj.Uid = int .Parse(item[ 12 ].ToString());
yj.YJDate = DateTime.Parse(item[ 13 ].ToString());
yj.YJState = item[ 14 ].ToString();
yj.YJBack = item[ 15 ].ToString();
yj.YJBDate = item[ 16 ].ToString();
if ( ! JIZHANGService.CreateYJBLL().SaveEntity(yj))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 销售产品设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete salesproductname where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SalesProductName spn = new SalesProductName();
spn.Uid = int .Parse(item[ 1 ].ToString());
spn.ProductName = item[ 2 ].ToString();
spn.ProductCost = decimal .Parse(item[ 3 ].ToString());
spn.ProductPrice = decimal .Parse(item[ 4 ].ToString());
spn.ProductBrand = item[ 5 ].ToString();
if ( ! JIZHANGService.CreateSalesProductNameBLL().SaveEntity(spn))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 品牌设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete sysbrand where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SYSBrand sb = new SYSBrand();
sb.Uid = int .Parse(item[ 1 ].ToString());
sb.Brand = item[ 2 ].ToString();
if ( ! JIZHANGService.CreateSYSBrandBLL().SaveEntity(sb))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 供应商设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete syssupplier where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SYSSupplier sss = new SYSSupplier();
sss.Uid = int .Parse(item[ 1 ].ToString());
sss.Supplier = item[ 2 ].ToString();
sss.Phone = item[ 3 ].ToString();
sss.Address = item[ 4 ].ToString();
if ( ! JIZHANGService.CreateSYSSupplierBLL().SaveEntity(sss))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 共享数据设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete usershare where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
UserShare us = new UserShare();
us.Uid = int .Parse(item[ 1 ].ToString());
us.Puid = int .Parse(item[ 2 ].ToString());
us.Pname = item[ 3 ].ToString();
us.Mname = item[ 4 ].ToString();
us.Rname = item[ 5 ].ToString();
if ( ! JIZHANGService.CreateUserShareBLL().SaveEntity(us))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
#endregion
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('数据恢复成功!')</script> " );
}
catch (Exception err)
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('数据恢复失败! " + err.Message.ToString().Replace( " ' " , "" ) + " ')</script> " );
}
}
protected void btnClear_Click( object sender, EventArgs e)
{
string uid = PublicFunction.GetUserID( this .Page.Request);
DBHelper.ExecuteCommand( " delete ininventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete usershare where uid= " + uid);
DBHelper.ExecuteCommand( " delete syssupplier where uid= " + uid);
DBHelper.ExecuteCommand( " delete sysbrand where uid= " + uid);
DBHelper.ExecuteCommand( " delete salesproductname where uid= " + uid);
DBHelper.ExecuteCommand( " delete yj where uid= " + uid);
DBHelper.ExecuteCommand( " delete salesinfo where uid= " + uid);
DBHelper.ExecuteCommand( " delete outinventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete inventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete inservice where uid= " + uid);
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('清空数据成功!')</script> " );
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DEV_JIZHANG.Service;
using DEV_JIZHANG.Models;
using DEV_JIZHANG.Common;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace DEV_JIZHANG.SystemManagement
{
public partial class SysBack : System.Web.UI.Page
{
private string strSplitChar = " | " ; // 分隔符,常量
private string strEncryptKey = " WXTBASIC " ;
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
}
}
#region 创建各维修点目录
// 创建维修点数据备份目录
private void CreateBackupDir()
{
string strSiteCode = PublicFunction.GetUserID( this .Request);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.filesCreatePath = Server.MapPath( " ~ " );
uzipfilescreate.CreateDirList( " \\DataBR\\ " + strSiteCode + " \\Backup " );
}
// 创建维修点数据恢复目录
private void CreateRecoveryDir()
{
string strSiteCode = PublicFunction.GetUserID( this .Request);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.filesCreatePath = Server.MapPath( " ~ " );
uzipfilescreate.CreateDirList( " \\DataBR\\ " + strSiteCode + " \\Recovery " );
}
#endregion
#region 数据 导出到CSV
public void ClassToDataTableToCsv( string strSavePath, string className)
{
CSVHelp csv = new CSVHelp();
strSavePath = Server.MapPath(strSavePath);
string strError = "" ;
string sql = "" ;
sql = " select * from " + className + " where uid= " + PublicFunction.GetUserID( this .Page.Request);
DataTable dt = DBHelper.GetDataSet(sql);
csv.DataTableToCSVFile(dt, strSavePath, strSplitChar, ref strError);
}
#endregion
protected void btnBack_Click( object sender, EventArgs e)
{
CSVHelp csv = new CSVHelp();
this .CreateBackupDir(); // 创建备份目录
string uid = PublicFunction.GetUserID( this .Page.Request);
string strBackupDir = " ~/DataBR/ " + uid + " /Backup/ " ; // 备份目录
#region 将维修点信息和用户信息 放入 csv文件
string strError = "" ;
string strLoginName = PublicFunction.GetUserID( this .Request);
string strEncryptUid = csv.EncryptDES(Server.UrlEncode(uid), strEncryptKey);
string strDate = csv.EncryptDES(Server.UrlEncode(DateTime.Now.ToString()), strEncryptKey);
DataTable dt = new DataTable();
dt.Columns.Add( " Uid " );
dt.Columns.Add( " BackDate " );
dt.Rows.Add( new object [] { strEncryptUid, strDate });
csv.DataTableToCSVFile(dt, Server.MapPath(strBackupDir + " 个人.cat " ), strSplitChar, ref strError);
#endregion
#region 从数据库中取出数据放到csv文件中
this .ClassToDataTableToCsv(strBackupDir + " 入库单.cat " , " InInventory " );
this .ClassToDataTableToCsv(strBackupDir + " 出库单.cat " , " OutInventory " );
this .ClassToDataTableToCsv(strBackupDir + " 库存.cat " , " Inventory " );
this .ClassToDataTableToCsv(strBackupDir + " 维修单.cat " , " InService " );
this .ClassToDataTableToCsv(strBackupDir + " 销售单.cat " , " SalesInfo " );
this .ClassToDataTableToCsv(strBackupDir + " 销售产品设置.cat " , " SalesProductName " );
this .ClassToDataTableToCsv(strBackupDir + " 品牌设置.cat " , " SYSBrand " );
this .ClassToDataTableToCsv(strBackupDir + " 供应商设置.cat " , " SYSSupplier " );
this .ClassToDataTableToCsv(strBackupDir + " 共享数据设置.cat " , " UserShare " );
this .ClassToDataTableToCsv(strBackupDir + " 邮寄单.cat " , " YJ " );
#endregion
#region 打包csv文件到zip文件,并弹出下载保存
MemoryStream ms = new MemoryStream();
byte [] buffer = null ;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new MyNameTransfom(); // 通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。
file.Add(Server.MapPath(strBackupDir + " 个人.cat " ));
file.Add(Server.MapPath(strBackupDir + " 入库单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 出库单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 库存.cat " ));
file.Add(Server.MapPath(strBackupDir + " 维修单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 销售单.cat " ));
file.Add(Server.MapPath(strBackupDir + " 销售产品设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 品牌设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 供应商设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 共享数据设置.cat " ));
file.Add(Server.MapPath(strBackupDir + " 邮寄单.cat " ));
file.CommitUpdate();
buffer = new byte [ms.Length];
ms.Position = 0 ;
ms.Read(buffer, 0 , buffer.Length);
}
string strFileName = " WXT_DataBackup_ " + PublicFunction.GetUserID( this .Page.Request) + " _ " + DateTime.Now.ToString( " yyyyMMdd " ) + " .zip " ;
Response.AddHeader( " content-disposition " , " attachment;filename= " + Server.UrlEncode(strFileName));
// Response.AddHeader("Content-Transfer-Encoding", "binary");
// Response.ContentType = "application/octet-stream";
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
#endregion
}
protected void btnUp_Click( object sender, EventArgs e)
{
this .CreateRecoveryDir();
string strSiteCode = PublicFunction.GetUserID( this .Request);
string strRecoveryDir = " ~/DataBR/ " + strSiteCode + " /Recovery/ " ; // 恢复目录
try
{
#region 上传并解压文件
CSVHelp csv = new CSVHelp();
if (FileUpload1.PostedFile.ContentLength == 0 )
{
// Page.RegisterClientScriptBlock("myKey", "<script>alert('请浏览并选择恢复的文件!')</script>");
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('请浏览并选择恢复的文件!')</script> " );
return ;
}
if (FileUpload1.PostedFile.ContentType != " application/x-zip-compressed " )
{
// Page.RegisterClientScriptBlock("myKey", "<script>alert('恢复的文件不正确!')</script>");
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复的文件不正确!')</script> " );
return ;
}
// 上传
string FileName = " RecoveryData.zip " ;
string svrFileName = Server.MapPath(strRecoveryDir) + FileName;
this .FileUpload1.PostedFile.SaveAs(svrFileName);
UZipFilesCreate uzipfilescreate = UZipFilesCreate.UZipFilesInstance();
uzipfilescreate.ZipFileName = svrFileName; // this.UploadFile.PostedFile.FileName.Trim();
uzipfilescreate.filesCreatePath = Server.MapPath(strRecoveryDir);
try
{
// uzipfilescreate.UZipCreateFiles();
ZipEntry entry;
// this.lblError1.Text = uzipfilescreate.ZipFileName.ToString();
ZipInputStream zipinputstream = new ZipInputStream(File.OpenRead(uzipfilescreate.ZipFileName)); // 此句有错误
while ((entry = zipinputstream.GetNextEntry()) != null )
{
// this.CreateDirList(entry.Name);
// string strPath = this.filescreatepath + "\\" + entry.Name;
// this.lblError1.Text = "in";
string strPath = uzipfilescreate.filesCreatePath + " \\ " + entry.Name;
FileStream streamWriter = File.Create(strPath);
byte [] data = new byte [entry.Size];
zipinputstream.Read(data, 0 , data.Length);
streamWriter.Write(data, 0 , data.Length);
streamWriter.Close();
File.SetLastWriteTime(strPath, entry.DateTime);
}
zipinputstream.Close();
}
catch (Exception ex)
{
string strex = ex.Message.ToString();
// this.lblError.Text = strex;
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('您刚刚恢复过,请稍后再试! " + strex.Replace( " ' " , "" ) + " ')</script> " );
return ;
}
#endregion
#region 取出导入文件的维修点和登录用户名,与系统登录的维修点和登录名比较,不匹配则提示错误
string strError = "" ;
DataTable dtTemp = null ;
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 个人.cat " ), ref strError);
if (dtTemp.Rows.Count != 1 )
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('备份源数据文件被破坏,不能进行恢复!')</script> " );
return ;
}
else
{
string strEncryptUid = dtTemp.Rows[ 0 ][ 0 ].ToString().Trim();
string strEncryptDate = dtTemp.Rows[ 0 ][ 1 ].ToString().Trim();
// string strSiteCode = PublicFunction.GetSiteCode(this.Request);
string strLoginUid = PublicFunction.GetUserID( this .Request);
if (csv.EncryptDES(strLoginUid, strEncryptKey) != strEncryptUid)
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('使用的文件非此登录用户的备份文件,不能进行恢复!')</script> " );
return ;
}
}
#endregion
#region csv 导出 datatable,并插入恢复数据
string uid = PublicFunction.GetUserID( this .Page.Request);
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 入库单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete ininventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
InInventory ininventory = new InInventory();
ininventory.InNo = item[ 1 ].ToString();
ininventory.Depot = item[ 2 ].ToString();
ininventory.InGong = item[ 3 ].ToString();
ininventory.InBrand = item[ 4 ].ToString();
ininventory.InModel = item[ 5 ].ToString();
ininventory.InName = item[ 6 ].ToString();
ininventory.InCount = int .Parse(item[ 7 ].ToString());
ininventory.InCost = decimal .Parse(item[ 8 ].ToString());
ininventory.InPeople = item[ 9 ].ToString();
ininventory.InRemark = item[ 10 ].ToString();
ininventory.InDate = DateTime.Parse(item[ 11 ].ToString());
ininventory.Uid = int .Parse(item[ 12 ].ToString());
if ( ! JIZHANGService.CreateInInventoryBLL().SaveEntity(ininventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 维修单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete inservice where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
InService inService = new InService();
inService.Customer = item[ 1 ].ToString();
inService.Sex = item[ 2 ].ToString();
inService.FixedPhone = item[ 3 ].ToString();
inService.MobilePhone = item[ 4 ].ToString();
inService.IMIE = item[ 5 ].ToString();
inService.IdNum = item[ 6 ].ToString();
inService.PhoneType = item[ 7 ].ToString();
inService.Brand = item[ 8 ].ToString();
inService.Model = item[ 9 ].ToString();
inService.PhoneClor = item[ 10 ].ToString();
inService.Facade = item[ 11 ].ToString();
inService.Symptom = item[ 12 ].ToString();
inService.Addon = item[ 13 ].ToString();
inService.Remark = item[ 14 ].ToString();
inService.WxDate = DateTime.Parse(item[ 15 ].ToString());
inService.WxType = item[ 16 ].ToString();
inService.WxNo = item[ 17 ].ToString();
inService.Uid = int .Parse(item[ 18 ].ToString());
inService.WxModel = item[ 19 ].ToString();
inService.WxPeople = item[ 20 ].ToString();
inService.WxResult = item[ 21 ].ToString();
inService.WxFab1 = item[ 22 ].ToString();
inService.WxFab2 = item[ 23 ].ToString();
inService.WxFab3 = item[ 24 ].ToString();
inService.WxFab4 = item[ 25 ].ToString();
inService.WxFab5 = item[ 26 ].ToString();
inService.WxFab6 = item[ 27 ].ToString();
inService.FabCost = decimal .Parse(item[ 28 ].ToString());
inService.FabPrice = decimal .Parse(item[ 29 ].ToString());
inService.FanRemark = item[ 30 ].ToString();
inService.FanShouType = item[ 31 ].ToString();
inService.FanShiPrice = decimal .Parse(item[ 32 ].ToString());
inService.FanPiaoType = item[ 33 ].ToString();
inService.FanXiaoPiao = item[ 34 ].ToString();
inService.WxwDate = DateTime.Parse(item[ 35 ].ToString());
inService.YnXF = item[ 36 ].ToString();
if ( ! JIZHANGService.CreateInServiceBLL().SaveEntity(inService))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 库存.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete inventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
Inventory inventory = new Inventory();
inventory.Brand = item[ 1 ].ToString();
inventory.Model = item[ 2 ].ToString();
inventory.InCount = int .Parse(item[ 3 ].ToString());
inventory.InCost = decimal .Parse(item[ 4 ].ToString());
inventory.InDate = DateTime.Parse(item[ 5 ].ToString());
inventory.Uid = int .Parse(item[ 6 ].ToString());
inventory.Depot = item[ 7 ].ToString();
inventory.Dname = item[ 8 ].ToString();
if ( ! JIZHANGService.CreateInventoryBLL().SaveEntity(inventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 出库单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete outinventory where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
OutInventory outInventory = new OutInventory();
outInventory.OutNo = item[ 1 ].ToString();
outInventory.Depot = item[ 2 ].ToString();
outInventory.OutGong = item[ 3 ].ToString();
outInventory.OutBrand = item[ 4 ].ToString();
outInventory.OutModel = item[ 5 ].ToString();
outInventory.OutName = item[ 6 ].ToString();
outInventory.OutCount = int .Parse(item[ 7 ].ToString());
outInventory.OutCost = decimal .Parse(item[ 8 ].ToString());
outInventory.OutPeople = item[ 9 ].ToString();
outInventory.OutRemark = item[ 10 ].ToString();
outInventory.OutDate = DateTime.Parse(item[ 11 ].ToString());
outInventory.Uid = int .Parse(item[ 12 ].ToString());
if ( ! JIZHANGService.CreateOutInventoryBLL().SaveEntity(outInventory))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 销售单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete salesinfo where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SalesInfo salesInfo = new SalesInfo();
salesInfo.SalesBrand = item[ 1 ].ToString();
salesInfo.SalesItem = item[ 2 ].ToString();
salesInfo.SalesCount = int .Parse(item[ 3 ].ToString());
salesInfo.SalesPrice = decimal .Parse(item[ 4 ].ToString());
salesInfo.SalesCost = decimal .Parse(item[ 5 ].ToString());
salesInfo.SaleNo = item[ 6 ].ToString();
salesInfo.SalesBackType = item[ 7 ].ToString();
salesInfo.SalesBackPrice = decimal .Parse(item[ 8 ].ToString());
salesInfo.Uid = int .Parse(item[ 9 ].ToString());
salesInfo.UserName = item[ 10 ].ToString();
salesInfo.Sex = item[ 11 ].ToString();
salesInfo.UserPhone = item[ 12 ].ToString();
salesInfo.PiaoType = item[ 13 ].ToString();
salesInfo.SmallPiao = item[ 14 ].ToString();
salesInfo.Remark = item[ 15 ].ToString();
salesInfo.SalesPeople = item[ 16 ].ToString();
salesInfo.SalesDate = DateTime.Parse(item[ 17 ].ToString());
salesInfo.SalesType = item[ 18 ].ToString();
salesInfo.SalesBackDate = DateTime.Parse(item[ 19 ].ToString());
if ( ! JIZHANGService.CreateSalesInfoBLL().SaveEntity(salesInfo))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 邮寄单.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete yj where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
YJ yj = new YJ();
yj.Brand = item[ 1 ].ToString();
yj.Model = item[ 2 ].ToString();
yj.Number = item[ 3 ].ToString();
yj.Count = int .Parse(item[ 4 ].ToString());
yj.UserName = item[ 5 ].ToString();
yj.Remark = item[ 6 ].ToString();
yj.SDate = DateTime.Parse(item[ 7 ].ToString());
yj.YJNO = item[ 8 ].ToString();
yj.YJUser = item[ 9 ].ToString();
yj.YJbao = item[ 10 ].ToString();
yj.Yname = item[ 11 ].ToString();
yj.Uid = int .Parse(item[ 12 ].ToString());
yj.YJDate = DateTime.Parse(item[ 13 ].ToString());
yj.YJState = item[ 14 ].ToString();
yj.YJBack = item[ 15 ].ToString();
yj.YJBDate = item[ 16 ].ToString();
if ( ! JIZHANGService.CreateYJBLL().SaveEntity(yj))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 销售产品设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete salesproductname where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SalesProductName spn = new SalesProductName();
spn.Uid = int .Parse(item[ 1 ].ToString());
spn.ProductName = item[ 2 ].ToString();
spn.ProductCost = decimal .Parse(item[ 3 ].ToString());
spn.ProductPrice = decimal .Parse(item[ 4 ].ToString());
spn.ProductBrand = item[ 5 ].ToString();
if ( ! JIZHANGService.CreateSalesProductNameBLL().SaveEntity(spn))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 品牌设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete sysbrand where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SYSBrand sb = new SYSBrand();
sb.Uid = int .Parse(item[ 1 ].ToString());
sb.Brand = item[ 2 ].ToString();
if ( ! JIZHANGService.CreateSYSBrandBLL().SaveEntity(sb))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 供应商设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete syssupplier where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
SYSSupplier sss = new SYSSupplier();
sss.Uid = int .Parse(item[ 1 ].ToString());
sss.Supplier = item[ 2 ].ToString();
sss.Phone = item[ 3 ].ToString();
sss.Address = item[ 4 ].ToString();
if ( ! JIZHANGService.CreateSYSSupplierBLL().SaveEntity(sss))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
dtTemp = csv.ImportCSVToDataTable(Server.MapPath(strRecoveryDir + " 共享数据设置.cat " ), ref strError);
DBHelper.ExecuteCommand( " delete usershare where uid= " + uid);
foreach (DataRow item in dtTemp.Rows)
{
UserShare us = new UserShare();
us.Uid = int .Parse(item[ 1 ].ToString());
us.Puid = int .Parse(item[ 2 ].ToString());
us.Pname = item[ 3 ].ToString();
us.Mname = item[ 4 ].ToString();
us.Rname = item[ 5 ].ToString();
if ( ! JIZHANGService.CreateUserShareBLL().SaveEntity(us))
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('恢复库存数据时失败! " + item[ 0 ] + " ')</script> " );
return ;
}
}
#endregion
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('数据恢复成功!')</script> " );
}
catch (Exception err)
{
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('数据恢复失败! " + err.Message.ToString().Replace( " ' " , "" ) + " ')</script> " );
}
}
protected void btnClear_Click( object sender, EventArgs e)
{
string uid = PublicFunction.GetUserID( this .Page.Request);
DBHelper.ExecuteCommand( " delete ininventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete usershare where uid= " + uid);
DBHelper.ExecuteCommand( " delete syssupplier where uid= " + uid);
DBHelper.ExecuteCommand( " delete sysbrand where uid= " + uid);
DBHelper.ExecuteCommand( " delete salesproductname where uid= " + uid);
DBHelper.ExecuteCommand( " delete yj where uid= " + uid);
DBHelper.ExecuteCommand( " delete salesinfo where uid= " + uid);
DBHelper.ExecuteCommand( " delete outinventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete inventory where uid= " + uid);
DBHelper.ExecuteCommand( " delete inservice where uid= " + uid);
this .ClientScript.RegisterClientScriptBlock( this .GetType(), " myKey " , " <script>alert('清空数据成功!')</script> " );
}
}
}