public ActionResult ImportQuotaCycleQty(IEnumerable<HttpPostedFileBase> attachments) { try { foreach (var file in attachments) { Stream inputStream = file.InputStream; if (inputStream.Length == 0) { throw new BusinessException("Import.Stream.Empty"); } HSSFWorkbook workbook = new HSSFWorkbook(inputStream); ISheet sheet = workbook.GetSheetAt(0); IEnumerator rows = sheet.GetRowEnumerator(); ImportHelper.JumpRows(rows, 10); BusinessException businessException = new BusinessException(); #region 列定义 int colItem = 1;//车系 int colCycleQty = 2;//车系 #endregion IList<QuotaCycleQty> exactCycleQtyList = new List<QuotaCycleQty>(); IList<QuotaCycleQty> allCycleQtyList = this.genericMgr.FindAll<QuotaCycleQty>(); IList<Item> allItemList = this.genericMgr.FindAll<Item>(); int i = 10; while (rows.MoveNext()) { i++; HSSFRow row = (HSSFRow)rows.Current; if (!ImportHelper.CheckValidDataRow(row, 1, 2)) { break;//边界 } string itemCode = string.Empty; decimal cycleQty = 0; QuotaCycleQty quotaCycleQty = new QuotaCycleQty(); #region 读取数据 #region 物料编号 itemCode = ImportHelper.GetCellStringValue(row.GetCell(colItem)); if (string.IsNullOrWhiteSpace(itemCode)) { businessException.AddMessage(string.Format("第{0}行物料编号不能为空", i)); } else { var items = allItemList.FirstOrDefault(a => a.Code == itemCode); var existsCycleQty = allCycleQtyList.FirstOrDefault(a => a.Item == itemCode); //var duplicateItemTrace= if (items == null) { businessException.AddMessage(string.Format("第{0}行{1}物料编号不存在。", i, itemCode)); continue; } else if (existsCycleQty != null) { quotaCycleQty = existsCycleQty; quotaCycleQty.IsUpdate = true; } else { quotaCycleQty.Item = items.Code; quotaCycleQty.ItemDesc = items.Description; quotaCycleQty.RefItemCode = items.ReferenceCode; } } #endregion #region 循环量 string readCycleQty = ImportHelper.GetCellStringValue(row.GetCell(colCycleQty)); if (string.IsNullOrWhiteSpace(readCycleQty)) { businessException.AddMessage(string.Format("第{0}行循环量不能为空", i)); continue; } else { if (decimal.TryParse(readCycleQty, out cycleQty)) { if (cycleQty <= 0) { businessException.AddMessage(string.Format("第{0}行循环量{1}不能小于等于0。", i,cycleQty)); continue; } quotaCycleQty.CycleQty = cycleQty; } else { businessException.AddMessage(string.Format("第{0}行循环量{1}填写有误。", i)); continue; } } #endregion #region 验证 var quotaList = this.genericMgr.FindAll<Quota>("select q from Quota as q where q.Item=? and Isactive=?", new object[] { quotaCycleQty.Item, true }); if (quotaList == null || quotaList.Count == 0) { businessException.AddMessage(string.Format("第{0}行:物料编号{1}没有维护有效的调整数,请确认。 ", i, quotaCycleQty.Item)); continue; } else { var supplierCodes = quotaList.Select(q => q.Supplier).ToArray(); var flowMasters = new List<FlowMaster>(); foreach (var supplier in supplierCodes) { var flowMasterBySupplier = this.genericMgr.FindAll<FlowMaster>("select f from FlowMaster as f where f.PartyFrom=? and exists( select 1 from FlowDetail as d where d.Flow=f.Code and d.Item=? )", new object[] { supplier, quotaCycleQty.Item }); //var flowMasterBySupplier = this.genericMgr.FindAll<FlowMaster>("select f from FlowMaster as f where f.PartyFrom=?", supplier); if (flowMasterBySupplier == null || flowMasterBySupplier.Count == 0) { businessException.AddMessage(string.Format(" 第{0}行:物料编号{1},供应商{2}没有找到有效的采购路线,请确认。 ", i, quotaCycleQty.Item, supplier)); continue; } else { flowMasters.Add(flowMasterBySupplier.First()); } } var locTos = flowMasters.Select(f => f.LocationTo).ToList(); var flowMasterGrooups = (from tak in flowMasters group tak by tak.PartyFrom into result select new { Supplier = result.Key, FlowMasters = result.ToList() }); foreach (var groups in flowMasterGrooups) { foreach (var locTo in locTos) { if (groups.FlowMasters.Where(f => f.LocationTo == locTo).Count() == 0) { businessException.AddMessage(string.Format("第"+i+"行: 物料编号{0},供应商{1}目的库位{2}没有找到有效的采购路线,与其他供应商不一致,请确认。 ", quotaCycleQty.Item, groups.FlowMasters.First().PartyFrom, locTo)); continue; } } } } #endregion exactCycleQtyList.Add(quotaCycleQty); #endregion } if (businessException.HasMessage) { throw businessException; } if (exactCycleQtyList == null || exactCycleQtyList.Count == 0) { throw new BusinessException("模版为空,请确认。"); } foreach (QuotaCycleQty cycleQty in exactCycleQtyList) { if (cycleQty.IsUpdate) { this.genericMgr.Update(cycleQty); } else { genericMgr.Create(cycleQty); } } } SaveSuccessMessage("导入成功。"); } catch (BusinessException ex) { SaveBusinessExceptionMessage(ex); } catch (Exception ex) { SaveErrorMessage("导入失败。 - " + ex.Message); } return Content(string.Empty); }
public ActionResult _CycleQtyUpdate(Int32 id, QuotaCycleQty quotaCycleQty, QuotaSearchModel searchModel) { try { ViewBag.HaveCycleEditPermission = CurrentUser.Permissions.Where(p => p.PermissionCode == "Url_QuotaCycleQty_Edit").Count() > 0; ViewBag.ItemCode = searchModel.ItemCode; if (quotaCycleQty.CycleQty <= 0) { throw new BusinessException(" 循环数不能小于等于0"); } QuotaCycleQty upQuotaCycleQty = this.genericMgr.FindById<QuotaCycleQty>(id); var quotaList = this.genericMgr.FindAll<Quota>("select q from Quota as q where q.Item=? and Isactive=?", new object[] { upQuotaCycleQty.Item, true }); if (quotaList == null || quotaList.Count == 0) { throw new BusinessException(string.Format(" 物料编号{0}没有维护有效的调整数,请确认。 ", upQuotaCycleQty.Item)); } else { var supplierCodes = quotaList.Select(q => q.Supplier).ToArray(); var flowMasters = new List<FlowMaster>(); foreach (var supplier in supplierCodes) { var flowMasterBySupplier = this.genericMgr.FindAll<FlowMaster>("select f from FlowMaster as f where f.IsActive=1 and f.PartyFrom=? and exists( select 1 from FlowDetail as d where d.Flow=f.Code and d.Item=? )", new object[] { supplier, upQuotaCycleQty.Item}); if (flowMasterBySupplier == null || flowMasterBySupplier.Count == 0) { throw new BusinessException(string.Format(" 物料编号{0},供应商{1}没有找到有效的采购路线,请确认。 ", upQuotaCycleQty.Item, supplier)); } else { flowMasters.Add(flowMasterBySupplier.First()); } } var locTos = flowMasters.Select(f => f.LocationTo).ToList(); var flowMasterGrooups = (from tak in flowMasters group tak by tak.PartyFrom into result select new { Supplier = result.Key, FlowMasters = result.ToList() }); foreach (var groups in flowMasterGrooups) { foreach (var locTo in locTos) { if (groups.FlowMasters.Where(f => f.LocationTo == locTo).Count() == 0) { throw new BusinessException(string.Format(" 物料编号{0},供应商{1}目的库位{2}没有找到有效的采购路线,与其他供应商不一致,请确认。 ", upQuotaCycleQty.Item, groups.FlowMasters.First().PartyFrom, locTo)); } } } } upQuotaCycleQty.CycleQty = quotaCycleQty.CycleQty; this.genericMgr.Update(upQuotaCycleQty); } catch (BusinessException ex) { SaveErrorMessage("修改失败。" + ex.GetMessages()[0].GetMessageString()); } catch (Exception ex) { SaveErrorMessage("修改失败。" + ex.Message); } ViewBag.HaveCycleEditPermission = CurrentUser.Permissions.Where(p => p.PermissionCode == "Url_QuotaCycleQty_Edit").Count() > 0; GridCommand command = (GridCommand)TempData["CycleQtyGridCommand"]; TempData["CycleQtyGridCommand"] = command; SearchStatementModel searchStatementModel = PrepareCycleQtySearchStatement(command, searchModel); return PartialView(GetAjaxPageData<QuotaCycleQty>(searchStatementModel, command)); }