EmployeeService.php 47 KB

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