EmployeeService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeDepartPermission;
  6. use App\Model\EmployeeManagerDepart;
  7. use App\Model\EmployeeMenuPermission;
  8. use App\Model\EmployeeRole;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\Role;
  11. use App\Model\RoleMenu;
  12. use App\Model\SysMenu;
  13. use App\Model\Team;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Hash;
  16. use Mockery\Exception;
  17. /**
  18. * 人员相关
  19. * @package App\Models
  20. */
  21. class EmployeeService extends Service
  22. {
  23. public function employeeEdit($data,$user){
  24. list($status,$msg) = $this->employeeRule($data,false);
  25. if(!$status) return [$status,$msg];
  26. try{
  27. DB::beginTransaction();
  28. //人员
  29. $model = new Employee();
  30. $model = $model->where('id',$data['id'])->first();
  31. $model->mark = $data['mark'] ?? '';
  32. if($data['password'] !== '********') $model->password = Hash::make($data['password']);
  33. $model->save();
  34. //角色
  35. EmployeeRole::where('employee_id',$data['id'])->update([
  36. 'del_time' => time()
  37. ]);
  38. if(isset($data['role'])){
  39. $insert = [];
  40. foreach ($data['role'] as $value){
  41. $insert[] = [
  42. 'employee_id' => $model->id,
  43. 'role_id' => $value,
  44. 'crt_time' => time(),
  45. 'upd_time' => time(),
  46. ];
  47. }
  48. EmployeeRole::insert($insert);
  49. }
  50. DB::commit();
  51. }catch (Exception $e){
  52. DB::rollBack();
  53. return [false, $e->getMessage()];
  54. }
  55. return [true,'保存成功!'];
  56. }
  57. public function employeeAdd($data,$user){
  58. list($status,$msg) = $this->employeeRule($data);
  59. if(!$status) return [$status,$msg];
  60. try {
  61. $model = new Employee();
  62. $model->account = $data['account'];
  63. $model->password = Hash::make($data['password']);
  64. $model->mark = $data['mark'] ?? '';
  65. $model->save();
  66. if(isset($data['role'])){
  67. $insert = [];
  68. foreach ($data['role'] as $value){
  69. $insert[] = [
  70. 'employee_id' => $model->id,
  71. 'role_id' => $value,
  72. 'crt_time' => time(),
  73. 'upd_time' => time(),
  74. ];
  75. }
  76. EmployeeRole::insert($insert);
  77. }
  78. DB::commit();
  79. }catch (Exception $e){
  80. DB::rollBack();
  81. return [false, $e->getMessage()];
  82. }
  83. return [true,'保存成功!'];
  84. }
  85. public function employeeDel($data){
  86. if($this->isEmpty($data,'id')) return [false,'请选择人员!'];
  87. Employee::where('id',$data['id'])->update([
  88. 'del_time'=>time()
  89. ]);
  90. return [true,'删除成功'];
  91. }
  92. public function employeeList($data){
  93. $model = Employee::where('del_time',0)
  94. ->select('crt_time','account','upd_time','id','mark')
  95. ->orderBy('id','desc');
  96. if(! empty($data['account'])) $model->where('account', 'LIKE', '%'.$data['account'].'%');
  97. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  98. $list = $this->limit($model,'',$data);
  99. //组织数据
  100. $list = $this->organizationEmployeeData($list);
  101. return [true, $list];
  102. }
  103. public function organizationEmployeeData($data) {
  104. if (empty($data['data'])) return $data;
  105. $res = DB::table('employee_role as a')
  106. ->join('role as b','a.employee_id','=','b.id')
  107. ->where('a.del_time',0)
  108. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  109. ->select('a.employee_id','b.title')
  110. ->get()->toArray();
  111. $role = [];
  112. foreach ($res as $value){
  113. if(isset($role[$value['employee_id']])){
  114. $role[$value['employee_id']] .= ',' . $value['title'];
  115. }else{
  116. $role[$value['employee_id']] = $value['title'];
  117. }
  118. }
  119. foreach ($data['data'] as $key => $value){
  120. $data['data'][$key]['role'] = $role[$value['id']] ?? '';
  121. $data['data'][$key]['crt_time'] = date("Y-m-d",$value['crt_time']);
  122. }
  123. return $data;
  124. }
  125. public function employeeRule($data,$is_add = true){
  126. if(empty($data['account'])) return [false,'账户不能为空!'];
  127. if(! $is_add){
  128. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  129. }else{
  130. $bool = Employee::where('account',$data['account'])
  131. ->where('del_time',0)->exists();
  132. if($bool) return [false,'账户已存在!'];
  133. }
  134. return [true,''];
  135. }
  136. public function roleEdit($data){
  137. list($status,$msg) = $this->roleRule($data,false);
  138. if(!$status) return [$status,$msg];
  139. $model = new Role();
  140. $model = $model->where('id',$data['id'])->first();
  141. $model->code = $data['code'];
  142. $model->title = $data['title'];
  143. $model->save();
  144. return [true,'保存成功!'];
  145. }
  146. public function roleAdd($data,$user){
  147. list($status,$msg) = $this->roleRule($data);
  148. if(!$status) return [$status,$msg];
  149. $model = new Role();
  150. $model->code = $data['code'];
  151. $model->title = $data['title'] ;
  152. $model->save();
  153. return [true,'保存成功!'];
  154. }
  155. public function roleDel($data){
  156. if($this->isEmpty($data,'id')) return [false,'请选择角色!'];
  157. if(! is_array($data['id'])) $data['id'] = [$data['id']];
  158. $bool = EmployeeRole::where('del_time',0)
  159. ->whereIn('role_id',$data['id'])
  160. ->exists();
  161. if($bool) return [false,'角色已绑定人员!'];
  162. Role::whereIn('id',$data['id'])->update([
  163. 'del_time'=>time()
  164. ]);
  165. return [true,'删除成功'];
  166. }
  167. public function roleList($data){
  168. $list = Role::where('del_time',0)
  169. ->select('code','title','crt_time','id','upd_time')
  170. ->orderBy('id','desc');
  171. $list = $this->limit($list,'',$data);
  172. return [200,$list];
  173. }
  174. public function roleRule($data,$is_add = true){
  175. if($this->isEmpty($data,'title')) return [false,'角色名称不能为空!'];
  176. if($this->isEmpty($data,'code')) return [false,'角色编码不能为空!'];
  177. if(! $is_add){
  178. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  179. $bool = Role::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  180. ->where('id','<>',$data['id'])
  181. ->where('del_time',0)->exists();
  182. }else{
  183. $bool = Role::wherewhereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  184. ->where('del_time',0)->exists();
  185. }
  186. if($bool) return [false,'角色名称或编码已存在!'];
  187. return [true,''];
  188. }
  189. public function roleMenu($data){
  190. if(empty($data['role_id'])) return [false,'角色不能为空!'];
  191. if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
  192. DB::beginTransaction();
  193. try {
  194. RoleMenu::where('role_id',$data['role_id'])->update(['del_time' => time()]);
  195. $insert = [];
  196. foreach ($data['menu'] as $t){
  197. $insert[] = [
  198. 'role_id' => $data['role_id'],
  199. 'menu_id' => $t['menu_id'],
  200. 'type' => $t['type'],
  201. 'crt_time' => time()
  202. ];
  203. }
  204. RoleMenu::insert($insert);
  205. DB::commit();
  206. }catch (\Throwable $exception){
  207. DB::rollBack();
  208. return [false,$exception->getMessage()];
  209. }
  210. return [true,'保存成功!'];
  211. }
  212. public function departEdit($data){
  213. list($status,$msg) = $this->departRule($data,false);
  214. if(!$status) return [$status,$msg];
  215. $update = $msg['data'][0];
  216. $model = new Depart();
  217. $model->where('id',$data['id'])->update($update);
  218. return [true,'保存成功!'];
  219. }
  220. public function departAdd($data){
  221. list($status,$msg) = $this->departRule($data);
  222. if(!$status) return [$status,$msg];
  223. Depart::insert($msg['data']);
  224. return [true,'保存成功!'];
  225. }
  226. public function departDel($data){
  227. list($status,$msg) = $this->checkDepartDel($data);
  228. if(! $status) return [false, $msg];
  229. Depart::whereIn('id',$data['id'])->update([
  230. 'del_time'=>time()
  231. ]);
  232. return [true,'删除成功'];
  233. }
  234. public function departList($data){
  235. $model = Depart::where('del_time',0)
  236. ->select('title','id','code','parent_id','is_use')
  237. ->orderby('code', 'asc');
  238. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  239. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  240. $list = $model->get()->toArray();
  241. if(! empty($list)) {
  242. $list = $this->makeTree(0,$list);
  243. $list = $this->set_sort_circle($list);
  244. }
  245. return [200,$list];
  246. }
  247. public function departRule($data, $is_check = true){
  248. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  249. $code = array_column($data['data'],'code');
  250. $title = array_column($data['data'],'title');
  251. $code = array_map(function($val) {
  252. return $val !== null ? $val : 0;
  253. }, $code);
  254. $title = array_map(function($val) {
  255. return $val !== null ? $val : 0;
  256. }, $title);
  257. $code_count = array_count_values($code);
  258. $title_count = array_count_values($title);
  259. foreach ($code as $value){
  260. if(empty($value)) return [false,'编码不能为空!'];
  261. if($code_count[$value] > 1) return [false,'编码不能重复'];
  262. }
  263. foreach ($title as $value){
  264. if(empty($value)) return [false,'名称不能为空!'];
  265. if($title_count[$value] > 1) return [false,'名称不能重复'];
  266. }
  267. $depart_id = array_filter(array_column($data['data'],'parent_id'));
  268. $res = $this->checkDepartHasPerson($depart_id);
  269. if($res) return [false,'部门下已有人员,不能新建子部门!'];
  270. foreach ($data['data'] as $key => $value){
  271. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  272. $data['data'][$key]['upd_time'] = time();
  273. if($is_check){
  274. $data['data'][$key]['crt_time'] = time();
  275. $bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  276. ->where('del_time',0)
  277. ->exists();
  278. }else{
  279. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  280. if(! $value['is_use']) {
  281. $bool_is = $this->checkDepartHasPerson([$data['id']]);
  282. if($bool_is) return [false,'部门下已经有人员,停用失败!'];
  283. }
  284. $bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  285. ->where('id','<>',$data['id'])
  286. ->where('del_time',0)
  287. ->exists();
  288. }
  289. if($bool) return [false,'编码或部门名称不能重复'];
  290. }
  291. return [true, $data];
  292. }
  293. public function checkDepartDel($data){
  294. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  295. $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
  296. if($bool) return [false,'部门下有子部门!'];
  297. if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
  298. return [true, ''];
  299. }
  300. //检测部门下是否存在人员
  301. public function checkDepartHasPerson($depart_id = []){
  302. if(empty($depart_id)) return false;
  303. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  304. ->leftJoin('employee as b','b.id','a.employee_id')
  305. ->where('b.del_time',0)
  306. ->whereIn('a.depart_id',$depart_id)
  307. ->exists();
  308. return $bool;
  309. }
  310. public function teamEdit($data){
  311. list($status,$msg) = $this->teamRule($data,false);
  312. if(!$status) return [$status,$msg];
  313. $model = new Team();
  314. $model = $model->where('id',$data['id'])->first();
  315. $model->title = $data['title'];
  316. $model->code = $data['code'];
  317. $model->save();
  318. return [true,'保存成功!'];
  319. }
  320. public function teamAdd($data,$user){
  321. list($status,$msg) = $this->teamRule($data);
  322. if(!$status) return [$status,$msg];
  323. $model = new Team();
  324. $model->title = $data['title'] ;
  325. $model->code = $data['code'];
  326. $model->save();
  327. return [true,'保存成功!'];
  328. }
  329. public function teamDel($data){
  330. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  331. Team::where('id',$data['id'])->update([
  332. 'del_time'=>time()
  333. ]);
  334. return [true,'删除成功'];
  335. }
  336. public function teamList($data){
  337. $list = Team::where('del_time',0)->select('title','id','crt_time','upd_time','code')->orderBy('id','desc');
  338. $list = $this->limit($list,'',$data);
  339. return [200,$list];
  340. }
  341. public function teamRule($data,$is_add = true){
  342. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  343. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  344. $model = Team::where('title',$data['title'])
  345. ->where('code',$data['code'])
  346. ->where('del_time',0);
  347. if(! $is_add){
  348. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  349. $model->where('id','<>',$data['id']);
  350. }
  351. $bool = $model->exists();
  352. if($bool) return [false,'名称和编码已存在!'];
  353. return [true,''];
  354. }
  355. public function teamDetail($data){
  356. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  357. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  358. ->leftJoin('employee as b','b.id','a.employee_id')
  359. ->where('team_id',$data['id'])
  360. ->select('b.id','b.emp_name','b.number as code')
  361. ->get()->toArray();
  362. return [true,$result];
  363. }
  364. public function employeeRole($data){
  365. $role_ids = [];
  366. $employee_ids = [];
  367. foreach ($data as $v){
  368. if(isset($v['role_id'])){
  369. if(!in_array($v['role_id'],$role_ids)){
  370. $role_ids[] = $v['role_id'];
  371. }
  372. }
  373. if(isset($v['employee_id'])){
  374. if(!in_array($v['employee_id'],$employee_ids)){
  375. $employee_ids[] = $v['employee_id'];
  376. }
  377. }
  378. }
  379. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  380. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  381. EmployeeMenuPermission::insert($data);
  382. return [200,'保存成功!'];
  383. }
  384. public function employeeDepart($data){
  385. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  386. DB::beginTransaction();
  387. try {
  388. if($data['type'] == 1){
  389. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  390. }else{
  391. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  392. }
  393. $insert = [];
  394. foreach ($data['insert']['depart_id'] as $t){
  395. foreach ($data['insert']['employee_id'] as $e){
  396. $insert[] = [
  397. 'depart_id' => $t,
  398. 'employee_id' => $e
  399. ];
  400. }
  401. }
  402. EmployeeDepartPermission::insert($insert);
  403. DB::commit();
  404. }catch (\Throwable $exception){
  405. DB::rollBack();
  406. return [false,$exception->getMessage()];
  407. }
  408. return [true,'保存成功!'];
  409. }
  410. public function employeeTeam($data){
  411. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  412. DB::beginTransaction();
  413. try {
  414. if($data['type'] == 1){
  415. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  416. }else{
  417. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  418. }
  419. $insert = [];
  420. foreach ($data['insert']['team_id'] as $t){
  421. foreach ($data['insert']['employee_id'] as $e){
  422. $insert[] = [
  423. 'team_id' => $t,
  424. 'employee_id' => $e
  425. ];
  426. }
  427. }
  428. EmployeeTeamPermission::insert($insert);
  429. DB::commit();
  430. }catch (\Throwable $exception){
  431. DB::rollBack();
  432. return [false,$exception->getMessage()];
  433. }
  434. return [true,'保存成功!'];
  435. }
  436. public function loginRule($data){
  437. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  438. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  439. $account = $data['account'];
  440. $res = Employee::where('del_time',0)
  441. ->where('account', $account)
  442. ->get()->toArray();
  443. if(empty($res)) return [false,'账号不存在或已被删除!'];
  444. $res = reset($res);
  445. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  446. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  447. //人员角色
  448. $role = EmployeeService::getPersonRole($res['id']);
  449. //人员角色的菜单
  450. $role_menu = EmployeeService::getMenuByRole($role);
  451. return [true, ['id'=>$res['id'], 'account' => $res['account'], 'role' => $role, 'role_menu' => $role_menu]];
  452. }
  453. public static function checkUser($userId){
  454. $res = Employee::where('id', $userId)
  455. ->where('del_time',0)
  456. ->where('state',Employee::USE)->get()->first();
  457. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  458. return [true, $res];
  459. }
  460. //获取登录账号的角色
  461. public static function getPersonRole($employee_id){
  462. if(empty($employee_id)) return [];
  463. $role = EmployeeRole::where('del_time',0)
  464. ->where('employee_id',$employee_id)
  465. ->select('role_id')
  466. ->get()->toArray();
  467. //组织
  468. $role_id = array_column($role,'role_id');
  469. asort($role_id);
  470. $role_id = array_values($role_id);
  471. return $role_id;
  472. }
  473. //获取登录账号的角色的菜单
  474. public static function getMenuByRole($role_id){
  475. if(empty($role_id)) return [];
  476. return RoleMenu::whereIn('role_id',$role_id)
  477. ->where('del_time',0)
  478. ->select('menu_id','type')
  479. ->get()->toArray();
  480. }
  481. //获取登录账号的权限部门
  482. public static function getPersonDepart($employee_id){
  483. if(empty($employee_id)) return [];
  484. //操作人员直接绑定部门
  485. $employee_manager_depart = EmployeeManagerDepart::where('del_time',0)
  486. ->where('employee_id',$employee_id)
  487. ->select('depart_id')
  488. ->get()->toArray();
  489. //操作人员绑定角色
  490. $employee_role = EmployeeRole::from('employee_role as a')
  491. ->leftJoin('role_depart as b','b.role_id','a.role_id')
  492. ->select('b.depart_id','b.role_id')
  493. ->where('a.del_time',0)
  494. ->where('b.del_time',0)
  495. ->where('a.employee_id',$employee_id)
  496. ->get()->toArray();
  497. return array_filter(array_merge_recursive(array_column($employee_manager_depart,'depart_id'),array_column($employee_role,'depart_id')));
  498. }
  499. //人员直接绑定部门
  500. public function employeeManagerDepart($data,$user){
  501. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  502. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  503. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  504. 'del_time' => time()
  505. ]);
  506. $insert = [];
  507. foreach ($data['depart_id'] as $value){
  508. $insert[] = [
  509. 'employee_id' => $data['employee_id'],
  510. 'depart_id' => $value,
  511. 'crt_time' => time(),
  512. 'upd_time' => time(),
  513. ];
  514. }
  515. EmployeeManagerDepart::insert($insert);
  516. return [true,''];
  517. }
  518. }