商城后台系统商品模型数据新增接口

商品模型是商品添加前的一个重要步骤,定义好商品模型具备哪些规格,这些规格具备哪些规格值,同时商品模型对应哪些属性,将这些数据入库保存后,在添加商品时:

可以选择要添加的商品属于哪种模型,随即会组装该商品对应的规格以及规格值,也就是SKU数据。

在添加商品模型数据时,会接收到:模型名称、规格名集合、规格值集合、属性集合,用户在添加时可能由于误操作导致规格名为空、规格名对应的一组规格值为空、个别规格值为空、属性名为空等等现象,需要在添加之前做过滤。数据添加一部分是单条数据入库,一部分是批量保存入库,所以更涉及到数组的转换。

数据库:类型表

 属性表

 规格表

 规格值表

 路由:

//列表
Route::get('index', 'Type/index');
//添加
Route::get('save', 'Type/save');

控制器:

<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Exception;
use think\facade\Db;
use think\Request;

class Type
{
    /**
     * 显示资源列表
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index($id)
    {
        $info = (new \app\admin\model\Type())->with(['attr','spec','spec.specvalue'])->find($id)->toArray();
        return success(200,'ok',$info);
    }

     /**
     * 添加
     * 
     * @throws Exception
     */
    public function save()
    {
        $data = input();
        //类型
        \app\admin\business\Type::check($data);
        return success(200,'ok','添加成功');
    }


}

业务层:

<?php

namespace app\admin\business;

use app\admin\model\Attribute;
use app\admin\model\Spec;
use app\admin\model\SpecValue;
use think\Exception;
use think\facade\Db;

class Type
{
    /**
     * @param $data
     * @throws Exception
     */
    public static function check($data){
        Db::startTrans();
        try {
            //类型表
            $type = \app\admin\model\Type::create([
                'type_name' => $data['type_name'],
                'create_time' => time()
            ]);
            $type_id = $type->id;

            //属性表
            $attrModel = new Attribute();
            $attrAdd = [];
            foreach($data['attr'] as $item){
                $attrAdd[] = [
                    'attr_name' => $item['attr_name'],
                    'type_id' => $type_id,
                    'attr_values' => $item['attr_values'],
                    'sort' => $item['sort'],
                    'create_time' => time()
                ];
            }
            //存入数据库
            $attrModel->saveAll($attrAdd);

            //规格表
            $specModel = new Spec();
            $specAdd = [];
            foreach($data['spec'] as $val){
                $specAdd[] = [
                    'spec_name' => $val['spec_name'],
                    'type_id' => $type_id,
                    'sort' => $val['sort'],
                    'create_time' => time()
                ];
            }
            //存入数据库
            $specResult = $specModel->saveAll($specAdd);

            //spec_value
            //存入规格表的数据
            $specInfo = $specResult->toArray();
//            dd($specInfo);
            //array_column() 返回输入数组中某个单一列的值。 
            $specInfo = array_column($specInfo,'id','spec_name');
//            dd($specInfo);
            //规格值
            $specvalueAdd = [];
            foreach($data['spec'] as $val){
                foreach ($val['specvalue'] as $value){
                    $specvalueAdd[] = [
                        'spec_id' => $specInfo[$val['spec_name']],
                        'spec_value' => $value,
                        'type_id' => $type_id,
                        'create_time' => time()
                    ];
                }
            }
            $specValueModel = new SpecValue();
            $specValueModel->saveAll($specvalueAdd);
            //提交事务
            Db::commit();
        }catch(\Exception $e){
            //回滚事务
            Db::rollback();
            throw new Exception($e->getMessage(),$e->getCode());
        }
    }
}

 这个是上面index列表展示模型关联

<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Type extends Model
{
    /**
     * 类型与属性的关系
     *
     * @return \think\model\relation\HasMany
     */
    public function attr(){
        return $this->hasMany(Attribute::class,'type_id','id');
    }

    /**
     *类型与规格名的关系
     *
     * @return \think\model\relation\HasMany
     */
    public function spec(){
        return $this->hasMany(Spec::class,'type_id','id');
    }

}

添加信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值