EmployeeService.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Depart;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeDepartPermission;
  7. use App\Model\EmployeeManagerDepart;
  8. use App\Model\EmployeeMenuPermission;
  9. use App\Model\EmployeeRole;
  10. use App\Model\EmployeeTeamPermission;
  11. use App\Model\ProductInventory;
  12. use App\Model\ProductInventorySet;
  13. use App\Model\Role;
  14. use App\Model\RoleMenu;
  15. use App\Model\RoleMenuButton;
  16. use App\Model\Storehouse;
  17. use App\Model\Supplier;
  18. use App\Model\SysMenu;
  19. use App\Model\SysMenuButton;
  20. use App\Model\Team;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Hash;
  23. use Mockery\Exception;
  24. /**
  25. * 人员相关
  26. * @package App\Models
  27. */
  28. class EmployeeService extends Service
  29. {
  30. /**
  31. * 用户编辑
  32. * @param $data
  33. * @param $user
  34. * @return array
  35. */
  36. public function employeeEdit($data,$user){
  37. list($status,$msg) = $this->employeeRule($data,false);
  38. if(!$status) return [$status,$msg];
  39. try {
  40. DB::beginTransaction();
  41. $model = new Employee();
  42. $model = $model->where('id',$data['id'])->first();
  43. $model->number = $data['number'];
  44. $model->emp_name = $data['emp_name'];
  45. $model->mobile = $data['mobile'] ?? '';
  46. $model->leave_time = $data['leave_time'] ?? '';
  47. $model->entry_time = $data['entry_time'] ?? '';
  48. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  49. $model->is_admin = $data['is_admin'];
  50. if($model->is_admin == 1){
  51. $model->account = $data['number'];
  52. if($data['password'] !== '******'){
  53. $model->password = Hash::make($data['password']);
  54. }
  55. }
  56. $model->save();
  57. EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
  58. if(isset($data['depart'])){
  59. $insert = [];
  60. foreach ($data['depart'] as $value){
  61. $insert[] = [
  62. 'employee_id' => $model->id,
  63. 'depart_id' => $value,
  64. ];
  65. }
  66. EmployeeDepartPermission::insert($insert);
  67. }
  68. EmployeeRole::where('employee_id',$data['id'])->update([
  69. 'del_time' => time()
  70. ]);
  71. if(isset($data['role'])){
  72. $insert = [];
  73. foreach ($data['role'] as $value){
  74. $insert[] = [
  75. 'employee_id' => $model->id,
  76. 'role_id' => $value,
  77. 'crt_time' => time(),
  78. 'upd_time' => time(),
  79. ];
  80. }
  81. EmployeeRole::insert($insert);
  82. }
  83. DB::commit();
  84. }catch (\Exception $exception){
  85. DB::rollBack();
  86. return [false, $exception->getMessage()];
  87. }
  88. return [true,''];
  89. }
  90. /**
  91. * 用户新增
  92. * @param $data
  93. * @param $user
  94. * @return array
  95. */
  96. public function employeeAdd($data,$user){
  97. list($status,$msg) = $this->employeeRule($data);
  98. if(!$status) return [$status,$msg];
  99. try{
  100. DB::beginTransaction();
  101. $model = new Employee();
  102. $model->number = $data['number'];
  103. $model->emp_name = $data['emp_name'];
  104. $model->mobile = $data['mobile'] ?? '';
  105. $model->leave_time = $data['leave_time'] ?? '';
  106. $model->entry_time = $data['entry_time'] ?? '';
  107. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  108. $model->crt_id = $user['id'];
  109. $model->is_admin = $data['is_admin'];
  110. if($model->is_admin == 1){
  111. $model->account = $data['number'];
  112. if($data['password'] !== '********'){
  113. $model->password = Hash::make($data['password']);
  114. }
  115. }
  116. $model->save();
  117. if(isset($data['depart'])){
  118. $insert = [];
  119. foreach ($data['depart'] as $value){
  120. $insert[] = [
  121. 'employee_id' => $model->id,
  122. 'depart_id' => $value,
  123. ];
  124. }
  125. EmployeeDepartPermission::insert($insert);
  126. }
  127. if(isset($data['role'])){
  128. $insert = [];
  129. foreach ($data['role'] as $value){
  130. $insert[] = [
  131. 'employee_id' => $model->id,
  132. 'role_id' => $value,
  133. 'crt_time' => time(),
  134. 'upd_time' => time(),
  135. ];
  136. }
  137. EmployeeRole::insert($insert);
  138. }
  139. DB::commit();
  140. }catch (Exception $e){
  141. DB::rollBack();
  142. return [false, $e->getMessage()];
  143. }
  144. return [true,''];
  145. }
  146. /**
  147. * 用户删除
  148. * @param $data
  149. * @return array
  150. */
  151. public function employeeDel($data){
  152. if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
  153. Employee::whereIn('id',$data['id'])->update([
  154. 'del_time'=>time()
  155. ]);
  156. EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
  157. 'del_time'=>time()
  158. ]);
  159. return [true,'删除成功'];
  160. }
  161. /**
  162. * 用户列表
  163. * @param $data
  164. * @param $user
  165. * @return array
  166. */
  167. public function employeeList($data,$user){
  168. $model = Employee::where('del_time',0)
  169. ->select('number','mobile','emp_name','id','entry_time','leave_time','is_admin','state')
  170. ->orderBy('id','desc');
  171. if(! empty($data['depart'])) {
  172. $employee_id = DB::table('employee_depart_permission')
  173. ->where("depart_id", $data['depart'])
  174. ->select("employee_id")
  175. ->get()->toArray();
  176. $employee_id = array_column($employee_id,'employee_id');
  177. $model->whereIn("id", $employee_id);
  178. }else{
  179. $employee_id = $this->getEmployee($user);
  180. $model->whereIn('id',$employee_id);
  181. }
  182. if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
  183. if(! empty($data['emp_name'])) $model->where('emp_name', 'LIKE', '%'.$data['emp_name'].'%');
  184. if(! empty($data['state'])) $model->where('state',$data['state']);
  185. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  186. if(! isset($data['all_emp'])) $model->where('id','<>',Employee::SPECIAL_ADMIN);
  187. if(! empty($data['role'])) {
  188. $emp = EmployeeRole::where('role_id',$data['role'])
  189. ->where('del_time',0)
  190. ->select('employee_id')->get()->toArray();
  191. $model->whereIn('id',array_column($emp,'employee_id'));
  192. }
  193. if($user['id'] != Employee::SPECIAL_ADMIN) $model->where('is_manager',0);
  194. $list = $this->limit($model,'',$data);
  195. //组织数据
  196. $list = $this->organizationEmployeeData($list);
  197. return [true , $list];
  198. }
  199. /**
  200. * 用户数据组装
  201. * @param $data
  202. * @return array
  203. */
  204. public function organizationEmployeeData($data) {
  205. if (empty($data['data'])) return $data;
  206. $res = DB::table('employee_role as a')
  207. ->leftJoin('role as b','a.role_id','=','b.id')
  208. ->where('a.del_time',0)
  209. ->where('b.del_time',0)
  210. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  211. ->select('a.employee_id','b.title','b.id')
  212. ->get()->toArray();
  213. $role = $role2 = [];
  214. foreach ($res as $value){
  215. if(isset($role[$value->employee_id])){
  216. $role[$value->employee_id] .= ',' . $value->title;
  217. }else{
  218. $role[$value->employee_id] = $value->title;
  219. }
  220. $role2[$value->employee_id][] = $value->id;
  221. }
  222. $res = DB::table('employee_depart_permission as a')
  223. ->select('a.employee_id','b.title','b.id')
  224. ->join('depart as b','a.depart_id','=','b.id')
  225. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  226. ->orderBy('b.id')
  227. ->get()->toArray();
  228. $depart_title = $depart_id = [];
  229. foreach ($res as $value){
  230. if(isset($depart_title[$value->employee_id])){
  231. $depart_title[$value->employee_id] .= ',' . $value->title;
  232. }else{
  233. $depart_title[$value->employee_id] = $value->title;
  234. }
  235. $depart_id[$value->employee_id][] = $value->id;
  236. }
  237. foreach ($data['data'] as $key => $value){
  238. $data['data'][$key]['role'] = $role2[$value['id']] ?? [];
  239. $data['data'][$key]['role_name'] = $role[$value['id']] ?? '';
  240. $data['data'][$key]['depart'] = $depart_id[$value['id']] ?? [];
  241. $data['data'][$key]['depart_title'] = $depart_title[$value['id']] ?? '';
  242. }
  243. return $data;
  244. }
  245. //获取当前顶级部门下人员id
  246. public function getEmployee($user){
  247. $top_depart_id = $user['depart_top'][0] ?? [];
  248. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  249. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  250. // 查找所有子级id
  251. $childIds = $this->findChildIds($top_depart_id, $list);
  252. $childIds[] = $top_depart_id;
  253. $employee_id = EmployeeDepartPermission::whereIn('depart_id',$childIds)
  254. ->select("employee_id")
  255. ->get()->toArray();
  256. return array_unique(array_column($employee_id,'employee_id'));
  257. }
  258. /**
  259. * 用户参数规则
  260. * @param $data
  261. * @param $is_add
  262. * @return array
  263. */
  264. public function employeeRule($data,$is_add = true){
  265. if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
  266. if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
  267. if(empty($data['depart'])) return [false,'部门不能为空'];
  268. $mobile = $data['mobile'] ?? "";
  269. $number = $data['number'] ?? "";
  270. if(! $is_add){
  271. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  272. $bool = Employee::where('del_time',0)
  273. ->where('id','<>',$data['id'])
  274. ->where(function ($query) use ($mobile, $number){
  275. $query->where('number', $number);
  276. $query->when(! empty($mobile), function ($query) use ($mobile) {
  277. return $query->orWhere('mobile', $mobile);
  278. });
  279. })->exists();
  280. }else{
  281. $bool = Employee::where('del_time',0)
  282. ->where(function ($query) use ($mobile, $number){
  283. $query->where('number', $number);
  284. $query->when(! empty($mobile), function ($query) use ($mobile) {
  285. return $query->orWhere('mobile', $mobile);
  286. });
  287. })->exists();
  288. }
  289. if($bool) return [false,'工号或手机号码已存在!'];
  290. return [true,''];
  291. }
  292. /**
  293. * 角色编辑
  294. * @param $data
  295. * @return array
  296. */
  297. public function roleEdit($data,$user){
  298. list($status,$msg) = $this->roleRule($data,$user, false);
  299. if(!$status) return [$status,$msg];
  300. $model = new Role();
  301. $model = $model->where('id',$data['id'])->first();
  302. $model->title = $data['title'];
  303. $model->save();
  304. return [true,'保存成功!'];
  305. }
  306. /**
  307. * 角色新增
  308. * @param $data
  309. * @param $user
  310. * @return array
  311. */
  312. public function roleAdd($data,$user){
  313. list($status,$msg) = $this->roleRule($data,$user);
  314. if(!$status) return [$status,$msg];
  315. $model = new Role();
  316. $model->title = $data['title'] ;
  317. $model->depart_id = $data['depart_id'] ?? 0;
  318. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  319. $model->save();
  320. return [true,'保存成功!'];
  321. }
  322. /**
  323. * 角色删除
  324. * @param $data
  325. * @return array
  326. */
  327. public function roleDel($data){
  328. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  329. $bool = EmployeeRole::where('del_time',0)
  330. ->whereIn('role_id',$data['id'])
  331. ->exists();
  332. if($bool) return [false,'角色已绑定人员!'];
  333. Role::where('id',$data['id'])->update([
  334. 'del_time' => time()
  335. ]);
  336. RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
  337. 'del_time' => time()
  338. ]);
  339. RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
  340. 'del_time' => time()
  341. ]);
  342. return [true,'删除成功'];
  343. }
  344. /**
  345. * 角色列表
  346. * @param $data
  347. * @return array
  348. */
  349. public function roleList($data,$user){
  350. $model = Role::TopClear($user,$data);
  351. $model = $model->where('del_time',0)
  352. ->select('title','crt_time','id','upd_time')
  353. ->orderBy('id','desc');
  354. if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
  355. $list = $this->limit($model,'',$data);
  356. return [200,$list];
  357. }
  358. /**
  359. * 角色参数规则
  360. * @param $data
  361. * @param $is_check
  362. * @return array
  363. */
  364. public function roleRule(&$data,$user, $is_check = true){
  365. if($this->isEmpty($data,'title')) return [false,'名称不能为空!'];
  366. //所属部门 以及 顶级部门
  367. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  368. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  369. if($is_check){
  370. $bool = Role::where('title',$data['title'])
  371. ->where('top_depart_id',$data['top_depart_id'])
  372. ->where('del_time',0)
  373. ->exists();
  374. if($bool) return [false,'角色名称已存在!'];
  375. }else{
  376. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  377. $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
  378. $bool = Role::where('title',$data['title'])
  379. ->where('top_depart_id',$top_depart_id)
  380. ->where('id','<>',$data['id'])
  381. ->where('del_time',0)
  382. ->exists();
  383. if($bool) return [false,'角色名称已存在!'];
  384. }
  385. return [true,''];
  386. }
  387. /**
  388. * 角色菜单更新
  389. * @param $data
  390. * @return array
  391. */
  392. public function roleMenu($data){
  393. if(empty($data['role_id'])) return [false,'角色不能为空!'];
  394. if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
  395. DB::beginTransaction();
  396. try {
  397. RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  398. RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
  399. $insert = $insert2 = [];
  400. foreach ($data['menu'] as $t){
  401. $insert[] = [
  402. 'role_id' => $data['role_id'],
  403. 'menu_id' => $t['menu_id'],
  404. 'type' => $t['type'],
  405. 'crt_time' => time()
  406. ];
  407. if(! empty($t['button'])){
  408. foreach ($t['button'] as $b){
  409. $insert2[] = [
  410. 'role_id' => $data['role_id'],
  411. 'menu_id' => $t['menu_id'],
  412. 'button_id' => $b,
  413. 'crt_time' => time()
  414. ];
  415. }
  416. RoleMenuButton::insert($insert2);
  417. }
  418. }
  419. RoleMenu::insert($insert);
  420. DB::commit();
  421. }catch (\Throwable $exception){
  422. DB::rollBack();
  423. return [false,$exception->getMessage()];
  424. }
  425. return [true,'保存成功!'];
  426. }
  427. /**
  428. * 角色详情
  429. * @param $data
  430. * @return array
  431. */
  432. public function roleDetail($data){
  433. if(empty($data['role_id'])) return [false,'请选择角色'];
  434. $role = Role::where('id',$data['role_id'])
  435. ->where('del_time',0)
  436. ->select('id','title')
  437. ->first();
  438. if(empty($role)) return [false,'角色不存在或已被删除'];
  439. $role = $role->toArray();
  440. $menu = RoleMenu::where('role_id',$data['role_id'])
  441. ->where('del_time',0)
  442. ->select('menu_id','type')
  443. ->get()->toArray();
  444. $button = $this->fillRoleButton([$data['role_id']]);
  445. foreach ($menu as $key => $value){
  446. $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
  447. }
  448. $role['menu'] = $menu;
  449. return [true, $role];
  450. }
  451. /**
  452. * 部门编辑
  453. * @param $data
  454. * @return array
  455. */
  456. public function departEdit($data, $user){
  457. list($status,$msg) = $this->departRule($data,$user,false);
  458. if(!$status) return [$status,$msg];
  459. $update = $msg['data'][0];
  460. $model = new Depart();
  461. $model->where('id',$data['id'])->update($update);
  462. return [true,'保存成功!'];
  463. }
  464. /**
  465. * 部门新增
  466. * @param $data
  467. * @param $user
  468. * @return array
  469. */
  470. public function departAdd($data,$user){
  471. list($status,$msg) = $this->departRule($data,$user);
  472. if(!$status) return [$status,$msg];
  473. try {
  474. DB::beginTransaction();
  475. $time = time();
  476. foreach ($msg['data'] as $value){
  477. $model = new Depart();
  478. $model->parent_id = $value['parent_id'];
  479. $model->title = $value['title'];
  480. $model->code = $value['code'];
  481. $model->is_main = $value['is_main'];
  482. $model->basic_type_id = $value['basic_type_id'] ?? 0;
  483. $model->save();
  484. $depart_id = $model->id;
  485. if(empty($depart_id)) {
  486. DB::rollBack();
  487. return [false,'部门新建失败'];
  488. }
  489. if(empty($value['parent_id'])){
  490. $m = new Storehouse();
  491. $m->title = $value['title'];
  492. $m->depart_id = $depart_id;
  493. $m->top_depart_id = $depart_id;
  494. $m->crt_id = $user['id'];
  495. $m->save();
  496. if(empty($m->id)) {
  497. DB::rollBack();
  498. return [false,'仓库生成失败'];
  499. }
  500. $employee = new Employee();
  501. $number = "admin" . $value['code'];
  502. $employee->number = $number;
  503. $employee->emp_name = $value['title'] . "管理员账号";
  504. $employee->entry_time = date('Y-m-d');
  505. $employee->state = 1;
  506. $employee->crt_id = $user['id'];
  507. $employee->is_admin = 1;
  508. $employee->account = $number;
  509. $employee->password = Hash::make("password");
  510. $employee->is_manager = 1;
  511. $employee->save();
  512. if(empty($employee->id)) {
  513. DB::rollBack();
  514. return [false,'管理员账号生成失败'];
  515. }
  516. $depart = new EmployeeDepartPermission();
  517. $depart->employee_id = $employee->id;
  518. $depart->depart_id = $depart_id;
  519. $depart->save();
  520. if(empty($depart->id)) {
  521. DB::rollBack();
  522. return [false,'管理员账号部门关联生成失败'];
  523. }
  524. //公司系统设置
  525. ProductInventorySet::insert(['top_depart_id' => $depart_id, 'param_one' => 1, 'crt_time' => $time]);
  526. // if(! empty($value['is_main'])){
  527. // //总供应商 所有分社可见
  528. // Supplier::insert([
  529. // 'title' => $value['title'],
  530. // 'crt_id' => $user['id'],
  531. // 'crt_time' => $time,
  532. // 'depart_id' => $depart_id,
  533. // 'top_depart_id' => $depart_id,
  534. // 'is_main' => Supplier::is_main,
  535. // ]);
  536. // }
  537. }
  538. }
  539. DB::commit();
  540. }catch (\Exception $exception){
  541. DB::rollBack();
  542. return [false,$exception->getMessage()];
  543. }
  544. return [true,'保存成功!'];
  545. }
  546. /**
  547. * 部门删除
  548. * @param $data
  549. * @return array
  550. */
  551. public function departDel($data){
  552. list($status,$msg) = $this->checkDepartDel($data);
  553. if(! $status) return [false, $msg];
  554. Depart::whereIn('id',$data['id'])->update([
  555. 'del_time'=>time()
  556. ]);
  557. return [true,'删除成功'];
  558. }
  559. /**
  560. * 判断部门是否可以删除
  561. * @param $data
  562. * @return array
  563. */
  564. public function checkDepartDel($data){
  565. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  566. $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
  567. if($bool) return [false,'部门下有子部门!'];
  568. if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
  569. $bool = ProductInventory::where('top_depart_id',$data['id'])->where('del_time',0)->exists();
  570. if($bool) return [false,'部门下的仓库有产品信息!'];
  571. return [true, ''];
  572. }
  573. /**
  574. * 部门列表
  575. * @param $data
  576. * @param $user
  577. * @return array
  578. */
  579. public function departList($data,$user){
  580. $model = Depart::where('del_time',0)
  581. ->select('title','id','code','parent_id','is_main','basic_type_id')
  582. ->orderby('code', 'asc');
  583. if($user['id'] != Employee::SPECIAL_ADMIN && ! $user['is_all_depart']){
  584. $depart_id = $this->getDepartIdList($user);
  585. $model->whereIn('id',$depart_id);
  586. }
  587. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  588. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  589. $list = $model->get()->toArray();
  590. $list = $this->fillDepartList($list, $user);
  591. $list_tree = $list;
  592. if(! empty($list_tree)) {
  593. $list_tree = $this->makeTree(0,$list_tree);
  594. $list_tree = $this->set_sort_circle($list_tree);
  595. }
  596. return [200,['data' => $list,'tree' => $list_tree]];
  597. }
  598. public function fillDepartList($list,$user){
  599. if(empty($list)) return $list;
  600. $basic = BasicType::where('del_time',0)
  601. ->whereIn('id', array_unique(array_column($list,'basic_type_id')))
  602. ->pluck('title','id')->toArray();
  603. foreach ($list as $key => $value){
  604. $list[$key]['basic_type_title'] = $basic[$value['basic_type_id']] ?? '';
  605. $list[$key]['is_show_basic_type'] = $user['is_all_depart'];
  606. }
  607. return $list;
  608. }
  609. //获取可见的部门范围
  610. public function getDepartIdList($user){
  611. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  612. $result = [];
  613. foreach ($user['depart_range'] as $v){
  614. // 查找所有父级id
  615. $parentIds = $this->findParentIds($v, $list);
  616. // 查找所有子级id
  617. $childIds = $this->findChildIds($v, $list);
  618. // 合并父级和子级id
  619. $tmp = array_merge($parentIds, $childIds, [$v]);
  620. $result = array_merge($result,$tmp);
  621. }
  622. return array_unique($result);
  623. }
  624. /**
  625. * 部门参数规则
  626. * @param $data
  627. * @param $is_check
  628. * @return array
  629. */
  630. public function departRule($data,$user, $is_check = true){
  631. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  632. $code = array_column($data['data'],'code');
  633. $title = array_column($data['data'],'title');
  634. $code = array_map(function($val) {
  635. return $val !== null ? $val : 0;
  636. }, $code);
  637. $title = array_map(function($val) {
  638. return $val !== null ? $val : 0;
  639. }, $title);
  640. $code_count = array_count_values($code);
  641. $title_count = array_count_values($title);
  642. foreach ($code as $value){
  643. if(empty($value)) return [false,'编码不能为空!'];
  644. if($code_count[$value] > 1) return [false,'编码不能重复'];
  645. }
  646. foreach ($title as $value){
  647. if(empty($value)) return [false,'名称不能为空!'];
  648. if($title_count[$value] > 1) return [false,'名称不能重复'];
  649. }
  650. $count = 0;
  651. foreach ($data['data'] as $value){
  652. if(empty($value['parent_id']) && ! empty($value['is_main'])) $count ++;
  653. }
  654. if($count > 1) return [false,'顶级总社只允许存在一个!'];
  655. if($count == 1){
  656. $id = $data['id'] ?? 0;
  657. $bool = Depart::where('del_time',0)
  658. ->where('parent_id',0)
  659. ->where('is_main',1)
  660. ->when(! empty($id), function ($query) use ($id) {
  661. return $query->where('id', '<>',$id);
  662. })
  663. ->exists();
  664. if($bool) return [false,'顶级总社只允许存在一个!'];
  665. }
  666. foreach ($data['data'] as $key => $value){
  667. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  668. $data['data'][$key]['upd_time'] = time();
  669. //Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  670. if($is_check){
  671. if(empty($user['is_all_depart']) && empty($value['parent_id'])) return [false,'上级部门必须选择'];
  672. $data['data'][$key]['crt_time'] = time();
  673. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  674. ->where('del_time',0)
  675. ->exists();
  676. }else{
  677. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  678. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  679. ->where('id','<>',$data['id'])
  680. ->where('del_time',0)
  681. ->exists();
  682. }
  683. if($bool) return [false,'编码不能重复'];
  684. }
  685. return [true, $data];
  686. }
  687. /**
  688. * 检测部门下是否存在人员
  689. * @param $depart_id
  690. * @return false
  691. */
  692. public function checkDepartHasPerson($depart_id = []){
  693. if(empty($depart_id)) return false;
  694. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  695. ->leftJoin('employee as b','b.id','a.employee_id')
  696. ->where('b.del_time',0)
  697. ->whereIn('a.depart_id',$depart_id)
  698. ->exists();
  699. return $bool;
  700. }
  701. /**
  702. * 班组编辑
  703. * @param $data
  704. * @return array
  705. */
  706. public function teamEdit($data){
  707. list($status,$msg) = $this->teamRule($data,false);
  708. if(!$status) return [$status,$msg];
  709. $model = new Team();
  710. $model = $model->where('id',$data['id'])->first();
  711. $model->title = $data['title'];
  712. $model->code = $data['code'];
  713. $model->save();
  714. return [true,'保存成功!'];
  715. }
  716. /**
  717. * 班组新增
  718. * @param $data
  719. * @param $user
  720. * @return array
  721. */
  722. public function teamAdd($data,$user){
  723. list($status,$msg) = $this->teamRule($data);
  724. if(!$status) return [$status,$msg];
  725. $model = new Team();
  726. $model->title = $data['title'] ;
  727. $model->code = $data['code'];
  728. $model->save();
  729. return [true,'保存成功!'];
  730. }
  731. /**
  732. * 班组删除
  733. * @param $data
  734. * @return array
  735. */
  736. public function teamDel($data){
  737. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  738. Team::where('id',$data['id'])->update([
  739. 'del_time'=>time()
  740. ]);
  741. return [true,'删除成功'];
  742. }
  743. /**
  744. * 班组列表
  745. * @param $data
  746. * @return array
  747. */
  748. public function teamList($data){
  749. $list = Team::where('del_time',0)
  750. ->select('title','id','crt_time','upd_time','code')
  751. ->orderBy('id','desc');
  752. $list = $this->limit($list,'',$data);
  753. return [200,$list];
  754. }
  755. /**
  756. * 班组参数规则
  757. * @param $data
  758. * @param $is_add
  759. * @return array
  760. */
  761. public function teamRule($data,$is_add = true){
  762. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  763. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  764. $model = Team::where('title',$data['title'])
  765. ->where('code',$data['code'])
  766. ->where('del_time',0);
  767. if(! $is_add){
  768. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  769. $model->where('id','<>',$data['id']);
  770. }
  771. $bool = $model->exists();
  772. if($bool) return [false,'名称和编码已存在!'];
  773. return [true,''];
  774. }
  775. /**
  776. * 班组详情
  777. * @param $data
  778. * @return array
  779. */
  780. public function teamDetail($data){
  781. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  782. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  783. ->leftJoin('employee as b','b.id','a.employee_id')
  784. ->where('team_id',$data['id'])
  785. ->select('b.id','b.emp_name','b.number as code')
  786. ->get()->toArray();
  787. return [true,$result];
  788. }
  789. /**
  790. * 人员权限
  791. * @param $data
  792. * @return array
  793. */
  794. public function employeeRole($data){
  795. $role_ids = [];
  796. $employee_ids = [];
  797. foreach ($data as $v){
  798. if(isset($v['role_id'])){
  799. if(!in_array($v['role_id'],$role_ids)){
  800. $role_ids[] = $v['role_id'];
  801. }
  802. }
  803. if(isset($v['employee_id'])){
  804. if(!in_array($v['employee_id'],$employee_ids)){
  805. $employee_ids[] = $v['employee_id'];
  806. }
  807. }
  808. }
  809. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  810. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  811. EmployeeMenuPermission::insert($data);
  812. return [200,'保存成功!'];
  813. }
  814. /**
  815. * 人员部门关系更新
  816. * @param $data
  817. * @return array
  818. */
  819. public function employeeDepart($data){
  820. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  821. DB::beginTransaction();
  822. try {
  823. if($data['type'] == 1){
  824. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  825. }else{
  826. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  827. }
  828. $insert = [];
  829. foreach ($data['insert']['depart_id'] as $t){
  830. foreach ($data['insert']['employee_id'] as $e){
  831. $insert[] = [
  832. 'depart_id' => $t,
  833. 'employee_id' => $e
  834. ];
  835. }
  836. }
  837. EmployeeDepartPermission::insert($insert);
  838. DB::commit();
  839. }catch (\Throwable $exception){
  840. DB::rollBack();
  841. return [false,$exception->getMessage()];
  842. }
  843. return [true,'保存成功!'];
  844. }
  845. /**
  846. * 人员班组关系更新
  847. * @param $data
  848. * @return array
  849. */
  850. public function employeeTeam($data){
  851. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  852. DB::beginTransaction();
  853. try {
  854. if($data['type'] == 1){
  855. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  856. }else{
  857. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  858. }
  859. $insert = [];
  860. foreach ($data['insert']['team_id'] as $t){
  861. foreach ($data['insert']['employee_id'] as $e){
  862. $insert[] = [
  863. 'team_id' => $t,
  864. 'employee_id' => $e
  865. ];
  866. }
  867. }
  868. EmployeeTeamPermission::insert($insert);
  869. DB::commit();
  870. }catch (\Throwable $exception){
  871. DB::rollBack();
  872. return [false,$exception->getMessage()];
  873. }
  874. return [true,'保存成功!'];
  875. }
  876. /**
  877. * 登陆参数规则
  878. * @param $data
  879. * @return array
  880. */
  881. public function loginRule($data){
  882. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  883. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  884. $account = $data['account'];
  885. $res = Employee::where('del_time',0)
  886. ->where(function ($query)use($account) {
  887. $query->where('account', $account)
  888. ->orWhere('mobile', $account);
  889. })
  890. ->get()->toArray();
  891. if(empty($res)) return [false,'账号不存在或已被删除!'];
  892. if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
  893. $res = reset($res);
  894. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  895. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  896. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  897. //总社分社信息
  898. $return = EmployeeService::getLoginDepart($res['id']);
  899. $is_main = $return[4] ?? 0;
  900. $depart_top = $return[1] ?? [];
  901. $depart_top_title = $depart_top[0]['title'] ?? "";
  902. $depart_top_id = $depart_top[0]['depart_id'] ?? 0;
  903. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main, 'top_depart_id' => $depart_top_id,'top_depart_title' => $depart_top_title]];
  904. }
  905. /**
  906. * 检查人员信息
  907. * @param $userId
  908. * @return array
  909. */
  910. public static function checkUser($userId){
  911. $res = Employee::where('id', $userId)
  912. ->where('del_time',0)
  913. ->where('is_admin',Employee::IS_ADMIN)
  914. ->where('state',Employee::USE)->get()->first();
  915. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  916. return [true, $res];
  917. }
  918. /**
  919. * 获取登录账号的角色
  920. * @param $employee_id
  921. * @return array
  922. */
  923. public static function getPersonRole($employee_id){
  924. if(empty($employee_id) || $employee_id == Employee::SPECIAL_ADMIN) return [];
  925. $role = EmployeeRole::where('del_time',0)
  926. ->where('employee_id',$employee_id)
  927. ->select('role_id')
  928. ->get()->toArray();
  929. //组织
  930. $role_id = array_unique(array_column($role,'role_id'));
  931. asort($role_id);
  932. $role_id = array_values($role_id);
  933. return $role_id;
  934. }
  935. //通过角色获取菜单
  936. public function getMenuByRoleInList($user){
  937. $role_id = $user['role'] ?? [];
  938. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  939. if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
  940. //没绑定角色
  941. if(empty($role_id)) return [];
  942. $role_menu = RoleMenu::whereIn('role_id',$role_id)
  943. ->where('del_time',0)
  944. ->select('menu_id')
  945. ->get()->toArray();
  946. return array_column($role_menu,'menu_id');
  947. }
  948. //通过角色获取菜单以及按钮
  949. public function getMenuByRole($user){
  950. $role_id = $user['role'] ?? [];
  951. $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
  952. $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
  953. $button_map = [];
  954. foreach ($button as $value){
  955. $button_map[$value['menu_id']][] = $value;
  956. }
  957. $object = [];
  958. //超级管理员
  959. if($user['id'] == Employee::SPECIAL_ADMIN){
  960. foreach ($menu as $value){
  961. $object[] = [
  962. 'id' => $value['id'],
  963. // 'type' => 0,//所有权限
  964. 'uri' => $value['uri'],
  965. 'button' => $button_map[$value['id']] ?? [],
  966. ];
  967. }
  968. }else{
  969. //没绑定角色
  970. if(empty($role_id)) return [];
  971. $search = RoleMenu::whereIn('role_id',$role_id)
  972. ->where('del_time',0)
  973. ->select('menu_id','type')
  974. ->get()->toArray();
  975. $menu_map = array_column($menu,'uri','id');
  976. //该角色下 菜单里所有按钮
  977. $button_menu = $this->fillRoleButton($role_id);
  978. $button_t = array_column($button,null,'id');
  979. foreach ($search as $value){
  980. $bt = $button_menu[$value['menu_id']] ?? [];
  981. $new = [];
  982. foreach ($bt as $b){
  983. if(! empty($button_t[$b])) $new[] = $button_t[$b];
  984. }
  985. $object[] = [
  986. 'id' => $value['menu_id'],
  987. 'uri' => $menu_map[$value['menu_id']] ?? '',
  988. // 'type' => $value['type'],
  989. 'button' => $new,
  990. ];
  991. }
  992. }
  993. return $object;
  994. }
  995. /**
  996. * 人员直接绑定部门
  997. * @param $data
  998. * @param $user
  999. * @return array
  1000. */
  1001. public function employeeManagerDepart($data,$user){
  1002. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  1003. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  1004. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  1005. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  1006. 'del_time' => time()
  1007. ]);
  1008. $insert = [];
  1009. foreach ($data['depart_id'] as $value){
  1010. $insert[] = [
  1011. 'employee_id' => $data['employee_id'],
  1012. 'depart_id' => $value,
  1013. 'crt_time' => time(),
  1014. 'upd_time' => time(),
  1015. ];
  1016. }
  1017. EmployeeManagerDepart::insert($insert);
  1018. return [true,''];
  1019. }
  1020. /**
  1021. * 填充角色下的按钮
  1022. * @param $role_id
  1023. * @return array
  1024. */
  1025. public function fillRoleButton($role_id){
  1026. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1027. ->where('del_time',0)
  1028. ->select('menu_id','button_id')
  1029. ->get()->toArray();
  1030. $button_map = [];
  1031. foreach ($button as $value){
  1032. if(! isset($button_map[$value['menu_id']])){
  1033. $button_map[$value['menu_id']][] = $value['button_id'];
  1034. }else{
  1035. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1036. }
  1037. }
  1038. return $button_map;
  1039. }
  1040. /**
  1041. * 获取登录账号的部门
  1042. * @param $employee_id
  1043. * @return array|string[]
  1044. */
  1045. public static function getLoginDepart($employee_id){
  1046. if(empty($employee_id)) return [];
  1047. //自己绑定的部门 启用的部门
  1048. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1049. ->join('depart as b','b.id','a.depart_id')
  1050. ->where('a.employee_id',$employee_id)
  1051. ->where('b.is_use',Depart::IS_UES)
  1052. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1053. ->orderBy('b.parent_id','asc')
  1054. ->orderBy('b.is_main','desc')
  1055. ->orderBy('a.depart_id','asc')
  1056. ->get()->toArray();
  1057. $top = $map = $rule = $head = [];
  1058. $is_all_depart = $is_behind_main = 0;
  1059. if(! empty($depart)){
  1060. //库存校验
  1061. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1062. //所有部门
  1063. $list = Depart::where('del_time',0)->get()->toArray();
  1064. $depart_map = array_column($list,null,'id');
  1065. foreach ($depart as $key => $value){
  1066. if($value['parent_id'] == 0){//顶级
  1067. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1068. $depart[$key]['is_stock'] = $is_stock;
  1069. $top[$value['depart_id']] = [
  1070. 'depart_id' => $value['depart_id'],
  1071. 'is_main' => $value['is_main'],
  1072. 'basic_type_id' => $value['basic_type_id'],
  1073. 'title' => $value['title'],
  1074. 'is_stock' => $is_stock,
  1075. ];
  1076. $map[$value['depart_id']] = $value['depart_id'];
  1077. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1078. if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1079. }else{
  1080. $t = self::getTopParentId($value['depart_id'],$list);
  1081. if($t && isset($depart_map[$t])) {
  1082. $is_stock = $set_map[$t] ?? 1;
  1083. $depart[$key]['is_stock'] = $is_stock;
  1084. $t_tmp = $depart_map[$t] ?? [];
  1085. $top[$t_tmp['id']] = [
  1086. 'depart_id' => $t_tmp['id'],
  1087. 'is_main' => $t_tmp['is_main'],
  1088. 'basic_type_id' => $t_tmp['basic_type_id'],
  1089. 'title' => $t_tmp['title'],
  1090. 'is_stock' => $set_map[$t] ?? 1,
  1091. ];
  1092. $map[$value['depart_id']] = $t;
  1093. if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1094. if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1095. }
  1096. }
  1097. }
  1098. foreach ($depart as $value){
  1099. if(in_array($value['depart_id'],$rule)) continue;
  1100. if(! $value['parent_id']){ //顶级
  1101. if($value['is_main']) {//是总公司
  1102. //所有部门都有
  1103. $rule = array_column($list,'id');
  1104. }else{//不是总公司
  1105. //自己以及子部门
  1106. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1107. $rule = array_merge_recursive($rule,$depart_id);
  1108. }
  1109. }else{//非顶级
  1110. if($value['is_main']) {//是总社
  1111. $top_tmp = $map[$value['depart_id']];
  1112. if(! empty($depart_map[$top_tmp]['is_main'])){
  1113. //顶级公司是总公司 所有部门都有
  1114. $rule = array_column($list,'id');
  1115. }else{
  1116. //顶级公司是分公司 分公司所有部门
  1117. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1118. $rule = array_merge_recursive($rule,$depart_id);
  1119. }
  1120. }else{//不是总社
  1121. $rule = array_merge($rule,[$value['depart_id']]);
  1122. }
  1123. }
  1124. }
  1125. foreach ($list as $value){
  1126. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1127. }
  1128. }
  1129. $top = array_values($top);
  1130. $rule = array_unique($rule);
  1131. return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
  1132. }
  1133. //判断是否总公司
  1134. public static function isMain($employee_id){
  1135. $is_main = 0;
  1136. if(empty($employee_id)) return $is_main;
  1137. //admin账号
  1138. if($employee_id == Employee::SPECIAL_ADMIN) {
  1139. $is_main = 1;
  1140. return $is_main;
  1141. }
  1142. //自己绑定的部门 启用的部门
  1143. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1144. ->join('depart as b','b.id','a.depart_id')
  1145. ->where('a.employee_id',$employee_id)
  1146. ->where('b.is_use',Depart::IS_UES)
  1147. ->select('a.depart_id','b.is_main','b.parent_id')
  1148. ->orderBy('b.parent_id','asc')
  1149. ->orderBy('b.is_main','desc')
  1150. ->orderBy('a.depart_id','asc')
  1151. ->get()->toArray();
  1152. if(! empty($depart)){
  1153. $list = Depart::where('del_time',0)->get()->toArray();
  1154. $depart_map = array_column($list,null,'id');
  1155. foreach ($depart as $value){
  1156. if($value['parent_id'] == 0){//顶级
  1157. if(! empty($value['is_main']) && ! $is_main) $is_main = 1;
  1158. }else{
  1159. $t = self::getTopParentId($value['depart_id'],$list);
  1160. if($t && isset($depart_map[$t])) {
  1161. $tmp_is_main = $depart_map[$t]['is_main'] ?? 0;
  1162. if(! empty($tmp_is_main) && ! $is_main) $is_main = 1;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. return $is_main;
  1168. }
  1169. /**
  1170. * 获取顶级id
  1171. * @param $id
  1172. * @param $data
  1173. * @return int
  1174. */
  1175. public static function getTopParentId($id, $data) {
  1176. foreach ($data as $item) {
  1177. if ($item['id'] == $id) {
  1178. if ($item['parent_id'] == 0) {
  1179. // 找到最顶级的id
  1180. return $item['id'];
  1181. } else {
  1182. // 继续递归查找父级
  1183. return self::getTopParentId($item['parent_id'], $data);
  1184. }
  1185. }
  1186. }
  1187. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1188. return 0;
  1189. }
  1190. /**
  1191. * 递归获取所有id
  1192. * @param $data
  1193. * @param $id
  1194. * @return array
  1195. */
  1196. public static function getAllIds($data, $id) {
  1197. $result = array(); // 存储结果的数组
  1198. foreach ($data as $node) {
  1199. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1200. $result[] = $node['id'];
  1201. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1202. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1203. }
  1204. }
  1205. return $result;
  1206. }
  1207. }