public virtual bool Edit(T model, bool isCommit = true) { db.Set <T>().Attach(model); db.Entry <T>(model).State = EntityState.Modified; if (isCommit) { return(SaveChanges() > 0); } else { return(false); } }
/// <summary> /// 更新权限表 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool UpdateRight(SysRightOperate model) { using (DBContainer db = new DBContainer()) { SysRightOperate right = db.SysRightOperate.SingleOrDefault(r => r.Id == model.Id); if (right != null) { //也许right在上一句查询中已被添加到db容器中 right.IsValid = model.IsValid; db.Set <SysRightOperate>().Attach(right); db.Entry <SysRightOperate>(right).State = EntityState.Modified; } else { db.Set <SysRightOperate>().Add(model); } // int i = db.SaveChanges(); if (i > 0) { var sysRight = (from r in db.SysRight where r.Id == model.RightId select r).First(); db.P_Sys_UpdateSysRightRightFlag(sysRight.ModuleId, sysRight.RoleId); return(true); } return(false); } }
public int UpdateRight(SysRightOperate model) { //判断rightOperate是否存在,如果存在就更新rightOperate,否则就添加一条 using (DBContainer db = new DBContainer()) { SysRightOperate right = db.SysRightOperate.FirstOrDefault(o => o.Id == model.Id); if (right != null) { right.RightId = model.RightId; right.KeyCode = model.KeyCode; right.IsValid = model.IsValid; db.Entry(right).State = System.Data.Entity.EntityState.Modified; } else { db.SysRightOperate.Add(model); } if (db.SaveChanges() > 0) { //更新角色--模块的有效标志RightFlag var sysRight = (from r in db.SysRight where r.Id == model.RightId select r).First(); db.P_Sys_UpdateSysRightRightFlag(sysRight.ModuleId, sysRight.RoleId); return(1); } } return(0); }
/// <summary> /// 修改一个实体 /// </summary> /// <param name="db">数据库上下文</param> /// <param name="entity">实体</param> public int Edit(SysSample entity) { DBContainer db = new DBContainer(); db.Entry(entity).State = System.Data.Entity.EntityState.Modified; //db.SysSample.Attach(entity); return(db.SaveChanges()); }
public int Edit(SysRole entity) { using (DBContainer db = new DBContainer()) { db.Entry(entity).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges()); } }
public void ProcessRequest(HttpContext context) { int id = int.Parse(context.Request.Form["Id"]); DBContainer db = new DBContainer(); dtInfo info = (from i in db.dtInfo where i.Id==id select i).FirstOrDefault(); db.Entry<dtInfo>(info).State = EntityState.Deleted; db.SaveChanges(); }
public int Edit(SysRole entity) { using (DBContainer db = new DBContainer()) { db.SysRole.Attach(entity); db.Entry <SysRole>(entity).State = EntityState.Modified; return(db.SaveChanges()); } }
/// <summary> /// 修改一个实体 /// </summary> /// <param name="entity">数据库上下文</param> /// <returns>执行返回结果影响行数</returns> public int Edit(test_sys_sample entity) { using (DBContainer db = new DBContainer()) { db.Set <test_sys_sample>().Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges()); } }
/// <summary> /// 修改一个实体 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public int Edit(SysOperations entity) { using (DBContainer db = new DBContainer()) { db.Set <SysOperations>().Attach(entity); db.Entry <SysOperations>(entity).State = EntityState.Modified; return(db.SaveChanges()); } }
public void ProcessRequest(HttpContext context) { int id = int.Parse(context.Request.Form["Id"]); int val = int.Parse(context.Request.Form["Val"]); DBContainer db = new DBContainer(); dtInfo info = (from i in db.dtInfo where i.Id == id select i).FirstOrDefault(); info.BIsMode = (val==1); db.Entry<dtInfo>(info).State = System.Data.EntityState.Modified; db.SaveChanges();//执行数据库操作 }
public int Create(MIS_Article_Category entity) { using (DBContainer db = new DBContainer()) { // db.MIS_Article_Category.Remove(entity); db.Set <MIS_Article_Category>().Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Added; return(db.SaveChanges()); } }
public int Edit(MIS_Article_Category entity) { using (DBContainer db = new DBContainer()) { //db.MIS_Article_Category.Attach(entity); //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); db.Set <MIS_Article_Category>().Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges()); } }
public int Edit(SysRight entity) { using (DBContainer db = new DBContainer()) { // db.SysRight.Attach(entity); // db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); db.Set <SysRight>().Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges()); } }
public int Edit(SysRole entity) { using (DBContainer db = new DBContainer()) { // db.SysRole.Attach(entity); db.Set <SysRole>().Attach(entity); db.Entry(entity).State = EntityState.Modified; // db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); return(db.SaveChanges()); } }
/// <summary> /// 改 /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual bool Edit(T entity) { if (db.Entry <T>(entity).State == EntityState.Modified) { return(db.SaveChanges() > 0); } else if (db.Entry <T>(entity).State == EntityState.Detached) { try { db.Set <T>().Attach(entity); db.Entry <T>(entity).State = EntityState.Modified; } catch (InvalidOperationException ex) { } return(db.SaveChanges() > 0); } return(false); }
public virtual bool Edit(T model) { if (db.Entry <T>(model).State == EntityState.Modified) { return(db.SaveChanges() > 0); } else if (db.Entry <T>(model).State == EntityState.Detached) { try { db.Set <T>().Attach(model); db.Entry <T>(model).State = EntityState.Modified; } catch (InvalidOperationException) { //T old = Find(model._ID); //db.Entry<old>.CurrentValues.SetValues(model); } return(db.SaveChanges() > 0); } return(false); }
public int Delete(string id) { using (DBContainer db = new DBContainer()) { MIS_Article_Category entity = db.MIS_Article_Category.SingleOrDefault(a => a.Id == id); if (entity != null) { db.Set <MIS_Article_Category>().Attach(entity); db.Entry(entity).State = System.Data.Entity.EntityState.Deleted; db.MIS_Article_Category.Remove(entity); } return(db.SaveChanges()); } }
/// <summary> /// 保存数据 /// </summary> public void SaveData(IEnumerable <Spl_WarehouseWarrantDetailsOUTModel> personList) { try { using (DBContainer db = new DBContainer()) { foreach (var model in personList) { Spl_WarehouseWarrantDetailsOUT entity = new Spl_WarehouseWarrantDetailsOUT(); entity.Id = model.Id; entity.WareDetailsId = model.WareDetailsId; entity.WarehouseId = model.WarehouseId; entity.WarehouseWarrantId = model.WarehouseWarrantId; entity.Quantity = model.Quantity; entity.Price = model.Price; entity.TotalPrice = model.TotalPrice; entity.Defined = model.Defined; entity.CreateTime = model.CreateTime; //新增 if (string.IsNullOrEmpty(model.Id)) { entity.Id = ResultHelper.NewId; db.Spl_WarehouseWarrantDetailsOUT.Add(entity); } else { //修改 db.Set <Spl_WarehouseWarrantDetailsOUT>().Attach(entity); db.Entry <Spl_WarehouseWarrantDetailsOUT>(entity).State = EntityState.Modified; } } db.SaveChanges(); } } catch { throw; } }
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_AIModel>(x => x.ArrivalBillNum, "到货单据号"); excelFile.AddMapping <WMS_AIModel>(x => x.POId, "采购订单ID"); excelFile.AddMapping <WMS_AIModel>(x => x.PartId, "物料ID"); excelFile.AddMapping <WMS_AIModel>(x => x.BoxQty, "到货箱数"); excelFile.AddMapping <WMS_AIModel>(x => x.ArrivalQty, "到货数量"); excelFile.AddMapping <WMS_AIModel>(x => x.ArrivalDate, "到货日期"); excelFile.AddMapping <WMS_AIModel>(x => x.ReceiveMan, "接收人"); excelFile.AddMapping <WMS_AIModel>(x => x.ReceiveStatus, "到货状态"); excelFile.AddMapping <WMS_AIModel>(x => x.InspectBillNum, "送检单号"); excelFile.AddMapping <WMS_AIModel>(x => x.InspectMan, "送检人"); excelFile.AddMapping <WMS_AIModel>(x => x.InspectDate, "送检日期"); excelFile.AddMapping <WMS_AIModel>(x => x.InspectStatus, "送检状体"); excelFile.AddMapping <WMS_AIModel>(x => x.CheckOutDate, "检验日期"); excelFile.AddMapping <WMS_AIModel>(x => x.CheckOutResult, "检验结果"); excelFile.AddMapping <WMS_AIModel>(x => x.QualifyQty, "合格数量"); excelFile.AddMapping <WMS_AIModel>(x => x.NoQualifyQty, "不合格数量"); excelFile.AddMapping <WMS_AIModel>(x => x.CheckOutRemark, "检验说明"); excelFile.AddMapping <WMS_AIModel>(x => x.ReInspectBillNum, "重新送检单"); excelFile.AddMapping <WMS_AIModel>(x => x.InStoreBillNum, "入库单号"); excelFile.AddMapping <WMS_AIModel>(x => x.InStoreMan, ""); excelFile.AddMapping <WMS_AIModel>(x => x.InvId, "入库仓库"); excelFile.AddMapping <WMS_AIModel>(x => x.SubInvId, "子库"); excelFile.AddMapping <WMS_AIModel>(x => x.InStoreStatus, "入库状态"); excelFile.AddMapping <WMS_AIModel>(x => x.Attr1, ""); excelFile.AddMapping <WMS_AIModel>(x => x.Attr2, ""); excelFile.AddMapping <WMS_AIModel>(x => x.Attr3, ""); excelFile.AddMapping <WMS_AIModel>(x => x.Attr4, ""); excelFile.AddMapping <WMS_AIModel>(x => x.Attr5, ""); excelFile.AddMapping <WMS_AIModel>(x => x.CreatePerson, "创建人"); excelFile.AddMapping <WMS_AIModel>(x => x.CreateTime, "创建时间"); excelFile.AddMapping <WMS_AIModel>(x => x.ModifyPerson, "修改人"); excelFile.AddMapping <WMS_AIModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_AIModel>(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_AIModel(); model.Id = row.Id; model.ArrivalBillNum = row.ArrivalBillNum; model.POId = row.POId; model.PartId = row.PartId; model.BoxQty = row.BoxQty; model.ArrivalQty = row.ArrivalQty; model.ArrivalDate = row.ArrivalDate; model.ReceiveMan = row.ReceiveMan; model.ReceiveStatus = row.ReceiveStatus; model.InspectBillNum = row.InspectBillNum; model.InspectMan = row.InspectMan; model.InspectDate = row.InspectDate; model.InspectStatus = row.InspectStatus; model.CheckOutDate = row.CheckOutDate; model.CheckOutResult = row.CheckOutResult; model.QualifyQty = row.QualifyQty; model.NoQualifyQty = row.NoQualifyQty; model.CheckOutRemark = row.CheckOutRemark; model.ReInspectBillNum = row.ReInspectBillNum; model.InStoreBillNum = row.InStoreBillNum; model.InStoreMan = row.InStoreMan; model.InvId = row.InvId; model.SubInvId = row.SubInvId; model.InStoreStatus = row.InStoreStatus; 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_AI entity = new WMS_AI(); entity.Id = model.Id; entity.ArrivalBillNum = model.ArrivalBillNum; entity.POId = model.POId; entity.PartId = model.PartId; entity.BoxQty = model.BoxQty; entity.ArrivalQty = model.ArrivalQty; entity.ArrivalDate = model.ArrivalDate; entity.ReceiveMan = model.ReceiveMan; entity.ReceiveStatus = model.ReceiveStatus; entity.InspectBillNum = model.InspectBillNum; entity.InspectMan = model.InspectMan; entity.InspectDate = model.InspectDate; entity.InspectStatus = model.InspectStatus; entity.CheckOutDate = model.CheckOutDate; entity.CheckOutResult = model.CheckOutResult; entity.QualifyQty = model.QualifyQty; entity.NoQualifyQty = model.NoQualifyQty; entity.CheckOutRemark = model.CheckOutRemark; entity.ReInspectBillNum = model.ReInspectBillNum; entity.InStoreBillNum = model.InStoreBillNum; entity.InStoreMan = model.InStoreMan; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.InStoreStatus = model.InStoreStatus; 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_AI.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 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_Product_EntryModel>(x => x.ProductBillNum, "入库单号(业务)(必输)"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.EntryBillNum, "入库单号(系统)"); excelFile.AddMapping <WMS_Product_EntryModel>(x => x.Department, "本货部门"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Partid, "物料"); excelFile.AddMapping <WMS_Product_EntryModel>(x => x.PartCode, "物料编码(必输)"); excelFile.AddMapping <WMS_Product_EntryModel>(x => x.ProductQty, "数量(必输)"); excelFile.AddMapping <WMS_Product_EntryModel>(x => x.Lot, "批次(格式:YYYY-MM-DD)"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.InvId, "库存"); excelFile.AddMapping <WMS_Product_EntryModel>(x => x.InvName, "库房"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.SubInvId, "子库存"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Remark, "备注"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Attr1, ""); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Attr2, ""); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Attr3, ""); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Attr4, ""); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.Attr5, ""); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_Product_EntryModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_Product_EntryModel>(0); //开启事务 using (DBContainer db = new DBContainer()) { var tran = db.Database.BeginTransaction(); //开启事务 int rowIndex = 0; string productBillNum = "Z" + DateTime.Now.ToString("yyyyMMddHHmmssff"); //检查数据正确性 foreach (var row in excelContent) { rowIndex += 1; string errorMessage = String.Empty; var model = new WMS_Product_EntryModel(); model.Id = row.Id; model.ProductBillNum = productBillNum; //productBillNum = row.ProductBillNum; model.EntryBillNum = row.EntryBillNum; model.Department = row.Department; //model.Partid = row.Partid; model.PartCode = row.PartCode; model.ProductQty = row.ProductQty; model.Lot = row.Lot; //model.InvId = row.InvId; model.InvName = row.InvName; //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(db, 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_Product_Entry entity = new WMS_Product_Entry(); entity.Id = model.Id; entity.ProductBillNum = model.ProductBillNum; //entity.ProductBillNum = "ZZJ" + DateTime.Now.ToString("yyyyMMddHHmmssff"); entity.EntryBillNum = model.EntryBillNum; //entity.EntryBillNum = "ZZJ" + DateTime.Now.ToString("yyyyMMddHHmmssff"); entity.Department = "总装车间"; entity.Partid = model.Partid; entity.ProductQty = model.ProductQty; entity.InvId = model.InvId; entity.Lot = model.Lot; //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 = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_Product_Entry.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) { //全部保存成功后,调用存储过程“P_WMS_ProcessProductEntry”入库。 ObjectParameter returnValue = new ObjectParameter("ReturnValue", typeof(string)); db.P_WMS_ProcessProductEntry(oper, productBillNum, returnValue); if (returnValue.Value == DBNull.Value) { tran.Commit(); } else { tran.Rollback(); errors.Add((string)returnValue.Value); } //tran.Commit(); //必须调用Commit(),不然数据不会保存 } else { tran.Rollback(); //出错就回滚 } } } wb.Save(); } return(rtn); }
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_POModel>(x => x.PO, "采购订单(必输)"); excelFile.AddMapping <WMS_POModel>(x => x.PODate, "采购日期(必输格式:YYYY-MM-DD)"); excelFile.AddMapping <WMS_POModel>(x => x.SupplierShortName, "供应商简称(必输)"); excelFile.AddMapping <WMS_POModel>(x => x.PartCode, "物料编码(必输)"); excelFile.AddMapping <WMS_POModel>(x => x.QTY, "数量(必输)"); excelFile.AddMapping <WMS_POModel>(x => x.PlanDate, "计划到货日期"); excelFile.AddMapping <WMS_POModel>(x => x.POType, "采购订单类型"); //excelFile.AddMapping<WMS_POModel>(x => x.Status, "状态"); excelFile.AddMapping <WMS_POModel>(x => x.Remark, "说明"); //excelFile.AddMapping<WMS_POModel>(x => x.Attr1, ""); //excelFile.AddMapping<WMS_POModel>(x => x.Attr2, ""); //excelFile.AddMapping<WMS_POModel>(x => x.Attr3, ""); //excelFile.AddMapping<WMS_POModel>(x => x.Attr4, ""); //excelFile.AddMapping<WMS_POModel>(x => x.Attr5, ""); //excelFile.AddMapping<WMS_POModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_POModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_POModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_POModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_POModel>(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_POModel(); model.Id = row.Id; model.PO = row.PO; model.PODate = row.PODate; model.SupplierShortName = row.SupplierShortName; model.PartCode = row.PartCode; model.QTY = row.QTY; model.PlanDate = row.PlanDate; model.POType = row.POType; model.Status = "有效"; 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(db, 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_PO entity = new WMS_PO(); entity.Id = model.Id; entity.PO = model.PO; entity.PODate = model.PODate; entity.SupplierId = model.SupplierId; entity.PartId = model.PartId; entity.QTY = model.QTY; entity.PlanDate = model.PlanDate; entity.POType = model.POType; entity.Status = "有效"; 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 = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_PO.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 bool DeleteEntity(T entity) { Db.Entry <T>(entity).State = System.Data.Entity.EntityState.Deleted; //return Db.SaveChanges() > 0; return(true); }
public bool ImportExcelData(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_InvInfoModel>(x => x.InvCode, "库房编码"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.InvName, "库房名称"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.Remark, "说明"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.Status, "状态"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.CreatePerson, "创建人"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.CreateTime, "创建时间"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.ModifyPerson, "修改人"); excelFile.AddMapping <WMS_InvInfoModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_InvInfoModel>(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_InvInfoModel(); model.Id = row.Id; model.InvCode = row.InvCode; model.InvName = row.InvName; model.Remark = row.Remark; model.Status = row.Status; 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, 10).Value = errorMessage; continue; } //执行额外的数据校验 try { AdditionalCheckExcelData(model); } catch (Exception ex) { rtn = false; errorMessage = ex.Message; errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); wws.Cell(rowIndex + 1, 10).Value = errorMessage; continue; } //写入数据库 WMS_InvInfo entity = new WMS_InvInfo(); entity.Id = model.Id; entity.InvCode = model.InvCode; entity.InvName = model.InvName; entity.Remark = model.Remark; entity.Status = model.Status; entity.CreatePerson = model.CreatePerson; entity.CreateTime = model.CreateTime; entity.ModifyPerson = model.ModifyPerson; entity.ModifyTime = model.ModifyTime; db.WMS_InvInfo.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, 10).Value = errorMessage; } } if (rtn) { tran.Commit(); //必须调用Commit(),不然数据不会保存 } else { tran.Rollback(); //出错就回滚 } } } wb.Save(); } return(rtn); }
public bool ImportStoreMan(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_PartModel>(x => x.PartCode, "物料编码(必输)"); excelFile.AddMapping <WMS_PartModel>(x => x.StoreMan, "保管员(必输)"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_PartModel>(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_PartModel(); model.Id = row.Id; if (row.PartCode != null) { model.PartCode = row.PartCode.Replace(" ", ""); } if (row.StoreMan != null) { model.StoreMan = row.StoreMan.Replace(" ", ""); } 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 { AdditionalCheckStoreMan(db, 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_Part entity = m_Rep.GetById(model.Id); entity.Id = model.Id; //entity.PartCode = model.PartCode; entity.StoreMan = model.StoreMan; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; m_Rep.Edit(entity); //db.WMS_Part.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 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_PartModel>(x => x.PartCode, "物料编码(必输)"); excelFile.AddMapping <WMS_PartModel>(x => x.PartName, "物料名称(必输)"); excelFile.AddMapping <WMS_PartModel>(x => x.PartType, "物料类型(必输)"); excelFile.AddMapping <WMS_PartModel>(x => x.CustomerCode, "主机厂编码"); excelFile.AddMapping <WMS_PartModel>(x => x.LogisticsCode, "物流号"); excelFile.AddMapping <WMS_PartModel>(x => x.OtherCode, "额外信息编码"); excelFile.AddMapping <WMS_PartModel>(x => x.PCS, "每箱数量"); excelFile.AddMapping <WMS_PartModel>(x => x.StoreMan, "保管员(必输)"); excelFile.AddMapping <WMS_PartModel>(x => x.Unit, "单位"); excelFile.AddMapping <WMS_PartModel>(x => x.Volume, "每箱体积"); excelFile.AddMapping <WMS_PartModel>(x => x.BelongSupplier, "所属供应商"); excelFile.AddMapping <WMS_PartModel>(x => x.BelongCustomer, "所属客户"); excelFile.AddMapping <WMS_PartModel>(x => x.Remark, "说明"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_PartModel>(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_PartModel(); model.Id = row.Id; if (row.PartCode != null) { model.PartCode = row.PartCode.Replace(" ", ""); } if (row.PartName != null) { model.PartName = row.PartName.Replace(" ", ""); } if (row.PartType != null) { model.PartType = row.PartType.Replace(" ", ""); } if (row.CustomerCode != null) { model.CustomerCode = row.CustomerCode.Replace(" ", "").Replace(";", ";"); } //model.PartName = row.PartName.Replace(" ", ""); //model.PartType = row.PartType.Replace(" ", ""); //model.CustomerCode = row.CustomerCode.Replace(" ", "").Replace(";",";"); model.LogisticsCode = row.LogisticsCode; model.OtherCode = row.OtherCode; model.PCS = row.PCS; model.StoreMan = row.StoreMan.Replace(" ", ""); model.Unit = row.Unit; model.Volume = row.Volume; model.Remark = row.Remark; if (row.BelongCustomer != null) { model.BelongCustomer = row.BelongCustomer.Replace(" ", "").Replace(";", ";"); } if (row.BelongSupplier != null) { model.BelongSupplier = row.BelongSupplier.Replace(" ", "").Replace(";", ";"); } //model.BelongCustomer = row.BelongCustomer.Replace(" ", "").Replace(";", ";"); //model.BelongSupplier = row.BelongSupplier.Replace(" ", "").Replace(";", ";"); //model.CreatePerson = row.oper; //model.CreateTime = row.CreateTime; //model.ModifyPerson = row.oper; //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(db, 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_Part entity = new WMS_Part(); entity.Id = model.Id; entity.PartCode = model.PartCode; entity.PartName = model.PartName; entity.PartType = model.PartType; entity.CustomerCode = ";" + model.CustomerCode + ";"; entity.LogisticsCode = model.LogisticsCode; entity.OtherCode = model.OtherCode; entity.PCS = model.PCS; entity.StoreMan = model.StoreMan; entity.Status = "有效"; entity.CreatePerson = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; entity.Unit = model.Unit; entity.Volume = model.Volume; entity.Remark = model.Remark; if (model.BelongCustomer != null) { entity.BelongCustomer = ";" + model.BelongCustomer + ";"; } if (model.BelongSupplier != null) { entity.BelongSupplier = ";" + model.BelongSupplier + ";"; } db.WMS_Part.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 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 <SysParamModel>(x => x.TypeCode, "参数类别编码"); excelFile.AddMapping <SysParamModel>(x => x.TypeName, "参数类别名称"); excelFile.AddMapping <SysParamModel>(x => x.ParamCode, "参数值编码"); excelFile.AddMapping <SysParamModel>(x => x.ParamName, "参数值名称"); excelFile.AddMapping <SysParamModel>(x => x.Sort, "排序"); excelFile.AddMapping <SysParamModel>(x => x.Enable, "是否启用"); excelFile.AddMapping <SysParamModel>(x => x.CreatePerson, "创建人"); excelFile.AddMapping <SysParamModel>(x => x.CreateTime, "创建时间"); excelFile.AddMapping <SysParamModel>(x => x.ModifyPerson, "修改人"); excelFile.AddMapping <SysParamModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <SysParamModel>(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 SysParamModel(); model.Id = row.Id; model.TypeCode = row.TypeCode; model.TypeName = row.TypeName; model.ParamCode = row.ParamCode; model.ParamName = row.ParamName; model.Sort = row.Sort; model.Enable = row.Enable; 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; } //写入数据库 SysParam entity = new SysParam(); entity.Id = model.Id; entity.TypeCode = model.TypeCode; entity.TypeName = model.TypeName; entity.ParamCode = model.ParamCode; entity.ParamName = model.ParamName; entity.Sort = model.Sort; entity.Enable = model.Enable; 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.SysParam.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 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)) { int headId = 0; var inventoryType = "抽检1"; //第一个Sheet using (IXLWorksheet wws = wb.Worksheets.First()) { //对应列头 excelFile.AddMapping <WMS_Inventory_DModel>(x => x.Inventory_HName, "盘点名称"); excelFile.AddMapping <WMS_Inventory_DModel>(x => x.PartCode, "物料编码"); excelFile.AddMapping <WMS_Inventory_DModel>(x => x.InventoryQty, "盘点数量"); excelFile.AddMapping <WMS_Inventory_DModel>(x => x.InvName, "库房名称"); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.SubInvId, "子库存"); excelFile.AddMapping <WMS_Inventory_DModel>(x => x.Lot, "批次号"); excelFile.AddMapping <WMS_Inventory_DModel>(x => x.Remark, "备注"); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.Attr1, ""); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.Attr2, ""); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.Attr3, ""); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.Attr4, ""); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.Attr5, ""); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_Inventory_DModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_Inventory_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_Inventory_DModel(); model.Id = row.Id; model.Inventory_HName = row.Inventory_HName; model.PartCode = row.PartCode; model.InventoryQty = row.InventoryQty; model.InvName = row.InvName; model.Lot = row.Lot; //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(db, 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; } headId = model.HeadId; inventoryType = model.InventoryType; //写入数据库 Expression <Func <WMS_Inventory_D, bool> > exp = x => x.PartId == model.PartId && x.InvId == model.InvId && x.Lot == model.Lot; WMS_Inventory_D entity; entity = db.WMS_Inventory_D.FirstOrDefault(exp); //WMS_Inventory_D entity1 = m_Rep.GetSingleWhere(model.Id); if (entity != null) { entity.InventoryQty = model.InventoryQty; } else { entity = new WMS_Inventory_D(); entity.Id = model.Id; entity.HeadId = model.HeadId; entity.PartId = model.PartId; entity.SnapshootQty = 0; entity.InventoryQty = model.InventoryQty; entity.Lot = model.Lot; 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_Inventory_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) { if (inventoryType == "抽检") { tran.Commit(); //if (SpecialInventory(ref errors, oper, headId)) //{ // tran.Commit(); //必须调用Commit(),不然数据不会保存 //} //else //{ // tran.Rollback(); //出错就回滚 //} } else { tran.Commit(); //必须调用Commit(),不然数据不会保存 } } else { tran.Rollback(); //出错就回滚 } } } wb.Save(); } return(rtn); }
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_CustomerModel>(x => x.CustomerCode, "客户编码(必输)"); excelFile.AddMapping <WMS_CustomerModel>(x => x.CustomerShortName, "客户简称(必输)"); excelFile.AddMapping <WMS_CustomerModel>(x => x.CustomerName, "客户名称(必输)"); excelFile.AddMapping <WMS_CustomerModel>(x => x.CustomerType, "客户类型"); excelFile.AddMapping <WMS_CustomerModel>(x => x.LinkMan, "联系人"); excelFile.AddMapping <WMS_CustomerModel>(x => x.LinkManTel, "联系电话"); excelFile.AddMapping <WMS_CustomerModel>(x => x.LinkManAddress, "联系地址"); //excelFile.AddMapping<WMS_CustomerModel>(x => x.Status, "状态"); excelFile.AddMapping <WMS_CustomerModel>(x => x.Remark, "说明"); //excelFile.AddMapping<WMS_CustomerModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_CustomerModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_CustomerModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_CustomerModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_CustomerModel>(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_CustomerModel(); model.Id = row.Id; if (row.CustomerCode != null) { model.CustomerCode = row.CustomerCode.Replace(" ", ""); } if (row.CustomerShortName != null) { model.CustomerShortName = row.CustomerShortName.Replace(" ", ""); } if (row.CustomerName != null) { model.CustomerName = row.CustomerName.Replace(" ", ""); } model.CustomerType = row.CustomerType; model.LinkMan = row.LinkMan; model.LinkManTel = row.LinkManTel; model.LinkManAddress = row.LinkManAddress; //model.Status = row.Status; model.Remark = row.Remark; //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; //rtn = false; //errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); //wws.Cell(rowIndex + 1, 15).Value = errorMessage; //continue; } //执行额外的数据校验 try { AdditionalCheckExcelData(db, ref model); } catch (Exception ex) { rtn = false; errorMessage = ex.Message; errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); //wws.Cell(rowIndex + 1, 15).Value = errorMessage; wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage; continue; } //写入数据库 WMS_Customer entity = new WMS_Customer(); entity.Id = model.Id; entity.CustomerCode = model.CustomerCode; entity.CustomerShortName = model.CustomerShortName; entity.CustomerName = model.CustomerName; entity.CustomerType = model.CustomerType; entity.LinkMan = model.LinkMan; entity.LinkManTel = model.LinkManTel; entity.LinkManAddress = model.LinkManAddress; entity.Status = "有效"; entity.Remark = model.Remark; entity.CreatePerson = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_Customer.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, 15).Value = errorMessage; wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage; } } if (rtn) { tran.Commit(); //必须调用Commit(),不然数据不会保存 } else { tran.Rollback(); //出错就回滚 } } } wb.Save(); } return(rtn); }
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_ReturnOrder_DModel>(x => x.ReturnOrderDNum, "退货单号"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.HeadId, "头表ID"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ReturnQty, "退货数量"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Remark, "备注"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.PrintStaus, "打印状态"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.PrintDate, "打印日期"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.PrintMan, "打印人"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ConfirmStatus, "确认状态"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ConfirmMan, "确认人"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ConfirmDate, "确认时间"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Attr1, ""); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Attr2, ""); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Attr3, ""); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Attr4, ""); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.Attr5, ""); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.CreatePerson, "创建人"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.CreateTime, "创建时间"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ModifyPerson, "修改人"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.ModifyTime, "修改时间"); excelFile.AddMapping <WMS_ReturnOrder_DModel>(x => x.BatchId, ""); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_ReturnOrder_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_ReturnOrder_DModel(); model.Id = row.Id; model.ReturnOrderDNum = row.ReturnOrderDNum; model.HeadId = row.HeadId; model.ReturnQty = row.ReturnQty; model.Remark = row.Remark; model.PrintStaus = row.PrintStaus; model.PrintDate = row.PrintDate; model.PrintMan = row.PrintMan; model.ConfirmStatus = row.ConfirmStatus; model.ConfirmMan = row.ConfirmMan; model.ConfirmDate = row.ConfirmDate; 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; model.BatchId = row.BatchId; 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_ReturnOrder_D entity = new WMS_ReturnOrder_D(); entity.Id = model.Id; entity.ReturnOrderDNum = model.ReturnOrderDNum; entity.HeadId = model.HeadId; entity.ReturnQty = model.ReturnQty; entity.Remark = model.Remark; entity.PrintStaus = model.PrintStaus; entity.PrintDate = model.PrintDate; entity.PrintMan = model.PrintMan; entity.ConfirmStatus = model.ConfirmStatus; entity.ConfirmMan = model.ConfirmMan; entity.ConfirmDate = model.ConfirmDate; 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.BatchId = model.BatchId; entity.CreatePerson = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_ReturnOrder_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 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_InvRecordModel>(x => x.PartId, "物料编码"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.QTY, "数量"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.InvId, "库房编码"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.SubInvId, ""); excelFile.AddMapping <WMS_InvRecordModel>(x => x.BillId, "单据ID"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.SourceBill, "单据来源"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.OperateDate, "操作时间"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.Lot, ""); excelFile.AddMapping <WMS_InvRecordModel>(x => x.Type, "出入库类型"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.OperateMan, "操作人"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.Stock_InvId, "备料库存"); excelFile.AddMapping <WMS_InvRecordModel>(x => x.StockStatus, "备料状态:1-不适用(直接修改库存现有量);2-已备料;3-无效备料(取消备料后将2改成3);4-取消备料(当前操作是取消备料)"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_InvRecordModel>(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_InvRecordModel(); model.Id = row.Id; model.PartId = row.PartId; model.QTY = row.QTY; model.InvId = row.InvId; model.SubInvId = row.SubInvId; model.BillId = row.BillId; model.SourceBill = row.SourceBill; model.OperateDate = row.OperateDate; model.Lot = row.Lot; model.Type = row.Type; model.OperateMan = row.OperateMan; model.Stock_InvId = row.Stock_InvId; model.StockStatus = row.StockStatus; 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_InvRecord entity = new WMS_InvRecord(); entity.Id = model.Id; entity.PartId = model.PartId; entity.QTY = model.QTY; entity.InvId = model.InvId; entity.SubInvId = model.SubInvId; entity.BillId = model.BillId; entity.SourceBill = model.SourceBill; entity.OperateDate = model.OperateDate; entity.Lot = model.Lot; entity.Type = model.Type; entity.OperateMan = model.OperateMan; entity.Stock_InvId = model.Stock_InvId; entity.StockStatus = model.StockStatus; db.WMS_InvRecord.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 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_Feed_ListModel>(x => x.FeedBillNum, "投料单号(业务)"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ReleaseBillNum, "投料单号(系统)"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Department, "投料部门"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.AssemblyPartCode, "总成物料"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.SubAssemblyPartCode, "投料物料"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Lot, "批次号"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.FeedQty, "投料数量"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.BoxQty, "箱数"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Capacity, "体积"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.InvName, "库房"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.SubInvId, "子库存"); excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Remark, "备注"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintStaus, "打印状态"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintDate, "打印时间"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintMan, "打印人"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmStatus, "确认状态"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmMan, "确认人"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmDate, "确认时间"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr1, ""); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr2, ""); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr3, ""); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr4, ""); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr5, ""); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_Feed_ListModel>(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_Feed_ListModel(); model.Id = row.Id; model.FeedBillNum = row.FeedBillNum; //model.ReleaseBillNum = row.ReleaseBillNum; model.Department = row.Department; model.AssemblyPartCode = row.AssemblyPartCode; model.SubAssemblyPartCode = row.SubAssemblyPartCode; model.FeedQty = row.FeedQty; model.BoxQty = row.BoxQty; model.Capacity = row.Capacity; model.InvName = row.InvName; model.Lot = row.Lot; //model.SubInvId = row.SubInvId; model.Remark = row.Remark; //model.PrintStaus = row.PrintStaus; //model.PrintDate = row.PrintDate; //model.PrintMan = row.PrintMan; //model.ConfirmStatus = row.ConfirmStatus; //model.ConfirmMan = row.ConfirmMan; //model.ConfirmDate = row.ConfirmDate; //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(db, 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_Feed_List entity = new WMS_Feed_List(); entity.Id = model.Id; entity.FeedBillNum = model.FeedBillNum; //entity.ReleaseBillNum = model.ReleaseBillNum; //entity.ReleaseBillNum = "TL" + DateTime.Now.ToString("yyyyMMddHHmmssff");打印时生成 entity.Department = model.Department; entity.AssemblyPartId = model.AssemblyPartId; entity.SubAssemblyPartId = model.SubAssemblyPartId; entity.FeedQty = model.FeedQty; entity.BoxQty = model.BoxQty; entity.Capacity = model.Capacity; entity.InvId = model.InvId; entity.Lot = model.Lot; //entity.SubInvId = model.SubInvId; entity.Remark = model.Remark; entity.PrintStaus = "未打印"; //entity.PrintDate = model.PrintDate; //entity.PrintMan = model.PrintMan; entity.ConfirmStatus = "未确认"; //entity.ConfirmMan = model.ConfirmMan; //entity.ConfirmDate = model.ConfirmDate; //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_Feed_List.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); }
// POST: api/ExpressInfo public Root Post([FromBody] Datas model) { if (model.RequestData != null) { var requestData = JsonConvert.DeserializeObject <RequestData>(model.RequestData); if (requestData.Data.Count() > 0) { //Apps.IService.IServiceSession serviceSession = OpeCur.ServiceSession; using (DBContainer dbContext = new DBContainer()) { DbSet <Spm_ExpressInfo> dbSet0 = dbContext.Set <Spm_ExpressInfo>(); DbSet <Spm_TracesInfo> dbSet1 = dbContext.Set <Spm_TracesInfo>(); DateTime pushTime = requestData.PushTime; //ValidationErrors error = new ValidationErrors(); foreach (var item in requestData.Data) { Spm_ExpressInfo exInfo = dbSet0.Where(o => o.TrackingNumber == item.LogisticCode).FirstOrDefault(); //var exInfo = serviceSession.Spm_ExpressInfo.GetList(o => o.TrackingNumber == item.LogisticCode).FirstOrDefault(); //if (exInfo == null) //{ // string newId = ResultHelper.NewId; // Spm_ExpressInfoModel eModel = new Spm_ExpressInfoModel() // { // Id = newId, // TrackingNumber = item.LogisticCode, // State = item.State, // ShipperCode = item.ShipperCode, // CallBack = item.CallBack, // EBusinessID = item.EBusinessID, // OrderCode = item.OrderCode, // Reason = item.Reason, // Success = item.Success, // PushTime = pushTime, // }; // serviceSession.Spm_ExpressInfo.Create(ref error, eModel); // if (item.Traces.Count() > 0) // { // List<Spm_TracesInfoModel> ti = serviceSession.Spm_TracesInfo.GetList(t => t.ExInfoId == newId).OrderByDescending(a => a.AcceptTime).ToList(); // foreach (var tc in item.Traces) // { // if (ti.Count > 0) // { // if ((tc.AcceptTime > ti.First().AcceptTime)) // { // Spm_TracesInfoModel m = new Spm_TracesInfoModel() // { // Id = ResultHelper.NewId, // ExInfoId = newId, // AcceptTime = tc.AcceptTime, // AcceptStation = tc.AcceptStation, // }; // serviceSession.Spm_TracesInfo.Create(ref error, m); // } // } // else // { // Spm_TracesInfoModel m = new Spm_TracesInfoModel() // { // Id = ResultHelper.NewId, // ExInfoId = newId, // AcceptTime = tc.AcceptTime, // AcceptStation = tc.AcceptStation, // }; // serviceSession.Spm_TracesInfo.Create(ref error, m); // } // } // } //} if (exInfo != null) { //Spm_ExpressInfoModel eModel = new Spm_ExpressInfoModel() //{ // Id = exInfo.Id, // State = item.State, // //ShipperCode = item.ShipperCode, // CallBack = item.CallBack, // EBusinessID = item.EBusinessID, // OrderCode = item.OrderCode, // Reason = item.Reason, // Success = item.Success, // PushTime = pushTime, //}; exInfo.State = item.State; exInfo.CallBack = item.CallBack; exInfo.EBusinessID = item.EBusinessID; exInfo.OrderCode = item.OrderCode; exInfo.Reason = item.Reason; exInfo.Success = item.Success; exInfo.PushTime = pushTime; //if (exInfo.ShipperCode.IsNullOrEmpty()) //{ // exInfo.ShipperCode = item.ShipperCode; //serviceSession.Spm_ExpressInfo.Modify(ref error, eModel, "State", "CallBack", "ShipperCode", "EBusinessID", "OrderCode", "Reason", "Success", "PushTime"); //} //else { // serviceSession.Spm_ExpressInfo.Modify(ref error, eModel, "State", "CallBack", "EBusinessID", "OrderCode", "Reason", "Success", "PushTime"); //} dbContext.Entry <Spm_ExpressInfo>(exInfo).State = EntityState.Modified; if (item.Traces.Count() > 0) { IQueryable <Spm_TracesInfo> ti = dbSet1.Where(t => t.ExInfoId == exInfo.Id).OrderByDescending(a => a.AcceptTime); //List<Spm_TracesInfoModel> ti = serviceSession.Spm_TracesInfo.GetList(t => t.ExInfoId == exInfo.Id).OrderByDescending(a => a.AcceptTime).ToList(); foreach (var tc in item.Traces) { if (ti.Count() > 0) { if ((tc.AcceptTime > ti.First().AcceptTime)) { Spm_TracesInfo m = new Spm_TracesInfo() { Id = ResultHelper.NewId, ExInfoId = exInfo.Id, AcceptTime = tc.AcceptTime, AcceptStation = tc.AcceptStation, }; //dbContext.Entry<Spm_TracesInfo>(m).State = EntityState.Added; dbSet1.Add(m); //serviceSession.Spm_TracesInfo.Create(ref error, m); } } else { Spm_TracesInfo m = new Spm_TracesInfo() { Id = ResultHelper.NewId, ExInfoId = exInfo.Id, AcceptTime = tc.AcceptTime, AcceptStation = tc.AcceptStation, }; //dbContext.Entry<Spm_TracesInfo>(m).State = EntityState.Added; dbSet1.Add(m); //serviceSession.Spm_TracesInfo.Create(ref error, m); } } } } //serviceSession.SaveChange(); } if (dbContext.SaveChanges() >= 0) { return(new Root() { EBusinessID = "1281577", UpdateTime = "", Success = true, Reason = "", }); } else { return(new Root() { EBusinessID = "1281577", UpdateTime = DateTime.Now.ToString(), Success = false, Reason = "获取数据失败", }); } } } } Root root = new Root() { EBusinessID = "1281577", UpdateTime = "", Success = true, Reason = "", }; return(root); }