public virtual WMS_Inv_History_DModel GetById(object id) { if (IsExists(id)) { WMS_Inv_History_D entity = m_Rep.GetById(id); WMS_Inv_History_DModel model = new WMS_Inv_History_DModel(); model.Id = entity.Id; model.HeadId = entity.HeadId; model.PartId = entity.PartId; model.SnapshootQty = entity.SnapshootQty; model.InvId = entity.InvId; model.SubInvId = entity.SubInvId; model.Remark = entity.Remark; model.Attr1 = entity.Attr1; model.Attr2 = entity.Attr2; model.Attr3 = entity.Attr3; model.Attr4 = entity.Attr4; model.Attr5 = entity.Attr5; model.CreatePerson = entity.CreatePerson; model.CreateTime = entity.CreateTime; model.ModifyPerson = entity.ModifyPerson; model.ModifyTime = entity.ModifyTime; return(model); } else { return(null); } }
public virtual bool Edit(ref ValidationErrors errors, WMS_Inv_History_DModel model) { try { WMS_Inv_History_D entity = m_Rep.GetById(model.Id); if (entity == null) { errors.Add(Resource.Disable); return(false); } entity.Id = model.Id; entity.HeadId = model.HeadId; entity.PartId = model.PartId; entity.SnapshootQty = model.SnapshootQty; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.Remark = model.Remark; entity.Attr1 = model.Attr1; entity.Attr2 = model.Attr2; entity.Attr3 = model.Attr3; entity.Attr4 = model.Attr4; entity.Attr5 = model.Attr5; entity.CreatePerson = model.CreatePerson; entity.CreateTime = model.CreateTime; entity.ModifyPerson = model.ModifyPerson; entity.ModifyTime = model.ModifyTime; 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 void AdditionalCheckExcelData(ref WMS_Inv_History_DModel model) { }
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_Inv_History_DModel>(x => x.HeadId, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.PartId, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.SnapshootQty, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.InvId, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.SubInvId, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Remark, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr1, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr2, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr3, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr4, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr5, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.CreatePerson, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.CreateTime, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.ModifyPerson, ""); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.ModifyTime, ""); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_Inv_History_DModel>(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_Inv_History_DModel(); model.Id = row.Id; model.HeadId = row.HeadId; model.PartId = row.PartId; model.SnapshootQty = row.SnapshootQty; model.InvId = row.InvId; model.SubInvId = row.SubInvId; model.Remark = row.Remark; model.Attr1 = row.Attr1; model.Attr2 = row.Attr2; model.Attr3 = row.Attr3; model.Attr4 = row.Attr4; model.Attr5 = row.Attr5; model.CreatePerson = row.CreatePerson; model.CreateTime = row.CreateTime; model.ModifyPerson = row.ModifyPerson; model.ModifyTime = row.ModifyTime; 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_History_D entity = new WMS_Inv_History_D(); entity.Id = model.Id; entity.HeadId = model.HeadId; entity.PartId = model.PartId; entity.SnapshootQty = model.SnapshootQty; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.Remark = model.Remark; entity.Attr1 = model.Attr1; entity.Attr2 = model.Attr2; entity.Attr3 = model.Attr3; entity.Attr4 = model.Attr4; entity.Attr5 = model.Attr5; entity.CreatePerson = model.CreatePerson; entity.CreateTime = model.CreateTime; entity.ModifyPerson = model.ModifyPerson; entity.ModifyTime = model.ModifyTime; entity.CreatePerson = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_Inv_History_D.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 ActionResult Details(long id) { WMS_Inv_History_DModel entity = m_BLL.GetById(id); return(View(entity)); }
/// <summary> /// 校验Excel数据,这个方法一般用于重写校验逻辑 /// </summary> public virtual bool CheckImportData(string fileName, List <WMS_Inv_History_DModel> list, ref ValidationErrors errors) { var targetFile = new FileInfo(fileName); if (!targetFile.Exists) { errors.Add("导入的数据文件不存在"); return(false); } var excelFile = new ExcelQueryFactory(fileName); //对应列头 excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.HeadId, "头表ID"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.PartId, "物料"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.SnapshootQty, "数量"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.InvId, "库房"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.SubInvId, "子库房"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Remark, "备注"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr1, "Attr1"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr2, "Attr2"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr3, "Attr3"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr4, "Attr4"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.Attr5, "Attr5"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.CreatePerson, "创建人"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.CreateTime, "创建时间"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.ModifyPerson, "修改人"); excelFile.AddMapping <WMS_Inv_History_DModel>(x => x.ModifyTime, "修改时间"); //SheetName var excelContent = excelFile.Worksheet <WMS_Inv_History_DModel>(0); int rowIndex = 1; //检查数据正确性 foreach (var row in excelContent) { var errorMessage = new StringBuilder(); var entity = new WMS_Inv_History_DModel(); entity.Id = row.Id; entity.HeadId = row.HeadId; entity.PartId = row.PartId; entity.SnapshootQty = row.SnapshootQty; entity.InvId = row.InvId; entity.SubInvId = row.SubInvId; entity.Remark = row.Remark; entity.Attr1 = row.Attr1; entity.Attr2 = row.Attr2; entity.Attr3 = row.Attr3; entity.Attr4 = row.Attr4; entity.Attr5 = row.Attr5; entity.CreatePerson = row.CreatePerson; entity.CreateTime = row.CreateTime; entity.ModifyPerson = row.ModifyPerson; entity.ModifyTime = row.ModifyTime; //============================================================================= 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); }