<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Navigation_model extends CI_Model {
        
    function __construct() 
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }
    //获取卡片列表
    public function get_card_list($nt_uid){
        $sql="SELECT distinct nt_sn,nt_name,nt_uid FROM  navigation_type WHERE nt_uid=? or nt_sn<5 ORDER BY nt_sn ASC";
        $query=$this->HT->query($sql, array($nt_uid));
        return $query->result();
    }

    //获取导航列表
    public function get_nav_list($n_nt_sn_string,$search_key=false)
    {
        $top='';
        $map=" n_nt_sn in ($n_nt_sn_string)" ;
        if($search_key){
            $top=' TOP 50 ';
            $map=" n_name like '%$search_key%' ";
        }
        $sql="SELECT  $top  n_sn,
                        n_name,
                        n_link,
                        n_description,
                        n_nt_sn,
                        n_uid,
                        n_status 
                FROM    navigation 
               WHERE    $map AND n_status=1 
               ORDER BY n_sn ASC";
        $query=$this->HT->query($sql);
        return $query->result();
    }

    //添加分类
    public function add_nav_type($nt_name,$nt_uid)
    {
        $sql="INSERT INTO navigation_type (nt_name,nt_uid ) VALUES (?,?) ";
        $this->HT->query($sql, array($nt_name,$nt_uid));
        $insertid = $this->HT->last_id('navigation_type');
        return $insertid;
    }

    //添加导航
    public function add_nav($n_name,$n_link,$n_description,$n_nt_sn,$n_uid,$n_status)
    {
        $sql="INSERT INTO navigation (n_name,n_link,n_description,n_nt_sn,n_uid,n_status) VALUES (?,?,?,?,?,?) ";
        $this->HT->query($sql, array($n_name,$n_link,$n_description,$n_nt_sn,$n_uid,$n_status));
        $insertid = $this->HT->last_id('navigation');
        return $insertid;
    }

    //更新分类
    public function update_type($nt_name,$nt_sn,$nt_uid){
        $sql="UPDATE navigation_type SET nt_name=? WHERE nt_sn=? and nt_uid=? ";
        $query=$this->HT->query($sql, array($nt_name,$nt_sn,$nt_uid));
        return $query;
    }

    //更新导航
    public function update_nav($n_name,$n_link,$n_sn,$n_description,$n_uid){
        $sql="UPDATE navigation SET n_name=?,n_link=?,n_description=? WHERE n_sn=? and n_uid=? ";
        $query=$this->HT->query($sql, array($n_name,$n_link,$n_description,$n_sn,$n_uid));
        return $query;
    }

    //删除分类
    public function delete_type($nt_sn,$nt_uid){
        $sql="DELETE FROM navigation_type WHERE nt_sn=? and nt_uid=?";
        $query=$this->HT->query($sql, array($nt_sn,$nt_uid));
        if ($query) {
            $sql2="DELETE FROM navigation WHERE n_nt_sn=? and n_uid=?";
            $result2=$this->HT->query($sql2, array($nt_sn,$nt_uid));
            return TRUE;
        }else{
            return false;
        }

    }

    //删除导航
    public function delete_nav($n_sn,$n_uid){
        $sql="DELETE FROM navigation WHERE n_sn=? and n_uid=?";
        $query=$this->HT->query($sql, array($n_sn,$n_uid));
        return $query;
        
    }

}