电商系统sku设计

1.sku含义

sku :(stock keeping unit)是库存保存单位。即库存进出量的单位,可以是件,盒等为单位,在使用时候根据不同的业务状态,不同的管理模式来处理。在服装,鞋类使用最普遍
如图所示:
电商系统sku设计_第1张图片

2.数据库设计

#规格属性表(对应上面的颜色尺码属性 例如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`)
);

电商系统sku设计_第2张图片

#商品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`)
) ;

电商系统sku设计_第3张图片

#商品表
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`)
);

电商系统sku设计_第4张图片

3.添加商品sku操作逻辑

电商系统sku设计_第5张图片

数据格式如图所示:电商系统sku设计_第6张图片

4.添加代码

//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.页面

接口返回的api数据格式

{
    "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"
        }
    }
}

你可能感兴趣的:(tp5常用功能)