Admin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace app\admin\model\department;
  3. use app\admin\model\department\Department as DepartmentModel;
  4. use fast\Tree;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. class Admin extends Model
  9. {
  10. // 表名
  11. protected $name = 'department_admin';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'create_time';
  16. protected $updateTime = 'update_time';
  17. /**
  18. * 关联部门
  19. */
  20. public function department()
  21. {
  22. return $this->hasOne('app\admin\model\department\Department', 'id', 'department_id');
  23. }
  24. /**
  25. * 获取指定部门的员工
  26. * @param $admin_id
  27. * @param bool $is_principal 是否取负责部门
  28. * @return array|bool|string
  29. */
  30. public static function getDepartmentAdminIds($departmentids)
  31. {
  32. //获取当前部门负责人
  33. $AdminIds = Db::name('department_admin')
  34. ->alias('da')
  35. ->join('__' . strtoupper('department') . '__ d', 'da.department_id = d.id')
  36. ->where('da.department_id', 'in', $departmentids)
  37. ->where('d.status', 'normal')
  38. ->column('da.admin_id');
  39. return $AdminIds;
  40. }
  41. /**
  42. * 获取员工者的部门ids
  43. * @param $admin_id
  44. * @param bool $is_principal 是否取负责部门
  45. * @return array|bool|string
  46. */
  47. public static function getDepartmentIds($admin_id, $is_principal = false)
  48. {
  49. $model = new self();
  50. if ($is_principal) $model->where('is_principal', 1);
  51. return $model->where('admin_id', $admin_id)->column('department_id');
  52. }
  53. /**
  54. * 获取负责的部门IDs
  55. * @param $admin_id
  56. * @return array|bool|string
  57. */
  58. public static function getPrincipalIds($admin_id)
  59. {
  60. return self::where('admin_id', $admin_id)->where('is_principal', 1)->column('department_id');
  61. }
  62. /**
  63. * 获取组织(公司)ids
  64. * @param $admin_id
  65. * @param int $is_principal 是否只获取负责的部门
  66. * @return array|bool|string
  67. */
  68. public static function getOrganiseIds($admin_id, $is_principal = 0)
  69. {
  70. $where = array();
  71. if ($is_principal) $where['is_principal'] = 1;
  72. return self::where('admin_id', $admin_id)->where($where)->column('organise_id');
  73. }
  74. /**
  75. * 当前负责人下属ids
  76. * @param int $admin_id 某个管理员ID
  77. * @param boolean $withself 是否包含自身
  78. * @param string $department_ids 是否指定某个管理部门id,多个逗号id隔开
  79. * @return array
  80. */
  81. public static function getChildrenAdminIds($admin_id, $withself = false, $department_ids = null)
  82. {
  83. $cache_name="getChildrenAdminIds".((string)$withself).json_encode($department_ids).$admin_id;
  84. $childrenAdminIds = cache($cache_name);
  85. if ($childrenAdminIds){
  86. return $childrenAdminIds;
  87. }
  88. $childrenAdminIds=[];
  89. if (self::isSuperAdmin($admin_id)) {
  90. $childrenAdminIds = \app\admin\model\department\AuthAdmin::column('id');
  91. } else {
  92. $departmentIds = self::getChildrenDepartmentIds($admin_id, true);
  93. $authDepartmentList = self::field('admin_id,department_id')
  94. ->where('department_id', 'in', $departmentIds)
  95. ->select();
  96. foreach ($authDepartmentList as $k => $v) {
  97. $childrenAdminIds[] = $v['admin_id'];
  98. }
  99. }
  100. if ($withself) {
  101. if (!in_array($admin_id, $childrenAdminIds)) {
  102. $childrenAdminIds[] = $admin_id;
  103. }
  104. } else {
  105. $childrenAdminIds = array_diff($childrenAdminIds, [$admin_id]);
  106. }
  107. cache($cache_name,$childrenAdminIds,3600);//缓存一个小时
  108. halt($childrenAdminIds);
  109. return $childrenAdminIds;
  110. }
  111. /**
  112. * 判断是否是超级管理员
  113. * @return bool
  114. */
  115. public static function isSuperAdmin($admin_id)
  116. {
  117. $auth = new \app\admin\library\Auth();
  118. return in_array('*', $auth->getRuleIds($admin_id)) ? true : false;
  119. }
  120. /**
  121. * 取出当前负责人管理的下级部门
  122. * @param boolean $withself 是否包含当前所在的分组
  123. * @return array
  124. */
  125. public static function getChildrenDepartmentIds($admin_id, $withself = false)
  126. {
  127. //取出当前负责人所有部门
  128. if (self::isSuperAdmin($admin_id)) {
  129. $departments = DepartmentModel::allDepartment();
  130. } else {
  131. $departments = self::getDepartments($admin_id, 1);
  132. }
  133. $departmenIds = [];
  134. foreach ($departments as $k => $v) {
  135. $departmenIds[] = $v['id'];
  136. }
  137. $originDepartmenId = $departmenIds;
  138. foreach ($departments as $k => $v) {
  139. if (in_array($v['parent_id'], $originDepartmenId)) {
  140. $departmenIds = array_diff($departmenIds, [$v['id']]);
  141. unset($departments[$k]);
  142. }
  143. }
  144. // 取出所有部门
  145. $departmentList = \app\admin\model\department\Department::allDepartment();
  146. $objList = [];
  147. foreach ($departments as $k => $v) {
  148. // 取出包含自己的所有子节点
  149. $childrenList = Tree::instance()->init($departmentList, 'parent_id')->getChildren($v['id'], true);
  150. $obj = Tree::instance()->init($childrenList, 'parent_id')->getTreeArray($v['parent_id']);
  151. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  152. }
  153. $childrenDepartmenIds = [];
  154. foreach ($objList as $k => $v) {
  155. $childrenDepartmenIds[] = $v['id'];
  156. }
  157. if (!$withself) {
  158. $childrenDepartmenIds = array_diff($childrenDepartmenIds, $departmenIds);
  159. }
  160. return $childrenDepartmenIds;
  161. }
  162. /**
  163. * 根据用户id获取所在部门,返回值为数组
  164. * @param int $admin_id admin_id
  165. * @param int $admin_id $is_principal 是否只取负责的部分
  166. * @return array 用户所属的部门 array(
  167. * array('admin_id'=>'员工id','department_id'=>'部门id','name'=>'部门名称'),
  168. * ...)
  169. */
  170. public static function getDepartments($admin_id, $is_principal = 0)
  171. {
  172. static $departments = [];
  173. if (isset($departments[$admin_id])) {
  174. return $departments[$admin_id];
  175. }
  176. // 执行查询
  177. $user_departments = Db::name('department_admin')
  178. ->alias('da')
  179. ->join('__' . strtoupper('department') . '__ d', 'da.department_id = d.id', 'LEFT')
  180. ->field('da.admin_id,da.department_id,d.id,d.parent_id,d.name,d.tags')
  181. ->where("da.admin_id='{$admin_id}' " . ($is_principal ? "and is_principal=1" : '') . " and d.status='normal'")
  182. ->fetchSql(false)
  183. ->select();
  184. $departments[$admin_id] = $user_departments ?: [];
  185. return $departments[$admin_id];
  186. }
  187. /**
  188. * 获取当前用户可管理的所有部门
  189. * @param $admin_id
  190. * @param bool $isSuperAdmin
  191. * @return array|bool|false|\PDOStatement|string|\think\Collection
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public static function getAllDepartments($admin_id, $isSuperAdmin = false)
  197. {
  198. if ($isSuperAdmin) {
  199. $departmentList = DepartmentModel::allDepartment();
  200. } else {
  201. $departmentIds = \app\admin\model\department\Admin::getChildrenDepartmentIds($admin_id, true);
  202. $departmentList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  203. }
  204. return $departmentList;
  205. }
  206. /**
  207. * 获取当前用户可管理的所有部门[key=>value]
  208. * @param $admin_id
  209. * @param bool $isSuperAdmin
  210. * @return array|bool|false|\PDOStatement|string|\think\Collection
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\ModelNotFoundException
  213. * @throws \think\exception\DbException
  214. */
  215. public static function getAllDepartmentsTreeArray($admin_id, $isSuperAdmin = false)
  216. {
  217. $departmentdata = array();
  218. if ($isSuperAdmin) {
  219. $departmentList = DepartmentModel::allDepartment();
  220. Tree::instance()->init($departmentList, 'parent_id');
  221. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  222. foreach ($result as $k => $v) {
  223. $departmentdata[$v['id']] = $v['name'];
  224. }
  225. } else {
  226. //获取当前可管理部门
  227. $departmentIds = \app\admin\model\department\Admin::getChildrenDepartmentIds($admin_id, true);
  228. $departmentList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  229. Tree::instance()->init($departmentList, 'parent_id');
  230. $departments = \app\admin\model\department\Admin::getDepartments($admin_id);
  231. $issetIDs = array_column($departments, 'id');
  232. foreach ($departments as $m => $n) {
  233. if ($n['parent_id'] == 0) {
  234. $result1 = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  235. foreach ($result1 as $k => $v) {
  236. $departmentdata[$v['id']] = $v['name'];
  237. }
  238. } else {
  239. if (in_array($n['parent_id'], $issetIDs)) continue;
  240. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(($n['parent_id'])));
  241. foreach ($childlist as $k => $v) {
  242. $departmentdata[$v['id']] = $v['name'];
  243. }
  244. }
  245. }
  246. }
  247. return $departmentdata;
  248. }
  249. /**
  250. * 获取当前用户可管理的所有部门
  251. * @param $admin_id
  252. * @param bool $isSuperAdmin
  253. * @return array|bool|false|\PDOStatement|string|\think\Collection
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. * @throws \think\exception\DbException
  257. */
  258. public static function getAllDepartmentsArray($admin_id, $isSuperAdmin = false)
  259. {
  260. $departmentList = array();
  261. if ($isSuperAdmin) {
  262. $departmentList = DepartmentModel::allDepartment();
  263. Tree::instance()->init($departmentList, 'parent_id');
  264. $departmentList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  265. } else {
  266. //获取当前可管理部门
  267. $departmentIds = self::getChildrenDepartmentIds($admin_id, true);
  268. $dList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  269. Tree::instance()->init($dList, 'parent_id');
  270. $departments = \app\admin\model\department\Admin::getDepartments($admin_id);
  271. $issetIDs = array_column($departments, 'id');
  272. foreach ($departments as $m => $n) {
  273. if ($n['parent_id'] != 0) {
  274. if (in_array($n['parent_id'], $issetIDs)) continue;
  275. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(($n['parent_id'])));
  276. foreach ($childlist as $k => $v) {
  277. $k == 0 ? $v['parent_id'] = 0 : '';
  278. $departmentList[] = $v;
  279. }
  280. } else {
  281. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  282. $childlist ? $n['haschild'] = 1 : '';
  283. $departmentList[] = $n;
  284. foreach ($childlist as $k => $v) {
  285. $departmentList[] = $v;
  286. }
  287. }
  288. }
  289. }
  290. return $departmentList;
  291. }
  292. /**
  293. * 获取上级负责人
  294. * @param bool $parent 如果当前部门没负责人,是否逐级寻找?
  295. * @param array $ignore 是否忽略当前uid
  296. * @return array
  297. * @throws \think\db\exception\DataNotFoundException
  298. * @throws \think\db\exception\ModelNotFoundException
  299. * @throws \think\exception\DbException
  300. */
  301. public static function getParentAdminIds($uid, $parent = true,$ignore=false)
  302. {
  303. $principalIds = [];
  304. $departmentIds = self::getDepartmentIds($uid);//获取当前用户的所有部门ID,
  305. if ($departmentIds) {
  306. $principalIds = self::getDprincipalIds($departmentIds, $parent,$ignore?[$uid]:[]);
  307. }
  308. return $principalIds;
  309. }
  310. /**
  311. * 获取部门的负责人
  312. * @param $departmentIds 部门IDs
  313. * @param bool $parent 如果当前部门没负责人,是否逐级寻找?
  314. * @param array $ignore_ids 忽略的adminids
  315. * @return array|bool|string
  316. */
  317. public static function getDprincipalIds($departmentIds, $parent = true,$ignore_ids=[])
  318. {
  319. $daModel=Db::name('department_admin');
  320. if ($ignore_ids){
  321. $daModel->where('da.admin_id', 'not in', $ignore_ids);
  322. }
  323. //获取当前部门负责人
  324. $principalIds =$daModel
  325. ->alias('da')
  326. ->join('__' . strtoupper('department') . '__ d', 'da.department_id = d.id')
  327. ->where('da.department_id', 'in', $departmentIds)
  328. ->where('is_principal', 1)
  329. ->where('d.status', 'normal')
  330. ->column('da.admin_id');
  331. if ($principalIds) {
  332. return $principalIds;//如果存在就直接返回
  333. }
  334. //上一级查找
  335. foreach ($departmentIds as $k => $v) {
  336. $newDepartmentIds = Department::getParentId($v);
  337. if ($newDepartmentIds) {
  338. return self::getDprincipalIds($newDepartmentIds, $parent);
  339. }
  340. }
  341. return [];
  342. }
  343. /**
  344. * 数据权限校验
  345. * @param $auth
  346. * @param $row
  347. * @param string $field
  348. * @return bool
  349. */
  350. public static function checkDataAuth($auth,$row,$field="admin_id"){
  351. if ($auth->data_scope!=1&&!$auth->isSuperAdmin()){
  352. $childrenAdminIds = \app\admin\model\department\Admin::getChildrenAdminIds($auth->id, true);
  353. if (!$row[$field] || !in_array($row[$field], $childrenAdminIds)) {
  354. return false;
  355. }
  356. }
  357. return true;
  358. }
  359. /**
  360. * 获取员工上级所有部门ids
  361. * @param $admin_id
  362. * @param bool $withself
  363. * @return array|mixed
  364. */
  365. public static function getParentDepartmentIds($admin_id, $withself = false)
  366. {
  367. //如已经存在直接返回
  368. static $parentDepartment = [];
  369. if (isset($parentDepartment[$admin_id])) {
  370. return $parentDepartment[$admin_id];
  371. }
  372. //获取当前员工的所在部门
  373. $departmentIds=self::getDepartmentIds($admin_id);
  374. if (!$departmentIds) return [];
  375. $tempdata=array();
  376. foreach ($departmentIds as $departmentId){
  377. $tempdata= array_merge($tempdata,\app\admin\model\department\Department::getParentIds($departmentId,$withself));
  378. }
  379. $parentDepartment[$admin_id] = $tempdata ?: [];
  380. return $parentDepartment[$admin_id];
  381. }
  382. }