public JsonResult SaveExcelData()
        {
            var    reader     = new System.IO.StreamReader(HttpContext.Request.InputStream);
            string data       = reader.ReadToEnd();
            var    tempdata   = JsonConvert.DeserializeObject <Dictionary <string, string> >(data);
            var    list       = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(tempdata["data"]);
            var    BidOfferID = this.Request["BidOfferID"];
            var    bid        = this.GetEntityByID <S_M_BidOffer>(BidOfferID);

            if (bid == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到投标报价记录,无法导入EXCEL");
            }
            var deviceRoot = bid.S_M_BidOffer_CBS.Where(d => d.CBSType == "Device").OrderBy(d => d.CBSFullID).FirstOrDefault();

            if (deviceRoot == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到投标报价记录中的设备费用节点,无法导入!");
            }
            var BOMMajor = EnumBaseHelper.GetEnumDef("Base.BOMMajor");

            if (BOMMajor == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到对应的采购专业分类枚举【Base.BOMMajor】");
            }
            var enumItems = BOMMajor.EnumItem.ToList();

            if (deviceRoot.CBSTemplateNode == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到设备材料费用的定义科目信息,导入失败");
            }
            var deviceTemplate = deviceRoot.CBSTemplateNode;
            var structDic      = new Dictionary <string, S_M_BidOffer_CBS>();

            structDic.SetValue(deviceTemplate.ID, deviceRoot);
            S_M_BidOffer_CBS lastParent = null;

            for (int i = 0; i < list.Count; i++)
            {
                var item     = list[i];
                var nodeType = getNodeType(item);
                if (nodeType != "Detail")
                {
                    var templateNode = deviceTemplate.AllChildren.FirstOrDefault(c => c.NodeType == nodeType);
                    if (templateNode == null)
                    {
                        continue;
                    }
                    if (templateNode.Parent == null)
                    {
                        continue;
                    }
                    lastParent = structDic.GetValue(templateNode.Parent.ID);
                    if (lastParent == null || lastParent.CBSTemplateNode == null)
                    {
                        continue;
                    }
                    var childTemplateNode = lastParent.CBSTemplateNode.Children.FirstOrDefault(c => c.NodeType == nodeType);
                    if (childTemplateNode == null)
                    {
                        continue;
                    }
                    var name      = item.GetValue("Name");
                    var childNode = new S_M_BidOffer_CBS();
                    this.UpdateEntity <S_M_BidOffer_CBS>(childNode, item);
                    childNode.ID          = FormulaHelper.CreateGuid();
                    childNode.NodeType    = nodeType;
                    childNode.CBSDefineID = childTemplateNode.ID;
                    lastParent.AddChild(childNode);
                    structDic.SetValue(childTemplateNode.ID, childNode);
                }
                else
                {
                    var templateNode = deviceTemplate.AllChildren.LastOrDefault();
                    if (templateNode == null)
                    {
                        continue;
                    }
                    var cbsNode = structDic.GetValue(templateNode.ID);
                    if (cbsNode == null)
                    {
                        continue;
                    }
                    var majorName = item.GetValue("MajorName");
                    var codeItem  = enumItems.FirstOrDefault(c => c.Name == majorName);
                    if (codeItem == null)
                    {
                        throw new Formula.Exceptions.BusinessValidationException("没有找到对应的专业内容【" + majorName + "】");
                    }
                    var majorCode = codeItem.Code;
                    item.SetValue("MajorCode", majorCode);
                    var detail = new S_M_BidOffer_CBS_Detail();
                    FormulaHelper.UpdateEntity <S_M_BidOffer_CBS_Detail>(detail, item);
                    detail.BidOfferID     = bid.ID;
                    detail.ID             = FormulaHelper.CreateGuid();
                    detail.CBSParentID    = cbsNode.CBSID;
                    detail.BomID          = FormulaHelper.CreateGuid();
                    detail.OfferCBSFullID = cbsNode.CBSFullID;
                    detail.OfferCBSID     = cbsNode.ID;
                    detail.FullID         = detail.ID;
                    detail.SortIndex      = i;
                    cbsNode.S_M_BidOffer_CBS_Detail.Add(detail);
                }
            }
            this.EPCEntites.SaveChanges();
            return(Json("Success"));
        }
        public JsonResult ImportNodeTemplate(string NodeIDs, string BidInfoID)
        {
            var bid = this.GetEntityByID <S_M_BidOffer>(BidInfoID);

            if (bid == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到指定的报价就,无法导入");
            }
            var deviceRoot = bid.S_M_BidOffer_CBS.Where(d => d.CBSType == "Device").OrderBy(d => d.CBSFullID).FirstOrDefault();

            if (deviceRoot == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到投标报价记录中的设备费用节点,无法导入!");
            }
            if (deviceRoot.CBSTemplateNode == null)
            {
                throw new Formula.Exceptions.BusinessValidationException("没有找到设备材料费用的定义科目信息,导入失败");
            }

            var deviceTemplate = deviceRoot.CBSTemplateNode;
            var structDic      = new Dictionary <string, S_M_BidOffer_CBS>();

            structDic.SetValue(deviceTemplate.ID, deviceRoot);
            S_M_BidOffer_CBS lastParent = null;

            var infraDbContext = FormulaHelper.GetEntities <InfrastructureEntities>();
            var templateNodes  = infraDbContext.Set <S_T_NodeTemplate_Detail>().Where(c => NodeIDs.Contains(c.ID)).OrderBy(c => c.FullID).ToList();

            for (int i = 0; i < templateNodes.Count; i++)
            {
                var item     = templateNodes[i];
                var nodeType = item.NodeType;
                if (nodeType != "Detail")
                {
                    var templateNode = deviceTemplate.AllChildren.FirstOrDefault(c => c.NodeType == nodeType);
                    if (templateNode == null)
                    {
                        continue;
                    }
                    if (templateNode.Parent == null)
                    {
                        continue;
                    }
                    lastParent = structDic.GetValue(templateNode.Parent.ID);
                    if (lastParent == null || lastParent.CBSTemplateNode == null)
                    {
                        continue;
                    }
                    var childTemplateNode = lastParent.CBSTemplateNode.Children.FirstOrDefault(c => c.NodeType == nodeType);
                    if (childTemplateNode == null)
                    {
                        continue;
                    }
                    if (lastParent.Children.Exists(c => c.Name == item.Name))
                    {
                        continue;
                    }
                    var name      = item.Name;
                    var childNode = new S_M_BidOffer_CBS();
                    childNode.Name        = name;
                    childNode.Code        = item.Code;
                    childNode.ID          = FormulaHelper.CreateGuid();
                    childNode.NodeType    = nodeType;
                    childNode.CBSDefineID = childTemplateNode.ID;
                    childNode.SortIndex   = item.SortIndex;
                    lastParent.AddChild(childNode);
                    structDic.SetValue(childTemplateNode.ID, childNode);
                }
            }
            this.EPCEntites.SaveChanges();
            return(Json(""));
        }