EmployeeService.php 46 KB

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