public MaterialPageBySearchList FindMaterialPageBySearchListByQDT(int PageIndex, int PageSize, string Keyword, string MatBrand, Guid CatID, string IsStatUs)
        {
            var query = (from M in db.Material
                         where true
                         select M).AsQueryable();

            //品牌判断
            if (!string.IsNullOrEmpty(MatBrand))
            {
                query = query.Where(m => m.MatBrand.Contains(MatBrand)).AsQueryable();
            }

            ICategory ICat = new CategoryBase();
            CatTree CT = ICat.GetCatTreeByQDT();

            //分类判断
            if (CatID != Guid.Empty)
            {
                if (CT.SecondCatTree.Where(c => c.CatID == CatID).Count() > 0)
                {
                    query = query.Where(c => c.LinkQDTCatID == CatID).AsQueryable();
                }
            }

            //发布 新品 置顶判断
            if (IsStatUs == MatISNo.IsPublic.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.GroupToMatID == Guid.Empty).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsNotPublic.ToString())
            {
                query = query.Where(x => x.IsPublic == 0 && x.GroupToMatID == Guid.Empty).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsTop.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsTop == 1 && x.GroupToMatID == Guid.Empty).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsNotTop.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsTop == 0 && x.GroupToMatID == Guid.Empty).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsNew.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsNew == 1 && x.GroupToMatID == Guid.Empty).AsQueryable();

            }
            else if (IsStatUs == MatISNo.IsNotNew.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsNew == 0 && x.GroupToMatID == Guid.Empty).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsPromotion.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsPromotion == 1 && x.IsGroupMat <= 0).AsQueryable();
            }
            else if (IsStatUs == MatISNo.IsNotPromotion.ToString())
            {
                query = query.Where(x => x.IsPublic == 1 && x.IsPromotion == 0 && x.IsGroupMat <= 0).AsQueryable();
            }

            if (!string.IsNullOrEmpty(Keyword))
            {
                Keyword = Regex.Replace(Keyword, @"( )\1+", "$1", RegexOptions.None);
                string[] KeywordStr = Keyword.Split(' ');

                foreach (var K in KeywordStr)
                {
                    if (!string.IsNullOrEmpty(K))
                    {
                        query = query.Where(
                           m => m.MatSn.Contains(K) ||
                           m.MatBrand.Contains(K) ||
                           m.MatManufacturerSn.Contains(K) ||
                           m.MatSpecifications.Contains(K) ||
                           m.MatName.Contains(K) ||
                           m.KeywordIndex.Contains(K)
                           ).AsQueryable();
                    }
                }
            }

            //获取以过滤的品牌项
            var queryBrand = query.Where(x => x.MatBrandID != Guid.Empty).GroupBy(x => x.MatBrandID).Select(x => x.Key).AsQueryable();
            List<P_Brand> PBL = new List<P_Brand>();
            P_Brand PB = new P_Brand();
            Brand B = new Brand();

            foreach (var x in queryBrand)
            {
                B = db.Brand.Find(x);
                if (B != null)
                {
                    PB = new P_Brand();
                    PB.BID = B.BID;
                    PB.BrandName = B.BrandName;
                    PB.BrandEnName = B.BrandNameEn;
                    PB.BrandLogo = B.BrandLogo;
                    PBL.Add(PB);
                }else{
                    PB = new P_Brand();
                    PB.BID = Guid.Empty;
                    PB.BrandName = string.Empty;
                    PB.BrandEnName = string.Empty;
                    PB.BrandLogo = string.Empty;
                    PBL.Add(PB);
                }
            }

            List<P_Brand> PBLNew = new List<P_Brand>();
            var QueryBrandNew = from x in PBL
                                where true
                                &&
                                x.BID != Guid.Empty
                                &&
                                x.BrandLogo != string.Empty
                                &&
                                x.BrandName != string.Empty
                                group x by x.BrandName into g
                                select new {
                                    BID = g.FirstOrDefault().BID,
                                    BrandLogo = g.FirstOrDefault().BrandLogo,
                                    BrandName = g.FirstOrDefault().BrandName,
                                    BrandEnName = g.FirstOrDefault().BrandEnName,
                                };
            P_Brand NewB = new P_Brand();
               foreach(var x in QueryBrandNew)
               {
               NewB = new P_Brand();
               NewB.BID = x.BID;
               NewB.BrandLogo = x.BrandLogo;
               NewB.BrandEnName = x.BrandEnName;
               NewB.BrandName = x.BrandName;
               PBLNew.Add(NewB);
               }

            //获取以过滤分类项
            var queryCat = from x in query
                           where true
                           &&
                           x.CategoryID != Guid.Empty
                           &&
                           x.LinkQDTCatID != Guid.Empty
                           group x by x.LinkQDTCatID into g
                           select new
                           {
                               CID = g.Key,
                               Count = g.Count()
                           };

            List<P_Cat> PCL = new List<P_Cat>();
            P_Cat PC = new P_Cat();
            Category Cat = new Category();

            foreach (var x in queryCat)
            {
                PC = new P_Cat();
                if (CT.SecondCatTree.Where(c => c.CatID == x.CID).Count() > 0)
                {
                    PC.CatID = x.CID;
                    PC.CatCount = x.Count;
                    PC.CatName = CT.SecondCatTree.Where(c => c.CatID == x.CID).FirstOrDefault().CategoryName;
                    PCL.Add(PC);
                }
            }

            List<Material> ML = query.OrderByDescending(s => s.LastUpdateTime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            List<Material> SubGroupML = new List<Material>();

            IBrand IB = new BrandBase();
            string MaxPrice = string.Empty;
            string MinPrice = string.Empty;
            foreach (var x in ML)
            {
                x.MoreDetail = string.Empty;
                if (x.IsGroupMat > 0)
                {
                    SubGroupML = this.GetMaterialByGroupMatID(Guid.Empty, x.MatID);
                    x.LinkGroupMatCount = SubGroupML.Count();
                    try
                    {
                        MaxPrice = SubGroupML.Where(c => c.PriceInfo.SalesPrice > 0).Max(c => c.PriceInfo.SalesPrice).ToString("N");
                        MinPrice = SubGroupML.Where(c => c.PriceInfo.SalesPrice > 0).Min(c => c.PriceInfo.SalesPrice).ToString("N");
                        if (MaxPrice != MinPrice)
                        {
                            x.LinkGroupPrice = MinPrice + " ~ " + MaxPrice;
                        }
                        else
                        {
                            x.LinkGroupPrice = MinPrice;
                        }
                    }
                    catch
                    {
                        x.LinkGroupPrice = string.Empty;
                    }
                }
            }

            ML = IB.GetMatSalesPriceInfoList(ML, Guid.Empty);

            MaterialPageBySearchList MP = new MaterialPageBySearchList();
            MP.PageIndex = PageIndex;
            MP.PageSize = PageSize;
            MP.TotalRecord = query.Count();
            MP.Rows = ML;
            MP.Brand = PBLNew;
            MP.Cat = PCL;
            return MP;
        }
        //获取未被克隆的品牌列表
        public List<BrandByMainCom> GetBrandByMainComList(int PageIndex, int PageSize, out int TotalRecord, string Keyword)
        {
            //获取所有未被克隆的品牌
            var query = (from x in db.Material
                         where true
                         &&
                         Keyword == string.Empty ? true : x.MatBrand.Contains(Keyword)
                         &&
                         x.MatBrandID != Guid.Empty
                         &&
                         x.IsClone <= 0
                         group x by x.MatBrandID into g
                         select new {
                             BrandID = g.Key,
                             ProCount = g.Count(),
                             ProIsPriceCount = g.Where(c=>c.CostPrice > 0).Count()
                         }).AsQueryable();

            TotalRecord = query.Count();

            List<BrandByMainCom> BML = new List<BrandByMainCom>();
            BrandByMainCom BM = new BrandByMainCom();
            Brand B = new Brand();
            foreach (var x in query.OrderByDescending(s => s.ProCount).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList())
            {
                B = db.Brand.Find(x.BrandID);

                if (B != null)
                {
                    BM = new BrandByMainCom();
                    BM.BID = B.BID;
                    BM.BrandName = B.BrandName;
                    BM.BrandType = B.BrandType;
                    BM.ProCount = x.ProCount;
                    BM.ProIsPriceCount = x.ProIsPriceCount;
                    BM.FromType = "分销商";
                    BM.LinkMainCID = B.LinkMainCID;
                    BM.LinkMainComName = string.Empty;
                    BML.Add(BM);
                }
            }

            MainCompany MCom = new MainCompany();
            foreach(var x in BML.GroupBy(x=>x.LinkMainCID).Select(x=>x.Key))
            {
                MCom = db.MainCompany.Find(x);
                if(MCom != null)
                {
                    foreach (var xx in BML.Where(c => c.LinkMainCID == x).ToList())
                    {
                        xx.LinkMainComName = MCom.MainCompanyName;
                    }
                }
            }
            return BML;
        }
        public MaterialPageBySearchList FindMaterialPageByIsGroupSearchList(int PageIndex, int PageSize, string Keyword, Guid LinkMainCID)
        {
            var query = (from M in db.Material
                         where true
                         &&
                         M.LinkMainCID.Equals(LinkMainCID)
                         &&
                         M.IsGroupMat == 1
                         select M).AsQueryable();

            if (!string.IsNullOrEmpty(Keyword))
            {
                Keyword = Regex.Replace(Keyword, @"( )\1+", "$1", RegexOptions.None);
                string[] KeywordStr = Keyword.Split(' ');

                foreach (var K in KeywordStr)
                {
                    if (!string.IsNullOrEmpty(K))
                    {
                        query = query.Where(
                           m => m.MatSn.Contains(K) ||
                           m.MatBrand.Contains(K) ||
                           m.MatManufacturerSn.Contains(K) ||
                           m.MatSpecifications.Contains(K) ||
                           m.MatName.Contains(K) ||
                           m.KeywordIndex.Contains(K)
                           ).AsQueryable();
                    }
                }
            }

            //获取以过滤的品牌项
            var queryBrand = query.Where(x => x.MatBrandID != Guid.Empty).GroupBy(x => x.MatBrandID).Select(x => x.Key).AsQueryable();
            List<P_Brand> PBL = new List<P_Brand>();
            P_Brand PB = new P_Brand();
            Brand B = new Brand();

            foreach (var x in queryBrand)
            {
                B = db.Brand.Find(x);
                if (B != null)
                {
                    PB = new P_Brand();
                    PB.BID = B.BID;
                    PB.BrandName = B.BrandName;
                    PB.BrandEnName = B.BrandNameEn;
                    PB.BrandLogo = B.BrandLogo;
                    PBL.Add(PB);
                }
            }

            //获取以过滤分类项
            var queryCat = from x in query
                           where true
                           &&
                           x.CategoryID != Guid.Empty
                           group x by x.CategoryID into g
                           select new
                           {
                               CID = g.Key,
                               Count = g.Count()
                           };

            ICategory IC = new CategoryBase();
            CatTree CT = IC.GetCatTree(LinkMainCID);
            List<P_Cat> PCL = new List<P_Cat>();
            P_Cat PC = new P_Cat();
            Category Cat = new Category();

            foreach (var x in queryCat)
            {
                PC = new P_Cat();
                if (CT.TopCatTree.Where(c => c.CatID == x.CID).Count() > 0)
                {
                    PC.CatID = x.CID;
                    PC.CatCount = x.Count;
                    PC.CatName = CT.TopCatTree.Where(c => c.CatID == x.CID).FirstOrDefault().CategoryName;
                    PCL.Add(PC);
                }
                else if (CT.SecondCatTree.Where(c => c.CatID == x.CID).Count() > 0)
                {
                    PC.CatID = x.CID;
                    PC.CatCount = x.Count;
                    PC.CatName = CT.SecondCatTree.Where(c => c.CatID == x.CID).FirstOrDefault().CategoryName;
                    PCL.Add(PC);
                }
                else if (CT.EndCatTree.Where(c => c.CatID == x.CID).Count() > 0)
                {
                    PC.CatID = x.CID;
                    PC.CatCount = x.Count;
                    PC.CatName = CT.EndCatTree.Where(c => c.CatID == x.CID).FirstOrDefault().CategoryName;
                    PCL.Add(PC);
                }
            }

            List<Material> ML = query.OrderByDescending(s => s.LastUpdateTime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            foreach (var x in ML)
            {
                x.MoreDetail = string.Empty;
                x.LinkGroupMatCount = db.Material.Where(c => c.GroupToMatID == x.MatID).Count();
            }

            MaterialPageBySearchList MP = new MaterialPageBySearchList();
            MP.PageIndex = PageIndex;
            MP.PageSize = PageSize;
            MP.TotalRecord = query.Count();
            MP.Rows = ML;
            MP.Brand = PBL;
            MP.Cat = PCL;

            return MP;
        }
        public int CloneMaterialToNewMainCom(Guid SourceMainCID, List<Guid> SourceBrandID, Guid ToMainCID)
        {
            if (SourceMainCID == Guid.Empty || ToMainCID == Guid.Empty)
            {
                throw new Exception("错误 - 经营主体不能为空!");
            }

            if (SourceMainCID == ToMainCID)
            {
                throw new Exception("错误 - 相同的经营主体!");
            }

            if (SourceBrandID.Count() <= 0)
            {
                throw new Exception("错误 - 未选择品牌!");
            }

            Brand S_Brand = new Brand();
            Brand New_Brand = new Brand();
            List<Brand> S_BList = new List<Brand>();

            //进行品牌的创建
            foreach (var BID in SourceBrandID)
            {
                S_Brand = db.Brand.Find(BID);
                S_BList.Add(S_Brand);
                if (db.Brand.Where(x => x.BrandName == S_Brand.BrandName && x.BrandType == S_Brand.BrandType && x.LinkMainCID == ToMainCID).Count() <= 0)
                {
                    New_Brand = new Brand();
                    New_Brand.BID = Guid.NewGuid();
                    New_Brand.BrandName = S_Brand.BrandName;
                    New_Brand.BrandNameEn = S_Brand.BrandNameEn;
                    New_Brand.BrandLogo = string.Empty;
                    New_Brand.BrandDes = S_Brand.BrandDes;
                    New_Brand.LinkMainCID = ToMainCID;
                    New_Brand.BrandType = S_Brand.BrandType;
                    New_Brand.BrandMyLevelJson = S_Brand.BrandMyLevelJson;
                    New_Brand.LinkSupID = Guid.Empty;
                    New_Brand.CertificateImg = string.Empty;
                    New_Brand.ShowMarkPrice = S_Brand.ShowMarkPrice;
                    db.Brand.Add(New_Brand);
                    db.SaveChanges();
                }
            }

            //获取新经营主体品牌列表
            List<Brand> NewBrandList = db.Brand.Where(x => x.LinkMainCID == ToMainCID).ToList();

            //进行产品的批量克隆
            Guid NewBID = Guid.Empty;
            int Count = 0;
            foreach (var B in S_BList)
            {
                //循环品牌对应源经营主体产品列表
                NewBID = NewBrandList.Where(x => x.BrandName == B.BrandName && x.BrandType == B.BrandType).FirstOrDefault().BID;
                foreach (var m in db.Material.Where(x => x.LinkMainCID == SourceMainCID && x.MatBrandID == B.BID && x.IsPublic == 1).ToList())
                {
                    Count = Count + this.CloneMatItem(m, NewBID, ToMainCID);
                }
            }
            return Count;
        }