Goods.php 4.1 KB
<?php

namespace app\admin\model;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'issale_text',
        'ismember_text',
        'ishot_text',
        'ismake_text',
        'spec_type_text'
    ];
    
    /**
     * 关联商品规格表
     */
    public function spec()
    {
        return $this->hasMany('GoodsSpec');
    }

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

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getIssaleList()
    {
        return ['0' => __('Issale 0'), '1' => __('Issale 1')];
    }

    public function getIsmemberList()
    {
        return ['0' => __('Ismember 0'), '1' => __('Ismember 1')];
    }

    public function getIshotList()
    {
        return ['0' => __('Ishot 0'), '1' => __('Ishot 1')];
    }

    public function getIsmakeList()
    {
        return ['0' => __('Ismake 0'), '1' => __('Ismake 1')];
    }

    public function getSpecTypeList()
    {
        return ['1' => __('Spec_type 1'), '2' => __('Spec_type 2')];
    }


    public function getIssaleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['issale']) ? $data['issale'] : '');
        $list = $this->getIssaleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsmemberTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ismember']) ? $data['ismember'] : '');
        $list = $this->getIsmemberList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIshotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ishot']) ? $data['ishot'] : '');
        $list = $this->getIshotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsmakeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ismake']) ? $data['ismake'] : '');
        $list = $this->getIsmakeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSpecTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
        $list = $this->getSpecTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 获取规格信息
     */
    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];
    }

}