Department.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\model\department;
  3. use app\admin\model\AuthGroup;
  4. use fast\Tree;
  5. use think\Cache;
  6. use think\Db;
  7. use think\Exception;
  8. use think\Model;
  9. class Department extends Model
  10. {
  11. // 表名
  12. protected $name = 'department';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'create_time';
  17. protected $updateTime = 'update_time';
  18. protected static $config = [];
  19. /**
  20. * 缓存key
  21. * @var string
  22. */
  23. protected static $cachekey = "AllDepartmentCachekey";
  24. protected static function init()
  25. {
  26. $config = static::$config = get_addon_config('department');
  27. self::beforeUpdate(function ($row) {
  28. if (isset($row['parent_id'])) {
  29. $childrenIds = self::getChildrenIds($row['id'], true);
  30. if (in_array($row['parent_id'], $childrenIds)) {
  31. throw new Exception("上级组织部门不能是其自身或子组织部门");
  32. }
  33. }
  34. });
  35. self::afterInsert(function ($row) {
  36. //创建时自动添加权重值
  37. $pk = $row->getPk();
  38. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  39. self::clearCache();
  40. });
  41. self::afterDelete(function ($row) {
  42. //删除时,删除子节点,同时将所有相关部门成员删除
  43. $childIds = self::getChildrenIds($row['id']);
  44. if ($childIds) {
  45. Department::destroy(function ($query) use ($childIds) {
  46. $query->where('id', 'in', $childIds);
  47. });
  48. }
  49. $childIds[] = $row['id'];
  50. db('department_admin')->where('department_id', 'in', $childIds)->delete();
  51. self::clearCache();
  52. });
  53. self::afterWrite(function ($row) use ($config) {
  54. $changed = $row->getChangedData();
  55. //隐藏时判断是否有子节点,有则隐藏
  56. if (isset($changed['status']) && $changed['status'] == 'hidden') {
  57. $childIds = self::getChildrenIds($row['id']);
  58. db('department')->where('id', 'in', $childIds)->update(['status' => 'hidden']);
  59. }
  60. self::clearCache();
  61. });
  62. }
  63. public static function getStatusList()
  64. {
  65. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  66. }
  67. /**
  68. * 获取栏目的所有子节点ID
  69. * @param int $id 栏目ID
  70. * @param bool $withself 是否包含自身
  71. * @return array
  72. */
  73. public static function getChildrenIds($id, $withself = false)
  74. {
  75. static $tree;
  76. if (!$tree) {
  77. $tree = \fast\Tree::instance();
  78. $tree->init(collection(self::order('weigh desc,id desc')->field('id,parent_id,name,status')->cache(self::$cachekey)->select())->toArray(), 'parent_id');
  79. }
  80. $childIds = $tree->getChildrenIds($id, $withself);
  81. return $childIds;
  82. }
  83. /**
  84. * 获取栏目的所有子节点
  85. * @param int $id 栏目ID
  86. * @param bool $withself 是否包含自身
  87. * @param bool $filter 是否把一级的parent_id设置为0
  88. * @return array
  89. */
  90. public static function getChildrens($id, $withself = false, $filter = true)
  91. {
  92. static $tree;
  93. if (!$tree) {
  94. $tree = \fast\Tree::instance();
  95. $tree->init(collection(self::order('weigh desc,id desc')->field('id,parent_id,name,status')->cache(self::$cachekey)->select())->toArray(), 'parent_id');
  96. }
  97. $departments = $tree->getChildren($id, $withself);
  98. if (!$filter) return $departments;
  99. $issetIDs = array_column($departments, 'id');
  100. $departmentList = array();
  101. foreach ($departments as $m => $n) {
  102. if ($n['parent_id'] != 0) {
  103. if (!in_array($n['parent_id'], $issetIDs)) {
  104. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(($n['id'])));
  105. $childlist ? $n['haschild'] = 1 : '';
  106. $n['parent_id'] = 0;
  107. $departmentList[] = $n;
  108. foreach ($childlist as $k => $v) {
  109. $departmentList[] = $v;
  110. }
  111. }
  112. } else {
  113. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  114. $childlist ? $n['haschild'] = 1 : '';
  115. $departmentList[] = $n;
  116. foreach ($childlist as $k => $v) {
  117. $departmentList[] = $v;
  118. }
  119. }
  120. }
  121. return $departmentList;
  122. }
  123. /**
  124. * 所有部门
  125. */
  126. public static function allDepartment()
  127. {
  128. return self::order('weigh desc,id desc')->where(['status' => 'normal'])->cache(self::$cachekey)->select();
  129. }
  130. /**
  131. * 清空缓存
  132. * @param $name
  133. */
  134. public static function clearCache()
  135. {
  136. Cache::rm(self::$cachekey);
  137. }
  138. /**
  139. * 获取权限组
  140. * @param array $childrenGroupIds
  141. * @param null $groups 如果不是超级管理员传auth->getGroups(),的值
  142. * @return array
  143. */
  144. public static function getGroupdata($childrenGroupIds = [], $groups = null)
  145. {
  146. $groupList = collection(AuthGroup::where('id', 'in', $childrenGroupIds)->select())->toArray();
  147. Tree::instance()->init($groupList, 'pid');
  148. $groupdata = [];
  149. if ($groups) {
  150. $result = [];
  151. foreach ($groups as $m => $n) {
  152. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  153. $temp = [];
  154. foreach ($childlist as $k => $v) {
  155. $temp[$v['id']] = $v['name'];
  156. }
  157. $result[__($n['name'])] = $temp;
  158. }
  159. $groupdata = $result;
  160. } else {
  161. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  162. foreach ($result as $k => $v) {
  163. $groupdata[$v['id']] = $v['name'];
  164. }
  165. }
  166. return $groupdata;
  167. }
  168. /**
  169. * 获取当前部门归属的最高组织id(最顶级)
  170. */
  171. public static function getOrganiseID($departmentid)
  172. {
  173. if (!$departmentid) return 0;
  174. $organise_id = self::where(['id' => $departmentid])->value('organise_id');
  175. return $organise_id ? $organise_id : $departmentid;//如果没有最高,本身自己就是最高;
  176. }
  177. /**
  178. * 获取上级id
  179. */
  180. public static function getParentId($id, $withself = false)
  181. {
  182. static $tree;
  183. if (!$tree) {
  184. $tree = \fast\Tree::instance();
  185. $tree->init(collection(self::allDepartment())->toArray(), 'parent_id');
  186. }
  187. $parent = $tree->getParent($id, $withself);
  188. return array_column($parent, 'id');
  189. }
  190. /**
  191. * 获取所有上级ids
  192. */
  193. public static function getParentIds($id, $withself = false)
  194. {
  195. static $tree;
  196. if (!$tree) {
  197. $tree = \fast\Tree::instance();
  198. $tree->init(collection(self::allDepartment())->toArray(), 'parent_id');
  199. }
  200. $parents = $tree->getParents($id, $withself);
  201. return array_column($parents, 'id');
  202. }
  203. }