using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Windows.Forms; namespace InvoicingTool { class DownLoadHelper { #region 属性 string str_t_sellercommodity = "t_sellercommodity"; //采购商品表名 string str_t_sellercommoditytype = "t_sellercommoditytype"; //采购商品类型表名 string str_t_roleprice = "t_roleprice"; //采购商品价格表名 //采购商品表字段信息 string sellercommodityField = "sellercommodity_id, sellerbusinesstype, commodity_code, commodity_name, commodity_barcode, " + "commodity_boxcode, commodity_allname, commodity_en, commodity_unit, commodity_rule, commodity_ori, " + "commodity_grade, commodity_state, commodity_desc, addtime, isbulk, meteringmethod, seller_id," + "sellercommoditytype_id, boxentry_count, supplier_id"; //采购商品类别表字段信息 string sellercommoditytypeField = "sellercommoditytype_id, type_name, type_state, type_desc, addtime, type_pid, type_index, seller_id, serverpart_id, " + "serverpartshop_id, autotype_staff, autotype_typeid, autotype_typename, warehouse_id, warehouse_name"; //采购商品价格表字段信息 string rolepriceField = "roleprice_id, seller_id, supplier_id, sellercommodity_id, commodity_code, barcode, roleprice_name, " + "commodity_boxsellprice, selltaxprice, sellprice, sellduty_paragraph, commodity_boxcostprice, costtaxprice, costprice, " + "duty_paragraph, roleprice_desc"; string str_SellerCommodity_addr = "http://183.129.232.100:8010/MobileServicePlatform/Handler/handler_ajax.ashx?action_type=DownLoadSeller&action_data=SellerCommodity"; string str_SellerCommoditytype_addr = "http://183.129.232.100:8010/MobileServicePlatform/Handler/handler_ajax.ashx?action_type=DownLoadSeller&action_data=SellerCommoditytype"; string str_Roleprice_addr = "http://183.129.232.100:8010/MobileServicePlatform/Handler/handler_ajax.ashx?action_type=DownLoadSeller&action_data=RolePrice"; string str_SellerCommodityById = "http://183.129.232.100:8010/MobileServicePlatform/Handler/handler_ajax.ashx?action_type=DownLoadSeller&action_data=SellerCommodityById"; //string str_SellerCommodityById = "http://192.168.10.110:8010/MobileServicePlatform/Handler/handler_ajax.ashx?action_type=DownLoadSeller&action_data=SellerCommodityById"; QualityHelper _QualityHelper = new QualityHelper(); #endregion /// /// 下载商品库信息 /// /// 门店ID /// 起始时间(可选) /// 是否服务区 /// 价格类型 /// 接口类型 /// 接口数据类型 /// 接口URL地址 /// 接口路径 /// public Boolean SellerCommodityById(string serverPartShopID, string strDate = "", Boolean isServerPart = false, string rolePriceName = "1021", string actionType = "DownLoadSeller", string actionData = "SellerCommodityById", string ajaxURL = "http://183.129.232.100:8010", string ajaxPath = "/MobileServicePlatform/Handler/handler_ajax.ashx") { //string strDate = ""; //try //{ // strDate = DBHelper.QueryOdbc("select max(addtime) from t_sellercommodity").Tables[0].Rows[0][0].ToString(); //} //catch //{ // strDate = ""; //} //string url = str_SellerCommodityById + "&action_record=" + strDate + "|" + _QualityHelper.serverpartshop_id; string url = ajaxURL + ajaxPath + "?action_type=" + actionType + "&action_data=" + actionData + "&action_record=" + strDate + "|" + serverPartShopID; try { using (DataSet _DataSet = JsonHelper.JsonToDataSet(GetAjaxData(url, isServerPart))) { if (_DataSet.Tables.Count > 0) { foreach (DataTable _DataTable in _DataSet.Tables) { switch (_DataTable.TableName.ToLower()) { //下载商品信息 case "t_sellercommodity": if (strDate == "" && _DataTable.Rows.Count == 0) { DBHelper.ExecuteSqlTran("delete from t_sellercommodity"); return false; } else { if (_DataTable.Rows.Count > 0) { string sellercommodity_id = ""; for (int i = 0; i < _DataTable.Rows.Count; i++) { sellercommodity_id += (sellercommodity_id == "" ? _DataTable.Rows[i]["sellercommodity_id"].ToString() : "," + _DataTable.Rows[i]["sellercommodity_id"].ToString()); } DataTable _CommodityTable = DBHelper.QueryOdbc("select " + sellercommodityField + " from t_sellercommodity where 1 < 0").Tables[0]; if (strDate == "") { DBHelper.ExecuteSqlTran("delete from t_sellercommodity"); } else { DBHelper.ExecuteSqlTran("delete from t_sellercommodity where sellercommodity_id in (" + sellercommodity_id + ")"); } string strInsertSql = DataBaseInsert(_DataTable, _CommodityTable, "t_sellercommodity"); if (strInsertSql != "") { DBHelper.ExecuteSqlTran(strInsertSql); } } } break; //下载商品类别 case "t_sellercommoditytype": if (_DataTable.Rows.Count > 0) { string sellercommoditytype_id = ""; for (int i = 0; i < _DataTable.Rows.Count; i++) { sellercommoditytype_id += (sellercommoditytype_id == "" ? _DataTable.Rows[i]["sellercommoditytype_id"].ToString() : "," + _DataTable.Rows[i]["sellercommoditytype_id"].ToString()); } DataTable _CommodityTypeTable = DBHelper.QueryOdbc("select " + sellercommoditytypeField + " from t_sellercommoditytype where 1 < 0").Tables[0]; if (strDate == "") { DBHelper.ExecuteSqlTran("delete from t_sellercommoditytype"); } else { DBHelper.ExecuteSqlTran("delete from t_sellercommoditytype where sellercommoditytype_id in (" + sellercommoditytype_id + ")"); } string strInsertSql = DataBaseInsert(_DataTable, _CommodityTypeTable, "t_sellercommoditytype"); if (strInsertSql != "") { DBHelper.ExecuteSqlTran(strInsertSql); } } break; //下载商品价格 case "t_roleprice": if (strDate == "" && _DataTable.Rows.Count == 0) { DBHelper.ExecuteSqlTran("delete from t_roleprice "); return false; } else { if (_DataTable.Rows.Count > 0) { string roleprice_id = ""; for (int i = 0; i < _DataTable.Rows.Count; i++) { roleprice_id += (roleprice_id == "" ? _DataTable.Rows[i]["roleprice_id"].ToString() : "," + _DataTable.Rows[i]["roleprice_id"].ToString()); } DataTable _CommodityTypeTable = DBHelper.QueryOdbc("select " + rolepriceField + " from t_roleprice where 1 < 0").Tables[0]; if (strDate == "") { DBHelper.ExecuteSqlTran("delete from t_roleprice "); } else { DBHelper.ExecuteSqlTran("delete from t_roleprice where (roleprice_id in (" + roleprice_id + ") or roleprice_name <> '" + rolePriceName + "')"); } string strInsertSql = DataBaseInsert(_DataTable, _CommodityTypeTable, "t_roleprice"); if (strInsertSql != "") { DBHelper.ExecuteSqlTran(strInsertSql); } } } break; default: break; } } } } return true; } catch { return false; } } /// /// 下载商品类别信息 /// public int SellerCommoditytypeDownLoad() { string url = str_SellerCommoditytype_addr; string tableName = str_t_sellercommoditytype; if (url == "" || tableName == "") { //MessageBox.Show("下载数据路径为空或表名为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } string strdata = GetAjaxData(url); DataTable Ddt = new DataTable(); if (strdata != "") { Ddt = JsonHelper.ToDataTable(strdata); } DataTable Ldt = DBHelper.QueryOdbc("select " + sellercommoditytypeField + " from " + tableName + " where 1 = 0").Tables[0]; if (Ddt.Rows.Count <= 0) { //MessageBox.Show("获取数据为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } string sqlWhere = ""; if (Ddt.Rows.Count > 0) { foreach (DataRow dr in Ddt.Rows) { string id = ""; try { id = dr["sellercommoditytype_id"].ToString(); } catch (Exception e) { //MessageBox.Show("数据错误,下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } sqlWhere += sqlWhere == "" ? id : "," + id; } sqlWhere = " where sellercommoditytype_id in (" + sqlWhere + ")"; string deleteSql = "delete from " + tableName + sqlWhere; string insertSql = DataBaseInsert(Ddt, Ldt, tableName); List sqlList = new List(); sqlList.Add(deleteSql); sqlList.Add(insertSql); try { DBHelper.ExecuteSqlTran(sqlList); } catch (Exception e) { //MessageBox.Show("商品类别下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } return 0; } //MessageBox.Show("商品类别下载成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else { return 9; } } /// /// 下载商品信息 /// public int SellerCommodityDownLoad() { string url = str_SellerCommodity_addr; string tableName = str_t_sellercommodity; if (url == "" || tableName == "") { //MessageBox.Show("下载数据路径为空或表名为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } DataTable Ldt = null; DataTable maxDateTimeDT = null; try { Ldt = DBHelper.QueryOdbc("select " + sellercommodityField + " from " + tableName + " where 1 = 0").Tables[0]; maxDateTimeDT = DBHelper.QueryOdbc("select max(addtime) from " + tableName).Tables[0]; } catch (Exception e) { //MessageBox.Show("数据库查询失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } if (maxDateTimeDT.Rows[0][0] != System.DBNull.Value) { url += "&action_record=" + maxDateTimeDT.Rows[0][0].ToString(); } string strdata = GetAjaxData(url); DataTable Ddt = new DataTable(); if (strdata != "") { Ddt = JsonHelper.ToDataTable(strdata); } if (Ddt.Rows.Count <= 0) { //MessageBox.Show("商品信息已更新", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 0; } string sqlWhere = ""; if (Ddt.Rows.Count > 0) { foreach (DataRow dr in Ddt.Rows) { string id = ""; try { id = dr["sellercommodity_id"].ToString(); } catch (Exception e) { //MessageBox.Show("数据错误,下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } sqlWhere += sqlWhere == "" ? id : "," + id; } sqlWhere = " where sellercommodity_id in(" + sqlWhere + ")"; string insertSql = DataBaseInsert(Ddt, Ldt, tableName); if (insertSql == "") { return 1; } string deleteSql = "delete from " + tableName + sqlWhere; List sqlList = new List(); sqlList.Add(deleteSql); sqlList.Add(insertSql); try { DBHelper.ExecuteSqlTran(sqlList); } catch (Exception e) { //MessageBox.Show("商品信息下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } //MessageBox.Show("商品信息下载成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 0; } else { return 9; } } /// /// 商品价格下载 /// public int RolepriceDownLoad() { string url = str_Roleprice_addr; string tableName = str_t_roleprice; string dateFlagTableName = str_t_sellercommodity; if (url == "" || tableName == "") { //MessageBox.Show("下载数据路径为空或表名为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } DataTable Ldt = null; DataTable maxDateTimeDT = null; try { Ldt = DBHelper.QueryOdbc("select " + rolepriceField + " from " + tableName + " where 1 = 0").Tables[0]; maxDateTimeDT = DBHelper.QueryOdbc("select max(addtime) from " + dateFlagTableName).Tables[0]; } catch (Exception e) { MessageBox.Show("数据库查询失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (maxDateTimeDT.Rows[0][0] != System.DBNull.Value) { url += "&action_record=" + maxDateTimeDT.Rows[0][0].ToString(); } string strdata = GetAjaxData(url); DataTable Ddt = new DataTable(); if (strdata != "") { Ddt = JsonHelper.ToDataTable(strdata); } if (Ddt.Rows.Count <= 0) { //MessageBox.Show("商品价格信息已更新", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 0; } string sqlWhere = ""; if (Ddt.Rows.Count > 0) { foreach (DataRow dr in Ddt.Rows) { string id = ""; try { id = dr["roleprice_id"].ToString(); } catch (Exception e) { //MessageBox.Show("数据错误,下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } sqlWhere += sqlWhere == "" ? id : "," + id; } sqlWhere = " where roleprice_id in(" + sqlWhere + ")"; string insertSql = DataBaseInsert(Ddt, Ldt, tableName); if (insertSql == "") { return 1; } string deleteSql = "delete from " + tableName + sqlWhere; List sqlList = new List(); sqlList.Add(deleteSql); sqlList.Add(insertSql); try { DBHelper.ExecuteSqlTran(sqlList); } catch (Exception e) { //MessageBox.Show("商品价格下载失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 1; } //MessageBox.Show("商品价格下载成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return 0; } else { return 9; } } /// /// 生成Insert语句 /// /// 服务器数据表 /// 本地数据表 /// 需要生成的表名 /// 返回InsertSQL语句 public string DataBaseInsert(DataTable DownLoadDT, DataTable LocalDT, string tableName) { if (DownLoadDT.Rows.Count <= 0 || LocalDT.Columns.Count <= 0 || tableName == "") { return ""; } StringBuilder sqlSB = new StringBuilder(); string tableInfo = "insert into " + tableName + "(TableContent) values"; string tableContent = ""; foreach (DataColumn dc in LocalDT.Columns) { tableContent += (tableContent == "" ? dc.ColumnName : "," + dc.ColumnName); } tableInfo = tableInfo.Replace("TableContent", tableContent); sqlSB.Append(tableInfo); foreach (DataRow dr in DownLoadDT.Rows) { string sql = "("; foreach (DataColumn dc in LocalDT.Columns) { string data = ""; try { if (dc.ColumnName == "commodity_desc") { data = dr["image_path"].ToString(); } else { data = dr[dc.ColumnName].ToString(); } } catch (Exception e) { sql += "null,"; continue; } if (data == "") { sql += "null,"; } else if (dc.DataType.ToString() == "System.DateTime") { sql += "datetime('" + data + "'),"; } else if (dc.DataType.ToString() == "System.Decimal") { sql += data + ","; } else { sql += "'" + data + "',"; } } sql = sql.Substring(0, sql.Length - 1); sql += "),"; sqlSB.Append(sql); } sqlSB = sqlSB.Remove(sqlSB.Length - 1, 1); return sqlSB.ToString(); } /// /// 获取Ajax接口数据 /// public string GetAjaxData(string url, Boolean isServerPart = false) { string pageHtml = ""; try { if (isServerPart) { string _strServiceURL = String.Format("http://{0}:{1}/service.asmx", _QualityHelper.dbip, _QualityHelper.serviceport); object[] _args = { url }; pageHtml = WSHelper.InvokeWebService(_strServiceURL, "DownLoadSeller", _args).ToString(); } else { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据 Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据 pageHtml = Encoding.UTF8.GetString(pageData); } //int index = pageHtml.IndexOf(":"); //pageHtml = pageHtml.Substring(index + 1, pageHtml.Length - index - 2); pageHtml = pageHtml.Replace(" ", ""); //pageHtml = HttpUtility.UrlDecode(pageHtml); return pageHtml; } catch (Exception ex) { return ""; } } } }