<?php

class IStructures_model extends CI_Model
{

    var $insert_id = -1;

    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

    function Detail($is_id)
    {
        $sql = "SELECT TOP 1 is1.is_id, \n"
                . "       is1.is_parent_id, \n"
                . "       is1.is_path, \n"
                . "       is1.is_level, \n"
                . "       is1.is_sort, \n"
                . "       is1.is_sitecode, \n"
                . "       is1.is_datetime, \n"
                . "       is1.is_ic_id \n"
                . "FROM   infoStructures is1 \n"
                . "WHERE  is1.is_id = ?";
        $query = $this->HT->query($sql, array($is_id));
        //print_r($this->HT->queries);
        if ($query->result())
        {
            $row = $query->row();
            return $row;
        }
        else
        {
            return FALSE;
        }
    }

    function Add($is_parent_id, $is_ic_id=-1)
    {
        if ($is_parent_id == 0)
        {
            $is_path = '';
            $is_level = 1;
            $is_sort = 999;
        }
        else
        {
            $infoStructure = $this->Detail($is_parent_id);
            if ($infoStructure)
            {
                $is_path = $infoStructure->is_path;
                $is_level = $infoStructure->is_level + 1;
                $is_sort = 999;
            }
            else
            {
                return false;
            }
        }
        $sql = "INSERT INTO infoStructures \n"
                . "  ( \n"
                . "	is_parent_id, is_path, is_level, is_sort, is_sitecode, is_datetime, is_ic_id  \n"
                . "  ) \n"
                . "VALUES \n"
                . "  (  \n"
                . "	?, ?, ?, ?, ?, getdate(), ? \n"
                . "  )";
        $query = $this->HT->query($sql, array($is_parent_id, $is_path, $is_level, $is_sort, "trippest", $is_ic_id));
        $this->insert_id = $this->HT->last_id('infoStructures');
        //把自己的ID加到path上
        $set_sql = "UPDATE infoStructures \n"
                . "SET    is_path  = is_path + CONVERT(VARCHAR(200), is_id) + ',' \n"
                . "WHERE  is_id    = ?";
        $query = $this->HT->query($set_sql, array($this->insert_id));

        return $this->insert_id;
    }

    function GetParent($is_path, $level=1)
    {
        if ($is_path == '')
        {
            return false;
        }
        $sql = "SELECT TOP 1 is1.is_id, \n"
                . "       is1.is_parent_id, \n"
                . "       is1.is_path, \n"
                . "       is1.is_level, \n"
                . "       is1.is_sort, \n"
                . "       is1.is_sitecode, \n"
                . "       is1.is_datetime, \n"
                . "       is1.is_ic_id \n"
                . "FROM   infoStructures is1 \n"
                . "WHERE  is1.is_level = ? \n"
                . "       AND is1.is_id IN ($is_path 0) \n"
                . "ORDER BY \n"
                . "       is1.is_id ASC";
        $query = $this->HT->query($sql, array($level));
        // print_r($this->HT->queries);
        if ($query->result())
        {
            $row = $query->row();
            return $row;
        }
        else
        {
            return FALSE;
        }
    }

    //查询节点的根节点
    function get_root($is_id)
    {
        $detail = $this->Detail($is_id);
        $root = $this->GetParent($detail->is_path, 1);
        if ($root)
        {
            return $root;
        }
        else
        {        //没有根节点则返回本节点
            return $detail;
        }
    }

    //判断是否有子节点
    function HasChild($is_id)
    {
        $sql = "SELECT TOP 1 is1.is_id \n"
                . "FROM   infoStructures is1 \n"
                . "WHERE  is1.is_parent_id = ?";
        $query = $this->HT->query($sql, array($is_id));
        if ($query->result())
        {
            return true;
        }
        else
        {
            return FALSE;
        }
    }

    //删除节点
    function Delete($is_id)
    {
        $sql = "DELETE  \n"
                . "FROM   infoStructures \n"
                . "WHERE  is_id = ?";
        $query = $this->HT->query($sql, array($is_id));
        return $query;
    }

    //设置排序
    function set_sort($is_id, $sort)
    {
        $sql = "UPDATE infoStructures \n"
                . "SET    is_datetime  = GETDATE(), \n"
                . "       is_sort      = ? \n"
                . "WHERE  is_id        = ?";
        $query = $this->HT->query($sql, array($sort, $is_id));
        //print_r($this->HT->queries);
        return $query;
    }

    //设置路径
    function set_path($parent_id, $is_id)
    {
        $structure = $this->Detail($is_id);
        //如果父级相同则不需要重新设置path
        if ($parent_id == $structure->is_parent_id)
        {
            return true;
        }
        $parent = $this->Detail($parent_id);
        //设置当前节点
        $set_parent_id = $parent->is_id;
        $set_level = $parent->is_level + 1;
        $set_path = $parent->is_path . $is_id . ',';

        $set_sql = "UPDATE infoStructures \n"
                . "SET    is_parent_id = ?, \n"
                . "       is_level = ?, \n"
                . "       is_path = ? \n"
                . "WHERE  is_id = ?";
        $this->HT->query($set_sql, array($set_parent_id, $set_level, $set_path, $is_id));

        $structure = $this->Detail($is_id);

        //查询当前节点的所有子节点
        $sql = "SELECT is1.is_id, \n"
                . "       is1.is_parent_id, \n"
                . "       is1.is_path, \n"
                . "       is1.is_level, \n"
                . "       is1.is_sort \n"
                . "FROM   infoStructures is1 \n"
                . "WHERE  ',' + is1.is_path LIKE '%,$structure->is_id,%' \n"
                . "ORDER BY \n"
                . "       is1.is_level ASC     ";
        $query = $this->HT->query($sql);
        foreach ($query->result() as $item)
        {
            if ($item->is_parent_id != $structure->is_id)
            {
                $structure = $this->Detail($item->is_parent_id);
            }
            $set_parent_id = $structure->is_id;
            $set_level = $structure->is_level + 1;
            $set_path = $structure->is_path . $item->is_id . ',';
            $set_sql = "UPDATE infoStructures \n"
                    . "SET    is_parent_id = ?, \n"
                    . "       is_level = ?, \n"
                    . "       is_path = ? \n"
                    . "WHERE  is_id = ?";
            $this->HT->query($set_sql, array($set_parent_id, $set_level, $set_path, $item->is_id));
        }
    }

}