EmployeeService.php 44 KB

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