1 using BO; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Configuration; 6 using System.Data; 7 using System.Drawing; 8 using System.IO; 9 using System.Linq; 10 using System.Net; 11 using System.Text; 12 using System.Threading; 13 using System.Windows.Forms; 14 using System.Xml; 15 16 namespace GncQuene.Forms 17 { 18 /// <summary> 19 /// 同步数据进度窗体 2014-12-16 20 /// </summary> 21 public partial class ProgressBarFrm : Form 22 { 23 long _totalBytes, _currentBytes;//同步数据的最大值和当前值 24 string formName;//窗体的text 25 string action, totalSize, currentFile, speed;//对应界面中的label 26 private Thread _workThread;//声明线程 27 public bool Completed = false;//进度是否完成,默认false 28 public bool Failed = false;//是否成功同步,默认false 29 public ProgressBarFrm() 30 { 31 InitializeComponent(); 32 } 33 /// <summary> 34 /// 读取门店编号 35 /// </summary> 36 /// <param name="prog"></param> 37 /// <param name="label1"></param> 38 public void UpdateVersion() 39 { 40 BO.QueueBO bo = new BO.QueueBO(); 41 string store_ID = ""; 42 string closeStoreTime = "05:00:00"; 43 XmlDocument dom = new XmlDocument(); 44 string xmlPath = Application.StartupPath + "\\" + "PosConfig.xml"; 45 try 46 { 47 if (!File.Exists(xmlPath)) 48 { 49 MessageBox.Show("xml文件不存在 " + xmlPath); 50 return; 51 } 52 dom.Load(xmlPath); 53 //读取数据 54 XmlElement rno = (XmlElement)dom.DocumentElement.SelectSingleNode("StoreId"); 55 if (rno == null) 56 { 57 MessageBox.Show("读取门店编号失败 " + xmlPath); 58 return; 59 } 60 else 61 { 62 store_ID = rno.InnerText; 63 } 64 } 65 catch (Exception err) 66 { 67 MessageBox.Show("读取门店编号失败 " + xmlPath); 68 return; 69 } 70 ClassSocket.CheckOffLine(store_ID, closeStoreTime); 71 //获取服务器最新logo 72 try 73 { 74 if (!BO.SqlHelperExtension.OffLine) 75 { 76 syncTable(store_ID); 77 Completed = true; 78 } 79 else 80 { 81 Failed = true;//同步失败 82 } 83 } 84 catch (Exception err) 85 { 86 LogCommon.LogLocalInfor("AppLog", err.Message); 87 MessageBox.Show("数据同步失败" + err.Message); 88 Failed = true;//同步失败 89 } 90 } 91 /// <summary> 92 /// 数据同步 93 /// </summary> 94 /// <returns></returns> 95 public void syncTable(string storeID) 96 { 97 string strSQL = ""; 98 string strLocalTempPath = ""; 99 WebFileLoader wfl = new WebFileLoader(); 100 int version = 0; 101 bool bFindOK = false; 102 103 #region 联网情况下 104 //联网情况下 105 if (!SqlHelperExtension.OffLine) 106 { 107 108 DataSet ds = new DataSet(); 109 DataTable dt = new DataTable(); 110 111 QueueBO bo = new QueueBO(); 112 string serverWebSite = bo.GetParam(storeID, "ServerWebSite"); 113 114 strLocalTempPath = Application.StartupPath + "\\" + ConfigurationManager.AppSettings["LocalTempPath"].ToString(); 115 #region 同步参数表 116 //同步参数表 117 try 118 { 119 if (!Directory.Exists(strLocalTempPath)) 120 { 121 Directory.CreateDirectory(strLocalTempPath); 122 } 123 124 version = 0; 125 strSQL = string.Format("select ISNULL(max(ISNULL(VERSION,0)),0) VERSION from T_TB_CONFIG "); 126 //dt = CSqlite.ExecuteQueryTable(strSQL); 127 dt = SqlHelperExtensionLocal.ExecuteDataset(strSQL).Tables[0]; 128 if (dt != null && dt.Rows.Count > 0) 129 { 130 version = dt.Rows[0]["VERSION"].ToString() == "" ? 0 : Convert.ToInt32(dt.Rows[0]["VERSION"].ToString()); 131 } 132 strSQL = string.Format("select * from T_TB_CONFIG where STORE_ID = {0} and VERSION >{1} order by VERSION ", storeID, version); 133 ds = SqlHelperExtension.ExecuteDataset(strSQL); 134 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 135 { 136 //进度条的最大值 137 _totalBytes = ds.Tables[0].Rows.Count; 138 totalSize = _totalBytes.ToString(); 139 action = "正在同步参数表"; 140 currentFile = "参数表"; 141 formName = "正在同步参数表"; 142 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 143 { 144 //System.Windows.Forms.Application.DoEvents(); 145 146 //进度条的value 147 _currentBytes = i; 148 speed = _currentBytes.ToString(); 149 //先删除后插入 150 151 strSQL = string.Format("DELETE FROM T_TB_CONFIG WHERE PARAMETER_ID=‘{0}‘", ds.Tables[0].Rows[i]["PARAMETER_ID"].ToString()); 152 //CSqlite.ExecuteNonQuery(strSQL); 153 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 154 strSQL = string.Format(@"insert into T_TB_CONFIG(STORE_ID,PARAMETER_ID,PARAMETER_DESCR,PARAMETER_TYPE,PARAMETER_VALUE 155 ,PARAMETER_DEFAULT,CREATED_ON,CREATED_BY,UPDATED_ON,UPDATED_BY,DELETE_FLAG,VERSION) 156 values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,‘{11}‘)" 157 , ds.Tables[0].Rows[i]["STORE_ID"].ToString() 158 , ds.Tables[0].Rows[i]["PARAMETER_ID"].ToString(), ds.Tables[0].Rows[i]["PARAMETER_DESCR"].ToString() 159 , ds.Tables[0].Rows[i]["PARAMETER_TYPE"].ToString(), ds.Tables[0].Rows[i]["PARAMETER_VALUE"].ToString() 160 , ds.Tables[0].Rows[i]["PARAMETER_DEFAULT"].ToString(), ds.Tables[0].Rows[i]["CREATED_ON"].ToString() 161 , ds.Tables[0].Rows[i]["CREATED_BY"].ToString(), ds.Tables[0].Rows[i]["UPDATED_ON"].ToString() 162 , ds.Tables[0].Rows[i]["UPDATED_BY"].ToString(), ds.Tables[0].Rows[i]["DELETE_FLAG"].ToString() 163 , ds.Tables[0].Rows[i]["VERSION"].ToString()); 164 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 165 166 } 167 } 168 } 169 catch (Exception e) 170 { 171 //CSqlite.WriteLogInfo(e.Message); 172 SqlHelperExtensionLocal.WriteLogInfo(e.Message); 173 Failed = true;//同步失败 174 } 175 #endregion 176 177 #region 同步播放列表 178 try 179 { 180 //同步播放列表 181 version = 0; 182 strSQL = string.Format("select ISNULL(max(ISNULL(VERSION,0)),0) VERSION from T_TB_PLAY_LIST where STORE_ID = {0} ", storeID); 183 //dt = CSqlite.ExecuteQueryTable(strSQL); 184 dt = SqlHelperExtensionLocal.ExecuteDataset(strSQL).Tables[0]; 185 if (dt != null && dt.Rows.Count > 0) 186 { 187 version = dt.Rows[0]["VERSION"].ToString() == "" ? 0 : Convert.ToInt32(dt.Rows[0]["VERSION"].ToString()); 188 } 189 190 strSQL = string.Format("select * from T_TB_PLAY_LIST where STORE_ID = {0} and VERSION > {1} order by VERSION ", storeID, version); 191 ds = SqlHelperExtension.ExecuteDataset(strSQL); 192 //获取下载的文件列表 193 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 194 { 195 //进度条的最大值 196 _totalBytes = ds.Tables[0].Rows.Count; 197 totalSize = _totalBytes.ToString(); 198 action = "正在同步播放列表"; 199 currentFile = "播放列表"; 200 formName = "正在同步播放列表"; 201 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 202 { 203 //System.Windows.Forms.Application.DoEvents(); 204 205 //进度条的value 206 _currentBytes = i; 207 speed = _currentBytes.ToString(); 208 //先删除后插入 209 strSQL = string.Format("DELETE FROM T_TB_PLAY_LIST WHERE PLAY_ID={0}", ds.Tables[0].Rows[i]["PLAY_ID"].ToString()); 210 //CSqlite.ExecuteNonQuery(strSQL); 211 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 212 strSQL = string.Format(@"insert into T_TB_PLAY_LIST(STORE_ID,PLAY_ID,AD_ID,PLAY_INDEX 213 ,CREATED_ON,CREATED_BY,UPDATED_ON,UPDATED_BY,DELETE_FLAG,VERSION) 214 values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘)" 215 , ds.Tables[0].Rows[i]["STORE_ID"].ToString() 216 , ds.Tables[0].Rows[i]["PLAY_ID"].ToString() 217 , ds.Tables[0].Rows[i]["AD_ID"].ToString() 218 , ds.Tables[0].Rows[i]["PLAY_INDEX"].ToString() 219 , ds.Tables[0].Rows[i]["CREATED_ON"].ToString() 220 , ds.Tables[0].Rows[i]["CREATED_BY"].ToString() 221 , ds.Tables[0].Rows[i]["UPDATED_ON"].ToString() 222 , ds.Tables[0].Rows[i]["UPDATED_BY"].ToString() 223 , ds.Tables[0].Rows[i]["DELETE_FLAG"].ToString() 224 , ds.Tables[0].Rows[i]["VERSION"].ToString()); 225 //CSqlite.ExecuteNonQuery(strSQL); 226 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 227 } 228 } 229 } 230 catch (Exception e) 231 { 232 //CSqlite.WriteLogInfo(e.Message); 233 SqlHelperExtensionLocal.WriteLogInfo(e.Message); 234 Failed = true;//同步失败 235 } 236 #endregion 237 238 #region 同步员工 239 try 240 { 241 //同步员工 242 version = 0; 243 strSQL = string.Format("select ISNULL(max(ISNULL(VERSION,0)),0) VERSION from t_user "); 244 //dt = CSqlite.ExecuteQueryTable(strSQL); 245 dt = SqlHelperExtensionLocal.ExecuteDataset(strSQL).Tables[0]; 246 if (dt != null && dt.Rows.Count > 0) 247 { 248 version = dt.Rows[0]["VERSION"].ToString() == "" ? 0 : Convert.ToInt32(dt.Rows[0]["VERSION"].ToString()); 249 } 250 251 strSQL = string.Format("select * from t_user where VERSION > {0} order by VERSION ", version); 252 //strSQL = string.Format("select * from t_user"); 253 ds = SqlHelperExtension.ExecuteDataset(strSQL); 254 //获取下载的文件列表 255 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 256 { 257 //进度条的最大值 258 _totalBytes = ds.Tables[0].Rows.Count; 259 totalSize = _totalBytes.ToString(); 260 action = "正在同步员工列表"; 261 currentFile = "员工列表"; 262 formName = "正在同步员工列表"; 263 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 264 { 265 //System.Windows.Forms.Application.DoEvents(); 266 267 //进度条的value 268 _currentBytes = i; 269 speed = _currentBytes.ToString(); 270 //先删除后插入 271 strSQL = string.Format("DELETE FROM t_user WHERE ID={0}", ds.Tables[0].Rows[i]["ID"].ToString()); 272 //CSqlite.ExecuteNonQuery(strSQL); 273 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 274 strSQL = string.Format(@"insert into t_user(Id,Name,Password,Enabled,VERSION) 275 values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘)" 276 , ds.Tables[0].Rows[i]["Id"].ToString() 277 , ds.Tables[0].Rows[i]["Name"].ToString() 278 , ds.Tables[0].Rows[i]["Password"].ToString() 279 , ds.Tables[0].Rows[i]["Enabled"].ToString() 280 , ds.Tables[0].Rows[i]["VERSION"].ToString()); 281 //CSqlite.ExecuteNonQuery(strSQL); 282 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 283 } 284 } 285 } 286 catch (Exception e) 287 { 288 //CSqlite.WriteLogInfo(e.Message); 289 SqlHelperExtensionLocal.WriteLogInfo(e.Message); 290 Failed = true;//同步失败 291 } 292 #endregion 293 294 #region 同步文件列表 295 try 296 { 297 #region 下载文件 298 strSQL = string.Format("select * from T_TB_AD where DELETE_FLAG=0 and STORE_ID = {0}", storeID); 299 ds = SqlHelperExtension.ExecuteDataset(strSQL); 300 //获取下载的文件列表 301 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 302 { 303 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 304 { 305 string filepath = ds.Tables[0].Rows[i]["FILE_PATH"].ToString().Replace("~", ""); 306 string filename = ds.Tables[0].Rows[i]["FILE_NAME"].ToString(); 307 string copypath = Application.StartupPath + "\\" + filepath; 308 string localPath = Application.StartupPath + filepath + filename;//本地资源路径 309 if (!Directory.Exists(copypath)) 310 { 311 Directory.CreateDirectory(copypath); 312 } 313 //如果本地上文件不存在,则下载图片 314 if (!File.Exists(localPath)) 315 { 316 bFindOK = GetFileFromHttp(serverWebSite, filepath, strLocalTempPath, filename); 317 if (bFindOK) 318 { 319 if (File.Exists(strLocalTempPath + "\\" + filename)) 320 { 321 if (File.Exists(copypath + "\\" + filename)) 322 { 323 File.SetAttributes(copypath + "\\" + filename, FileAttributes.Normal); 324 } 325 File.Copy(strLocalTempPath + "\\" + filename, copypath + "\\" + filename, true); 326 } 327 } 328 } 329 } 330 } 331 #endregion 332 333 #region 同步表T_TB_AD 334 //同步文件列表 335 version = 0; 336 strSQL = string.Format("select ISNULL(max(ISNULL(VERSION,0)),0) VERSION from T_TB_AD where STORE_ID = {0} ", storeID); 337 //dt = CSqlite.ExecuteQueryTable(strSQL); 338 dt = SqlHelperExtensionLocal.ExecuteDataset(strSQL).Tables[0]; 339 if (dt != null && dt.Rows.Count > 0) 340 { 341 version = Convert.ToInt32(dt.Rows[0]["VERSION"].ToString()); 342 } 343 344 strSQL = string.Format("select * from T_TB_AD where STORE_ID = {0} and VERSION > {1} order by VERSION ", storeID, version); 345 ds = SqlHelperExtension.ExecuteDataset(strSQL); 346 //获取下载的文件列表 347 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 348 { 349 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 350 { 351 //先删除后插入 352 strSQL = string.Format("DELETE FROM T_TB_AD WHERE ID={0}", ds.Tables[0].Rows[i]["ID"].ToString()); 353 //CSqlite.ExecuteNonQuery(strSQL); 354 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 355 356 strSQL = string.Format(@"insert into T_TB_AD(ID,STORE_ID,FILE_NAME,FILE_PATH,FILE_TYPE 357 ,TITLE,CONTENT,AUDIT_STATUS,AUDIT_BY,AUDIT_ON,CREATED_ON,CREATED_BY,UPDATED_ON,UPDATED_BY,DELETE_FLAG,VERSION) 358 values({0},{1},‘{2}‘,‘{3}‘,{4},‘{5}‘,‘{6}‘,‘{7}‘,‘{8}‘,‘{9}‘,‘{10}‘,‘{11}‘,‘{12}‘,‘{13}‘,‘{14}‘,{15})" 359 , ds.Tables[0].Rows[i]["ID"].ToString() 360 , ds.Tables[0].Rows[i]["STORE_ID"].ToString() 361 , ds.Tables[0].Rows[i]["FILE_NAME"].ToString() 362 , ds.Tables[0].Rows[i]["FILE_PATH"].ToString() 363 , ds.Tables[0].Rows[i]["FILE_TYPE"].ToString() 364 , ds.Tables[0].Rows[i]["TITLE"].ToString() 365 , ds.Tables[0].Rows[i]["CONTENT"].ToString() 366 , ds.Tables[0].Rows[i]["AUDIT_STATUS"].ToString() 367 , ds.Tables[0].Rows[i]["AUDIT_BY"].ToString() 368 , ds.Tables[0].Rows[i]["AUDIT_ON"].ToString() 369 , ds.Tables[0].Rows[i]["CREATED_ON"].ToString() 370 , ds.Tables[0].Rows[i]["CREATED_BY"].ToString() 371 , ds.Tables[0].Rows[i]["UPDATED_ON"].ToString() 372 , ds.Tables[0].Rows[i]["UPDATED_BY"].ToString() 373 , ds.Tables[0].Rows[i]["DELETE_FLAG"].ToString() 374 , ds.Tables[0].Rows[i]["VERSION"].ToString()); 375 //CSqlite.ExecuteNonQuery(strSQL); 376 SqlHelperExtensionLocal.ExecuteNoQuery(strSQL); 377 } 378 } 379 #endregion 380 } 381 catch (Exception e) 382 { 383 //CSqlite.WriteLogInfo(e.Message); 384 SqlHelperExtensionLocal.WriteLogInfo(e.Message); 385 Failed = true;//同步失败 386 } 387 #endregion 388 } 389 #endregion 390 #region 脱网情况下 391 else 392 { 393 Failed = true;//同步失败 394 } 395 #endregion 396 } 397 /// <summary> 398 /// 同步文件 399 /// </summary> 400 /// <param name="serverWebSite"></param> 401 /// <param name="HttpPath"></param> 402 /// <param name="TempFolderPath"></param> 403 /// <param name="FileName"></param> 404 /// <returns></returns> 405 public bool GetFileFromHttp(string serverWebSite, string HttpPath, string TempFolderPath, string FileName) 406 { 407 float percent = 0; 408 bool bResult = false; 409 try 410 { 411 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverWebSite + "/" + HttpPath + "/" + FileName); 412 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 413 _totalBytes = response.ContentLength; 414 415 //创建一个文件流 416 FileStream fs = null; 417 Stream responseStream = null; 418 responseStream = response.GetResponseStream(); 419 if (!Directory.Exists(TempFolderPath)) 420 { 421 Directory.CreateDirectory(TempFolderPath); 422 } 423 fs = File.Open(TempFolderPath + "/" + FileName, FileMode.OpenOrCreate, FileAccess.Write); 424 if (fs != null) 425 { 426 _currentBytes = 0; 427 int buffer_count = 1024; 428 byte[] buffer = new byte[buffer_count]; 429 int size = responseStream.Read(buffer, 0, buffer_count); 430 action = "正在同步文件列表"; 431 totalSize = (int)(_totalBytes / 1024) + "KB"; 432 currentFile = FileName; 433 formName = "正在同步文件列表"; 434 while (size > 0) 435 { 436 _currentBytes = size + _currentBytes; 437 fs.Write(buffer, 0, size); 438 size = responseStream.Read(buffer, 0, buffer_count); 439 percent = _currentBytes * 100 / _totalBytes; 440 speed = percent.ToString() + "%"; 441 } 442 } 443 fs.Flush(); 444 fs.Close(); 445 responseStream.Close(); 446 bResult = true; 447 return bResult; 448 449 } 450 catch (Exception e) 451 { 452 //CSqlite.WriteLogInfo(e.Message); 453 SqlHelperExtensionLocal.WriteLogInfo(e.Message); 454 Failed = true;//同步失败 455 return bResult; 456 } 457 } 458 459 460 /// <summary> 461 /// 进度取消 462 /// </summary> 463 /// <param name="sender"></param> 464 /// <param name="e"></param> 465 private void btnCancel_Click(object sender, EventArgs e) 466 { 467 _workThread.Abort();//结束线程 468 Application.Exit(); 469 } 470 /// <summary> 471 /// 窗体load事件 472 /// </summary> 473 /// <param name="sender"></param> 474 /// <param name="e"></param> 475 private void ProgressBarFrm_Load(object sender, EventArgs e) 476 { 477 timer1.Enabled = true; 478 _workThread = new Thread(UpdateVersion); 479 _workThread.IsBackground = true; 480 _workThread.Start(); 481 } 482 /// <summary> 483 /// timer 484 /// </summary> 485 /// <param name="sender"></param> 486 /// <param name="e"></param> 487 private void timer1_Tick(object sender, EventArgs e) 488 { 489 try 490 { 491 timer1.Enabled = false;//先禁用timer,因为timer会改变Completed,Failed的值 492 if (Completed) 493 { 494 Completed = false; 495 lblAction.Text = "Completed."; 496 lblTotalSize.Text = "Completed."; 497 lblCurrentFile.Text = "Completed."; 498 lblSpeed.Text = "Completed."; 499 _workThread.Abort();//结束线程 500 timer1.Enabled = false;//timer不可用 501 this.Hide();//隐藏当前窗体 502 //进度加载完成后,启动登陆窗体 503 QueueForm baseFrm = new QueueForm(); 504 GncQuene.Forms.LoginFrm login = new GncQuene.Forms.LoginFrm(baseFrm); 505 login.ShowDialog(); 506 return; 507 } 508 //如果同步失败,弹出确认对话框,是继续等待还是跳过数据同步 509 if (Failed) 510 { 511 Failed = false; 512 MessageBoxButtons messButton = MessageBoxButtons.YesNo; 513 DialogResult dr = MessageBox.Show("网络已断开,是否继续同步数据?点击是:继续等待同步数据;点击否:离线使用,跳转到登陆界面", "是否继续同步数据", messButton, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); 514 if (dr == DialogResult.No)//如果点击“否”按钮 515 { 516 Completed = true; 517 } 518 else 519 { 520 _workThread.Abort(); 521 _workThread = new Thread(UpdateVersion); 522 _workThread.IsBackground = true; 523 _workThread.Start(); 524 } 525 } 526 progressBar1.Maximum = (int)_totalBytes; 527 progressBar1.Value = (int)_currentBytes; 528 lblAction.Text = action;//actionLabel 529 lblTotalSize.Text = totalSize;//totalSizeLabel 530 lblCurrentFile.Text = currentFile;//currentFilelabel 531 lblSpeed.Text = speed;//speedlabel 532 this.Text = formName;//窗体命名 533 534 timer1.Enabled = true;//开启timer 535 } 536 catch 537 { 538 } 539 } 540 541 } 542 }
时间: 2024-10-08 17:34:45