E-commerce system sku design

1.sku meaning

sku: (stock keeping unit) is the stock keeping unit. That is, the unit of inventory input and output, which can be pieces, boxes, etc., which are handled according to different business conditions and different management modes when used. In clothing, footwear is most commonly used
as shown in the figure:
Insert picture description here

2. Database design

#规格属性表(对应上面的颜色尺码属性 例如specs_id代表颜色,name表示红色,蓝色;specs_id代表尺码,name表示40,42)
CREATE TABLE `mall_specs_value` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `specs_id` int(10) unsigned NOT NULL COMMENT '规格ID  1:颜色 2:尺码 3:材质',
 `name` varchar(100) NOT NULL DEFAULT '' COMMENT '规格属性名',
 `create_time` int(10) unsigned NOT NULL DEFAULT '0',
 `update_time` int(10) unsigned NOT NULL DEFAULT '0',
 `operate_user` varchar(100) NOT NULL DEFAULT '',
 `status` tinyint(3) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 KEY `specs_id` (`specs_id`)
);

Insert picture description here

#商品sku表,(选中上图中的黑色42尺码的鞋子)
CREATE TABLE `mall_goods_sku` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `goods_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品Id',
 `specs_value_ids` varchar(255) NOT NULL COMMENT '每行规则属性ID 按逗号连接',
 `price` decimal(10,2) unsigned NOT NULL COMMENT '现价',
 `cost_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '原价',
 `stock` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '库存',
 `status` tinyint(1) NOT NULL DEFAULT '1',
 `create_time` int(10) unsigned NOT NULL DEFAULT '0',
 `update_time` int(10) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 KEY `goods_id` (`goods_id`)
) ;

The first row of data indicates that the color is white, the material is windows, the current price of product 1 with a size of 20 inches is 1 piece, the original price is 1 piece, and the stock is 1

#商品表
CREATE TABLE `mall_goods` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
 `title` varchar(255) NOT NULL DEFAULT '' COMMENT '商品标题',
 `category_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品分类',
 `category_path_id` varchar(20) NOT NULL DEFAULT '' COMMENT '栏目ID path',
 `promotion_title` varchar(255) NOT NULL DEFAULT '' COMMENT '商品促销语',
 `goods_unit` varchar(20) NOT NULL DEFAULT '' COMMENT '商品单位',
 `keywords` varchar(100) NOT NULL DEFAULT '' COMMENT '关键词',
 `sub_title` varchar(100) NOT NULL DEFAULT '' COMMENT '副标题',
 `stock` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '库存',
 `price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '现价',
 `cost_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '原价',
 `sku_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品默认的sku_id',
 `is_show_stock` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否显示库存',
 `production_time` varchar(10) NOT NULL DEFAULT '0' COMMENT '生产日期',
 `goods_specs_type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '商品规则 1统一,2多规格',
 `big_image` varchar(255) NOT NULL DEFAULT '' COMMENT '大图',
 `recommend_image` varchar(255) NOT NULL DEFAULT '' COMMENT '商品推荐图',
 `carousel_image` varchar(500) NOT NULL DEFAULT '' COMMENT '详情页轮播图',
 `description` text NOT NULL COMMENT '商品详情',
 `is_index_recommend` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否首页推荐大图商品',
 `goods_specs_data` varchar(255) NOT NULL DEFAULT '' COMMENT '所有规则属性存放json',
 `create_time` int(10) unsigned NOT NULL DEFAULT '0',
 `update_time` int(10) unsigned NOT NULL DEFAULT '0',
 `operate_user` varchar(255) NOT NULL DEFAULT '',
 `status` tinyint(3) unsigned NOT NULL DEFAULT '0',
 `listorder` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序字段',
 PRIMARY KEY (`id`),
 KEY `title` (`title`),
 KEY `category_path_id` (`category_path_id`)
);

Insert picture description here

3. Add commodity sku operation logic

Insert picture description here

The data format is shown in the figure:Insert picture description here

4. Add code

//good.php
public function insertData($data)
    {
    
    
        $this->model->startTrans();
        try{
    
    
            $goodsId =  $this->add($data);

            if (!$goodsId){
    
    
                return $goodsId;
            }
            //执行数据插入到sku表中
            //如果是统一规格
            if ($data['goods_specs_type'] ==1){
    
    
                $goodsSkuData = [
                  "goods_id"=>$goodsId
                ];
            }elseif ($data['goods_specs_type'] == 2){
    
    
               $goodsSkuBusiObj = new GoodSku();
               $data['goods_id'] = $goodsId;
                try {
    
    
                    $res = $goodsSkuBusiObj->saveAll($data);
                }catch (\Throwable $e){
    
    
                    dd($e->getMessage());
                }

               if (!empty($res))
               {
    
    

                   //总库存
                   $stock = array_sum(array_column($res,"stock"));

                   $goodsUpdateData  = [
                       "price"=>$res[0]['price'],
                       "cost_price"=>$res[0]['cost_price'],
                       "stock"=>$stock,
                       "sku_id"=>$res[0]['id']
                   ];
                   //执行完毕之后,更新主要表中的数据
                   $goodsRes = $this->model->updateById($goodsId,$goodsUpdateData);

                    if(!$goodsRes)
                    {
    
    
                        throw new Exception("insertData:goods主表更新失败");
                    }
               }else{
    
    
                   throw new Exception("sku表新增失败");
               }
            }
            // 事务提交
            $this->model->commit();
            return true;

        }catch (\Exception $e){
    
    

            $this->model->rollback();
            return show(0,$e->getMessage());
            return false;
        }
    }

//goodsku.php
 public function saveAll($data)
    {
    
    
        if (!$data['skus'])
        {
    
    
            return false;
        }

        foreach ($data['skus'] as $value)
        {
    
    
            $insertData[] = [
              "goods_id" => $data['goods_id'],
              "specs_value_ids" => $value['propvalnames']['propvalids'],
              "price" => $value['propvalnames']['skuSellPrice'],
              "cost_price"=> $value['propvalnames']['skuMarketPrice'],
              "stock" => $value['propvalnames']['skuStock'],
            ];
        }
        try{
    
    
            $result = $this->model->saveAll($insertData);
            return $result->toArray();
        }catch (\Exception $e){
    
    
            return false;
        }

        return true;

    }

    public function add($data)
    {
    
    
        $data['status'] = 1;
        try{
    
    
            $this->model->save($data);
        }catch (\Exception $e){
    
    
            return 0;
        }
        return $this->model->id;
    }

5. Page

API data format returned by the interface

{
    
    
    "status": 1,
    "message": "OK",
    "result": {
    
    
        "title": "Thinkpad E550联想电脑",
        "price": "1.00",
        "cost_price": "1.00",
        "sales_count": 0,
        "stock": 1,
        "gids": {
    
    
            "5,12,10": 1,
            "5,12,11": 2,
            "5,13,10": 3,
            "5,13,11": 4,
            "6,12,10": 5,
            "6,12,11": 6,
            "6,13,10": 7,
            "6,13,11": 8
        },
        "image": "http://www.tp6shop.com//storage/upload/20200927/285c86b5682aa680a4efc8f235657645.jpg",
        "sku": [
            {
    
    
                "name": "",
                "list": [
                    {
    
    
                        "id": "5",
                        "name": "白色",
                        "flag": 1
                    },
                    {
    
    
                        "id": "6",
                        "name": "黑色",
                        "flag": 0
                    }
                ]
            },
            {
    
    
                "name": "",
                "list": [
                    {
    
    
                        "id": "12",
                        "name": "windows系统",
                        "flag": 1
                    },
                    {
    
    
                        "id": "13",
                        "name": "linux系统",
                        "flag": 0
                    }
                ]
            },
            {
    
    
                "name": "",
                "list": [
                    {
    
    
                        "id": "10",
                        "name": "20英寸",
                        "flag": 1
                    },
                    {
    
    
                        "id": "11",
                        "name": "30英寸",
                        "flag": 0
                    }
                ]
            }
        ],
        "detail": {
    
    
            "d1": {
    
    
                "商品编码": 1,
                "上架时间": "2020-09-27 17:36:01"
            },
            "d2": "dsgfdsgsgsggdfghfdgdfg"
        }
    }
}

Guess you like

Origin blog.csdn.net/kevlin_V/article/details/108845046