EmployeeService.php 41 KB

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