Goods.php 4.0 KB
<?php

namespace app\common\model;

use think\Model;

class Goods extends Model
{
	/**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'createtime',
        'updatetime'
    ];

    protected $append = [
        'goods_image',
        'goods_price'
    ];

    /**
     * 关联商品规格表
     */
    public function spec()
    {
        return $this->hasMany('GoodsSpec');
    }

    /**
     * 关联商品规格关系表
     */
    public function specRel()
    {
        return $this->belongsToMany('SpecValue', 'GoodsSpecRel');
    }

    /**
     * 商品单图
     */
    public function getGoodsImageAttr($value, $data)
    {
        $goods_images = $this->getAttr('goods_images');
        return !empty($goods_images) ? $goods_images[0] : '';
    }

	/**
     * 商品图片
     */
    public function getGoodsImagesAttr($value, $data)
    {
    	$list = !empty($value) ? explode(',',$value) : [];
    	foreach($list as &$v){
    		$v = cdnurl($v,true);
    	}
    	return $list;
    }

    /**
     * 商品价格
     */
    public function getGoodsPriceAttr($value, $data)
    {
        return $this['spec'][0]['goods_price'];
    }

    /**
     * 买家秀
     */
    public function getBuyerImagesAttr($value, $data)
    {
    	$list = !empty($value) ? explode(',',$value) : [];
    	foreach($list as &$v){
    		$v = cdnurl($v,true);
    	}
    	return $list;
    }

    /**
     * 商品列表
     */
    public static function getList($where,$page,$rows)
    {
        $list = self::where('issale','1')
            ->where($where)
            ->order(['updatetime'=>'desc'])
            ->paginate($rows,false,['page'=>$page])
            ->each(function($v){
                $v->visible(['id','goods_image','goods_name','goods_price','ismake']);
            })->toArray();
        return ['total'=>$list['total'],'list'=>$list['data']];
    }

    /**
     * 获取规格信息
     */
    public function getManySpecData($spec_rel, $skuData)
    {
        // spec_attr
        $specAttrData = [];
        foreach ($spec_rel as $item) {
            if (!isset($specAttrData[$item['spec_id']])) {
                $specAttrData[$item['spec_id']] = [
                    'spec_id' => $item['spec']['id'],
                    'spec_name' => $item['spec']['spec_name'],
                    'spec_value_list' => [],
                ];
            }
            $specAttrData[$item['spec_id']]['spec_value_list'][] = [
                'spec_value_id' => $item['id'],
                'spec_value_name' => $item['spec_value_name'],
            ];
        }

        // spec_list
        $goods_spec_list = [];
        foreach ($skuData as $item) {
            $goods_spec_list[] = [
                'goods_spec_id' => $item['id'],
                'spec_sku_id' => $item['spec_sku_id'],
                'rows' => [],
                'form' => [
                    'goods_price' => $item['goods_price'],
                    'goods_weight' => $item['goods_weight'],
                    'stock_num' => $item['stock_num'],
                ],
            ];
        }
        return ['spec_attr' => array_values($specAttrData), 'goods_spec_list' => $goods_spec_list];
    }

    /**
     * 商品多规格信息
     */
    public function getGoodsSku($goods_sku_id)
    {
        $goodsSkuData = array_column(collection($this['spec'])->toArray(), null, 'spec_sku_id');
        if (!isset($goodsSkuData[$goods_sku_id])) {
            return false;
        }
        $goods_sku = $goodsSkuData[$goods_sku_id];
        // 多规格文字内容
        $goods_sku['goods_attr'] = '';
        if ($this['spec_type'] == '2') {
            $attrs = explode('_', $goods_sku['spec_sku_id']);
            $spec_rel = array_column(collection($this['spec_rel'])->toArray(), null, 'id');
            foreach ($attrs as $specValueId) {
                $goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':'
                    . $spec_rel[$specValueId]['spec_value_name'] . '; ';
            }
        }
        return $goods_sku;
    }
}