using JwKdsV.Core; using JwKdsV.Core.Download; using JwKdsV.Core.Utils; using JwKdsV.Entity.Normal; using JwKdsV.Entity.Product; using LiteDB; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace JwKdsV { public partial class DownloadingForm : BaseForm { private System.Timers.Timer timer; //执行间隔 private const int TIME_INTERVAL = 500; /// /// 是否开始下载 /// private bool isDownloading = false; /// /// 是否下载完成 /// private bool isFinished = false; private Dictionary downloadError = null; private Dictionary downloadSuccess = null; public DownloadingForm() { InitializeComponent(); this.Text = string.Format("巨为厨显({0})", Application.ProductVersion); this.timer = new System.Timers.Timer(TIME_INTERVAL); this.timer.Elapsed += OnTimerElapsed; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); //内容显示在中央 this.downLoadContainer.Location = new Point((this.Width - this.downLoadContainer.Width) / 2, (this.Height - this.downLoadContainer.Height) / 2); this.timer.Interval = TIME_INTERVAL; this.timer.Start(); } private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) { if (!isDownloading) { this.downloadError = new Dictionary(); this.downloadSuccess = new Dictionary(); isDownloading = true; if (Global.Instance.Worker.WorkerOnlineLogin) { //在线登录,下载 StartDownload(); } else { //离线登录,不下载,只组装数据 //StartInstall(); isFinished = true; } } if (isDownloading && isFinished) { this.timer.Stop(); this.timer.Enabled = false; //判断是否发生过错误 if (downloadError != null && downloadError.Keys.Count > 0) { this.DialogResult = DialogResult.None; } else { if (this.InvokeRequired) { this.BeginInvoke(new MethodInvoker(() => { this.DialogResult = DialogResult.OK; this.Close(); })); } else { this.DialogResult = DialogResult.OK; this.Close(); } } } } private void StartDownload() { this.downloadError = new Dictionary(); this.downloadSuccess = new Dictionary(); //清理历史缓存脚本 if (File.Exists(Global.Instance.CacheFullPath)) { File.Delete(Global.Instance.CacheFullPath); } RefreshStatus("清除缓存成功......"); RefreshStatus("开始下载门店数据......"); //清理历史数据 DownloadApi.Instance.InitCache(); //下载操作员数据 var worker = DownloadApi.DownloadWorker(); this.ShowMessage(this.lblStatus, worker.Message); if (worker.Success) { this.AddDownloadSuccess(worker.CacheName, worker); } else { this.AddDownloadError(worker.CacheName, worker); } //下载商品类别信息 var productType = DownloadApi.DownloadProductType(Constant.DEFAULT_PAGESIZE); this.ShowMessage(this.lblStatus, productType.Message); //第一页下载成功 if (productType.Success) { //判断是否全部下载成功 bool isAllSuccess = true; //记录最后下载失败的通知消息 DownloadNotify lastFailure = null; //分页下载 if (productType.IsPager) { //当前页码 int pageNum = productType.PageNumber; //总页数 int pageCount = productType.PageCount; //每页大小 int pageSize = productType.PageSize; //逐页下载,任意页下载失败退出下载 for (int page = pageNum + 1; page < pageCount + 1; page++) { var notify = DownloadApi.DownloadProductTypeNextPage(page, pageSize); if (notify.Success) { this.ShowMessage(this.lblStatus, notify.Message); } else { //最后的失败通知 lastFailure = notify; isAllSuccess = false; break; } System.Threading.Thread.Sleep(10); } } if (isAllSuccess && lastFailure == null) { this.AddDownloadSuccess(productType.CacheName, productType); } else { this.AddDownloadError(lastFailure.CacheName, lastFailure); } } else { this.AddDownloadError(productType.CacheName, productType); } //下载菜品 var product = DownloadApi.DownloadProduct(Constant.DEFAULT_PAGESIZE); this.ShowMessage(this.lblStatus, product.Message); //第一页下载成功 if (product.Success) { //判断是否全部下载成功 bool isAllSuccess = true; //记录最后下载失败的通知消息 DownloadNotify lastFailure = null; //分页下载 if (product.IsPager) { //当前页码 int pageNum = product.PageNumber; //总页数 int pageCount = product.PageCount; //每页大小 int pageSize = product.PageSize; //逐页下载,任意页下载失败退出下载 for (int page = pageNum + 1; page < pageCount + 1; page++) { var notify = DownloadApi.DownloadProductNextPage(page, pageSize); if (notify.Success) { this.ShowMessage(this.lblStatus, notify.Message); } else { //最后的失败通知 lastFailure = notify; isAllSuccess = false; break; } System.Threading.Thread.Sleep(10); } } if (isAllSuccess && lastFailure == null) { this.AddDownloadSuccess(product.CacheName, product); } else { this.AddDownloadError(lastFailure.CacheName, lastFailure); } } else { this.AddDownloadError(product.CacheName, product); } //下载商品单位信息 var productUnit = DownloadApi.DownloadProductUnit(Constant.DEFAULT_PAGESIZE); this.ShowMessage(this.lblStatus, productUnit.Message); //第一页下载成功 if (productUnit.Success) { //判断是否全部下载成功 bool isAllSuccess = true; //记录最后下载失败的通知消息 DownloadNotify lastFailure = null; //分页下载 if (productUnit.IsPager) { //当前页码 int pageNum = productUnit.PageNumber; //总页数 int pageCount = productUnit.PageCount; //每页大小 int pageSize = productUnit.PageSize; //逐页下载,任意页下载失败退出下载 for (int page = pageNum + 1; page < pageCount + 1; page++) { var notify = DownloadApi.DownloadProductUnitNextPage(page, pageSize); if (notify.Success) { this.ShowMessage(this.lblStatus, notify.Message); } else { //最后的失败通知 lastFailure = notify; isAllSuccess = false; break; } System.Threading.Thread.Sleep(10); } } if (isAllSuccess && lastFailure == null) { this.AddDownloadSuccess(productUnit.CacheName, productUnit); } else { this.AddDownloadError(lastFailure.CacheName, lastFailure); } } else { this.AddDownloadError(productUnit.CacheName, productUnit); } //下载商品规格信息 var productSpec = DownloadApi.DownloadProductSpec(Constant.DEFAULT_PAGESIZE); this.ShowMessage(this.lblStatus, productSpec.Message); //第一页下载成功 if (productSpec.Success) { //判断是否全部下载成功 bool isAllSuccess = true; //记录最后下载失败的通知消息 DownloadNotify lastFailure = null; //分页下载 if (productSpec.IsPager) { //当前页码 int pageNum = productSpec.PageNumber; //总页数 int pageCount = productSpec.PageCount; //每页大小 int pageSize = productSpec.PageSize; //逐页下载,任意页下载失败退出下载 for (int page = pageNum + 1; page < pageCount + 1; page++) { var notify = DownloadApi.DownloadProductSpecNextPage(page, pageSize); if (notify.Success) { this.ShowMessage(this.lblStatus, notify.Message); } else { //最后的失败通知 lastFailure = notify; isAllSuccess = false; break; } System.Threading.Thread.Sleep(10); } } if (isAllSuccess && lastFailure == null) { this.AddDownloadSuccess(productSpec.CacheName, productSpec); } else { this.AddDownloadError(lastFailure.CacheName, lastFailure); } } else { this.AddDownloadError(productSpec.CacheName, productSpec); } ////下载商品类别信息 //var productType = DownloadApi.DownloadProductType(Constant.DEFAULT_PAGESIZE); //this.ShowMessage(this.lblStatus, productType.Message); ////第一页下载成功 //if (productType.Success) //{ // //判断是否全部下载成功 // bool isAllSuccess = true; // //记录最后下载失败的通知消息 // DownloadNotify lastFailure = null; // //分页下载 // if (productType.IsPager) // { // //当前页码 // int pageNum = productType.PageNumber; // //总页数 // int pageCount = productType.PageCount; // //每页大小 // int pageSize = productType.PageSize; // //逐页下载,任意页下载失败退出下载 // for (int page = pageNum + 1; page < pageCount + 1; page++) // { // var notify = DownloadApi.DownloadProductTypeNextPage(page, pageSize); // if (notify.Success) // { // this.ShowMessage(this.lblStatus, notify.Message); // } // else // { // //最后的失败通知 // lastFailure = notify; // isAllSuccess = false; // break; // } // System.Threading.Thread.Sleep(10); // } // } // if (isAllSuccess && lastFailure == null) // { // //处理下载数据到SQL缓存中 // if (DownloadApi.CacheProductType(productType)) // { // this.AddDownloadSuccess(productType.CacheName, productType); // } // else // { // productType.Success = false; // productType.Operate = DownloadCacheName.ProductTypeException; // productType.Message = "处理商品类别信息发生异常......"; // this.AddDownloadError(productType.CacheName, productType); // } // } // else // { // this.AddDownloadError(lastFailure.CacheName, lastFailure); // } //} //else //{ // this.AddDownloadError(productType.CacheName, productType); //} ////下载菜品 //var product = DownloadApi.DownloadProduct(Constant.DEFAULT_PAGESIZE); //this.ShowMessage(this.lblStatus, product.Message); ////第一页下载成功 //if (product.Success) //{ // //判断是否全部下载成功 // bool isAllSuccess = true; // //记录最后下载失败的通知消息 // DownloadNotify lastFailure = null; // //分页下载 // if (product.IsPager) // { // //当前页码 // int pageNum = product.PageNumber; // //总页数 // int pageCount = product.PageCount; // //每页大小 // int pageSize = product.PageSize; // //逐页下载,任意页下载失败退出下载 // for (int page = pageNum + 1; page < pageCount + 1; page++) // { // var notify = DownloadApi.DownloadProductNextPage(page, pageSize); // if (notify.Success) // { // this.ShowMessage(this.lblStatus, notify.Message); // } // else // { // //最后的失败通知 // lastFailure = notify; // isAllSuccess = false; // break; // } // System.Threading.Thread.Sleep(10); // } // } // if (isAllSuccess && lastFailure == null) // { // //处理下载数据到SQL缓存中 // if (DownloadApi.CacheProduct(product)) // { // this.AddDownloadSuccess(product.CacheName, product); // } // else // { // product.Success = false; // product.Operate = DownloadCacheName.ProductException; // product.Message = "处理商品信息发生异常......"; // this.AddDownloadError(product.CacheName, product); // } // } // else // { // this.AddDownloadError(lastFailure.CacheName, lastFailure); // } //} //else //{ // this.AddDownloadError(product.CacheName, product); //} ////下载商品单位信息 //var productUnit = DownloadApi.DownloadProductUnit(Constant.DEFAULT_PAGESIZE); //this.ShowMessage(this.lblStatus, productUnit.Message); ////第一页下载成功 //if (productUnit.Success) //{ // //判断是否全部下载成功 // bool isAllSuccess = true; // //记录最后下载失败的通知消息 // DownloadNotify lastFailure = null; // //分页下载 // if (productUnit.IsPager) // { // //当前页码 // int pageNum = productUnit.PageNumber; // //总页数 // int pageCount = productUnit.PageCount; // //每页大小 // int pageSize = productUnit.PageSize; // //逐页下载,任意页下载失败退出下载 // for (int page = pageNum + 1; page < pageCount + 1; page++) // { // var notify = DownloadApi.DownloadProductUnitNextPage(page, pageSize); // if (notify.Success) // { // this.ShowMessage(this.lblStatus, notify.Message); // } // else // { // //最后的失败通知 // lastFailure = notify; // isAllSuccess = false; // break; // } // System.Threading.Thread.Sleep(10); // } // } // if (isAllSuccess && lastFailure == null) // { // //处理下载数据到SQL缓存中 // if (DownloadApi.CacheProductUnit(productUnit)) // { // this.AddDownloadSuccess(productUnit.CacheName, productUnit); // } // else // { // productUnit.Success = false; // productUnit.Operate = DownloadCacheName.ProductUnitException; // productUnit.Message = "处理商品单位信息发生异常......"; // this.AddDownloadError(productUnit.CacheName, productUnit); // } // } // else // { // this.AddDownloadError(lastFailure.CacheName, lastFailure); // } //} //else //{ // this.AddDownloadError(productUnit.CacheName, productUnit); //} ////下载商品单位信息 //var productSpec = DownloadApi.DownloadProductSpec(Constant.DEFAULT_PAGESIZE); //this.ShowMessage(this.lblStatus, productSpec.Message); ////第一页下载成功 //if (productSpec.Success) //{ // //判断是否全部下载成功 // bool isAllSuccess = true; // //记录最后下载失败的通知消息 // DownloadNotify lastFailure = null; // //分页下载 // if (productSpec.IsPager) // { // //当前页码 // int pageNum = productSpec.PageNumber; // //总页数 // int pageCount = productSpec.PageCount; // //每页大小 // int pageSize = productSpec.PageSize; // //逐页下载,任意页下载失败退出下载 // for (int page = pageNum + 1; page < pageCount + 1; page++) // { // var notify = DownloadApi.DownloadProductSpecNextPage(page, pageSize); // if (notify.Success) // { // this.ShowMessage(this.lblStatus, notify.Message); // } // else // { // //最后的失败通知 // lastFailure = notify; // isAllSuccess = false; // break; // } // System.Threading.Thread.Sleep(10); // } // } // if (isAllSuccess && lastFailure == null) // { // //处理下载数据到SQL缓存中 // if (DownloadApi.CacheProductSpec(productSpec)) // { // this.AddDownloadSuccess(productSpec.CacheName, productSpec); // } // else // { // productSpec.Success = false; // productSpec.Operate = DownloadCacheName.ProductSpecException; // productSpec.Message = "处理商品规格信息发生异常......"; // this.AddDownloadError(productSpec.CacheName, productSpec); // } // } // else // { // this.AddDownloadError(lastFailure.CacheName, lastFailure); // } //} //else //{ // this.AddDownloadError(productSpec.CacheName, productSpec); //} EndDownload(); } /// /// 处理下载结束后的工作 /// private void EndDownload() { try { if (downloadError != null && downloadError.Keys.Count > 0) { if(MessageBox.Show("数据下载失败,是否放弃更新,直接进入厨房显示系统?", "下载失败提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { StartInstall(); isFinished = true; downloadError = null; } else { MessageBox.Show("数据下载失败,请重新登录"); } } else { this.RefreshStatus("数据下载成功,更新中......"); var list = new ConcurrentQueue(); foreach (var entity in DownloadApi.Instance.Cache()) { list.Enqueue(entity.Sql); } lock (Global.Instance.CacheLock) { SQLiteUtils.ExecuteTransaction(list); } this.ProcessProductType(); //Stopwatch sw = Stopwatch.StartNew(); //var list = new ConcurrentQueue(); //using (var db = new LiteDatabase(Global.Instance.CacheFullPath)) //{ // var table = db.GetCollection(DownloadApi.SQL_CACHE_NAME); // var lists = table.FindAll(); // foreach (var entity in lists) // { // list.Enqueue(entity.Sql); // } //} //SQLiteUtils.ExecuteTransaction(list); StartInstall(); isFinished = true; } } catch (Exception ex) { logger.Error(ex, ex.Message); } } private bool ProcessProductType() { bool isException = false; try { List productTypes = null; using (var db = Global.Instance.OpenDataBase) { productTypes = db.Query().ToList(); } //将品类对应的单品数量汇总 if (productTypes != null) { var list = new ConcurrentQueue(); var sql = "update pos_product_type set products = {0} where id = '{1}';"; foreach (var entity in productTypes) { using (var db = Global.Instance.OpenDataBase) { var products = from o in db.Query() where o.Type >= 0 && o.Type <= 9 && o.StopFlag == 0 && o.TypePath.Contains(entity.Id) select o; list.Enqueue(string.Format(sql, products.Count(), entity.Id)); } } if (list.Count > 0) { lock (Global.Instance.SyncLock) { SQLiteUtils.ExecuteTransaction(list); } } } } catch (Exception ex) { isException = true; logger.Error(ex, "加工品类信息异常"); } return isException; } private void StartInstall() { //删除历史数据(3天前的数据) DeleteHistoryData(); } private void DeleteHistoryData() { this.RefreshStatus("正在清理垃圾数据……"); ConcurrentQueue lists = new ConcurrentQueue(); lists.Enqueue(string.Format("delete from [pos_order] where [saleDate] < '{0}'", DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd HH:mm:ss"))); lists.Enqueue(string.Format("delete from [pos_order_item] where [saleDate] < '{0}'", DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd HH:mm:ss"))); SQLiteUtils.ExecuteTransaction(lists); } private void AddDownloadError(string key, DownloadNotify notify) { if (this.downloadError.ContainsKey(key)) { this.downloadError.Remove(key); } this.downloadError.Add(key, notify); } private void AddDownloadSuccess(string key, DownloadNotify notify) { if (this.downloadSuccess.ContainsKey(key)) { this.downloadSuccess.Remove(key); } this.downloadSuccess.Add(key, notify); } /// /// 显示下载进程描述 /// /// private void RefreshStatus(string message) { logger.Info(message); if (this.lblStatus.InvokeRequired) { this.BeginInvoke(new MethodInvoker(() => { this.lblStatus.Text = message; this.lblStatus.Refresh(); })); } else { this.lblStatus.Text = message; this.lblStatus.Refresh(); } } } }