EmployeeService.php 45 KB

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