iOS小技能:SKU视图搭建

3,309 阅读3分钟

“我正在参加「掘金·启航计划」”

引言

最小库存管理单元(Stock Keeping Unit, SKU)是一个会计学名词,定义为库存管理中的最小可用单元。 对于一种商品而言,当他的品牌、型号、配置、花色、容量、生产日期、保质期、用途、价格、产地等属性与其他商品存在不同时,就是一个不同的最小存货单元。

SKU 就是商品在规格上的一种组合,比如油焖大虾有微辣小份, 也有不辣中份 ,不同的组合就是不同的SKU,把一组满足条件的属性叫做条件式 。

商品规格  
款式 : F    M
颜色 : R    G    B
尺寸 : L    X    S    

SKU
M,G,X  -  36元,20件
F,G,S  -  38元,22件
F,R,X  -  39元,35件  

I SKU商品规格组合算法

SKU 组合算法:对商品规格组合的筛选过滤。根据已选中的一个或多个属性过滤出剩余属性的可选性,以及选完所有属性之后对应的结果信息(库存、价格等)

  1. 根据已选中的一个或多个属性过滤出剩余属性的可选性
@interface SKUDataFilter : NSObject

@property (nonatomic, assign) id<SKUDataFilterDataSource> dataSource;

//当前 选中的属性indexPath
@property (nonatomic, strong, readonly) NSArray <NSIndexPath *> *selectedIndexPaths;
//当前 可选的属性indexPath
@property (nonatomic, strong, readonly) NSSet <NSIndexPath *> *availableIndexPathsSet;

  1. 根据选中的所有属性查询对应的结果(库存、价格等)
//条件式 对应的 结果数据(库存、价格等)
- (id)filter:(SKUDataFilter *)filter resultOfConditionForRow:(NSInteger)row;

为规格属性加一个坐标(属性ID),记录他们的位置

    0    1    2
0   F    M
1   R    G    B
2   L    X    S    

SKU:  用下标(属性ID)表示条件式
M,G,X  -  26元,30件 --- (111)
F,G,S  -  28元,32件 --- (012)
F,R,X  -  29元,45件 --- (001)

SKUDataFilterDataSource代理方法: 判断某个属性是否存在于某个条件式中

//属性种类个数
- (NSInteger)numberOfSectionsForPropertiesInFilter:(SKUDataFilter *)filter;

/*
 * 对应的条件式
 * 这里条件式的属性值,需要和propertiesInSection里面的数据类型保持一致
 */
- (NSArray *)filter:(SKUDataFilter *)filter conditionForRow:(NSInteger)row;


//满足条件 的 个数
- (NSInteger)numberOfConditionsInFilter:(SKUDataFilter *)filter;
/*
 * 每个种类所有的的属性值
 * 这里不关心具体的值,可以是属性ID, 属性名,字典、model
 */
- (NSArray *)filter:(SKUDataFilter *)filter propertiesInSection:(NSInteger)section;


//检查数据是否正确
- (BOOL)checkConformToSkuConditions:(NSArray *)conditions {
    if (conditions.count != [_dataSource numberOfSectionsForPropertiesInFilter:self]) {
        return NO;
    }
    
    __block BOOL  flag = YES;
    [conditions enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *properties = [_dataSource filter:self propertiesInSection:idx];
        if (![properties containsObject:obj]) {
            flag = NO;
            *stop = YES;
        }
    }];
    return flag;
}

II 相关问题

2.1 demo

获取demo请关注关注公号:iOS逆向

2.2 数据问题

问题:规格数据和条件式的规格ID数据顺序不一致,导致数据检测不正确checkConformToSkuConditions

例子:对应式的颜色ID在第一个位置,但是规格数据的颜色在第二个位置。所以导致判断对应式完整性的时候,从尺码数据组中寻找颜色ID。

对应式:"skuPropertyValId" : "1149583675277578240,1163819789202886656",

规格数据:productProductSpecifications

        "productProductSpecifications" : [
          {
            "id" : "1163389120697995264",
            "name" : "女装尺码",
            "anotherName" : "尺码",
            "specificationVals" : [
              {
                "value" : null,
                "id" : "1163819789173526528",
                "specificationId" : "1163389120697995264",
                "type" : 1,
                "name" : "XXS"
              },
              {
                "value" : null,
                "id" : "1163819789202886656",
                "specificationId" : "1163389120697995264",
                "type" : 1,
                "name" : "L"
              },
              {
                "value" : null,
                "id" : "1163819789286772736",
                "specificationId" : "1163389120697995264",
                "type" : 1,
                "name" : "5XL"
              }
            ]
          },
          {
            "id" : "0",
            "name" : "颜色",
            "anotherName" : "颜色",
            "specificationVals" : [
              {
                "value" : "#FFFFFF",
                "id" : "1149583675277578240",
                "specificationId" : "0",
                "type" : 2,
                "name" : "白色"
              },
              {
                "value" : "#E7E7E7",
                "id" : "1149583822787055616",
                "specificationId" : "0",
                "type" : 2,
                "name" : "米白"
              }
            ]
          }
        ],