/// <summary> /// GET: 新增機種 /// </summary> /// <param name="id">RDRMain.ID (RDR主表ID)</param> /// <returns></returns> public ActionResult Create(int id) { //Search By RDRMain.ID RDRMain main = db.RDRMains.Where(m => m.ID == id).FirstOrDefault(); // .Find(id); if (main != null) { RDRModuleCreateViewModel viewModel = new RDRModuleCreateViewModel(); viewModel.ParentID = id; viewModel.MainCode = main.RDRNumber; viewModel.StartYear = main.SOPDate.Year; viewModel.EndYear = main.EOLDate.Year; viewModel.CustomerTeamID = main.CustomerTeamID; viewModel.rdrModuleList = (from m in db.RDRModules.Where(m => m.ParentID == id) join p in db.ProductGroups on m.ProductGroupID equals p.ID join c in db.Customers on m.CustomerID equals c.ID select new RDRModuleDetailsViewModel { ID = m.ID, ParentID = m.ParentID, RDRNumber = m.RDRNumber, ModuleName = m.ModuleName, CustomerBOM = m.CustomerBOM, ProductGroupName = p.Name, CustomerName = c.Name, EstimateProduct = m.EstimateProduct, Attachment = m.Attachment, Remark = m.Remark, CreateTime = m.CreateTime }).ToList(); return View(viewModel); } else { //拋出例外 return ShowMsgThenRedirect("找不到RDR表單", Url.Action("Details","RDRManage", new { id = id })); } //RDRModuleViewModel viewModel = new RDRModuleViewModel(); //viewModel.ParentID = main.ID; //viewModel.MainCode = main.RDRNumber; //viewModel.StartYear = main.SOPDate.Year; //viewModel.EndYear = main.EOLDate.Year; //viewModel.CustomerID = main.CustomerTeamID; //ViewData["ParentID"] = viewModel.ParentID; //viewModel.RDRModuleTempList = db.RDRModuleTemps.Where(m => m.ParentID == id).ToList(); //return View(viewModel); }
public ActionResult Create(RDRModuleCreateViewModel viewModel, HttpPostedFileBase file, FormCollection fc) { string fileName = ""; string path = ""; bool IsUploadOK = false; bool IsWriteToRDRModule = false; int moduleID = 0; int pid = 0; int.TryParse(fc["ProductGroupsList"], out pid); //表單產品別ID int cid = 0; int.TryParse(fc["CustomersList"], out cid); //表單客戶ID //1.若有上傳附件則執行上傳驗證,若無上傳略過此步驟 if (file != null) { #region 上傳 //從RDRNumber 取出資料夾結構: RFQFiles > 客戶群> ProjectName > 交貨地 > ModuleName > Version string UploadFilesType = "RFQFiles"; string team = viewModel.MainCode.Substring(0, viewModel.MainCode.IndexOf('.')); //取出客戶群,ex. AL.15.0001 string projName = db.RDRMains.Find(viewModel.ParentID).ProjectName; //專案名稱 string customerName = db.Customers.Find(cid).Name; string version = ".0"; //新增RDR表單的機種內容,預設版本號為0 //UP是IIS的虛擬目錄,於IIS設定指向Web Server上的實體資料夾 var target = "/UP/" + UploadFilesType + "/" + team + "/" + customerName + "/" + projName + "/" + viewModel.ModuleName + "/" + version; path = Server.MapPath(target); bool IsUpload = GOUpload(file); //驗證是否真的有檔案上傳 & 驗證檔案大小、格式是否符合規範 if (IsUpload) { fileName = System.IO.Path.GetFileName(file.FileName); //完整檔名 try { if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); //檢查資料夾路徑是否存在, 不存在就自動建立 } path = System.IO.Path.Combine(path, fileName); file.SaveAs(path); //檔案存放到儲存路徑上 IsUploadOK = true; } catch (Exception ex) { path = ""; return ShowMsgThenRedirect(ex.ToString(), Url.Action("Create", "RDRModule", new { id = viewModel.ParentID })); } } else { path = ""; } #endregion } //2.上傳驗證OK>>寫入db.RDRModule,若寫入db.RDRModule失敗則刪除附件&拋出例外 try { RDRModule rdrModule = new RDRModule(); rdrModule.ParentID = viewModel.ParentID; rdrModule.RDRNumber = CreateRDRNumber(viewModel.MainCode, pid, cid); rdrModule.ModuleName = viewModel.ModuleName; if (string.IsNullOrWhiteSpace(viewModel.CustomerBOM)) { rdrModule.CustomerBOM = "N/A"; //客戶料號 } else { rdrModule.CustomerBOM = viewModel.CustomerBOM; } rdrModule.ProductGroupID = pid; //產品別ID rdrModule.CustomerID = cid; //交貨地ID rdrModule.EstimateProduct = viewModel.EstimateProduct; rdrModule.Attachment = path; rdrModule.Remark = viewModel.Remark; rdrModule.ModuleVersion = 0; //版本號 預設值0 rdrModule.IsDelete = false; rdrModule.CreateTime = DateTime.Now; db.RDRModules.Add(rdrModule); db.SaveChanges(); IsWriteToRDRModule = true; moduleID = rdrModule.ID; } catch(Exception ex) { //刪除已上傳的的檔案 if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } return ShowMsgThenRedirect(ex.ToString(), Url.Action("Create", "RDRModule", new { id = viewModel.ParentID })); } //3.寫入db.RDRModule OK>>將附件資訊寫入db.UploadFile,若寫入db.UploadFile失敗則 delete db.RDRModule 與附件, 保持 RDRModule 與 UploadFile 資料一致 if (IsUploadOK && IsWriteToRDRModule) { try { UploadFile uploadfile = new UploadFile(); uploadfile.ModuleID = moduleID; // UploadFile.RefID 參考 RDRModule.ID uploadfile.Name = fileName; uploadfile.FileSize = file.ContentLength; uploadfile.UploadType = 1; uploadfile.ContentType = file.ContentType; uploadfile.Location = path; uploadfile.IsProductSpec = viewModel.IsProductSpec; uploadfile.IsTestInstruction = viewModel.IsTestInstruction; uploadfile.IsCustomerBOM = viewModel.IsCustomerBOM; uploadfile.IsBinResistorTable = viewModel.IsBinResistorTable; uploadfile.IsPCBA = viewModel.IsPCBA; uploadfile.IsPCB = viewModel.IsPCB; uploadfile.IsHarness = viewModel.IsHarness; uploadfile.IsGerber = viewModel.IsGerber; uploadfile.IsCoordinate = viewModel.IsCoordinate; uploadfile.IsSchematics = viewModel.IsSchematics; uploadfile.IsComp = viewModel.IsComp; uploadfile.IsPVTestPlan = viewModel.IsPVTestPlan; uploadfile.IsSVRF = viewModel.IsSVRF; uploadfile.CreateDateTime = DateTime.Now; uploadfile.CreateUserID = 1; uploadfile.ModifyDateTime = DateTime.Now; uploadfile.ModifyUserID = 1; db.UploadFiles.Add(uploadfile); db.SaveChanges(); } catch (Exception ex) { //刪除附件 if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } //刪除RDRModule記錄 RDRModule mo = db.RDRModules.Where(m => m.ID == moduleID).FirstOrDefault(); if (mo != null) { db.RDRModules.Remove(mo); db.SaveChanges(); } return ShowMsgThenRedirect(ex.ToString(), Url.Action("Index", "RDRManage")); } } return RedirectToAction("Create", new { id = viewModel.ParentID }); }