public void CheckProductImages(IList<Product> productsNotExistInDb, string ImageFolder, out IList<Product> productsHasPicture , out IList<Product> productsNotHasPicture , out IList<FileInfo> imagesHasProduct , out IList<FileInfo> imagesHasNotProduct) { DirectoryInfo dir = new DirectoryInfo(ImageFolder); FileInfo[] images = dir.GetImageFiles(SearchOption.AllDirectories).ToArray<FileInfo>();// dirImage.GetFiles(); productsHasPicture = new List<Product>(); productsNotHasPicture = new List<Product>(); imagesHasProduct = new List<FileInfo>(); imagesHasNotProduct = new List<FileInfo>(); //写一个通用类,比较两个序列,返回匹配结果. //Compare<T1,T2> T1和T2需要实现他们两者比较的接口 foreach (Product p in productsNotExistInDb) { bool productHasImage = false; Console.WriteLine("productModel:" + p.ModelNumber); foreach (FileInfo image in images) { string imageName = StringHelper.ReplaceSpace(Path.GetFileNameWithoutExtension(image.Name)); Console.Write("imageName:" + imageName); if (imageName.Equals(StringHelper.ReplaceSpace(p.ModelNumber), StringComparison.OrdinalIgnoreCase)) { //找到型号相同的图片 p.UpdateImageList(image.FullName, WebProductImagesPath); productsHasPicture.Add(p); imagesHasProduct.Add(image); productHasImage = true; break; } } if (!productHasImage) { productsNotHasPicture.Add(p); } } foreach (FileInfo f in images) { bool imageHasProduct = false; foreach (FileInfo f2 in imagesHasProduct) { if (f.Name.Equals(f2.Name)) { imageHasProduct = true; break; } } if (!imageHasProduct) { imagesHasNotProduct.Add(f); } } }
private void CheckSingleFolder2Test( string folderName , int amountProductsHasPicture , int amountProductNotHasPicture , int amountProductExistsInDb , int amountImagesHasNotProduct , bool needCheckDataBase , DALProduct dalProduct , DALSupplier dalSupplier , IFormatSerialNoPersistent ifsp , DalBase<Product> dalBaseProduct) { string folderFullPath = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\" + folderName + "\\"; SingleFolderImport checker = new SingleFolderImport(folderFullPath); BizProduct bizP = new BizProduct(); bizP.DalProduct = dalProduct; bizP.DalBase = dalBaseProduct; BizSupplier bizS = new BizSupplier(); bizS.DalSupplier = dalSupplier; bizP.DalSupplier = dalSupplier; checker.NeedCheckWithDB = needCheckDataBase; checker.Import(bizP, bizS, ifsp); IList<Product> productsHasPicture = checker.ProductsPassedDBCheck , productsNotHasPicture = checker.ProductsNotHasImage , productsExistedInDB = checker.ProductsExistedInDB; IList<FileInfo> imagesHasProduct = checker.ImagesHasProduct , imagesHasNotProduct = checker.ImagesNotHasProduct; // Assert.AreEqual("Success", FormatChecker.Check(folderContainsExcelAndImages)); Assert.AreEqual(amountProductsHasPicture, checker.ProductsPassedDBCheck.Count); Assert.AreEqual(amountProductNotHasPicture, checker.ProductsNotHasImage.Count); Assert.AreEqual(amountProductExistsInDb, checker.ProductsExistedInDB.Count); Assert.AreEqual(amountImagesHasNotProduct, checker.ImagesNotHasProduct.Count); DateTime beginSaveResult = DateTime.Now; string saveFolder = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\检测结果\\"; string saveFolderOfSupplier; if (productsHasPicture.Count > 0) saveFolderOfSupplier = bizS.GetByCode(productsHasPicture[0].SupplierCode).Name; else if (productsNotHasPicture.Count > 0) saveFolderOfSupplier = bizS.GetByCode(productsNotHasPicture[0].SupplierCode).Name; else throw new Exception(); DirectoryInfo dirOfSavedSupplier = new DirectoryInfo(saveFolder + "合格数据\\" + saveFolderOfSupplier + "\\"); if (dirOfSavedSupplier.Exists) { dirOfSavedSupplier.Delete(true); } string supplierName = string.Empty; if (productsExistedInDB.Count > 0) supplierName = bizS.GetByCode(productsExistedInDB[0].SupplierCode).Name; else if (productsHasPicture.Count > 0) supplierName = bizS.GetByCode(productsHasPicture[0].SupplierCode).Name; else if (productsNotHasPicture.Count > 0) supplierName = bizS.GetByCode(productsNotHasPicture[0].SupplierCode).Name; else { return; } supplierName = StringHelper.ReplaceInvalidChaInFileName(supplierName, string.Empty); checker.HandlerCheckResult( supplierName, saveFolder, WebProductImagesPath ); Assert.AreEqual(productsHasPicture.Count, dirOfSavedSupplier.GetImageFiles().ToArray().Length); Console.WriteLine("Time Cost CheckImage:" + (DateTime.Now - beginSaveResult).TotalSeconds); }
/// <summary> /// /// </summary> /// <param name="folderName">包含excel和对应产品图片的文件夹</param> /// <param name="amountProductsHasPicture_NotExitsted">可供导入的产品(没有导入过,有图片)</param> /// <param name="amountProductNotHasPicture_OrHasExisted">不合格产品(已导入,或 没图片</param> /// <param name="amountProductExistsInDb_HasExisted">不合格产品(已导入)</param> /// <param name="amountImagesHasNotProduct_OrHasExisted">没有产品信息的图片</param> /// <param name="needCheckDataBase">是否从数据库查询产品是否存在</param> /// <param name="dalProduct">供mock,判断是否存在的方法</param> /// <param name="dalSupplier">供mock,获取供应商的方法.</param> private void CheckSingleFolderTest( string folderName , int amountProductsHasPicture , int amountProductNotHasPicture , int amountProductExistsInDb , int amountImagesHasNotProduct , bool needCheckDataBase , DALProduct dalProduct , DALSupplier dalSupplier) { IList<Product> productsHasPicture, productsNotHasPicture, productsExistedInDB; IList<FileInfo> imagesHasProduct, imagesHasNotProduct; string folderFullPath = Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\" + folderName + "\\"; ProductImportor checker = new ProductImportor(); checker.BizProduct.DalBase = dalProduct; checker.BizProduct.DalSupplier = dalSupplier; checker.CheckWithDatabase = needCheckDataBase; checker.CheckSingleFolder(folderFullPath , out productsHasPicture , out productsNotHasPicture , out productsExistedInDB , out imagesHasProduct , out imagesHasNotProduct); // Assert.AreEqual("Success", FormatChecker.Check(folderContainsExcelAndImages)); Assert.AreEqual(amountProductsHasPicture, productsHasPicture.Count); Assert.AreEqual(amountProductNotHasPicture, productsNotHasPicture.Count); Assert.AreEqual(amountProductExistsInDb, productsExistedInDB.Count); Assert.AreEqual(amountImagesHasNotProduct, imagesHasNotProduct.Count); DateTime beginSaveResult = DateTime.Now; string saveFolder=Environment.CurrentDirectory + "\\TestFiles\\FormatCheck\\检测结果\\"; string saveFolderOfSupplier; if (productsHasPicture.Count > 0) saveFolderOfSupplier = productsHasPicture[0].SupplierName; else if (productsNotHasPicture.Count > 0) saveFolderOfSupplier = productsNotHasPicture[0].SupplierName; else throw new Exception(); DirectoryInfo dirOfSavedSupplier = new DirectoryInfo(saveFolder + "合格数据\\" + saveFolderOfSupplier+"\\"); if (dirOfSavedSupplier.Exists) { dirOfSavedSupplier.Delete(true); } string supplierName = string.Empty; if (productsExistedInDB.Count > 0) supplierName = productsExistedInDB[0].SupplierName; else if (productsHasPicture.Count > 0) supplierName = productsHasPicture[0].SupplierName; else if (productsNotHasPicture.Count > 0) supplierName = productsNotHasPicture[0].SupplierName; else { return; } supplierName = StringHelper.ReplaceInvalidChaInFileName(supplierName, string.Empty); checker.HandlerCheckResult( supplierName, productsHasPicture , productsNotHasPicture , productsExistedInDB , imagesHasProduct , imagesHasNotProduct , saveFolder); Assert.AreEqual(productsHasPicture.Count, dirOfSavedSupplier.GetImageFiles().ToArray().Length); Console.WriteLine("Time Cost CheckImage:" + (DateTime.Now - beginSaveResult).TotalSeconds); }
/// <summary> /// 扫描指定文件夹,根绝文件夹和图片名称提取图片信息 /// <param name="targetPath">拷贝的目标地址,应该为网站的虚拟目录的物理路径</param> /// </summary> public IList<ImageInfo> ImportImage(string folderPath, string targetPath, out string message) { StringBuilder sb = new StringBuilder(); IList<ImageInfo> images = new List<ImageInfo>(); DirectoryInfo dir = new DirectoryInfo(folderPath); DirectoryInfo[] supplierDirs = dir.GetDirectories(); //foreach (DirectoryInfo dirSupplier in supplierDirs) //{ // string supplierNameOfFolder = dirSupplier.Name; // Supplier s = dalSupplier.GetOneByName(supplierNameOfFolder); // if (s == null) // { // string errmsg = "没有找到与文件夹同名的供应商名称,请核查." + supplierNameOfFolder; // NLibrary.NLogger.Logger.Debug(errmsg); // sb.AppendLine(errmsg); // continue; // } FileInfo[] imageFiles = dir.GetImageFiles().ToArray(); if (imageFiles.Length == 0) { string errmsg = "文件夹内没有图片:" + dir.FullName; NLibrary.NLogger.Logger.Debug(errmsg); sb.AppendLine(errmsg); } //本次操作中出现过的供应商 IList<Supplier> supplierList = new List<Supplier>(); foreach (FileInfo imageFile in imageFiles) { string supplierNameOfFolder = imageFile.Directory.Name; Supplier s = supplierList.SingleOrDefault(x => x.Name == supplierNameOfFolder || x.EnglishName == supplierNameOfFolder); if (s == null) { s = DalSupplier.GetOneByName(supplierNameOfFolder); if (s == null) { string errmsg = "没有找到与文件夹同名的供应商名称,请核查." + supplierNameOfFolder; NLibrary.NLogger.Logger.Debug(errmsg); sb.AppendLine(errmsg); continue; } if (!supplierList.Contains(s)) { supplierList.Add(s); } } IList<Product> ProductsOfSupplier = DalProduct.GetListBySupplierCode(s.Code); if (ProductsOfSupplier.Count == 0) { string errmsg = "没有属于该供应商的产品,请导入对应的报价单 或者 核实文件夹名称与供应商名称的一致性." + supplierNameOfFolder; NLibrary.NLogger.Logger.Debug(errmsg); sb.AppendLine(errmsg); continue; } //读取型号名称 string modelNumber =StringHelper.ToDBC(Path.GetFileNameWithoutExtension(imageFile.Name).Trim()); Product p = null;//= dalProduct.GetOneByModelNumberAndSupplier(modelNumber, dirSupplier.Name); IList<Product> productSupplierAndModel = ProductsOfSupplier.Where( x => StringHelper.ToDBC(x.ModelNumber.Trim().Replace("\n", "")).ToLower() == modelNumber.ToLower()).ToList(); if (productSupplierAndModel.Count == 0) { string errmsg = "该图片没有对应的产品信息:" + supplierNameOfFolder + "," + imageFile.Name; NLibrary.NLogger.Logger.Debug(errmsg); sb.AppendLine(errmsg); // NLibrary.NLogger.Logger.Debug("没有找到对应的图片.供应商:" + dirSupplier.FullName+",型号:"+modelNumber); continue; } else if (productSupplierAndModel.Count > 1) { string errmsg = "该图片对应多条产品信息,:" + supplierNameOfFolder + "," + imageFile.Name; NLibrary.NLogger.Logger.Debug(errmsg); sb.AppendLine(errmsg); } //拷贝图片 到 对应文件夹 p = productSupplierAndModel[0]; p.UpdateImageList(imageFile.FullName, targetPath + "\\"); DalProduct.Update(p); ImageInfo ii = new ImageInfo(); ii.ImagePath = imageFile.FullName; ii.ModelNumber = modelNumber; ii.SupplierName = s.Name; images.Add(ii); } message = sb.ToString(); return images; }