/// <summary> /// 上传图片后更新数据库文件 /// </summary> /// <param name="sysID">ID</param> /// <param name="imgUrl_1">图片路径一</param> /// <param name="imgUrl_2">图片路径二</param> /// <returns></returns> public static bool UploadPictureSql(string sysID, string imgUrl_1, string imgUrl_2, string type) { string path = string.Empty; if (new DB.BLL.MB_Bll(DBEnum.Master).UploadImgFile(sysID, imgUrl_1, imgUrl_2, type))//更新数据库数据 { return(true); } else //数据库更新失败,则删除相关文件 { try { if (!string.IsNullOrEmpty(imgUrl_1)) { path = HttpContext.Current.Server.MapPath(imgUrl_1); FileHelper.DeleteFile(path); } if (!string.IsNullOrEmpty(imgUrl_2)) { path = HttpContext.Current.Server.MapPath(imgUrl_2); FileHelper.DeleteFile(path); } } catch { WebLogHelper.WriteErrLog(DateTime.Now + WebLoginHelper.GetAdminName() + "删除文件出错:" + imgUrl_1 + "/" + imgUrl_2, string.Empty); return(false); } return(false); } }
/// <summary> /// EXCLE上传商品信息 /// </summary> /// <param name="path"></param> /// <returns></returns> public string ReadExclToSqlProduct(string path) { string resInfo = string.Empty; if (WebLoginHelper.IsAuthority(AdminAuth.GoodsManage)) { DataTable dt = new ExcelHelper().ReadExcel(HttpContext.Current.Server.MapPath(path)); decimal outMarketPrice = 0; decimal outShopPrice = 0; string sqlPath_1 = string.Empty; string sqlPath_2 = string.Empty; string url = string.Empty; //复制DataTable表结构到错误表 DataTable dtError = dt.Clone(); if (!dtError.Columns.Contains("错误信息")) { dtError.Columns.Add("错误信息", Type.GetType("System.String")); } if (dt != null) { resInfo = "共上传" + dt.Rows.Count + "条数据"; DateTime nowTime = DateTime.Now; IEnumerable <dynamic> dtCategoryList = new DB.BLL.MB_Bll(DBEnum.Slave).GetCategoryList(); //获取所有的类目数据(包括父级) IEnumerable <dynamic> dtFareTempList = new DB.BLL.MB_Bll(DBEnum.Slave).GetModelList(" SELECT FareName,FareSysID FROM M_FareTemplate WHERE 1=1 ", null); List <M_Goods> dtGoodsList = new DB.BLL.MB_Bll(DBEnum.Slave).GetModelList <M_Goods>("1=1 AND IsDelete = 0 ORDER BY AddTime DESC ", null); //获取当前已有的产品信息 List <M_Brand> dtBrandList = new DB.BLL.MB_Bll(DBEnum.Slave).GetModelList <M_Brand>(" 1=1 ", null); //获取当前已有的品牌信息 List <M_Suppliers> dtSuppliers = new DB.BLL.MB_Bll(DBEnum.Slave).GetModelList <M_Suppliers>(" 1=1 ", null); string errInfo = string.Empty; string tempErr = string.Empty; M_Goods goodsInfo = new M_Goods(); for (int i = 0; i < dt.Rows.Count; i++) { goodsInfo = new M_Goods(); errInfo = string.Empty; sqlPath_1 = string.Empty; sqlPath_2 = string.Empty; url = string.Empty; outMarketPrice = 0; outShopPrice = 0; try { if (dtGoodsList != null) { foreach (var item in dtGoodsList)//遍历商品信息表查看该商品编号是否存在,如果存在则跳出本次循环 { if (item.GoodsSn == dt.Rows[i]["GoodsSn"].ToString()) { errInfo += "该商品编号已经存在,勿重复创建;"; break; } } } tempErr = "供应商不存在"; if (dtSuppliers != null) { foreach (var item in dtSuppliers) { if (item.SuppliersName == Convert.ToString(dt.Rows[i]["SuppliersID"])) { goodsInfo.SuppliersID = item.SuppliersID; tempErr = string.Empty; break; } } } errInfo += tempErr; tempErr = "类目不存在;";//临时错误信息用来存储临时判断环节可能出现的错误 if (dtCategoryList != null) { foreach (var item in dtCategoryList) { if (item.CateName_1 == dt.Rows[i]["CateName_1"].ToString() && item.CateName_2 == dt.Rows[i]["CateName_2"].ToString()) { goodsInfo.CateID = Convert.ToInt32(item.CateID); tempErr = string.Empty;//符合条件后清空临时错误 break; } } } errInfo += tempErr; //判断一个环节结束后将临时错误加到这个要输出的错误信息后面去 tempErr = "运费模板不存在;"; //临时错误信息用来存储临时判断环节可能出现的错误 if (dtFareTempList != null) { foreach (var item in dtFareTempList) { if (item.FareName == dt.Rows[i]["FareName"].ToString())//遍历运费模板页,找到这个运费模板信息后清空错误信息 { goodsInfo.FareSysID = item.FareSysID; tempErr = string.Empty;//符合条件后清空临时错误 break; } } } errInfo += tempErr; tempErr = "金额出错;"; if (decimal.TryParse(dt.Rows[i]["MarketPrice"].ToString(), out outMarketPrice) && decimal.TryParse(dt.Rows[i]["ShopPrice"].ToString(), out outShopPrice)) { goodsInfo.MarketPrice = outMarketPrice; goodsInfo.ShopPrice = outShopPrice; tempErr = string.Empty;//符合条件后清空临时错误 } errInfo += tempErr; if (!string.IsNullOrEmpty(dt.Rows[i]["BrandName"].ToString())) { tempErr = "品牌不存在"; if (dtBrandList != null) { foreach (var item in dtBrandList) { if (item.BrandName == dt.Rows[i]["BrandName"].ToString()) { goodsInfo.BrandID = item.BrandID; tempErr = string.Empty; break; } } } errInfo += tempErr; } if (string.IsNullOrEmpty(errInfo))//最后判断这个临时错误信息 { url = dt.Rows[i]["GoodsImg"].ToString(); url = GetInterNetImgUrl(url); if (!string.IsNullOrEmpty(url)) { DownloadFiles(url, out sqlPath_1, out sqlPath_2); } goodsInfo.GoodsImg = sqlPath_1; goodsInfo.GoodsThumb = sqlPath_2; goodsInfo.OriginalImg = sqlPath_1; goodsInfo.GoodsID = Guid.NewGuid().ToString(); goodsInfo.GoodsSn = dt.Rows[i]["GoodsSn"].ToString(); goodsInfo.GoodsName = dt.Rows[i]["GoodsName"].ToString(); goodsInfo.GoodsNumber = Convert.ToInt32(dt.Rows[i]["GoodsNumber"].ToString()); goodsInfo.GoodsBrief = dt.Rows[i]["GoodsBrief"].ToString(); goodsInfo.IsReal = Convert.ToInt32(dt.Rows[i]["IsReal"].ToString()); goodsInfo.IsNew = Convert.ToInt32(dt.Rows[i]["IsNew"].ToString()); goodsInfo.IsHot = Convert.ToInt32(dt.Rows[i]["IsHot"].ToString()); goodsInfo.SellerNote = dt.Rows[i]["SellerNote"].ToString(); goodsInfo.Weight = Convert.ToDouble(dt.Rows[i]["Weight"].ToString()); if (!(new DB.BLL.MB_Bll(DBEnum.Master).AddGoodsInfo(goodsInfo.CateID, goodsInfo.MarketPrice, goodsInfo.ShopPrice, goodsInfo.BrandID, goodsInfo.GoodsImg, goodsInfo.GoodsThumb, goodsInfo.OriginalImg, goodsInfo.SuppliersID, goodsInfo.GoodsSn, goodsInfo.GoodsName, goodsInfo.GoodsNumber, goodsInfo.GoodsBrief, goodsInfo.IsReal, goodsInfo.IsNew, goodsInfo.IsHot, goodsInfo.Weight, goodsInfo.SellerNote, goodsInfo.FareSysID, WebLoginHelper.GetAdminName()))) { dtError.ImportRow(dt.Rows[i]); dtError.Rows[dtError.Rows.Count - 1]["错误信息"] = "该条记录写入数据库失败"; } else { dtGoodsList.Add(goodsInfo);//将创建成功后的这条信息加到数据集合中 } } else { dtError.ImportRow(dt.Rows[i]); dtError.Rows[dtError.Rows.Count - 1]["错误信息"] = errInfo; } } catch (Exception eMsg) { dtError.ImportRow(dt.Rows[i]); dtError.Rows[dtError.Rows.Count - 1]["错误信息"] = "该条记录使系统产生严重错误,请联系管理员:" + eMsg.Message; } } if (dtError != null) { resInfo += " 上传失败" + dtError.Rows.Count + "条"; new ExcelHelper().DataTableToExcel(dtError, "xls", "上传错误信息表", Path.GetDirectoryName(HttpContext.Current.Server.MapPath(path)) + "/上传错误信息表.xls"); } } } else { resInfo = "上传失败,当前账户无权执行ECXEL商品信息导入操作"; } return(resInfo); }