EmployeeService.php 47 KB

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