You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/models/infoStructures_model.php

291 lines
9.5 KiB
PHTML

<?php
class InfoStructures_model extends CI_Model
{
var $insert_id = -1;
function __construct()
{
parent::__construct();
$this->HT = $this->load->database('INFORMATION', TRUE);
}
function Detail($is_id)
{
$sql = "SELECT TOP 1 is1.is_id, \n"
2 years ago
. " 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);
2 years ago
if ($query->result()) {
$row = $query->row();
return $row;
2 years ago
} else {
return FALSE;
}
}
function Detail_gm($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 information_gm.dbo.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;
}
}
2 years ago
function Add($is_parent_id, $is_ic_id = -1, $is_sort = 999)
{
2 years ago
if ($is_parent_id == 0) {
$is_path = '';
$is_level = 1;
2 years ago
} else if ($is_parent_id == -99) {
$is_path = '';
$is_level = 0;
2 years ago
$is_sort = 0;
} else {
$infoStructure = $this->Detail($is_parent_id);
2 years ago
if ($infoStructure) {
$is_path = $infoStructure->is_path;
$is_level = $infoStructure->is_level + 1;
2 years ago
} else {
return false;
}
}
$sql = "INSERT INTO infoStructures \n"
2 years ago
. " ( \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, $this->config->item('site_code'), $is_ic_id));
$this->insert_id = $this->HT->last_id('infoStructures');
//把自己的ID加到path上
$set_sql = "UPDATE infoStructures \n"
2 years ago
. "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;
}
2 years ago
function Add_with_sitecode($is_parent_id, $is_ic_id = -1, $is_sort = 999, $site_code = '')
{
if (empty ($site_code)) {
2 years ago
$site_code = $this->config->item('site_code');
}
if ($is_parent_id == 0) {
$is_path = '';
$is_level = 1;
} else if ($is_parent_id == -99) {
$is_path = '';
$is_level = 0;
$is_sort = 0;
} else {
$infoStructure = $this->Detail($is_parent_id);
if ($infoStructure) {
$is_path = $infoStructure->is_path;
$is_level = $infoStructure->is_level + 1;
} 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, $site_code, $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"
2 years ago
. " 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);
2 years ago
if ($query->result()) {
$row = $query->row();
return $row;
2 years ago
} else {
return FALSE;
}
}
//查询节点的根节点
function get_root($is_id)
{
$detail = $this->Detail($is_id);
$root = $this->GetParent($detail->is_path, 1);
2 years ago
if ($root) {
return $root;
2 years ago
} else { //没有根节点则返回本节点
return $detail;
}
}
//判断是否有子节点
function HasChild($is_id)
{
$sql = "SELECT TOP 1 is1.is_id \n"
2 years ago
. "FROM infoStructures is1 \n"
. "WHERE is1.is_parent_id = ?";
$query = $this->HT->query($sql, array($is_id));
2 years ago
if ($query->result()) {
return true;
2 years ago
} else {
return FALSE;
}
}
//删除节点
function Delete($is_id)
{
$sql = "DELETE \n"
2 years ago
. "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"
2 years ago
. "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
2 years ago
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"
2 years ago
. "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"
2 years ago
. " 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);
2 years ago
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"
2 years ago
. "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));
}
}
/**
* @description: 用于生成一个特殊的根结点is_level和is_sort都为0CT移动优先项目用
* @param {type}
* @return:
* @Date Changed:
*/
2 years ago
public function AddRootNote($is_ic_id)
{
$sql = "INSERT INTO infoStructures \n"
2 years ago
. " ( \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(-99, '', 0, 0, $this->config->item('site_code'), $is_ic_id));
$this->insert_id = $this->HT->last_id('infoStructures');
//把自己的ID加到path上
$set_sql = "UPDATE infoStructures \n"
2 years ago
. "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;
}
}