public void SaveComponentMaterials(Sender sender, SaveComponentMaterialArgs args) { try { using (ObjectProxy op = new ObjectProxy(true)) { if (op.LoadComponentMaterial(args.ComponentMaterial) == 0) { // string key = "S" + DateTime.Now.ToString("yy"); //int index = this.GetIncrease(sender, key); //args.ComponentMaterial.ProduceNo= key + DateTime.Now.Month.ToString("00") + index.ToString("00000"); args.ComponentMaterial.Created = DateTime.Now; args.ComponentMaterial.CreatedBy = sender.UserCode + "." + sender.UserName; op.InsertComponentMaterial(args.ComponentMaterial); } else { args.ComponentMaterial.Created = DateTime.Now; args.ComponentMaterial.CreatedBy = sender.UserCode + "." + sender.UserName; op.UpdateComponentMaterialByID(args.ComponentMaterial); } #region 订单产品 //if (args.ComponentMaterials != null) //{ // foreach (ComponentMaterial Item in args.ComponentMaterials) // { // if (op.LoadComponentMaterial(args.ComponentMaterial) == 0) // { // op.InsertComponentMaterialComponent(Item); // } // else // { // op.UpdateComponentMaterialByID(Item); // } // } //} #endregion op.CommitTransaction(); } } catch (Exception ex) { PLogger.LogError(ex); throw ex; } }
public void SaveComponentMaterialAndExtension(Sender sender, SaveComponentMaterialArgs args) { try { using (ObjectProxy op = new ObjectProxy(true)) { if (args.ComponentMaterials != null) { foreach (ComponentMaterial Item in args.ComponentMaterials) { if (op.LoadComponentMaterial(Item) == 0) { Item.Created = DateTime.Now; Item.CreatedBy = sender.UserCode + "." + sender.UserName; Item.Modified = DateTime.Now; Item.ModifiedBy = sender.UserCode + "." + sender.UserName; int componentMaterialID = op.AddComponentMaterial(Item); Item.ExtensionModel.ComponentMaterialID = componentMaterialID; Item.ExtensionModel.Created = DateTime.Now; Item.ExtensionModel.CreatedBy = sender.UserCode + "." + sender.UserName; op.InsertComponentMaterialExtension(Item.ExtensionModel); } else { Item.Modified = DateTime.Now; Item.ModifiedBy = sender.UserCode + "." + sender.UserName; op.UpdateComponentMaterialByID(Item); } } } op.CommitTransaction(); } } catch (Exception ex) { PLogger.LogError(ex); throw ex; } }
/// <summary> /// 导入BOM文件 /// </summary> public void ImportBOM() { try { using (var p = new ProxyBE()) { string bomID = Request["BOMID"]; string productID = Request["ProductCode"]; string filePath = Request["FilePath"]; if (string.IsNullOrEmpty(bomID) || string.IsNullOrEmpty(productID)) { throw new Exception("BOMID和产品编号为空或者不存在"); } if (p.Client.GetProductComponentByProductCode(SenderUser, productID).Count > 0) //检验产品是否已经导入 { throw new Exception("该产品的BOM已经导入,请更换其他产品"); } if (string.IsNullOrEmpty(filePath)) { throw new Exception("要导入的BOM文件路径错误,请检查后重新上传"); } DataTable table = NPOIHelper.ImportExceltoDt(Server.MapPath(filePath)); if (table.Rows.Count <= 0) { throw new Exception("BOM文件没有相应数据,不能为空"); } string[] componentTypeLevel = { "第一阶层", "第二阶层", "第三阶层" }; List <ComponentType> componentTypeList = GetComponentTypeList(); //获取数据库表中所有组件类型列表 List <DataRow> lstAllRow = table.AsEnumerable().Where(x => x.Field <string>("产品ID").ToString().Equals(productID)).ToList(); //获取BOM表中该产品所有行 if (lstAllRow.Count <= 0) { throw new Exception("BOM文件中的产品ID与要导入的产品编号不一致"); } List <string> lstFistType = lstAllRow.Select(x => x.Field <string>(componentTypeLevel[0])).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList(); //获取BOM表中第一阶层的组件类型(去重) SaveProductComponentArgs args = new SaveProductComponentArgs(); args.ProductComponents = new List <ProductComponent>(); foreach (string firstTypeName in lstFistType) { List <DataRow> lstFirstTypeRow = lstAllRow.Where(x => x.Field <string>(componentTypeLevel[0]).ToString().Equals(firstTypeName)).ToList(); //循环取出第一阶层下面第二、三阶层的所有行 for (int i = 0; i < componentTypeLevel.Length; i++) { List <string> lstChildType = lstFirstTypeRow.Select(x => x.Field <string>(componentTypeLevel[i])).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList(); //获取第一阶层下面第二、三阶层的组件类型(去重) foreach (string childTypeName in lstChildType) { //首先确认BOM表中组件类型在数据库表中是存在并且有效的 var firstComponentType = componentTypeList.FirstOrDefault(x => x.ComponentTypeName.Equals(childTypeName)); if (firstComponentType != null) { //每一阶层,每种组件类型所有的行 List <DataRow> lstChildTypeRow = GetDataRowListBy(componentTypeLevel[i], firstComponentType.ComponentTypeName, lstFirstTypeRow); ProductComponent productComponent = new ProductComponent(); productComponent.ComponentCode = productID + "-" + firstComponentType.ComponentTypeCode; productComponent.ProductCode = productID; productComponent.ComponentTypeID = firstComponentType.ComponentTypeID; productComponent.ComponentTypeName = firstComponentType.ComponentTypeName; productComponent.Quantity = lstChildTypeRow.Count; productComponent.Amount = lstChildTypeRow.Select(x => x.Field <string>("用量")).Where(x => !string.IsNullOrEmpty(x)).Sum(x => Convert.ToDecimal(x)); args.ProductComponents.Add(productComponent); } } } } p.Client.SaveProductComponents(SenderUser, args); //Insert ProductComponent List <ComponentMaterial> lstComponentMaterial = new List <ComponentMaterial>(); List <ProductComponent> lstProductComponent = p.Client.GetProductComponentByProductCode(SenderUser, productID); LoadComponentMaterialList(lstAllRow, componentTypeLevel.ToList(), lstProductComponent, ref lstComponentMaterial); SaveComponentMaterialArgs componentMaterialArgs = new SaveComponentMaterialArgs(); componentMaterialArgs.ComponentMaterials = lstComponentMaterial; p.Client.SaveComponentMaterialAndExtension(SenderUser, componentMaterialArgs); //Insert ComponentMaterial p.Client.UpdateProductBOMStatusByBOMID(SenderUser, new ProductBOM() { BOMID = bomID, Status = true }); //Update ProductBOM Status更新状态为已上传 WriteJsonSuccess("导入成功"); } } catch (Exception ex) { WriteJsonError(ex.Message); } }