EmployeeService.php 48 KB

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