Files
2024-10-29 14:04:59 +08:00

215 lines
6.9 KiB
PHP
Executable File

<?php
namespace app\admin\model;
use think\Model;
use think\Request;
use think\Config;
use think\Session;
class Dept extends Model {
use \app\common\traits\AdminModel;
public function getDeptLists($where = null, $order = null, $field = null, $limit = null, $level = 0, array &$alldata = array()) {
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select();
if ($list) {
foreach ($list as $k => $row) {
$row['level'] = $level;
$alldata[] = $row;
$where['pid'] = $row['id'];
!$row['haschild'] ? '' : self::getDeptLists($where, $order, $field, $limit, $level + 1, $alldata);
}
}
return $alldata;
}
public function getOption($id = 0, $where = null, $order = null, $field = null, $limit = null) {
$options = '';
if (is_array($where)) {
$where = array_merge([ 'stat' => ['eq', '0']], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select();
if ($list) {
foreach ($list as $k => $row) {
if ($row['id'] == $id) {
$options.='<option value="' . $row['id'] . '" selected>' . $row['name'] . '</option>' . "\n";
} else {
$options .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>' . "\n";
}
}
}
return $options;
}
public function getOptions($id = 0, $where = null, $order = null, $field = null, $limit = null, $level = 0) {
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
$options = '';
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select();
if ($list) {
foreach ($list as $k => $row) {
if ($row['id'] == $id) {
$options.='<option value="' . $row['id'] . '" selected>' . ($level ? str_repeat('&#160;&#160;&#160;&#160;', $level) . '&#8970;' : '' ) . $row['name'] . '</option>' . "\n";
} else {
$options.='<option value="' . $row['id'] . '">' . ($level ? str_repeat('&#160;&#160;&#160;&#160;', $level) . '&#8970;' : '' ) . $row['name'] . '</option>' . "\n";
}
$where['pid'] = $row['id'];
$options.=!$row['haschild'] ? '' : self::getOptions($id, $where, $order, $field, $limit, $level + 1);
}
}
return $options;
}
public function getDeptTree($where = null, $order = null, $field = null, $limit = 50, $level = 0) {
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
$result = array();
if ($level > 5) {
return $result;
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select(); // 获取
if ($list) {
foreach ($list as $row) {
$row['level'] = $level;
$where['pid'] = $row['id'];
$row['child'] = !$row['haschild'] ? [] : self:: getDeptTree($where, $order, $field, $limit, $level + 1);
$result[] = $row->toArray();
}
}
return $result;
}
public function getMenu($where = null, $order = null, $field = null, $limit = 50, $level = 0) {
if (is_array($where)) {
$where = array_merge(['stat' => ['eq', '0']], $where);
}
$result = array();
if ($level > 5) {
return $result;
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select(); // 获取
if ($list) {
foreach ($list as $row) {
$row['level'] = $level;
$where['pid'] = $row['id'];
$row['child'] = !$row['haschild'] ? [] : self::getMenu($where, $order, $field, $limit, $level + 1);
$result[] = $row->toArray();
}
}
return $result;
}
public function getChildIDArray($id) {
$list = $this->where([ 'pid' => $id, 'stat' => 0])->field(['id', 'haschild'])->select();
$childIDArray = array((int) $id);
if ($list) {
foreach ($list as $val) {
$childArray = $val['haschild'] ? self::getChildIDArray($val['id']) : array((int)
$val['id']);
$childIDArray = array_merge($childIDArray, $childArray);
}
}
return $childIDArray;
}
public function getTopParentID($id, $where = null) {
$data = $this::get(function($query)use($id, $where) {
$query->where(['id' => $id]);
if ($where) {
$query->where($where);
}
$query->field(['id', 'pid']);
});
if (isset($data['pid']) && $data['pid']) {
$topid = self::getTopParentID($data['pid'], $where);
} else {
$topid = $id;
}
return $topid;
}
public function deleteRow($id, $stat = -1) {
//$result = $this::destroy($id);
$data = ['id' => $id, 'pid' => 0, 'haschild' => 0, 'stat' => $stat,];
$object = $this::update($data);
return $object;
}
protected function setUrlAttr($value, $data) {
return trim($value, '/');
}
}