使用笛卡尔积生成sku

        /// <summary>
        /// 生成SKU价格表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Result<IList<SkuGroup>> GeneratePriceList(IList<SkuGroup> model)
        {
            try {
                var result = new List<SkuGroup>();
                var data = new SkuGroup();
                data.Items = new List<SkuItem>();
                //过滤掉无效的SkuGroup
                model = model.Where(x => x.Items != null && x.Items.Count > 0).ToList();
                Descartes(model, 0, result, data);
                return new Result<IList<SkuGroup>>(true, "操作成功!", result);
                //return result;
            }
            catch (Exception ex)
            {
                //LogException(ex);
                return new Result<IList<SkuGroup>>(false, "操作失败!");
            }
            
        }
        /// <summary>
        /// 笛卡尔积 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <param name="result"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public IList<SkuItem> Descartes(IList<SkuGroup> list,int count, IList<SkuGroup> result, SkuGroup data)
        {
            var temp = new List<SkuItem>();
            //获取当前SkuGroup
            var astr = new SkuGroup();
            astr = list[count];
            var group = new SkuGroup();
            group.Id = astr.Id;
            group.Name = astr.Name;
            group.Selected = astr.Selected;
            group.ItemType = astr.ItemType;
            //循环当前SkuGroup的Items
            foreach (var item in astr.Items)
            {
                item.Group = group;
                if (count + 1 < list.Count)
                {
                    //中转,以达成传输本层及上层的item
                    var transfer = new SkuGroup();
                    transfer.Items = new List<SkuItem>();
                    foreach (var i in data.Items)
                    {
                        transfer.Items.Add(i);
                    }
                    transfer.Items.Add(item);
                    Descartes(list, count + 1, result, transfer);
                }
                else
                {
                    var sku = new SkuGroup();
                    sku.Items = new List<SkuItem>();
                    //var items = new List<SkuItem>();
                    //存入上层的item
                    foreach (var i in data.Items)
                    {
                        sku.Items.Add(i);
                    }
                    //存入本次item
                    sku.Items.Add(item);
                    result.Add(sku);
                }
            }

            return temp;
        }
public class SkuGroup
    {
        public long Id { get; set; }
        public  string Name { get; set; }

        public  ICollection<SkuItem> Items { get; set; }

        public  bool Selected { get; set; }

        /// <summary>
        /// SkuItem数据表类名称
        /// </summary>
        /// <remarks>例:平台销售属性值为PlatformInfo,平台类目销售属性为PlatformIndustryInfo,版本销售属性为SkuItem</remarks>
        public string ItemType { get; set; }

        public Guid? CategoryId { get; set; }
    }
    public class SkuItem
    {
        public long Id { get; set; }

        public long GroupId { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// 选中
        /// </summary>
        public bool Selected { get; set; }

        public virtual SkuGroup Group { get; set; }
    }

 

posted @ 2017-11-29 16:34  爱上猫的红烧鱼  阅读(1431)  评论(0编辑  收藏  举报