Admin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. return $childrenAdminIds;
  109. }
  110. /**
  111. * 判断是否是超级管理员
  112. * @return bool
  113. */
  114. public static function isSuperAdmin($admin_id)
  115. {
  116. $auth = new \app\admin\library\Auth();
  117. return in_array('*', $auth->getRuleIds($admin_id)) ? true : false;
  118. }
  119. /**
  120. * 取出当前负责人管理的下级部门
  121. * @param boolean $withself 是否包含当前所在的分组
  122. * @return array
  123. */
  124. public static function getChildrenDepartmentIds($admin_id, $withself = false)
  125. {
  126. //取出当前负责人所有部门
  127. if (self::isSuperAdmin($admin_id)) {
  128. $departments = DepartmentModel::allDepartment();
  129. } else {
  130. $departments = self::getDepartments($admin_id, 1);
  131. }
  132. $departmenIds = [];
  133. foreach ($departments as $k => $v) {
  134. $departmenIds[] = $v['id'];
  135. }
  136. $originDepartmenId = $departmenIds;
  137. foreach ($departments as $k => $v) {
  138. if (in_array($v['parent_id'], $originDepartmenId)) {
  139. $departmenIds = array_diff($departmenIds, [$v['id']]);
  140. unset($departments[$k]);
  141. }
  142. }
  143. // 取出所有部门
  144. $departmentList = \app\admin\model\department\Department::allDepartment();
  145. $objList = [];
  146. foreach ($departments as $k => $v) {
  147. // 取出包含自己的所有子节点
  148. $childrenList = Tree::instance()->init($departmentList, 'parent_id')->getChildren($v['id'], true);
  149. $obj = Tree::instance()->init($childrenList, 'parent_id')->getTreeArray($v['parent_id']);
  150. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  151. }
  152. $childrenDepartmenIds = [];
  153. foreach ($objList as $k => $v) {
  154. $childrenDepartmenIds[] = $v['id'];
  155. }
  156. if (!$withself) {
  157. $childrenDepartmenIds = array_diff($childrenDepartmenIds, $departmenIds);
  158. }
  159. return $childrenDepartmenIds;
  160. }
  161. /**
  162. * 根据用户id获取所在部门,返回值为数组
  163. * @param int $admin_id admin_id
  164. * @param int $admin_id $is_principal 是否只取负责的部分
  165. * @return array 用户所属的部门 array(
  166. * array('admin_id'=>'员工id','department_id'=>'部门id','name'=>'部门名称'),
  167. * ...)
  168. */
  169. public static function getDepartments($admin_id, $is_principal = 0)
  170. {
  171. static $departments = [];
  172. if (isset($departments[$admin_id])) {
  173. return $departments[$admin_id];
  174. }
  175. // 执行查询
  176. $user_departments = Db::name('department_admin')
  177. ->alias('da')
  178. ->join('__' . strtoupper('department') . '__ d', 'da.department_id = d.id', 'LEFT')
  179. ->field('da.admin_id,da.department_id,d.id,d.parent_id,d.name,d.tags')
  180. ->where("da.admin_id='{$admin_id}' " . ($is_principal ? "and is_principal=1" : '') . " and d.status='normal'")
  181. ->fetchSql(false)
  182. ->select();
  183. $departments[$admin_id] = $user_departments ?: [];
  184. return $departments[$admin_id];
  185. }
  186. /**
  187. * 获取当前用户可管理的所有部门
  188. * @param $admin_id
  189. * @param bool $isSuperAdmin
  190. * @return array|bool|false|\PDOStatement|string|\think\Collection
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. * @throws \think\exception\DbException
  194. */
  195. public static function getAllDepartments($admin_id, $isSuperAdmin = false)
  196. {
  197. if ($isSuperAdmin) {
  198. $departmentList = DepartmentModel::allDepartment();
  199. } else {
  200. $departmentIds = \app\admin\model\department\Admin::getChildrenDepartmentIds($admin_id, true);
  201. $departmentList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  202. }
  203. return $departmentList;
  204. }
  205. /**
  206. * 获取当前用户可管理的所有部门[key=>value]
  207. * @param $admin_id
  208. * @param bool $isSuperAdmin
  209. * @return array|bool|false|\PDOStatement|string|\think\Collection
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. * @throws \think\exception\DbException
  213. */
  214. public static function getAllDepartmentsTreeArray($admin_id, $isSuperAdmin = false)
  215. {
  216. $departmentdata = array();
  217. if ($isSuperAdmin) {
  218. $departmentList = DepartmentModel::allDepartment();
  219. Tree::instance()->init($departmentList, 'parent_id');
  220. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  221. foreach ($result as $k => $v) {
  222. $departmentdata[$v['id']] = $v['name'];
  223. }
  224. } else {
  225. //获取当前可管理部门
  226. $departmentIds = \app\admin\model\department\Admin::getChildrenDepartmentIds($admin_id, true);
  227. $departmentList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  228. Tree::instance()->init($departmentList, 'parent_id');
  229. $departments = \app\admin\model\department\Admin::getDepartments($admin_id);
  230. $issetIDs = array_column($departments, 'id');
  231. foreach ($departments as $m => $n) {
  232. if ($n['parent_id'] == 0) {
  233. $result1 = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  234. foreach ($result1 as $k => $v) {
  235. $departmentdata[$v['id']] = $v['name'];
  236. }
  237. } else {
  238. if (in_array($n['parent_id'], $issetIDs)) continue;
  239. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(($n['parent_id'])));
  240. foreach ($childlist as $k => $v) {
  241. $departmentdata[$v['id']] = $v['name'];
  242. }
  243. }
  244. }
  245. }
  246. return $departmentdata;
  247. }
  248. /**
  249. * 获取当前用户可管理的所有部门
  250. * @param $admin_id
  251. * @param bool $isSuperAdmin
  252. * @return array|bool|false|\PDOStatement|string|\think\Collection
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\ModelNotFoundException
  255. * @throws \think\exception\DbException
  256. */
  257. public static function getAllDepartmentsArray($admin_id, $isSuperAdmin = false)
  258. {
  259. $departmentList = array();
  260. if ($isSuperAdmin) {
  261. $departmentList = DepartmentModel::allDepartment();
  262. Tree::instance()->init($departmentList, 'parent_id');
  263. $departmentList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  264. } else {
  265. //获取当前可管理部门
  266. $departmentIds = self::getChildrenDepartmentIds($admin_id, true);
  267. $dList = collection(DepartmentModel::where('id', 'in', $departmentIds)->select())->toArray();
  268. Tree::instance()->init($dList, 'parent_id');
  269. $departments = \app\admin\model\department\Admin::getDepartments($admin_id);
  270. $issetIDs = array_column($departments, 'id');
  271. foreach ($departments as $m => $n) {
  272. if ($n['parent_id'] != 0) {
  273. if (in_array($n['parent_id'], $issetIDs)) continue;
  274. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(($n['parent_id'])));
  275. foreach ($childlist as $k => $v) {
  276. $k == 0 ? $v['parent_id'] = 0 : '';
  277. $departmentList[] = $v;
  278. }
  279. } else {
  280. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  281. $childlist ? $n['haschild'] = 1 : '';
  282. $departmentList[] = $n;
  283. foreach ($childlist as $k => $v) {
  284. $departmentList[] = $v;
  285. }
  286. }
  287. }
  288. }
  289. return $departmentList;
  290. }
  291. /**
  292. * 获取上级负责人
  293. * @param bool $parent 如果当前部门没负责人,是否逐级寻找?
  294. * @param array $ignore 是否忽略当前uid
  295. * @return array
  296. * @throws \think\db\exception\DataNotFoundException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. * @throws \think\exception\DbException
  299. */
  300. public static function getParentAdminIds($uid, $parent = true,$ignore=false)
  301. {
  302. $principalIds = [];
  303. $departmentIds = self::getDepartmentIds($uid);//获取当前用户的所有部门ID,
  304. if ($departmentIds) {
  305. $principalIds = self::getDprincipalIds($departmentIds, $parent,$ignore?[$uid]:[]);
  306. }
  307. return $principalIds;
  308. }
  309. /**
  310. * 获取部门的负责人
  311. * @param $departmentIds 部门IDs
  312. * @param bool $parent 如果当前部门没负责人,是否逐级寻找?
  313. * @param array $ignore_ids 忽略的adminids
  314. * @return array|bool|string
  315. */
  316. public static function getDprincipalIds($departmentIds, $parent = true,$ignore_ids=[])
  317. {
  318. $daModel=Db::name('department_admin');
  319. if ($ignore_ids){
  320. $daModel->where('da.admin_id', 'not in', $ignore_ids);
  321. }
  322. //获取当前部门负责人
  323. $principalIds =$daModel
  324. ->alias('da')
  325. ->join('__' . strtoupper('department') . '__ d', 'da.department_id = d.id')
  326. ->where('da.department_id', 'in', $departmentIds)
  327. ->where('is_principal', 1)
  328. ->where('d.status', 'normal')
  329. ->column('da.admin_id');
  330. if ($principalIds) {
  331. return $principalIds;//如果存在就直接返回
  332. }
  333. //上一级查找
  334. foreach ($departmentIds as $k => $v) {
  335. $newDepartmentIds = Department::getParentId($v);
  336. if ($newDepartmentIds) {
  337. return self::getDprincipalIds($newDepartmentIds, $parent);
  338. }
  339. }
  340. return [];
  341. }
  342. /**
  343. * 数据权限校验
  344. * @param $auth
  345. * @param $row
  346. * @param string $field
  347. * @return bool
  348. */
  349. public static function checkDataAuth($auth,$row,$field="admin_id"){
  350. if ($auth->data_scope!=1&&!$auth->isSuperAdmin()){
  351. $childrenAdminIds = \app\admin\model\department\Admin::getChildrenAdminIds($auth->id, true);
  352. if (!$row[$field] || !in_array($row[$field], $childrenAdminIds)) {
  353. return false;
  354. }
  355. }
  356. return true;
  357. }
  358. /**
  359. * 获取员工上级所有部门ids
  360. * @param $admin_id
  361. * @param bool $withself
  362. * @return array|mixed
  363. */
  364. public static function getParentDepartmentIds($admin_id, $withself = false)
  365. {
  366. //如已经存在直接返回
  367. static $parentDepartment = [];
  368. if (isset($parentDepartment[$admin_id])) {
  369. return $parentDepartment[$admin_id];
  370. }
  371. //获取当前员工的所在部门
  372. $departmentIds=self::getDepartmentIds($admin_id);
  373. if (!$departmentIds) return [];
  374. $tempdata=array();
  375. foreach ($departmentIds as $departmentId){
  376. $tempdata= array_merge($tempdata,\app\admin\model\department\Department::getParentIds($departmentId,$withself));
  377. }
  378. $parentDepartment[$admin_id] = $tempdata ?: [];
  379. return $parentDepartment[$admin_id];
  380. }
  381. }