/// <summary> /// 校验Excel数据,这个方法一般用于重写校验逻辑 /// </summary> public virtual bool CheckImportData(string fileName, List <WMS_InvModel> list, ref ValidationErrors errors) { var targetFile = new FileInfo(fileName); if (!targetFile.Exists) { errors.Add("导入的数据文件不存在"); return(false); } var excelFile = new ExcelQueryFactory(fileName); //对应列头 excelFile.AddMapping <WMS_InvModel>(x => x.InvId, "InvId"); excelFile.AddMapping <WMS_InvModel>(x => x.SubInvId, "SubInvId"); excelFile.AddMapping <WMS_InvModel>(x => x.PartId, "PartId"); excelFile.AddMapping <WMS_InvModel>(x => x.Lot, "批次号:YYYYMM"); excelFile.AddMapping <WMS_InvModel>(x => x.Qty, "Qty"); excelFile.AddMapping <WMS_InvModel>(x => x.OutQty, "当前出库数量"); excelFile.AddMapping <WMS_InvModel>(x => x.StockQty, "备料数"); //SheetName var excelContent = excelFile.Worksheet <WMS_InvModel>(0); int rowIndex = 1; //检查数据正确性 foreach (var row in excelContent) { var errorMessage = new StringBuilder(); var entity = new WMS_InvModel(); entity.Id = row.Id; entity.InvId = row.InvId; entity.SubInvId = row.SubInvId; entity.PartId = row.PartId; entity.Lot = row.Lot; entity.Qty = row.Qty; entity.OutQty = row.OutQty; entity.StockQty = row.StockQty; //============================================================================= if (errorMessage.Length > 0) { errors.Add(string.Format( "第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); } list.Add(entity); rowIndex += 1; } if (errors.Count > 0) { return(false); } return(true); }
public virtual WMS_InvModel GetById(object id) { if (IsExists(id)) { WMS_Inv entity = m_Rep.GetById(id); WMS_InvModel model = new WMS_InvModel(); model.Id = entity.Id; model.InvId = entity.InvId; model.SubInvId = entity.SubInvId; model.PartId = entity.PartId; model.Qty = entity.Qty; model.Lot = entity.Lot; model.StockQty = entity.StockQty; return(model); } else { return(null); } }
public JsonResult Edit(WMS_InvModel model) { if (model != null && ModelState.IsValid) { if (m_BLL.Edit(ref errors, model)) { LogHandler.WriteServiceLog(GetUserTrueName(), "Id" + model.Id + ",InvId" + model.InvId, "成功", "修改", "WMS_Inv"); return(Json(JsonHandler.CreateMessage(1, Resource.EditSucceed))); } else { string ErrorCol = errors.Error; LogHandler.WriteServiceLog(GetUserTrueName(), "Id" + model.Id + ",InvId" + model.InvId + "," + ErrorCol, "失败", "修改", "WMS_Inv"); return(Json(JsonHandler.CreateMessage(0, Resource.EditFail + ErrorCol))); } } else { return(Json(JsonHandler.CreateMessage(0, Resource.EditFail))); } }
public virtual bool Create(ref ValidationErrors errors, WMS_InvModel model) { try { WMS_Inv entity = m_Rep.GetById(model.Id); if (entity != null) { errors.Add(Resource.PrimaryRepeat); return(false); } entity = new WMS_Inv(); entity.Id = model.Id; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.PartId = model.PartId; entity.Lot = model.Lot; entity.Qty = model.Qty; entity.OutQty = model.OutQty; entity.StockQty = model.StockQty; if (m_Rep.Create(entity)) { return(true); } else { errors.Add(Resource.InsertFail); return(false); } } catch (Exception ex) { errors.Add(ex.Message); ExceptionHander.WriteException(ex); return(false); } }
public virtual bool Edit(ref ValidationErrors errors, WMS_InvModel model) { try { WMS_Inv entity = m_Rep.GetById(model.Id); if (entity == null) { errors.Add(Resource.Disable); return(false); } entity.Id = model.Id; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.PartId = model.PartId; entity.Lot = model.Lot; entity.Qty = model.Qty; entity.OutQty = model.OutQty; entity.StockQty = model.StockQty; if (m_Rep.Edit(entity)) { return(true); } else { errors.Add(Resource.NoDataChange); return(false); } } catch (Exception ex) { errors.Add(ex.Message); ExceptionHander.WriteException(ex); return(false); } }
public ActionResult Edit(long id) { WMS_InvModel entity = m_BLL.GetById(id); return(View(entity)); }
public bool ImportExcelData(string oper, string filePath, ref ValidationErrors errors) { bool rtn = true; var targetFile = new FileInfo(filePath); if (!targetFile.Exists) { errors.Add("导入的数据文件不存在"); return(false); } var excelFile = new ExcelQueryFactory(filePath); using (XLWorkbook wb = new XLWorkbook(filePath)) { //第一个Sheet using (IXLWorksheet wws = wb.Worksheets.First()) { //对应列头 excelFile.AddMapping <WMS_InvModel>(x => x.InvId, ""); excelFile.AddMapping <WMS_InvModel>(x => x.SubInvId, ""); excelFile.AddMapping <WMS_InvModel>(x => x.PartId, ""); excelFile.AddMapping <WMS_InvModel>(x => x.Qty, ""); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_InvModel>(0); //开启事务 using (DBContainer db = new DBContainer()) { var tran = db.Database.BeginTransaction(); //开启事务 int rowIndex = 0; //检查数据正确性 foreach (var row in excelContent) { rowIndex += 1; string errorMessage = String.Empty; var model = new WMS_InvModel(); model.Id = row.Id; model.InvId = row.InvId; model.SubInvId = row.SubInvId; model.PartId = row.PartId; model.Qty = row.Qty; if (!String.IsNullOrEmpty(errorMessage)) { rtn = false; errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage; continue; } //执行额外的数据校验 try { AdditionalCheckExcelData(ref model); } catch (Exception ex) { rtn = false; errorMessage = ex.Message; errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage; continue; } //写入数据库 WMS_Inv entity = new WMS_Inv(); entity.Id = model.Id; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.PartId = model.PartId; entity.Qty = model.Qty; db.WMS_Inv.Add(entity); try { db.SaveChanges(); } catch (Exception ex) { rtn = false; //将当前报错的entity状态改为分离,类似EF的回滚(忽略之前的Add操作) db.Entry(entity).State = System.Data.Entity.EntityState.Detached; errorMessage = ex.InnerException.InnerException.Message; errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage; } } if (rtn) { tran.Commit(); //必须调用Commit(),不然数据不会保存 } else { tran.Rollback(); //出错就回滚 } } } wb.Save(); } return(rtn); }
public void AdditionalCheckExcelData(ref WMS_InvModel model) { }