<?php

// 文章分类模型
class parameterModel extends Model
{
    private $str;

    public function __construct()
    {
        parent::__construct("parameter");
    }

    public function getparameterByclassid($classid)
    {
        $sql = "SELECT *, u1 as name,classid as parentid  FROM {$this->table}  ";
        if ($classid != '') {
            $sql .= " where classid='$classid' ";
        }
        $sql .= " order by u4 asc,id asc";

        return $this->db->getAll($sql);
        //对获取的分类进行重新排序

    }

    //得到当前parameter_id到底层的json
    public function getparameterName($parameterId, $temp_arr)
    {

        $Currentparameter = $this->findBySql("select id as value, u1 as text from sl_parameter where id={$parameterId}");
        $Childrenparameter = $this->findBySql("select * from sl_parameter where classid={$Currentparameter[0]['value']}");
        if ($temp_arr == "") {
            $temp_arr = $Currentparameter;
        }
        if (count($Childrenparameter) > 0) {
            //该parameter_id的父级存在
            $temp_arr["children"] = $Childrenparameter;
            $this->getparameterNames($parentparameter[0]['value'], $temp_arr);
        } else {
            $this->str = $temp_arr;

        }
    }

    public function getparameterNames($curparameterId, $ChildparameterNames)
    {
        $this->getparameterName($curparameterId, $ChildparameterNames);
        return $this->str;
    }

    /**
     * 格式化数据
     * @date: 2019年6月13日 下午4:33:30
     * @author: 龚华尧
     * @param: variable
     * @return:
     */
    public function getparameterArrayByclassid($classid)
    {
        $sql = "SELECT *, u1 as name,classid as parentid  FROM {$this->table}  ";
        if ($classid != '') {
            $sql .= " where classid = '$classid' ";
        }
        $sql .= " order by u4 asc,id asc";

        $list = $this->db->getAll($sql);
        $_list = [];
        foreach ($list as $k => $v) {
            $_v["id"] = $v["id"];
            $_v["name"] = $v["u1"];
            $_v["value"] = $v["u2"];
            $_list[] = $_v;
        }
        return $_list;
        //对获取的分类进行重新排序

    }

    public function getparameterChildIdsByclassid($classid, $key = "id")
    {
        $sql = "SELECT *, u1 as name,classid as parentid  FROM {$this->table}  ";
        if ($classid != '') {
            $sql .= " where classid='$classid' ";
        }
        $sql .= " order by u4 asc,id asc";

        $list = $this->db->getAll($sql);
        $ids = "";
        foreach ($list as $k => $v) {
            if ($ids == "") {
                $ids = $v[$key];
            } else {
                $ids = $ids . "," . $v[$key];
            }
        }
        return $ids;
        //对获取的分类进行重新排序

    }

    //根据classid查询出所有子集
    public function getParameterAllChildByClassId($class_id)
    {
        $parameter = $this->getparameterArrayByclassid($class_id);
        $parameter_all = [];
        foreach ($parameter as &$v){
            $child = $this->getparameterArrayByclassid($v["id"]);
            foreach ($child as &$val){
                unset($val["id"]);
                $val["value"] = (int)$val["value"];
            }
            $parameter_all[$v["name"]] = $child;
        }

        return $parameter_all;
    }

}