public virtual WMS_SupplierModel GetById(object id) { if (IsExists(id)) { WMS_Supplier entity = m_Rep.GetById(id); WMS_SupplierModel model = new WMS_SupplierModel(); model.Id = entity.Id; model.SupplierCode = entity.SupplierCode; model.SupplierShortName = entity.SupplierShortName; model.SupplierName = entity.SupplierName; model.SupplierType = entity.SupplierType; model.LinkMan = entity.LinkMan; model.LinkManTel = entity.LinkManTel; model.LinkManAddress = entity.LinkManAddress; model.Status = entity.Status; model.MoreAccept = entity.MoreAccept; model.Remark = entity.Remark; model.CreatePerson = entity.CreatePerson; model.CreateTime = entity.CreateTime; model.ModifyPerson = entity.ModifyPerson; model.ModifyTime = entity.ModifyTime; return(model); } else { return(null); } }
/// <summary> /// 保存数据 /// </summary> public virtual void SaveImportData(IEnumerable <WMS_SupplierModel> list) { try { using (DBContainer db = new DBContainer()) { foreach (var model in list) { WMS_Supplier entity = new WMS_Supplier(); entity.Id = 0; entity.SupplierCode = model.SupplierCode; entity.SupplierShortName = model.SupplierShortName; entity.SupplierName = model.SupplierName; entity.SupplierType = model.SupplierType; entity.LinkMan = model.LinkMan; entity.LinkManTel = model.LinkManTel; entity.LinkManAddress = model.LinkManAddress; entity.Status = model.Status; entity.MoreAccept = model.MoreAccept; entity.Remark = model.Remark; entity.CreatePerson = model.CreatePerson; entity.CreateTime = ResultHelper.NowTime; entity.ModifyPerson = model.ModifyPerson; entity.ModifyTime = model.ModifyTime; db.WMS_Supplier.Add(entity); } db.SaveChanges(); } } catch (Exception ex) { throw; } }
public virtual bool Edit(ref ValidationErrors errors, WMS_SupplierModel model) { try { WMS_Supplier entity = m_Rep.GetById(model.Id); if (entity == null) { errors.Add(Resource.Disable); return(false); } entity.Id = model.Id; entity.SupplierCode = model.SupplierCode; entity.SupplierShortName = model.SupplierShortName; entity.SupplierName = model.SupplierName; entity.SupplierType = model.SupplierType; entity.LinkMan = model.LinkMan; entity.LinkManTel = model.LinkManTel; entity.LinkManAddress = model.LinkManAddress; entity.Status = model.Status; entity.MoreAccept = model.MoreAccept; entity.Remark = model.Remark; 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 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_SupplierModel>(x => x.SupplierCode, "供应商编码(必输)"); excelFile.AddMapping <WMS_SupplierModel>(x => x.SupplierShortName, "供应商简称(必输)"); excelFile.AddMapping <WMS_SupplierModel>(x => x.SupplierName, "供应商名称(必输)"); excelFile.AddMapping <WMS_SupplierModel>(x => x.SupplierType, "供应商类型"); excelFile.AddMapping <WMS_SupplierModel>(x => x.LinkMan, "联系人"); excelFile.AddMapping <WMS_SupplierModel>(x => x.LinkManTel, "联系人电话"); excelFile.AddMapping <WMS_SupplierModel>(x => x.LinkManAddress, "联系人地址"); excelFile.AddMapping <WMS_SupplierModel>(x => x.MoreAccept, "超量接收(允许/不允许)(必输)"); excelFile.AddMapping <WMS_SupplierModel>(x => x.Remark, "说明"); //excelFile.AddMapping<WMS_SupplierModel>(x => x.CreatePerson, "创建人"); //excelFile.AddMapping<WMS_SupplierModel>(x => x.CreateTime, "创建时间"); //excelFile.AddMapping<WMS_SupplierModel>(x => x.ModifyPerson, "修改人"); //excelFile.AddMapping<WMS_SupplierModel>(x => x.ModifyTime, "修改时间"); //SheetName,第一个Sheet var excelContent = excelFile.Worksheet <WMS_SupplierModel>(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_SupplierModel(); model.Id = row.Id; model.SupplierCode = row.SupplierCode; model.SupplierShortName = row.SupplierShortName; model.SupplierName = row.SupplierName; model.SupplierType = row.SupplierType; model.LinkMan = row.LinkMan; model.LinkManTel = row.LinkManTel; model.LinkManAddress = row.LinkManAddress; model.MoreAccept = row.MoreAccept; //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; } //执行额外的数据校验 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_Supplier entity = new WMS_Supplier(); entity.Id = model.Id; entity.SupplierCode = model.SupplierCode; entity.SupplierShortName = model.SupplierShortName; entity.SupplierName = model.SupplierName; entity.SupplierType = model.SupplierType; entity.LinkMan = model.LinkMan; entity.LinkManTel = model.LinkManTel; entity.LinkManAddress = model.LinkManAddress; entity.MoreAccept = model.MoreAccept; entity.MoreAccept = "允许"; entity.Status = "有效"; entity.Remark = model.Remark; entity.CreatePerson = oper; entity.CreateTime = DateTime.Now; entity.ModifyPerson = oper; entity.ModifyTime = DateTime.Now; db.WMS_Supplier.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); }