<?phpnamespace app\admin\model;
use think\Model;
class MenuModel extends Model
{
protected $pk = 'nav_id';
protected $table = 'tp_menu';
public function get_field($where, $field)
{
$data = $this->field($field)->where($where)->find();
return $data->getAttr($field);
}
public function get_info($where, $fields = "*")
{
$data = $this->field($fields)->where($where)->find();
return $data;
}
public function get_list($where, $order = "", $fields = "*")
{
$order = empty($order) ? $this->pk." asc" : $order;
$data = $this->field($fields)->where($where)->order($order)->select();
return $data->toArray();
}
public function get_count($where)
{
return $this->where($where)->count();
}
public function insert_data($param)
{
try{
$result = $this->create($param, true);
return (int)$result->{$this->pk};
}catch(\Exception $e){
return msg_error($e->getMessage());
}
}
public function edit_data_bantch($param)
{
try{
return $this->saveAll($param);
}catch(\Exception $e){
return msg_error($e->getMessage());
}
}
public function save_data($param)
{
try{
return $this->update($param);
}catch(PDOException $e){
return msg_error($e->getMessage());
}
}
public function del_data($where)
{
try{
return $this->where($where)->delete();
}catch(PDOException $e){
return msg_error($e->getMessage());
}
}
public function get_menu_list_tree($where, $start, $size)
{
$sql = "SELECT * FROM
(SELECT *,@sort := pid as sort FROM tp_menu WHERE pid > 1
UNION
SELECT *,@sort:= nav_id as sort FROM tp_menu WHERE pid <= 1) as menus
WHERE {$where}
ORDER BY sort asc,level asc
limit {$start},{$size}";
return $this->query($sql);
}
public function count_menu_list_tree($where)
{
$sql = "SELECT count(*) as total FROM
(SELECT *,@sort := pid as sort FROM tp_menu WHERE pid > 1
UNION
SELECT *,@sort:= nav_id as sort FROM tp_menu WHERE pid <= 1) as menus
WHERE {$where}
ORDER BY sort asc,level asc";
$res = $this->query($sql);
return $res[0]['total'];
}
}