'int', //组织ID 'pid' => 'int', //上级组织ID 'parent_name' => 'varchar', //上级组织名称 'lft' => 'int', //二叉树左标记 'rgt' => 'int', //二叉树右标记 'name' => 'varchar', //组织全称 'short_name' => 'varchar', //简称 'sort' => 'int', //排序 'status' => 'tinyint', //状态 'create_at' => 'datetime', //创建时间 'update_at' => 'datetime', //更新时间 'delete_at' => 'datetime', //删除时间 'type' => 'varchar', //组织类型码 'menu_ids' => 'text', //组织菜单权限 'request_ids' => 'text', //组织请求权限 'front_ids' => 'text', //组织移动端权限 'area_code' => 'varchar',//所在地区编码 'path' => 'varchar',//组织级别路径 'area_id' => 'int',//组织级别路径 ]; protected $append = [ 'status_txt', 'type_txt', 'hasChildren', 'area_code_txt', ]; // 组织状态列表 public function statusList() { return [1 => '启用', 2 => '禁用', 3 => '待审核', 4 => '已驳回']; } // 组织状态 public function getStatusTxtAttr($value, $data) { $value = $value ? $value : (isset($data['status']) ? $data['status'] : ''); $statusList = $this->statusList(); return isset($statusList[$value]) ? $statusList[$value] : ''; } // 组织类型列表 static public function typeList() { return OrgType::cache(60)->column('org_type_name', 'code'); } // 组织类型名称 public function getTypeTxtAttr($value, $data) { $typeList = self::typeList(); $value = $value ? $value : (isset($data['type']) ? $data['type'] : ''); if (empty($value)) { return "无"; } return isset($typeList[$value]) ? $typeList[$value] : '无'; } // 菜单权限修改器 public function setMenuIdsAttr($value, $data) { $value = $value ? $value : (isset($data['menu_ids']) ? $data['menu_ids'] : ''); if (!empty($value) && is_array($value)) { return implode(',', $value); } else { return $value; } } // 菜单权限获取器 public function getMenuIdsAttr($value, $data) { $value = $value ? $value : (isset($data['menu_ids']) ? $data['menu_ids'] : []); return is_string($value) ? explode(',', $value) : $value; } // 请求权限修改器 public function setRequestIdsAttr($value, $data) { $value = $value ? $value : (isset($data['request_ids']) ? $data['request_ids'] : ''); if (!empty($value) && is_array($value)) { return implode(',', $value); } else { return $value; } } // 请求权限获取器 public function getRequestIdsAttr($value, $data) { $value = $value ? $value : (isset($data['request_ids']) ? $data['request_ids'] : []); return is_string($value) ? explode(',', $value) : $value; } // 手机端权限修改器 public function setFrontIdsAttr($value, $data) { $value = $value ? $value : (isset($data['front_ids']) ? $data['front_ids'] : ''); if (!empty($value) && is_array($value)) { return implode(',', $value); } else { return $value; } } // 手机端权限获取器 public function getFrontIdsAttr($value, $data) { $value = $value ? $value : (isset($data['front_ids']) ? $data['front_ids'] : []); return is_string($value) ? explode(',', $value) : $value; } // 判断是否有下级部门 public function getHasChildrenAttr($value, $data) { if (isset($data['rgt']) && isset($data['lft'])) { return $data['rgt'] - $data['lft'] > 2; } else { return false; } } // 部门列表 public function depart() { return $this->hasMany(OrgDepart::class, 'org_id', 'org_id'); } // 设置上级组织名称 public function setParentNameAttr($value, $data) { if (isset($data['pid']) && $data['pid'] != 0) { return self::where('org_id', $data['pid'])->value('name'); } else { return ''; } } // 逐级获取pid,返回一个pid数组 static public function getPid($org_id, &$ary = []) { $allOrg = self::cache(60)->column("pid", 'org_id'); if (isset($allOrg[$org_id])) { $pid = $allOrg[$org_id]; if ($pid != 0) { $ary[] = $pid; return self::getPid($pid, $ary); } else { return $ary; } } else { return $ary; } } // 设置组织级别路径 public function setPathAttr($value, $data) { $ary = []; if (isset($data['pid'])) { $pid = $data['pid']; $ary[] = $pid; $ary = self::getPid($pid, $ary); sort($ary); return implode(",", $ary); } } // 缓存地区列表 static public function areaList() { return Area::cache(86400)->column('name', 'code'); } // 获取地区文本信息 public function getAreaCodeTxtAttr($value, $data) { $value = $value ? $value : (isset($data['area_code']) ? $data['area_code'] : ''); $areaList = $this->areaList(); return isset($areaList[$value]) ? $areaList[$value] : ''; } /** * @title: 获取下级组织 * @desc: 描述 * @parem {int} {org_id} {0} {组织ID} * @parem {boolean} {self} {false} {是否包含自己} * @return {*} * @author: Rock * @Date: 2023-03-23 20:27:35 */ static public function getChildrenIds(int $org_id = 0, $self = false) { $info = self::where('org_id', $org_id)->find(); $where = []; if ($self) { $where[] = ['lft', '>=', $info->lft]; $where[] = ['rgt', '<=', $info->rgt]; } else { $where[] = ['lft', '>', $info->lft]; $where[] = ['rgt', '<', $info->rgt]; } return self::where($where)->column('org_id'); } /** * @title 获取直接上级 * @desc: 获取当前组织的直接上级 * @param : {int} {org_id} {0} {组织ID} * @return {int} {org_id} {} {上级组织ID} * @return {varchar} {name} {} {上级组织名称} * @author: Rock */ static public function getSuperior(int $org_id) { $list = self::cache(600)->column('org_id,name', 'org_id'); return isset($list[$org_id]) ? $list[$org_id] : null; } /** * @title 获取所有上级 * @desc:获取当前组织的所有上级 * @param: {int} {org_id} {0} {组织ID} * @return {array.int} {org_id} {} {组织ID} * @return {array.varchar} {name} {} {组织名称} * @return {array.varchar} {type} {} {组织类型编码} */ static public function getSuperiors(int $org_id) { $list = self::cache(600)->column('org_id,name,path,type', 'org_id'); $info = $list[$org_id]; $ids = is_string($info['path']) ? explode(',', $info['path']) : $info['path']; $res = []; foreach ($ids as $id) { $res[] = $list[$id]; } return $res; } // 地区编码设置 public function setAreaCodeAttr($value, $row) { // 处理area_code if (isset($row['type'])) { // 集团无需地区,幼儿园、小学、初中、高中是创建时选择的,无需继承上级组织所属地区, if (!in_array($row['type'], ['GROUP', 'KINDER', 'PRIMARY', 'JUNIOR', 'HIGH'])) { $parent = (new self)->find($row['pid']); return $parent['area_code'] ?? null; } else { return $value ?? $row['area_code']; } } } }