<?php

namespace app\common\model;

use think\Model;

class Spec extends Model
{
    /**
     * 正常显示规格
     */
    public static function specList($spec_template_id){
        // 一级规格组
        $list = self::getSpecList(['spec_template_id'=>$spec_template_id,'pid'=>0,'display_mode'=>'1']);
        return $list;
    }

    /**
     * 规格值下规格
     */
    public static function valueSpecList($spec_value_id)
    {
        //规格值下规格组ID集合
        $spec_ids = SpecValue::where('id',$spec_value_id)->value('spec_ids');
        // 一级规格组
        $list = self::where('id','in',$spec_ids)
            ->field('id,pid,spec_name,spec_type')
            ->order('weigh desc')
            ->select();
        foreach($list as &$v){
            // 规格组选项
            $v['options'] = self::getOptions($v['id']);
        }
        return $list;
    }

    /**
     * 身体信息
     */
    public static function bodyInfo($spec_template_id){
        // 一级规格组
        $list = self::getSpecList(['spec_template_id'=>$spec_template_id,'pid'=>0,'display_mode'=>'1']);
        return $list;
    }

    /**
     * 获取规格列表
     */
    public static function getSpecList($where){
        $list = self::where($where)
            ->order('weigh desc')
            ->select();
        foreach($list as $v){
            $v['spec_value_list'] = self::getSpecValueList($v['id']);
            $v['two_spec_list'] = self::getSpecList(['pid'=>$v['id']]);
            $v->visible(['id','spec_name','spec_type','display_mode'])->append(['spec_value_list','two_spec_list']);
        }
        $list = collection($list)->toArray();
        return $list;
    }

    /**
     * 获取规格值列表
     */
    public static function getSpecValueList($spec_id){
        $list = SpecValue::where('spec_id',$spec_id)
            ->order('weigh desc')
            ->select();
        foreach($list as $v){
            $v['spec_list'] = self::getSpecList(['id'=>['in',$v['spec_ids']]]);
            $v->visible(['id','spec_id','spec_value','spec_value_image','spec_value_description','spec_ids'])->append(['spec_list']);
        }
        $list = collection($list)->toArray();
        return $list;
    }
}