多规格商品库存展示 算法(一)

场景

我们平时在网上购物时,经常会遇到一个商品有多种规格,没种规格中又有多个规格值,然后选择每种型号时,动态的置灰展示有没有库存.
我找了个样例场景:
如下:


作图-1

上图就有4中规格:<颜色><尺码><购买方式><配送方式>
每个规格种有不同的规格值.
我们先只看<颜色>和<尺码>的选择联动

    1. 选<灰色>
      尺码只能选 160,165, 180
    1. 选<黑色>
      尺码只能选 160, 165, 170, 175, 180
  • 3.选藏青色
    尺码只能选 165, 170, 175, 180

我们平时点外卖应该也常遇到选规格这种场景.(大杯,中杯,特大杯;原味,微辣,麻辣)

大家想过这种联动怎么搞的吗

数据设计

设计

这里准备测试数据,3种规格,每种规格内有3个规格值
web页面大概这样的


编组.png

明细这肯定是这样的:


编组1.png

27条明细,(3 x 3 x 3 = 27)

数据设计

image.png
image.png

Python 代码

class Spec(object):
    def __init__(self, id, name):
        self.id = int(id)
        self.name = name


class SpecValue(object):
    def __init__(self, id: int, spec_id: int, spec_name, value):
        self.id = id
        self.spec_id = spec_id
        self.spec_name = spec_name
        self.value = value
class SpecStock(object):
    def __init__(self, id, spec_value_ids, spec_values, stock):
        self.id = int(id)
        self.spec_value_ids = spec_value_ids   # value_id 逗号拼接 > 1,4,7
        self.spec_values = spec_values         # value值 '/' 拼接  > Air/128/黄色
        self.stock = stock                     # 库存值
        # 冗余几个字段,为后面逻辑校验时使用
        id_list_tmp = spec_value_ids.split(',')
        self.id_list = []   # list[1,4,7]
        for x in id_list_tmp:
            self.id_list.append(int(x))
        self.id_set = set(self.id_list)  # set(1,4,5)

    def print(self):
        s = "%d - (%s) - (%s) - 库存:%d" % (self.id, self.spec_value_ids, self.spec_values, self.stock)
        print(s)

生成数据


spec1 = Spec(1, "型号")
spec2 = Spec(2, "大小")
spec3 = Spec(3, "颜色")

spv1 = SpecValue(1, spec1.id, spec1.name, "Air")
spv2 = SpecValue(2, spec1.id, spec1.name, "Pro")
spv3 = SpecValue(3, spec1.id, spec1.name, "Max")

spv4 = SpecValue(4, spec2.id, spec2.name, "128")
spv5 = SpecValue(5, spec2.id, spec2.name, "256")
spv6 = SpecValue(6, spec2.id, spec2.name, "512")

spv7 = SpecValue(7, spec3.id, spec3.name, "黄色")
spv8 = SpecValue(8, spec3.id, spec3.name, "橘色")
spv9 = SpecValue(9, spec3.id, spec3.name, "绿色")

spv_llist = [
    [spv1, spv2, spv3],
    [spv4, spv5, spv6],
    [spv7, spv8, spv9]
]

value_list = []
id_list = []
i = 0
while i < len(spv_llist):
    value_list = [spv.value for spv in spv_llist[i]]
    value_list = make_array(value_list, value_list, "/")

    id_list_tmp = [spv.id for spv in spv_llist[i]]
    id_list = make_array(id_list, id_list_tmp, ",")

    i = i + 1

# 库存 数组
# stock_list = [x for x in range(len(value_list))]
# print(stock_list)
stock_list = [
    0, 1, 2, 3, 4, 5, 6, 7, 8,
    9, 10, 11, 12, 13, 14, 15, 16, 17,
    18, 19, 20, 21, 22, 23, 24, 25, 26
]

spec_stock_list = []
for i in range(len(value_list)):
    spec_stock = SpecStock(i + 1, id_list[i], value_list[i], stock_list[i])
    spec_stock_list.append(spec_stock)

print("构建数据\n")
for x in spec_stock_list:
    x.print()

生成的数据就这样:

构建数据

1 - (1,4,7) - (Air/128/黄色) - 库存:0
2 - (1,4,8) - (Air/128/橘色) - 库存:1
3 - (1,4,9) - (Air/128/绿色) - 库存:2
4 - (1,5,7) - (Air/256/黄色) - 库存:3
5 - (1,5,8) - (Air/256/橘色) - 库存:4
6 - (1,5,9) - (Air/256/绿色) - 库存:5
7 - (1,6,7) - (Air/512/黄色) - 库存:6
8 - (1,6,8) - (Air/512/橘色) - 库存:7
9 - (1,6,9) - (Air/512/绿色) - 库存:8
10 - (2,4,7) - (Pro/128/黄色) - 库存:9
11 - (2,4,8) - (Pro/128/橘色) - 库存:10
12 - (2,4,9) - (Pro/128/绿色) - 库存:11
13 - (2,5,7) - (Pro/256/黄色) - 库存:12
14 - (2,5,8) - (Pro/256/橘色) - 库存:13
15 - (2,5,9) - (Pro/256/绿色) - 库存:14
16 - (2,6,7) - (Pro/512/黄色) - 库存:15
17 - (2,6,8) - (Pro/512/橘色) - 库存:16
18 - (2,6,9) - (Pro/512/绿色) - 库存:17
19 - (3,4,7) - (Max/128/黄色) - 库存:18
20 - (3,4,8) - (Max/128/橘色) - 库存:19
21 - (3,4,9) - (Max/128/绿色) - 库存:20
22 - (3,5,7) - (Max/256/黄色) - 库存:21
23 - (3,5,8) - (Max/256/橘色) - 库存:22
24 - (3,5,9) - (Max/256/绿色) - 库存:23
25 - (3,6,7) - (Max/512/黄色) - 库存:24
26 - (3,6,8) - (Max/512/橘色) - 库存:25
27 - (3,6,9) - (Max/512/绿色) - 库存:26

待续(二)

你可能感兴趣的:(多规格商品库存展示 算法(一))