public ActionResult Add(int parentID) { //Insert Into RDRModules List<RDRModuleTemp> tempList = db.RDRModuleTemps.Where(m => m.ParentID == parentID).ToList(); if (tempList != null & tempList.Count > 0) { foreach (var item in tempList) { RDRModule module = new RDRModule(); module.ParentID = item.ParentID; module.RDRNumber = item.RDRNumber; module.ModuleName = item.ModuleName; module.ProductGroupID = item.ProductGroupID; module.CustomerID = item.CustomerID; module.EstimateProduct = item.EstimateProduct; module.Attachment = item.Attachment; module.Remark = item.Remark; module.ModuleVersion = item.ModuleVersion; module.CreateTime = item.CreateTime; module.RDRNumber = CreateRDRNumber(item.RDRNumber, item.ProductGroupID, item.CustomerID); module.CustomerBOM = item.CustomerBOM; if (ModelState.IsValid) { db.RDRModules.Add(module); db.SaveChanges(); db.RDRModuleTemps.Remove(item); db.SaveChanges(); } } return RedirectToAction("Details", "RDRManage", new { id = parentID }); } else { int pid = int.Parse(ViewData["ParentID"].ToString()); //日後有時間應該改成宗瑋的 ShowMsgThenRedirect(string Msg, string Url) 這樣共用比較好用 //return Content("<script language='javascript' type='text/javascript'>alert('尚未新增機種資料!'); location.replace('" + Url.Action("Create", "RDRModules", new { id = parentID }) + "');</script>"); return ShowMsgThenRedirect("尚未新增機種資料", Url.Action("Create", "RDRModules", new { id = parentID })); } }
public ActionResult Upgrade(int id) { RDRModule rdrmodule = db.RDRModules.FirstOrDefault(m => m.ID == id); //搜尋DB相似的RDRNumber & 取出目前尾碼最大碼 var lastChar = rdrmodule.RDRNumber.Substring(rdrmodule.RDRNumber.Length - 1, 1); //取RDRNumber最後一碼 var partString = rdrmodule.RDRNumber.Substring(0, rdrmodule.RDRNumber.Length - 2); //移除RDRNumber最後一碼 var allChar = (from c in db.RDRModules where c.RDRNumber.Contains(partString) orderby c.RDRNumber select new { c.RDRNumber, }).ToList(); List<int> LastCodeList = new List<int>(); foreach (var item in allChar) { string str_last = item.RDRNumber.Substring(rdrmodule.RDRNumber.Length - 1, 1); int i_last = int.Parse(str_last); LastCodeList.Add(i_last); } int currentMax = LastCodeList.Max(); int futureMax = currentMax + 1; //複製&新增 int newModuleID = 0; try { RDRModule newModule = new RDRModule(); newModule.ParentID = rdrmodule.ParentID; newModule.RDRNumber = partString + "." + futureMax.ToString(); newModule.ModuleName = rdrmodule.ModuleName; newModule.CustomerBOM = rdrmodule.CustomerBOM; newModule.ProductGroupID = rdrmodule.ProductGroupID; newModule.CustomerID = rdrmodule.CustomerID; newModule.EstimateProduct = rdrmodule.EstimateProduct; newModule.Remark = rdrmodule.Remark; newModule.CreateTime = DateTime.Now; db.RDRModules.Add(newModule); db.SaveChanges(); newModuleID = newModule.ID; return RedirectToAction("Edit", new { id = newModuleID }); } catch (Exception ex) { } return View(); }
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 }); }