Goods.php 1.8 KB
<?php


namespace app\api\model;


use addons\litestore\model\Litestoregoodsspec;
use think\Model;

class Goods extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    protected $name = 'litestore_goods';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $append = [
        'image_text',
        'images_text',
        'down_image_text',
        'four_image_text',
        'detail_images_text',
    ];

    public function getImageTextAttr($value, $data)
    {
        $value = !empty($data['image']) ? cdnurl($data['image'], true) : '';
        return $value;
    }

    public function getFourImageTextAttr($value, $data)
    {
        $value = !empty($data['four_images']) ? explode(',', $data['four_images']) : [];
        foreach ($value as $key => &$val) {
            $val = cdnurl($val, true);
        }
        return $value;
    }

    public function getDetailImageTextAttr($value, $data)
    {
        $value = !empty($data['detail_images']) ? explode(',', $data['detail_images']) : [];
        foreach ($value as $key => &$val) {
            $val = cdnurl($val, true);
        }
        return $value;
    }

    public function getImagesTextAttr($value, $data)
    {
        $value = !empty($data['images']) ? explode(',', $data['images']) : [];
        foreach ($value as $key => &$val) {
            $val = cdnurl($val, true);
        }
        return $value;
    }

    public function getDownImageTextAttr($value, $data)
    {
        $value = !empty($data['down_image']) ? cdnurl($data['down_image'], true) : '';
        return $value;
    }

    // 总库存
    public function getStockNumAttr($value, $data)
    {
        $value = GoodsSpec::where('goods_id', $data['goods_id'])->sum('stock_num');
        return $value;
    }
}