EmployeeService.php 44 KB

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