示例#1
0
        public void ImportKanBanCard(Stream inputStream, com.Sconit.CodeMaster.OrderType type, User currUser)
        {
            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);

            #region 列定义
            // FlowMaster
            int colCode = 1; // 路线代码
            int colItem = 2; // 物料号										
            int colRefItemCode = 3; // 旧图号
            int colItemDesc = 4; // 零件名称
            int colOpRef = 5; // 工位代码
            int colOpUseQty = 6; // 工位用量
            int colItemType = 7; // 零件类型
            int colOpRefSeq = 8; // 看板编号
            int colUC = 9; // 单包装
            int colUCDesc = 10; // 包装描述

            int colContainerDesc = 11; // 容器描述
            int colDock = 12; // 配送路径
            int colCardQty = 13; // 看板卡数量
            int colIsActive = 14; // 是否生效
            //int colShelf = 13; // 架位
            #endregion

            var errorMessage = new BusinessException();

            //IList<Item> ItemsList = new List<Item>();
            //IList<FlowMaster> exactFlowList = new List<FlowMaster>();
            IList<FlowDetail> exactFlowDetailList = new List<FlowDetail>();
            //IList<FlowDetail> deleteFlowDetailList = new List<FlowDetail>();
            int colCount = 0;

            #region 取所有的路线和路线明细
            //var totalFlowMasterList = genericMgr.FindAll<FlowMaster>("from FlowMaster f where f.Type = ? and f.FlowStrategy = ?", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            var totalFlowDetailList = genericMgr.FindEntityWithNativeSql<FlowDetail>("select d.* from scm_FlowDet d inner join scm_FlowMstr f on f.Code = d.Flow and f.Type = ? and exists(select 1 from SCM_FlowStrategy as fs where fs.Flow=f.Code and fs.Strategy=?)", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            //var totalFlowStrategyList = genericMgr.FindEntityWithNativeSql<FlowStrategy>("select g.* from scm_FlowStrategy g inner join scm_FlowMstr f on f.Code = g.Flow and f.Type = ? and f.FlowStrategy = ?", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            //var totalSupplierList = genericMgr.FindAll<Supplier>();
            //var totalRegionList = genericMgr.FindAll<Region>();
            //var totalLocationList = genericMgr.FindAll<Location>();
            //var totalItemList = genericMgr.FindAll<Item>("from Item where IsActive = ?", true);
            #endregion

            while (rows.MoveNext())
            {
                HSSFRow row = (HSSFRow)rows.Current;
                if (!ImportHelper.CheckValidDataRow(row, 1, 14))
                {
                    break;//边界
                }
                colCount++;

                var rowErrors = new List<Message>();

                string code = string.Empty;
                string item = string.Empty;
                string refItemCode = string.Empty;
                string itemDesc = string.Empty;
                string opRef = string.Empty;
                string opUseQty = string.Empty;
                string itemType = string.Empty;
                string opRefSeq = string.Empty;
                string uc = string.Empty;
                string ucDesc = string.Empty;
                string dock = string.Empty;
                string containerDesc = string.Empty;
                string cardQty = string.Empty;
                string isActive = string.Empty;


                #region 读取数据

                code = ImportHelper.GetCellStringValue(row.GetCell(colCode));
                if (code == null || code.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.SCM.FlowMaster.FlowMaster_Code));
                }

                opRef = ImportHelper.GetCellStringValue(row.GetCell(colOpRef));
                if (opRef == null || opRef.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "工位"));
                }

                cardQty = ImportHelper.GetCellStringValue(row.GetCell(colCardQty));
                decimal qtyVar = 0;
                if (cardQty == null || cardQty.Trim() == string.Empty || !decimal.TryParse(cardQty,out qtyVar))
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "看板张数"));
                }
                
                item = ImportHelper.GetCellStringValue(row.GetCell(colItem));
                if (item == null || item.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.SCM.FlowDetail.FlowDetail_Item));
                }
                
                #endregion

                #region 检查路线明细是否都在数据库中
                var flowDet = totalFlowDetailList.FirstOrDefault(f => f.Flow.ToUpper() == code.ToUpper() && f.Item.ToUpper() == item.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper());
                if (flowDet == null)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, "导入的数据的第{0}行路线明细不存在", colCount.ToString()));
                }
                else
                {
                    flowDet.OrderQty = qtyVar;
                    exactFlowDetailList.Add(flowDet);
                }
                #endregion


                errorMessage.AddMessages(rowErrors);
            }

            if (errorMessage.HasMessage)
            {
                throw errorMessage;
            }

            if (exactFlowDetailList.Count > 0)
            {
                var flowCodes = exactFlowDetailList.Select(f => f.Flow).Distinct();
                foreach (var flow in flowCodes)
                {
                    this.AddManuallyByKanbanFlow(flow, exactFlowDetailList.Where(f => f.Flow == flow).ToList(), DateTime.Now, currUser, true);
                }
            }
        }
示例#2
0
        public void ImportKanBanFlow(Stream inputStream, com.Sconit.CodeMaster.OrderType type)
        {
            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);

            #region 列定义
            // FlowMaster
            int colCode = 1; // 路线代码
            int colItem = 2; // 物料号										
            int colRefItemCode = 3; // 旧图号
            int colItemDesc = 4; // 零件名称
            int colOpRef = 5; // 工位代码
            int colOpUseQty = 6; // 工位用量
            int colItemType = 7; // 零件类型
            int colOpRefSeq = 8; // 看板编号
            int colUC = 9; // 单包装
            int colContainer = 10; // 容器代码

            int colContainerDesc = 11; // 容器描述
            int colDock = 12; // 配送路径
            int colCardQty = 13; // 看板卡数量
            int colIsActive = 14; // 是否生效
            int colOpRefDesc = 15; //工位中文描述
            //int colShelf = 13; // 架位
            #endregion

            var errorMessage = new BusinessException();

            IList<Item> ItemsList = new List<Item>();
            IList<FlowMaster> exactFlowList = new List<FlowMaster>();
            IList<FlowStrategy> exactFlowStrategyList = new List<FlowStrategy>();
            IList<FlowDetail> exactFlowDetailList = new List<FlowDetail>();
            IList<FlowDetail> deleteFlowDetailList = new List<FlowDetail>();
            int colCount = 10;

            #region 取所有的路线和路线明细
            var totalFlowMasterList = genericMgr.FindAll<FlowMaster>("from FlowMaster f where f.Type = ? and exists(select 1 from FlowStrategy as fs where fs.Flow=f.Code and fs.Strategy=? )", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            var totalFlowDetailList = genericMgr.FindEntityWithNativeSql<FlowDetail>("select d.* from scm_FlowDet d inner join scm_FlowMstr f on f.Code = d.Flow where f.Type = ? and exists(select 1 from SCM_FlowStrategy as fs where fs.Flow=f.Code and fs.Strategy=?)", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            //var totalFlowStrategyList = genericMgr.FindEntityWithNativeSql<FlowStrategy>("select g.* from scm_FlowStrategy g inner join scm_FlowMstr f on f.Code = g.Flow and f.Type = ? and f.FlowStrategy = ?", new object[] { (int)type, (int)CodeMaster.FlowStrategy.ANDON });
            //var totalSupplierList = genericMgr.FindAll<Supplier>();
            var totalRegionList = genericMgr.FindAll<Region>();
            var totalLocationList = genericMgr.FindAll<Location>();
            //var totalItemList = genericMgr.FindAll<Item>("from Item where IsActive = ?", true);
            #endregion

            while (rows.MoveNext())
            {
                HSSFRow row = (HSSFRow)rows.Current;
                if (!ImportHelper.CheckValidDataRow(row, 1, 14))
                {
                    break;//边界
                }
                colCount++;

                var rowErrors = new List<Message>();

                string code = string.Empty;
                string item = string.Empty;
                string refItemCode = string.Empty;
                string itemDesc = string.Empty;
                string opRef = string.Empty;
                string opUseQty = string.Empty;
                string itemType = string.Empty;
                string opRefSeq = string.Empty;
                string uc = string.Empty;
                string container = string.Empty;
                string dock = string.Empty;
                string containerDesc = string.Empty;
                string cardQty = string.Empty;
                string isActive = string.Empty;
                string opRefDesc = string.Empty;


                #region 读取数据
                //路线
                code = ImportHelper.GetCellStringValue(row.GetCell(colCode));
                if (code == null || code.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.SCM.FlowMaster.FlowMaster_Code));
                }

                //工位
                opRef = ImportHelper.GetCellStringValue(row.GetCell(colOpRef));
                if (opRef == null || opRef.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "工位"));
                }

                //零件类型
                itemType = ImportHelper.GetCellStringValue(row.GetCell(colItemType));

                //工位
                opRefSeq = ImportHelper.GetCellStringValue(row.GetCell(colOpRefSeq));
                if (opRefSeq == null || opRefSeq.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "看板号"));
                }

                uc = ImportHelper.GetCellStringValue(row.GetCell(colUC));
                //cardQty = ImportHelper.GetCellStringValue(row.GetCell(colCardQty));
                decimal ucVar = 0;
                if (uc == null || uc.Trim() == string.Empty || !decimal.TryParse(uc, out ucVar))
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "单包装数量"));
                }

                //容器代码
                container = ImportHelper.GetCellStringValue(row.GetCell(colContainer));
                if (container == null || container.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), "容器代码"));
                }

                //容器描述
                containerDesc = ImportHelper.GetCellStringValue(row.GetCell(colContainerDesc));

                dock = ImportHelper.GetCellStringValue(row.GetCell(colDock));

                item = ImportHelper.GetCellStringValue(row.GetCell(colItem));
                if (item == null || item.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.SCM.FlowDetail.FlowDetail_Item));
                }



                opRefDesc = ImportHelper.GetCellStringValue(row.GetCell(colOpRefDesc));

                //containerDesc = ImportHelper.GetCellStringValue(row.GetCell(colContainerDesc));

                //dock = ImportHelper.GetCellStringValue(row.GetCell(colDock));

                isActive = ImportHelper.GetCellStringValue(row.GetCell(colIsActive));
                var isActiveVal = false;
                if (isActive == null || isActive.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.SCM.FlowMaster.FlowMaster_IsActive));
                }
                else
                {
                    switch (isActive.ToUpper())
                    {
                        case "Y":
                            isActiveVal = true;
                            break;
                        case "N":
                            isActiveVal = false;
                            break;
                        default:
                            rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineBooleanFieldValueError, colCount.ToString(), Resources.SCM.FlowMaster.FlowMaster_IsActive, isActive));
                            break;
                    }
                }
                #endregion

                #region 验证
                if (totalFlowMasterList.FirstOrDefault(c => c.Code.ToUpper() == code.ToUpper()) == null)
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), string.Format("路线头{0}不存在,请确认。", code)));
                }

                //物料编号数据存在性
                Item itemInstance = null;
                if (!string.IsNullOrEmpty(item))
                {
                    itemInstance = ValidatItem(ItemsList, item, colCount, rowErrors);
                }

                //数据库中重复性验证
                FlowDetail sameFlowDet = null;
                if (itemInstance != null)
                {
                    var dbFlowDet = totalFlowDetailList.FirstOrDefault(c => c.Flow.ToUpper() == code.ToUpper() && c.Item.ToUpper() == item.ToUpper() && (c.BinTo == null && string.IsNullOrWhiteSpace(opRef) || c.BinTo != null && c.BinTo.ToUpper() == opRef.ToUpper()));
                    //if (dbFlowDet != null && (dbFlowDet.BinTo != null && dbFlowDet.BinTo.Equals(opRef, StringComparison.OrdinalIgnoreCase)))
                    if (dbFlowDet != null)
                    {
                        sameFlowDet = dbFlowDet;
                    }
                }

                FlowMaster flowMaster = this.genericMgr.FindById<FlowMaster>(code);



                #endregion

                #region Instance



                if (itemInstance != null)
                {
                    if (sameFlowDet != null)
                    {
                        sameFlowDet.ItemType = itemType;
                        //sameFlowDet.ContainerDescription = containerDesc;
                        sameFlowDet.BinTo = opRef;
                        sameFlowDet.OprefSequence = opRefSeq;
                        sameFlowDet.Dock = dock;
                        sameFlowDet.ProductionScan = opRefDesc;
                        sameFlowDet.Container = container;
                        sameFlowDet.ContainerDescription = containerDesc;
                        sameFlowDet.MinUnitCount = ucVar;
                        sameFlowDet.PartyFrom = flowMaster.PartyFrom;
                        sameFlowDet.LocationTo = flowMaster.LocationTo;
                        sameFlowDet.FlowMasterStrategy = (int)flowMaster.FlowStrategy;
                        //sameFlowDet.BinTo = binTo;
                        sameFlowDet.IsCreate = false;
                        //sameFlowDet.IsRejectInspect = isRejectInspectVal;
                        //模板中 路线代码+物料编号+工位 不能重复
                        if (exactFlowDetailList.Where(f => f.Flow.ToUpper() == code.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper() && f.Item.ToUpper() == item.ToUpper()).Count() > 0
                         || deleteFlowDetailList.Where(f => f.Flow.ToUpper() == code.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper() && f.Item.ToUpper() == item.ToUpper()).Count() > 0)
                        {
                            rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), string.Format("路线头{0}+工位{1}+物料编号{2}在模板中重复,请确认。", code, opRef, item)));
                        }

                        if (isActiveVal)
                        {
                            //sameFlowMaster.AddFlowDetail(sameFlowDet);
                            #region 来源区域 目的库位 物料 不能再不同的策略上出现
                            int checkeSame = this.genericMgr.FindAllWithNativeSql<int>(" select COUNT(*) as countSum  from SCM_FlowDet as d where d.Item=? and exists( select 1 from SCM_FlowMstr as m where m.Code=d.Flow and m.PartyFrom=? and m.LocTo=? and m.Type=2 and not exists(select 1 from SCM_FlowStrategy as fs where fs.Flow=m.Code and fs.Strategy=7) ) ", new object[] { item, flowMaster.PartyFrom, flowMaster.LocationTo })[0];
                            if (checkeSame > 0)
                            {
                                rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), (string.Format("来源区域{0}+物料{1}+目的库位{2}+在其他策略中已经存在。", flowMaster.PartyFrom, item, flowMaster.LocationTo, sameFlowDet.FlowMasterStrategy))));
                            }
                            if (exactFlowDetailList.Where(ef => ef.PartyFrom == flowMaster.PartyFrom && ef.LocationTo == flowMaster.LocationTo && ef.Item == item && ef.Id != sameFlowDet.Id && ef.FlowMasterStrategy != sameFlowDet.FlowMasterStrategy).Count() > 0)
                            {
                                rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), (string.Format("来源区域{0}+物料{1}+目的库位{2}+策略不同{3}在模板中重复。", flowMaster.PartyFrom, item, flowMaster.LocationTo, sameFlowDet.FlowMasterStrategy))));
                            }
                            #endregion

                            exactFlowDetailList.Add(sameFlowDet);
                        }
                        else
                        {
                            deleteFlowDetailList.Add(sameFlowDet);
                        }

                    }
                    else
                    {
                        //if (exactFlowDetailList.FirstOrDefault(f => f.Flow.ToUpper() == code.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper() && f.Item.ToUpper() == item.ToUpper()) == null)
                        //{
                        sameFlowDet = new FlowDetail
                        {
                            Flow = code,
                            Item = itemInstance.Code,
                            ItemDescription = itemInstance.Description,
                            BaseUom = itemInstance.Uom,
                            Uom = itemInstance.Uom,
                            ReferenceItemCode = itemInstance.ReferenceCode,
                            UnitCount = itemInstance.UnitCount,
                            MinUnitCount = ucVar,
                            //UnitCountDescription = ucDesc,
                            Container = container,
                            ContainerDescription = containerDesc,
                            //SafeStock = safeStockVal,
                            ItemType = itemType,
                            Dock = dock,
                            OprefSequence = opRefSeq,
                            BinTo = opRef,
                            IsCreate = true,
                            PartyFrom = flowMaster.PartyFrom,
                            LocationTo = flowMaster.LocationTo,
                            FlowMasterStrategy = (int)flowMaster.FlowStrategy,
                            ProductionScan = opRefDesc,
                            //IsRejectInspect = isRejectInspectVal
                        };
                        //sameFlowMaster.AddFlowDetail(sameFlowDet);
                        #region 来源区域 目的库位 物料 不能再不同的策略上出现
                        int checkeSame = this.genericMgr.FindAllWithNativeSql<int>(" select COUNT(*) as countSum  from SCM_FlowDet as d where d.Item=? and exists( select 1 from SCM_FlowMstr as m where m.Code=d.Flow and m.PartyFrom=? and m.LocTo=? and m.Type=2 and exists(select 1 from SCM_FlowStrategy as fs where fs.Flow=m.Code and fs.Strategy<>7)) ", new object[] { item, flowMaster.PartyFrom, flowMaster.LocationTo })[0];
                        if (checkeSame > 0)
                        {
                            rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), (string.Format("来源区域{0}+物料{1}+目的库位{2}+在其他策略中已经存在", flowMaster.PartyFrom, item, flowMaster.LocationTo, sameFlowDet.FlowMasterStrategy))));
                        }
                        if (exactFlowDetailList.Where(ef => ef.PartyFrom == flowMaster.PartyFrom && ef.LocationTo == flowMaster.LocationTo && ef.Item == item && ef.Id != sameFlowDet.Id && ef.FlowMasterStrategy != sameFlowDet.FlowMasterStrategy).Count() > 0)
                        {
                            rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), (string.Format("来源区域{0}+物料{1}+目的库位{2}+策略不同{3}在模板中重复。", flowMaster.PartyFrom, item, flowMaster.LocationTo, sameFlowDet.FlowMasterStrategy))));
                        }
                        #endregion
                        //模板中 路线代码+物料编号+工位 不能重复
                        if (exactFlowDetailList.Where(f => f.Flow.ToUpper() == code.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper() && f.Item.ToUpper() == item.ToUpper()).Count() > 0
                         || deleteFlowDetailList.Where(f => f.Flow.ToUpper() == code.ToUpper() && f.BinTo.ToUpper() == opRef.ToUpper() && f.Item.ToUpper() == item.ToUpper()).Count() > 0)
                        {
                            rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), string.Format("路线头{0}+工位{1}+物料编号{2}在模板中重复,请确认", code, opRef, item)));
                        }
                        exactFlowDetailList.Add(sameFlowDet);
                        //}

                    }

                }
                else
                {
                    rowErrors.Add(new Message(com.Sconit.CodeMaster.MessageType.Error, Resources.ErrorMessage.Errors_Import_LineError, colCount.ToString(), string.Format("第{0}行:物料编号{1}不存在,请确认", code, opRef, item)));

                }
                #endregion

                errorMessage.AddMessages(rowErrors);
            }

            if (errorMessage.HasMessage)
            {
                throw errorMessage;
            }
            if ((exactFlowDetailList == null || exactFlowDetailList.Count == 0) && (deleteFlowDetailList == null || deleteFlowDetailList.Count == 0))
            {
                throw new BusinessException("模板为空,请确认。");
            }
            #region Save Data
            foreach (FlowDetail instance in exactFlowDetailList)
            {
                instance.IsActive = true;
                if (instance.IsCreate)
                {
                    CreateFlowDetail(instance);
                }
                else
                {
                    UpdateFlowDetail(instance);
                    this.genericMgr.UpdateWithNativeQuery("update KB_KanbanCard set Qty=?,Container=? where FlowDetId=?",
                        new object[] { instance.MinUnitCount, instance.Container, instance.Id },
                        new IType[] { NHibernateUtil.Decimal, NHibernateUtil.String, NHibernateUtil.Int32 });
                }
                this.genericMgr.FlushSession();
            }
            foreach (FlowDetail instance in deleteFlowDetailList)
            {
                genericMgr.Delete(instance);
                genericMgr.Delete("from KanbanCard k where k.FlowDetailId = ?", instance.Id, NHibernate.NHibernateUtil.Int32);
            }

            #endregion
        }
        public string CreateMultiSupplyItemXlsx(Stream 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);

            #region 列定义
            int colGroupNo = 1; // 多轨组号
            int colDescription = 2; // 供应商代码
            int colSupplier = 3; // 供应商代码
            int colCycleQty = 4; // 循环量
            int colProportion = 5; //循环量比例
            int colSubstituteGroup = 6; //SAP替代组
            int colItem = 7; // 物料编号

            #endregion

            var errorMessage = new BusinessException();
            int colCount = 0;
            IList<MultiSupplyItem> exactMultiSupplyItem = new List<MultiSupplyItem>();
            IList<MultiSupplySupplier> exactMultiSupplySupplier = new List<MultiSupplySupplier>();
            IList<MultiSupplyGroup> exactMultiSupplyGroup = new List<MultiSupplyGroup>();
            IList<Supplier> supplierList = new List<Supplier>();
            IList<Item> itemList = new List<Item>();
            while (rows.MoveNext())
            {
                HSSFRow row = (HSSFRow)rows.Current;
                if (!ImportHelper.CheckValidDataRow(row, 1, 5))
                {
                    break;//边界
                }
                colCount++;

                var rowErrors = new List<Message>();

                string GroupNo = string.Empty;
                string description = string.Empty;
                string Item = string.Empty;
                string Supplier = string.Empty;
                string CycleQty = string.Empty;

                #region 读取数据
                GroupNo = ImportHelper.GetCellStringValue(row.GetCell(colGroupNo));
                if (GroupNo == null || GroupNo.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.PRD.MultiSupplyGroup.MultiSupplyGroup_GroupNo));
                }
                else
                {
                    GroupNo = GroupNo.ToUpper();
                }

                description = ImportHelper.GetCellStringValue(row.GetCell(colDescription));

                Supplier = ImportHelper.GetCellStringValue(row.GetCell(colSupplier));
                if (Supplier == null || Supplier.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.PRD.MultiSupplySupplier.MultiSupplySupplier_Supplier));
                }
                else
                {
                    Supplier = Supplier.ToUpper();
                }

                CycleQty = ImportHelper.GetCellStringValue(row.GetCell(colCycleQty));
                int cy;
                int.TryParse(CycleQty, out cy);
                if (cy <= 0)
                {
                    rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeZero, colCount.ToString(), Resources.PRD.MultiSupplySupplier.MultiSupplySupplier_CycleQty));
                }

                Item = ImportHelper.GetCellStringValue(row.GetCell(colItem));
                if (Item == null || Item.Trim() == string.Empty)
                {
                    rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldCanNotBeNull, colCount.ToString(), Resources.PRD.MultiSupplyItem.MultiSupplyItem_Item));
                }
                else
                {
                    Item = Item.ToUpper();
                }

                string proportion = ImportHelper.GetCellStringValue(row.GetCell(colProportion));
                string substituteGroup = ImportHelper.GetCellStringValue(row.GetCell(colSubstituteGroup));

                //Excel重复性验证
                Item ItemInstance = null;
                if (!string.IsNullOrWhiteSpace(GroupNo) && !string.IsNullOrWhiteSpace(Supplier) && !string.IsNullOrWhiteSpace(Item))
                {
                    MultiSupplyItem innerSameMultiSupplyItem = exactMultiSupplyItem.FirstOrDefault(m => m.Item.ToUpper() == Item);
                    if (innerSameMultiSupplyItem != null)
                    {
                        if (innerSameMultiSupplyItem.GroupNo.ToUpper() != GroupNo)
                        {
                            rowErrors.Add(new Message(MessageType.Error, Resources.PRD.MultiSupplyItem.MultiSupplyItem_Import_ItemOnlyBelongOneGroup, colCount.ToString(), Item, innerSameMultiSupplyItem.GroupNo));
                        }
                        else if (innerSameMultiSupplyItem.GroupNo.ToUpper() == GroupNo && innerSameMultiSupplyItem.Supplier.ToUpper() == Supplier)
                        {
                            rowErrors.Add(new Message(MessageType.Error, Resources.PRD.MultiSupplyItem.MultiSupplyItem_Import_ItemOnlyBelongOneGroupDiffSupplier, colCount.ToString(), Item, innerSameMultiSupplyItem.GroupNo, innerSameMultiSupplyItem.Supplier));
                        }
                    }

                    //循环量相同性验证
                    if (exactMultiSupplySupplier.FirstOrDefault(m => m.GroupNo.ToUpper() == GroupNo && m.Supplier.ToUpper() == Supplier && m.CycleQty != cy) != null)
                    {
                        rowErrors.Add(new Message(MessageType.Error, Resources.PRD.MultiSupplyItem.MultiSupplyItem_Import_GroupSupplierDiffCycleQty, colCount.ToString(), GroupNo, Supplier));
                    }
                    //供应商存在性验证
                    if (supplierList.FirstOrDefault(i => i.Code.ToUpper() == Supplier) == null)
                    {
                        IList<Supplier> SupplierCount = this.genericMgr.FindAll<Supplier>("from Supplier as s where s.Code = ?", Supplier);
                        if (SupplierCount == null || SupplierCount.Count() == 0)
                        {
                            rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldValueNotExist, colCount.ToString(), Resources.PRD.MultiSupplyGroup.MultiSupplyGroup_Supplier, Supplier));
                        }
                        else
                        {
                            supplierList.Add(SupplierCount[0]);
                        }
                    }

                    //零件存在性
                    ItemInstance = itemList.FirstOrDefault(i => i.Code.ToUpper() == Item);
                    if (ItemInstance == null)
                    {
                        IList<Item> ItemCount = this.genericMgr.FindAll<Item>("from Item as i where i.Code= ?", Item);
                        if (ItemCount == null || ItemCount.Count() == 0)
                        {
                            rowErrors.Add(new Message(MessageType.Error, Resources.ErrorMessage.Errors_Import_LineFieldValueNotExist, colCount.ToString(), colCount.ToString(), Resources.PRD.MultiSupplyItem.MultiSupplyItem_Item, Item));
                        }
                        else
                        {
                            ItemInstance = ItemCount[0];
                            itemList.Add(ItemCount[0]);
                        }
                    }
                    //数据库重复性验证
                    var items = this.genericMgr.FindAll<MultiSupplyItem>(duiplicateItemVerifyStatement, new object[] { Item });
                    var sameItemInDiffGroup = items.FirstOrDefault(c => c.GroupNo.ToUpper() != GroupNo);
                    if (sameItemInDiffGroup != null)
                    {
                        rowErrors.Add(new Message(MessageType.Error, Resources.PRD.MultiSupplyItem.MultiSupplyItem_Import_ItemOnlyBelongOneGroup, colCount.ToString(), Item, sameItemInDiffGroup.GroupNo));
                    }

                    var sameItemInSameGroupandSupplier = items.FirstOrDefault(c => c.GroupNo.ToUpper() == GroupNo && c.Supplier.ToUpper() == Supplier);
                    if (sameItemInSameGroupandSupplier != null)
                    {
                        rowErrors.Add(new Message(MessageType.Error, Resources.PRD.MultiSupplyItem.MultiSupplyItem_Import_ItemOnlyBelongOneGroupDiffSupplier, colCount.ToString(), Item, sameItemInSameGroupandSupplier.GroupNo, sameItemInSameGroupandSupplier.Supplier));
                    }
                }

                if (rowErrors.Count > 0)
                {
                    errorMessage.AddMessages(rowErrors);
                }
                else
                {
                    var msi = new MultiSupplyItem
                                              {
                                                  GroupNo = GroupNo,
                                                  Supplier = Supplier,
                                                  Item = ItemInstance.Code,
                                                  ItemDescription = ItemInstance.Description,
                                                  SubstituteGroup = substituteGroup,
                                              };
                    exactMultiSupplyItem.Add(msi);

                    if (exactMultiSupplySupplier.FirstOrDefault(m => m.GroupNo.ToUpper() == GroupNo && m.Supplier.ToUpper() == Supplier) == null)
                    {
                        var suppliers = this.genericMgr.FindAll<MultiSupplySupplier>(duiplicateSupplierVerifyStatement,
                                                                                     new object[] { GroupNo, Supplier });
                        if (suppliers == null || suppliers.Count() == 0)
                        {
                            var mss = new MultiSupplySupplier
                                                          {
                                                              GroupNo = GroupNo,
                                                              Supplier = Supplier,
                                                              CycleQty = cy,
                                                              IsActive = true,
                                                              Proportion = proportion
                                                          };
                            exactMultiSupplySupplier.Add(mss);
                        }
                    }

                    if (exactMultiSupplyGroup.FirstOrDefault(m => m.GroupNo.ToUpper() == GroupNo) == null)
                    {
                        var msgGroupNo = this.genericMgr.FindAll<MultiSupplyGroup>(duiplicateVerifyStatement, GroupNo);
                        if (msgGroupNo == null || msgGroupNo.Count() == 0)
                        {
                            var msg = new MultiSupplyGroup { GroupNo = GroupNo, Description = description };
                            exactMultiSupplyGroup.Add(msg);
                        }
                    }
                }

                #endregion
            }

            foreach (MultiSupplyGroup msGroup in exactMultiSupplyGroup)
            {
                IList<MultiSupplySupplier> msSupplierList = exactMultiSupplySupplier.Where(p => p.GroupNo.ToUpper() == msGroup.GroupNo.ToUpper()).ToList();
                if (msSupplierList.Count <= 1)
                {
                    errorMessage.AddMessage("导入的数据中,多轨组{0},必须包含大于1个供应商才可以创建。", msGroup.GroupNo);
                }
            }

            if (errorMessage.HasMessage)
            {
                throw errorMessage;
            }

            #region SaveData
            foreach (MultiSupplyGroup msGroup in exactMultiSupplyGroup)
            {
                this.genericMgr.Create(msGroup);

                IList<MultiSupplySupplier> msSupplierList = exactMultiSupplySupplier.Where(p => p.GroupNo.ToUpper() == msGroup.GroupNo.ToUpper()).ToList();

                int count = 1;
                foreach (MultiSupplySupplier msSupplier in msSupplierList)
                {
                    msSupplier.Seq = count;
                    this.genericMgr.Create(msSupplier);
                    count++;
                    exactMultiSupplySupplier.Remove(msSupplier);
                }
            }

            if (exactMultiSupplySupplier.Count > 0)
            {
                var groups = exactMultiSupplySupplier.Select(c => c.GroupNo.ToUpper()).Distinct();
                foreach (var group in groups)
                {
                    var dbbMaxSeq = this.genericMgr.FindAll(selectMaxSupplierSeq, group)[0];
                    int maxSeq;
                    int.TryParse((dbbMaxSeq != null ? dbbMaxSeq.ToString() : "0"), out maxSeq);

                    foreach (MultiSupplySupplier msSupplier in exactMultiSupplySupplier.Where(c => c.GroupNo.ToUpper() == group))
                    {
                        maxSeq++;
                        msSupplier.Seq = maxSeq;
                        this.genericMgr.Create(msSupplier);
                    }
                }
            }

            foreach (MultiSupplyItem msItem in exactMultiSupplyItem)
            {
                this.genericMgr.Create(msItem);
            }
            #endregion
            
            return Resources.Global.ImportSuccess_BatchImportSuccessful;
        }