public void DownloadProductSync() { var taskCount = 1; MultiThreadResetEvent threadEvent = new MultiThreadResetEvent(taskCount); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadProduct), threadEvent); threadEvent.WaitAll(); threadEvent.Dispose(); }
public void DownloadData() { var taskCount = 7; MultiThreadResetEvent threadEvent = new MultiThreadResetEvent(taskCount); //启动线程池来进行多线程下载数据 ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadAccount), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadProduct), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadStore), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadVipCard), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadVipProduct), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadProductAreaPrice), threadEvent); ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadProductStorePrice), threadEvent); threadEvent.WaitAll(); threadEvent.Dispose(); }
private void DownloadStore(object state) { MultiThreadResetEvent are = (MultiThreadResetEvent)state; try { // 下载数据 string url = string.Format("{0}/PosSync/QueryStore?{1}", _serverUrl, BuildAccessToken()); _log.Info("开始下载门店数据,请求{0}", url); var result = HttpHelper.HttpGet(url); if (!string.IsNullOrEmpty(result)) { var rows = JsonConvert.DeserializeObject <List <Store> >(result); //入库 _log.Info("已下载门店数据{0}条", rows.Count); string sql = "INSERT INTO Store (Id,Code,Name,LicenseCode)VALUES (@Id,@Code,@Name,@LicenseCode)"; var usql = "update Store set Code=@Code,Name=@Name,LicenseCode=@LicenseCode where Id=@Id"; foreach (var entity in rows) { if (_db.ExecuteScalar <int>("select count(*) from Store where Id=@Id", new { Id = entity.Id }) > 0) { _db.ExecuteSql(usql, entity); } else { _db.ExecuteSql(sql, entity); } } _log.Info("结束下载门店数据"); } else { _log.Info("门店数据为空,下载失败"); } } catch (Exception ex) { _log.Error(ex, "门店数据下载失败"); } are.Set(); }
private void DownloadAccount(object state) { MultiThreadResetEvent are = (MultiThreadResetEvent)state; try { string url = string.Format("{0}/PosSync/QueryAccount?{1}", _serverUrl, BuildAccessToken()); // 下载数据 _log.Info("开始下载账户数据,请求{0}", url); var result = HttpHelper.HttpGet(url); if (!string.IsNullOrEmpty(result)) { var rows = JsonConvert.DeserializeObject <List <Account> >(result); _log.Info("已下载账户数据{0}条", rows.Count); var sql = "INSERT INTO Account (Id,UserName,Password,StoreId,Status,RoleId,NickName)VALUES (@Id,@UserName,@Password,@StoreId,@Status,@RoleId,@NickName)"; var usql = "update Account set UserName=@UserName,Password=@Password,StoreId=@StoreId,Status=@Status,RoleId=@RoleId,NickName=@NickName where Id=@Id"; foreach (var entity in rows) { if (_db.ExecuteScalar <int>("select count(*) from Account where Id=@Id", new { Id = entity.Id }) > 0) { _db.ExecuteSql(usql, entity); } else { _db.ExecuteSql(sql, entity); } } _log.Info("结束账号数据下载"); } else { _log.Info("下载账号数据为空,下载失败."); } } catch (Exception ex) { _log.Error(ex, "下载数据失败"); } are.Set(); }
private void DownloadProductAreaPrice(object state) { MultiThreadResetEvent are = (MultiThreadResetEvent)state; try { // 下载数据 string url = string.Format("{0}/PosSync/QueryProductAreaPrice?{1}", _serverUrl, BuildAccessToken()); _log.Info("开始下载商品区域价数据,请求{0}", url); var result = HttpHelper.HttpGet(url); if (!string.IsNullOrEmpty(result)) { var rows = JsonConvert.DeserializeObject <List <ProductAreaPrice> >(result); //入库 _log.Info("已下载商品区域价数据{0}条", rows.Count); string sql = "INSERT INTO ProductAreaPrice (Id,ProductId,AreaId,SalePrice)VALUES (@Id,@ProductId,@AreaId,@SalePrice)"; var usql = "update ProductAreaPrice set ProductId=@ProductId,SalePrice=@SalePrice,AreaId=@AreaId where Id=@Id"; foreach (var entity in rows) { if (_db.ExecuteScalar <int>("select count(*) from ProductAreaPrice where Id=@Id", new { Id = entity.Id }) > 0) { _db.ExecuteSql(usql, entity); } else { _db.ExecuteSql(sql, entity); } } _log.Info("结束下载商品区域价数据"); } else { _log.Info("下载数据为空,下载商品区域价失败"); } } catch (Exception ex) { _log.Error(ex, "下载失败"); } are.Set(); }
public void DownloadProduct(object state) { MultiThreadResetEvent are = (MultiThreadResetEvent)state; string url = string.Format("{0}/PosSync/QueryProduct?{1}", _serverUrl, BuildAccessToken()); // 下载数据 _log.Info("开始下载商品数据,请求{0}", url); var result = HttpHelper.HttpGet(url); if (!string.IsNullOrEmpty(result)) { var rows = JsonConvert.DeserializeObject <List <Product> >(result); //入库 _log.Info("已下载商品数据{0}条", rows.Count); string sql = "INSERT INTO Product (Id,Code,Name,BarCode,Specification,Unit,SalePrice,UpdatedOn) values (@Id,@Code,@Name,@BarCode,@Specification,@Unit,@SalePrice,@UpdatedOn)"; var usql = "update Product set Code=@Code,Name=@Name,BarCode=@BarCode,Specification=@Specification,Unit=@Unit,SalePrice=@SalePrice,UpdatedOn=@UpdatedOn where Id=@Id"; foreach (var entity in rows) { if (_db.ExecuteScalar <int>("select count(*) from Product where Id=@Id", new { Id = entity.Id }) > 0) { _log.Info("更新:id={0},code={1},barcode={2},saleprice={3}", entity.Id, entity.Code, entity.BarCode, entity.SalePrice); _db.ExecuteSql(usql, entity); } else { _log.Info("添加:id={0},code={1},barcode={2},saleprice={3}", entity.Id, entity.Code, entity.BarCode, entity.SalePrice); _db.ExecuteSql(sql, entity); } } _log.Info("结束下载商品数据"); } else { _log.Info("下载数据为空,下载失败"); } // 标记线程执行完毕 are.Set(); }