public ActionResult Create([Bind(Include = "id, partnr, orderedDate, requiredDate, quantity, source, sourceDoc,status,unitId")] MP mp) { try { IMpsService mps = new MpsService(Settings.Default.db); mps.Create(mp); return(RedirectToAction("Index")); } catch { if (String.IsNullOrWhiteSpace(mp.source)) { ViewBag.SourceError = "Source 字段是必需的。"; } if (String.IsNullOrWhiteSpace(mp.partnr)) { ViewBag.Error = "PartNr 字段是必需的。"; } else { ViewBag.Error = "PartNr 不存在。"; } SetMpStatusDisplayList(null); return(View()); } }
public ActionResult ImportMpsRecord(HttpPostedFileBase mpsFile) { if (mpsFile == null) { //TODO: Mps Import 优化上传失败界面 throw new Exception("No file is uploaded to system"); } var appData = Server.MapPath("~/TmpFile/"); var filename = Path.Combine(appData, DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(mpsFile.FileName)); mpsFile.SaveAs(filename); string ex = Path.GetExtension(filename); List <MpsImportModel> records = new List <MpsImportModel>(); if (ex.Equals(".csv")) { CsvConfiguration configuration = new CsvConfiguration(); configuration.Delimiter = Settings.Default.csvDelimiter; configuration.HasHeaderRecord = true; configuration.SkipEmptyRecords = true; configuration.RegisterClassMap <MpsCsvModelMap>(); configuration.TrimHeaders = true; configuration.TrimFields = true; try { using (TextReader treader = System.IO.File.OpenText(filename)) { CsvReader csvReader = new CsvReader(treader, configuration); records = csvReader.GetRecords <MpsImportModel>().ToList(); Console.Write(records); } } catch (Exception e) { //ViewBag.TextExpMsg = "<-------------Read Csv File Exception!,Please Check.------------->" + e; ViewBag.TextExpMsg = "<-------------读取CSV文件异常,请查看原因:------------->" + e; } List <Dictionary <string, string> > CreateErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > UpdateErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > DeleteErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > ActionNullErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > OtherErrorDic = new List <Dictionary <string, string> >(); if (records.Count > 0) { int AllQty = records.Count; int CreateSuccessQty = 0; int CreateFailureQty = 0; int UpdateSuccessQty = 0; int UpdateFailureQty = 0; int DeleteSuccessQty = 0; int DeleteFailureQty = 0; int ActionNullQty = 0; int OtherQty = 0; IMpsService ps = new MpsService(Settings.Default.db); foreach (MpsImportModel record in records) { if (string.IsNullOrWhiteSpace(record.Action)) { ActionNullQty++; Dictionary <string, string> ActionNullErrorList = new Dictionary <string, string>(); ActionNullErrorList.Add("ID", record.ID); ActionNullErrorList.Add("PartNr", record.PartNr); ActionNullErrorList.Add("OrderedDate", record.OrderedDate.ToString()); ActionNullErrorList.Add("RequiredDate", record.RequiredDate.ToString()); ActionNullErrorList.Add("Quantity", record.Quantity.ToString()); ActionNullErrorList.Add("Source", record.Source); ActionNullErrorList.Add("SourceDoc", record.SourceDoc); ActionNullErrorList.Add("Status", record.Status.ToString()); ActionNullErrorList.Add("UnitId", record.UnitId); ActionNullErrorList.Add("Action", record.Action); ActionNullErrorDic.Add(ActionNullErrorList); ViewData["actionNullErrorDic"] = ActionNullErrorDic; } else { MP mps = new MP() { id = Convert.ToInt32(record.ID), partnr = record.PartNr, orderedDate = record.OrderedDate, requiredDate = record.RequiredDate, quantity = record.Quantity, source = record.Source, sourceDoc = record.SourceDoc, status = record.Status, unitId = record.UnitId }; if (record.Action.Trim().ToLower().Equals("create")) { //新建 try { ps.Create(mps); CreateSuccessQty++; } catch (Exception) { CreateFailureQty++; Dictionary <string, string> CreateErrorList = new Dictionary <string, string>(); CreateErrorList.Add("ID", record.ID); CreateErrorList.Add("PartNr", record.PartNr); CreateErrorList.Add("OrderedDate", record.OrderedDate.ToString()); CreateErrorList.Add("RequiredDate", record.RequiredDate.ToString()); CreateErrorList.Add("Quantity", record.Quantity.ToString()); CreateErrorList.Add("Source", record.Source); CreateErrorList.Add("SourceDoc", record.SourceDoc); CreateErrorList.Add("Status", record.Status.ToString()); CreateErrorList.Add("UnitId", record.UnitId); CreateErrorList.Add("Action", record.Action); CreateErrorDic.Add(CreateErrorList); ViewData["createErrorDic"] = CreateErrorDic; } } else if (record.Action.Trim().ToLower().Equals("update")) { //更新 try { bool result = ps.Update(mps); if (result) { UpdateSuccessQty++; } else { UpdateFailureQty++; Dictionary <string, string> UpdateErrorList = new Dictionary <string, string>(); UpdateErrorList.Add("ID", record.ID); UpdateErrorList.Add("PartNr", record.PartNr); UpdateErrorList.Add("OrderedDate", record.OrderedDate.ToString()); UpdateErrorList.Add("RequiredDate", record.RequiredDate.ToString()); UpdateErrorList.Add("Quantity", record.Quantity.ToString()); UpdateErrorList.Add("Source", record.Source); UpdateErrorList.Add("SourceDoc", record.SourceDoc); UpdateErrorList.Add("Status", record.Status.ToString()); UpdateErrorList.Add("UnitId", record.UnitId); UpdateErrorList.Add("Action", record.Action); UpdateErrorDic.Add(UpdateErrorList); ViewData["updateErrorDic"] = UpdateErrorDic; } } catch (Exception) { UpdateFailureQty++; Dictionary <string, string> UpdateErrorList = new Dictionary <string, string>(); UpdateErrorList.Add("ID", record.ID); UpdateErrorList.Add("PartNr", record.PartNr); UpdateErrorList.Add("OrderedDate", record.OrderedDate.ToString()); UpdateErrorList.Add("RequiredDate", record.RequiredDate.ToString()); UpdateErrorList.Add("Quantity", record.Quantity.ToString()); UpdateErrorList.Add("Source", record.Source); UpdateErrorList.Add("SourceDoc", record.SourceDoc); UpdateErrorList.Add("Status", record.Status.ToString()); UpdateErrorList.Add("UnitId", record.UnitId); UpdateErrorList.Add("Action", record.Action); UpdateErrorDic.Add(UpdateErrorList); ViewData["updateErrorDic"] = UpdateErrorDic; } } else if (record.Action.Trim().ToLower().Equals("delete")) { try { //删除 bool result = ps.DeleteById(mps.id.ToString()); if (result) { DeleteSuccessQty++; } else { DeleteFailureQty++; Dictionary <string, string> DeleteErrorList = new Dictionary <string, string>(); DeleteErrorList.Add("ID", record.ID); DeleteErrorList.Add("PartNr", record.PartNr); DeleteErrorList.Add("OrderedDate", record.OrderedDate.ToString()); DeleteErrorList.Add("RequiredDate", record.RequiredDate.ToString()); DeleteErrorList.Add("Quantity", record.Quantity.ToString()); DeleteErrorList.Add("Source", record.Source); DeleteErrorList.Add("SourceDoc", record.SourceDoc); DeleteErrorList.Add("Status", record.Status.ToString()); DeleteErrorList.Add("UnitId", record.UnitId); DeleteErrorList.Add("Action", record.Action); DeleteErrorDic.Add(DeleteErrorList); ViewData["deleteErrorDic"] = DeleteErrorDic; } } catch (Exception e) { DeleteFailureQty++; Dictionary <string, string> DeleteErrorList = new Dictionary <string, string>(); DeleteErrorList.Add("ID", record.ID); DeleteErrorList.Add("PartNr", record.PartNr); DeleteErrorList.Add("OrderedDate", record.OrderedDate.ToString()); DeleteErrorList.Add("RequiredDate", record.RequiredDate.ToString()); DeleteErrorList.Add("Quantity", record.Quantity.ToString()); DeleteErrorList.Add("Source", record.Source); DeleteErrorList.Add("SourceDoc", record.SourceDoc); DeleteErrorList.Add("Status", record.Status.ToString()); DeleteErrorList.Add("UnitId", record.UnitId); DeleteErrorList.Add("Action", record.Action); DeleteErrorDic.Add(DeleteErrorList); ViewData["deleteErrorDic"] = DeleteErrorDic; } } else { //错误 忽略 Dictionary <string, string> OtherErrorList = new Dictionary <string, string>(); OtherErrorList.Add("ID", record.ID); OtherErrorList.Add("PartNr", record.PartNr); OtherErrorList.Add("OrderedDate", record.OrderedDate.ToString()); OtherErrorList.Add("RequiredDate", record.RequiredDate.ToString()); OtherErrorList.Add("Quantity", record.Quantity.ToString()); OtherErrorList.Add("Source", record.Source); OtherErrorList.Add("SourceDoc", record.SourceDoc); OtherErrorList.Add("Status", record.Status.ToString()); OtherErrorList.Add("UnitId", record.UnitId); OtherErrorList.Add("Action", record.Action); OtherErrorDic.Add(OtherErrorList); ViewData["otherErrorDic"] = OtherErrorDic; } } } OtherQty = AllQty - CreateSuccessQty - CreateFailureQty - UpdateSuccessQty - UpdateFailureQty - DeleteSuccessQty - DeleteFailureQty - ActionNullQty; Dictionary <string, int> Qty = new Dictionary <string, int>(); Qty.Add("AllQty", AllQty); Qty.Add("CreateSuccessQty", CreateSuccessQty); Qty.Add("CreateFailureQty", CreateFailureQty); Qty.Add("UpdateSuccessQty", UpdateSuccessQty); Qty.Add("UpdateFailureQty", UpdateFailureQty); Qty.Add("DeleteSuccessQty", DeleteSuccessQty); Qty.Add("DeleteFailureQty", DeleteFailureQty); Qty.Add("ActionNullQty", ActionNullQty); Qty.Add("OtherQty", OtherQty); ViewData["Qty"] = Qty; } else { //ViewBag.NotCheckedData = "No Data Checked. Please Check Delimiter or Column Name."; ViewBag.NotCheckedData = "没有检测到数据。请检查分隔符和列名。"; } } else { //ViewBag.NotCsv = "Your File is not .Csv File, Please Check FileName."; ViewBag.NotCsv = "你上传的文件不是.CSV格式。请检查文件名。"; } if (ViewBag.NotCsv == null) { //ViewBag.NotCsv = "CSV File is OK."; ViewBag.NotCsv = "上传CSV文件正确!"; } if (ViewBag.NotCheckedData == null) { //ViewBag.NotCheckedData = "Check Data is OK."; ViewBag.NotCheckedData = "检查数据完成!"; } return(View()); }