public void SaveProduct() { try { using (ProxyBE op = new ProxyBE()) { //存储文件路径:根目录/CompanyID/Products/yyyyMM/ProductCode/FileType string RootPath = "";// Config.StorageFolder; RootPath = Path.Combine(RootPath, "Products"); RootPath = Path.Combine(RootPath, DateTime.Now.ToString("yyyyMM")); RootPath = Path.Combine(RootPath, parm.ProductCode); if (!Directory.Exists(RootPath)) { Directory.CreateDirectory(RootPath); } string ServerPath = Server.MapPath("/"); SaveProductArgs args = new SaveProductArgs(); List <ProductProcessFile> ProcessFiles = new List <ProductProcessFile>(); #region 处理主表 //主图片数据 string HeardImg = (ServerPath + Request["HeadImgUrl"].ToString().Replace("/", "\\")).Replace("\\\\", "\\"); if (!File.Exists(HeardImg)) { throw new Exception("上传的图片文件损坏,请重新上传。"); } //保存图片文件 string heardimg_savepath = Path.Combine(RootPath, "HeardImg\\" + parm.ProductCode + Path.GetExtension(HeardImg)); //if (!Directory.Exists(Path.GetDirectoryName(heardimg_savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(heardimg_savepath)); //} // UploadFile(HeardImg, heardimg_savepath); //File.Copy(HeardImg, heardimg_savepath, true); ProductMain main = op.Client.GetProductMain(SenderUser, parm.ProductID); if (main == null) { main = new ProductMain(); main.ProductID = parm.ProductID; } main.ProductCode = parm.ProductCode; main.ProductName = parm.ProductName; main.CategoryID = parm.CategoryID; main.Color = parm.Color; main.Size = parm.Size; main.MaterialStyle = parm.MaterialStyle; main.MaterialCategory = parm.MaterialCategory; main.Price = parm.Price; main.Remark = ""; main.ImageUrl = heardimg_savepath; //检查ProductCode 是否已经存在 ProductMain temp = op.Client.GetProductMainByProductCode(SenderUser, parm.ProductCode); if (temp != null && temp.ProductID != main.ProductID) { throw new PException("产品编号【{0}】已经存在,请重新编号。", parm.ProductCode); } args.ProductMain = main; #endregion #region 处理BOM表 string BOMFilePath = (ServerPath + Request["BOMFileUrl"].ToString().Replace("/", "\\")).Replace("\\\\", "\\"); if (!File.Exists(BOMFilePath)) { throw new Exception("上传的BOM文件损坏,请重新上传。"); } //开始解析BOM表 List <ProductDetail> list_ProductDetails = ImportBOM(BOMFilePath, main.ProductID); if (list_ProductDetails.Count == 0) { throw new Exception("上传的BOM文件数据有误,请重新上传。"); } args.ProductDetails = list_ProductDetails; //保存BOM表 string bom_savepath = Path.Combine(RootPath, "BOM\\" + parm.ProductCode + Path.GetExtension(BOMFilePath)); //if (!Directory.Exists(Path.GetDirectoryName(bom_savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(bom_savepath)); //} //File.Copy(BOMFilePath, bom_savepath, true); UploadFile(BOMFilePath, bom_savepath); ProductProcessFile pf = new ProductProcessFile(); pf.ProductID = parm.ProductID; pf.FileID = Guid.NewGuid(); pf.FileName = Path.GetFileName(bom_savepath); pf.FilePath = bom_savepath; pf.FileType = "BOM"; ProcessFiles.Add(pf); #endregion #region 处理五金表 string HardwareFilePath = (ServerPath + Request["HardwareFileUrl"].ToString().Replace("/", "\\")).Replace("\\\\", "\\");; if (!File.Exists(HardwareFilePath)) { throw new Exception("上传的五金文件损坏,请重新上传。"); } //开始解析五金表 List <Product2Hardware> list_HardwareDetails = ImportHardwareExcel(HardwareFilePath, main.ProductID); if (list_ProductDetails.Count == 0) { throw new Exception("上传的五金文件数据有误,请重新上传。"); } args.Product2Hardwares = list_HardwareDetails; //保存五金表 string hardware_savepath = Path.Combine(RootPath, "HardwareBOM\\" + parm.ProductCode + Path.GetExtension(HardwareFilePath)); //if (!Directory.Exists(Path.GetDirectoryName(hardware_savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(hardware_savepath)); //} //File.Copy(HardwareFilePath, hardware_savepath, true); UploadFile(HardwareFilePath, hardware_savepath); pf = new ProductProcessFile(); pf.ProductID = parm.ProductID; pf.FileID = Guid.NewGuid(); pf.FileName = Path.GetFileName(hardware_savepath); pf.FilePath = hardware_savepath; pf.FileType = "Hardware"; ProcessFiles.Add(pf); #endregion #region 处理加工文件 if (string.IsNullOrEmpty(Request["CNCFileUrl"])) { throw new Exception("上传的CNC文件损坏,请重新上传。"); } string[] cnc_files = Request["CNCFileUrl"].ToString().Split(','); foreach (string cnc_File in cnc_files) { string SourceFile = (ServerPath + cnc_File.Replace("/", "\\")).Replace("\\\\", "\\");; string savepath = Path.Combine(RootPath, "ProcessFile\\" + Path.GetFileName(cnc_File)); //if (!Directory.Exists(Path.GetDirectoryName(savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(savepath)); //} //if (File.Exists(savepath)) //{ // File.Delete(savepath); //} //File.Copy(SourceFile, savepath, true); UploadFile(SourceFile, savepath); pf = new ProductProcessFile(); pf.ProductID = parm.ProductID; pf.FileID = Guid.NewGuid(); pf.FileName = Path.GetFileName(savepath); pf.FilePath = savepath; if (Path.GetExtension(savepath).ToLower() == ".dxf") { pf.FileType = "DXF"; } else { pf.FileType = "CNC"; } ProcessFiles.Add(pf); } #endregion #region 处理图纸文件 string[] drawing_files = Request["DrawingFileUrl"].ToString().Split(','); foreach (string drawing_File in drawing_files) { string SourceFile = (ServerPath + drawing_File.Replace("/", "\\")).Replace("\\\\", "\\");; string savepath = Path.Combine(RootPath, "DrawingFile\\" + Path.GetFileName(drawing_File)); //if (!Directory.Exists(Path.GetDirectoryName(savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(savepath)); //} //if (File.Exists(savepath)) //{ // File.Delete(savepath); //} //File.Copy(SourceFile, savepath, true); UploadFile(SourceFile, savepath); pf = new ProductProcessFile(); pf.ProductID = parm.ProductID; pf.FileID = Guid.NewGuid(); pf.FileName = Path.GetFileName(savepath); pf.FilePath = savepath; pf.FileType = "DrawingFile"; ProcessFiles.Add(pf); } #endregion #region 处理效果文件 string[] rendering_files = Request["DrawingFileUrl"].ToString().Split(','); foreach (string rendering_File in rendering_files) { string SourceFile = (ServerPath + rendering_File.Replace("/", "\\")).Replace("\\\\", "\\");; string savepath = Path.Combine(RootPath, "RenderingFile\\" + Path.GetFileName(rendering_File)); //if (!Directory.Exists(Path.GetDirectoryName(savepath))) //{ // Directory.CreateDirectory(Path.GetDirectoryName(savepath)); //} //if (File.Exists(savepath)) //{ // File.Delete(savepath); //} //File.Copy(SourceFile, savepath, true); UploadFile(SourceFile, savepath); pf = new ProductProcessFile(); pf.ProductID = parm.ProductID; pf.FileID = Guid.NewGuid(); pf.FileName = Path.GetFileName(savepath); pf.FilePath = savepath; pf.FileType = "RenderingFile"; ProcessFiles.Add(pf); } #endregion args.ProductProcessFiles = ProcessFiles; op.Client.SaveProduct(SenderUser, args); WriteSuccess(); } } catch (Exception ex) { PLogger.LogError(ex); WriteError(ex.Message, ex); } finally { string RootPath = Server.MapPath(@"/temp/"); RootPath += DateTime.Now.ToString("yyyyMMdd") + "\\"; string ProductID = Request["ProductID"]; if (ProductID != null) { RootPath += ProductID; } if (Directory.Exists(RootPath)) { Directory.Delete(RootPath, true); } } }
public void SaveProduct(Sender sender, SaveProductArgs args) { try { using (ObjectProxy op = new ObjectProxy(true)) { ProductMain obj = new ProductMain(); obj.ProductID = args.ProductMain.ProductID; if (op.LoadProductMainByProductID(obj) == 0) { args.ProductMain.Created = DateTime.Now; args.ProductMain.CreatedBy = sender.UserCode + "." + sender.UserName; args.ProductMain.Modified = DateTime.Now; args.ProductMain.ModifiedBy = sender.UserCode + "." + sender.UserName; op.InsertProductMain(args.ProductMain); } else { args.ProductMain.Modified = DateTime.Now; args.ProductMain.ModifiedBy = sender.UserCode + "." + sender.UserName; op.UpdateProductMainByProductID(args.ProductMain); } if (args.Product2Hardwares != null) { op.DeleteProduct2HardwaresByProductID(args.ProductMain.ProductID); foreach (Product2Hardware item in args.Product2Hardwares) { item.Created = DateTime.Now; item.CreatedBy = sender.UserCode + "." + sender.UserName; item.Modified = DateTime.Now; item.ModifiedBy = sender.UserCode + "." + sender.UserName; op.InsertProduct2Hardware(item); } } if (args.ProductDetails != null) { op.DeleteProductDetailsByProductID(args.ProductMain.ProductID); foreach (ProductDetail item in args.ProductDetails) { if (IsProductBarcodeDuplicated(sender, item)) { throw new PException("产品条码【{0}】已存在。", item.BarcodeNo); } item.Created = DateTime.Now; item.CreatedBy = sender.UserCode + "." + sender.UserName; item.Modified = DateTime.Now; item.ModifiedBy = sender.UserCode + "." + sender.UserName; op.InsertProductDetail(item); } } if (args.ProductProcessFiles != null) { op.DeleteProductProcessFilesByProductID(args.ProductMain.ProductID); foreach (ProductProcessFile item in args.ProductProcessFiles) { item.Created = DateTime.Now; item.CreatedBy = sender.UserCode + "." + sender.UserName; item.Modified = DateTime.Now; item.ModifiedBy = sender.UserCode + "." + sender.UserName; op.InsertProductProcessFile(item); } } op.CommitTransaction(); } } catch (Exception ex) { PLogger.LogError(ex); throw ex; } }