zoukankan      html  css  js  c++  java
  • 使用笛卡尔积生成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; }
        }
  • 相关阅读:
    实验0 了解和熟悉操作系统
    软件工程感想
    递归下降分析法--算数语法分析
    有限自动机的构造与识别
    评论
    文法解释
    【编译CEF3】2017-07 添加支持mp3 mp4的编译日记
    Visual Studio 2015/2013安装失败:Microsoft Visual Studio 2015 Shell (Minimum) Interop Assemblies 安装时发生严重错误
    [RAD Studio 10.2 Tokyo] Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major错误解决
    Delphi 编译出来的程序被小红伞报病毒 TR/Spy.Banker.Gen4 [trojan]
  • 原文地址:https://www.cnblogs.com/amber-L/p/7921289.html
Copyright © 2011-2022 走看看