/// <summary> /// 导入仓库货架 /// </summary> /// <param name="sExcelFile">Excel文件名</param> /// <param name="sSheetName">Sheet名</param> public void ImportWarehouseShelves(string sExcelFile, string sSheetName) { try { ExcelData oExcel = new ExcelData(sExcelFile, sSheetName); DataColumn colOrgan = oExcel.ExcelTable.Columns["组织"]; DataColumn colWhCode = oExcel.ExcelTable.Columns["仓库"]; DataColumn colCode = oExcel.ExcelTable.Columns["代码"]; DataColumn colBarcode = oExcel.ExcelTable.Columns["条码"]; DataColumn colReserved = oExcel.ExcelTable.Columns["保留"]; DataColumn colName = oExcel.ExcelTable.Columns["名称"]; DataColumn colRemark = oExcel.ExcelTable.Columns["备注"]; string sLastOrgan = ""; string sLastWarehouse = ""; WarehouseInformation oWarehouse = null; foreach (DataRow row in oExcel.ExcelTable.Rows) { string sOrgCode = row[colOrgan].ToString(); string sWhCode = row[colWhCode].ToString(); string sCode = row[colCode].ToString(); string sBarcode = row[colBarcode].ToString(); bool bReserved = (row[colReserved].ToString() == "1") ? true : false; string sName = row[colName].ToString(); string sRemark = row[colRemark].ToString(); if ((sWhCode != sLastWarehouse) || (sOrgCode != sLastOrgan)) { oWarehouse = (from w in dbEntity.WarehouseInformations where w.Parent.Code == sOrgCode && w.Code == sWhCode select w).FirstOrDefault(); sLastOrgan = sOrgCode; sLastWarehouse = sWhCode; } var oShelf = (from s in dbEntity.WarehouseShelves where s.WhID == oWarehouse.Gid && s.Code == sCode select s).FirstOrDefault(); if (oShelf == null) { oShelf = new WarehouseShelf { Warehouse = oWarehouse, Code = sCode }; dbEntity.WarehouseShelves.Add(oShelf); } if (String.IsNullOrEmpty(sBarcode)) oShelf.Barcode = sCode; else oShelf.Barcode = sBarcode; oShelf.Reserved = bReserved; oShelf.Name = sName; oShelf.Remark = sRemark; dbEntity.SaveChanges(); if (Utility.ConfigHelper.GlobalConst.IsDebug) Debug.WriteLine("{0} {1} {2}", this.ToString(), sCode, sRemark); } oEventBLL.WriteEvent(String.Format("导入WarehouseShelf成功: {0} {1}", sExcelFile, sSheetName), ModelEnum.ActionLevel.GENERIC, ModelEnum.ActionSource.SYSTEM, this.ToString()); } catch (Exception ex) { oEventBLL.WriteEvent(String.Format("导入WarehouseShelf错误: {0} {1} {2}", sExcelFile, sSheetName, ex.Message), ModelEnum.ActionLevel.ERROR, ModelEnum.ActionSource.SYSTEM, this.ToString()); } }
/// <summary> /// Excel数据导入 /// </summary> /// <param name="uploadFile">上传的文件</param> /// <returns></returns> public ActionResult UpdateXls(HttpPostedFileBase uploadFile, Guid WhlID) { #region 处理上传Excel文件 if (uploadFile == null || uploadFile.ContentLength == 0) return RedirectToAction("Error"); string ServerTempPath = HttpContext.Server.MapPath("~/Temp"); if (!Directory.Exists(ServerTempPath)) Directory.CreateDirectory(ServerTempPath); string OldFileName = Path.GetFileName(uploadFile.FileName); string extension = Path.GetExtension(OldFileName); string NewFileName = Guid.NewGuid() + extension; string filePath = Path.Combine(ServerTempPath, NewFileName); uploadFile.SaveAs(filePath); #endregion 处理上传Excel文件 #region 转储数据库 ExcelData data = new ExcelData(filePath); DataColumn CODE = data.ExcelTable.Columns["Code"]; DataColumn BARCODE = data.ExcelTable.Columns["BarCode"]; DataColumn NAME = data.ExcelTable.Columns["Name"]; DataColumn BRIEF = data.ExcelTable.Columns["Brief"]; DataColumn REMARK = data.ExcelTable.Columns["Remark"]; List<int> wrongRowNums = new List<int>(); using (var scope = new TransactionScope()) { int rowNum = -1; foreach (DataRow row in data.ExcelTable.Rows) { rowNum++; try { string tempCode = row[CODE].ToString(); string tempBarCode = row[BARCODE].ToString(); bool isCodeNull = (tempCode == string.Empty); bool isBarCodeNull = (tempBarCode == string.Empty); if (isCodeNull && isBarCodeNull) { throw new Exception("Both Code and BarCode are null."); } if (isCodeNull) { tempCode = tempBarCode; } else if (isBarCodeNull) { tempBarCode = tempCode; } WarehouseShelf wh = dbEntity.WarehouseShelves.FirstOrDefault(shelf => shelf.Code == tempCode && shelf.WhID == WhlID && shelf.Deleted); if (wh == null) { wh = new WarehouseShelf { WhID = WhlID, Code = tempCode, Barcode = tempBarCode, Name = row[NAME].ToString(), Brief = row[BRIEF].ToString(), Remark = row[REMARK].ToString() }; dbEntity.WarehouseShelves.Add(wh); } else { wh.Deleted = false; wh.Barcode = tempBarCode; wh.Name = row[NAME].ToString(); wh.Brief = row[BRIEF].ToString(); wh.Remark = row[REMARK].ToString(); } } catch { wrongRowNums.Add(rowNum); } } if (wrongRowNums.Count == 0) { dbEntity.SaveChanges(); // 提交事务,数据库物理写入 scope.Complete(); FileInfo f = new FileInfo(NewFileName); f.Delete(); } } #endregion 转储数据库 if (wrongRowNums.Count == 0) { return RedirectToAction("WarehouseShelf"); } else { StringBuilder sb = new StringBuilder(); sb.AppendLine("Wrong Rows:"); sb.Append(string.Join(",", wrongRowNums)); ViewBag.ErrorMessage = sb.ToString(); return View("Error"); } }
public ActionResult ShelfAddDB(WarehouseShelf model) { if (!base.CheckPrivilege("EnableEditShelf")) return RedirectToAction("ErrorPage", "Home", new { message = "Sorry you have no privilege to visit the Page" }); WarehouseShelf newShelf = (from shelf in dbEntity.WarehouseShelves where shelf.WhID == model.WhID && shelf.Code == model.Code select shelf).FirstOrDefault(); if (newShelf == null) { newShelf = new WarehouseShelf { WhID = model.WhID, Code = model.Code, Barcode = model.Barcode, Name = model.Name, Reserved = model.Reserved, Brief = model.Brief, Remark = model.Remark }; dbEntity.WarehouseShelves.Add(newShelf); } else if (newShelf.Deleted) { newShelf.Deleted = false; newShelf.Name = model.Name; newShelf.Barcode = model.Barcode; newShelf.Brief = model.Brief; newShelf.Remark = model.Remark; } else { return Error("输入数据存在冲突(待改进d(>_<)b)", Url.Action("Shelf")); } dbEntity.SaveChanges(); return RedirectToAction("Shelf"); }
public ActionResult ShelfEditDB(WarehouseShelf model) { if (!base.CheckPrivilege("EnableEditShelf")) return RedirectToAction("ErrorPage", "Home", new { message = "Sorry you have no privilege to visit the Page" }); WarehouseShelf shelf = dbEntity.WarehouseShelves.Find(model.Gid); if (shelf == null || shelf.Deleted) { return Error("记录不存在", Url.Action("Shelf")); } else { if (shelf.WhID != model.WhID) { return Error("记录异常", Url.Action("Shelf")); } shelf.Code = model.Code; shelf.Barcode = model.Barcode; shelf.Brief = model.Brief; shelf.Reserved = model.Reserved; shelf.Remark = model.Remark; dbEntity.SaveChanges(); return RedirectToAction("Shelf"); } }
/// <summary> /// 添加货架 /// </summary> /// <param name="whID">仓库Id</param> /// <returns></returns> public ActionResult ShelfAdd(Guid whID) { if (!base.CheckPrivilege("EnableEditShelf")) return RedirectToAction("ErrorPage", "Home", new { message = "Sorry you have no privilege to visit the Page" }); WarehouseInformation warehouse = (from w in dbEntity.WarehouseInformations.Include("FullName.ResourceItems") where w.Gid == whID && !w.Deleted select w).FirstOrDefault(); if (warehouse == null) { return Error("仓库不存在", Url.Action("Shelf")); } WarehouseShelf model = new WarehouseShelf { WhID = whID, Warehouse = warehouse }; return View(model); }