EmployeeService.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. $is_main = EmployeeService::isMain($user['id']);
  574. foreach ($list as $key => $value){
  575. $list[$key]['basic_type_title'] = $basic[$value['basic_type_id']] ?? '';
  576. $list[$key]['is_show_basic_type'] = $is_main;
  577. }
  578. return $list;
  579. }
  580. //获取可见的部门范围
  581. public function getDepartIdList($user){
  582. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  583. $result = [];
  584. foreach ($user['depart_range'] as $v){
  585. // 查找所有父级id
  586. $parentIds = $this->findParentIds($v, $list);
  587. // 查找所有子级id
  588. $childIds = $this->findChildIds($v, $list);
  589. // 合并父级和子级id
  590. $tmp = array_merge($parentIds, $childIds, [$v]);
  591. $result = array_merge($result,$tmp);
  592. }
  593. return array_unique($result);
  594. }
  595. /**
  596. * 部门参数规则
  597. * @param $data
  598. * @param $is_check
  599. * @return array
  600. */
  601. public function departRule($data,$user, $is_check = true){
  602. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  603. $code = array_column($data['data'],'code');
  604. $title = array_column($data['data'],'title');
  605. $code = array_map(function($val) {
  606. return $val !== null ? $val : 0;
  607. }, $code);
  608. $title = array_map(function($val) {
  609. return $val !== null ? $val : 0;
  610. }, $title);
  611. $code_count = array_count_values($code);
  612. $title_count = array_count_values($title);
  613. foreach ($code as $value){
  614. if(empty($value)) return [false,'编码不能为空!'];
  615. if($code_count[$value] > 1) return [false,'编码不能重复'];
  616. }
  617. foreach ($title as $value){
  618. if(empty($value)) return [false,'名称不能为空!'];
  619. if($title_count[$value] > 1) return [false,'名称不能重复'];
  620. }
  621. $count = 0;
  622. foreach ($data['data'] as $value){
  623. if(empty($value['parent_id']) && ! empty($value['is_main'])) $count ++;
  624. }
  625. if($count > 1) return [false,'顶级总社只允许存在一个!'];
  626. if($count == 1){
  627. $id = $data['id'] ?? 0;
  628. $bool = Depart::where('del_time',0)
  629. ->where('parent_id',0)
  630. ->where('is_main',1)
  631. ->when(! empty($id), function ($query) use ($id) {
  632. return $query->where('id', '<>',$id);
  633. })
  634. ->exists();
  635. if($bool) return [false,'顶级总社只允许存在一个!'];
  636. }
  637. foreach ($data['data'] as $key => $value){
  638. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  639. $data['data'][$key]['upd_time'] = time();
  640. //Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  641. if($is_check){
  642. if(empty($user['is_all_depart']) && empty($value['parent_id'])) return [false,'上级部门必须选择'];
  643. $data['data'][$key]['crt_time'] = time();
  644. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  645. ->where('del_time',0)
  646. ->exists();
  647. }else{
  648. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  649. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  650. ->where('id','<>',$data['id'])
  651. ->where('del_time',0)
  652. ->exists();
  653. }
  654. if($bool) return [false,'编码不能重复'];
  655. }
  656. return [true, $data];
  657. }
  658. /**
  659. * 检测部门下是否存在人员
  660. * @param $depart_id
  661. * @return false
  662. */
  663. public function checkDepartHasPerson($depart_id = []){
  664. if(empty($depart_id)) return false;
  665. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  666. ->leftJoin('employee as b','b.id','a.employee_id')
  667. ->where('b.del_time',0)
  668. ->whereIn('a.depart_id',$depart_id)
  669. ->exists();
  670. return $bool;
  671. }
  672. public function departSet($data,$user){
  673. if(empty($data['id'])) return [false,'请选择部门'];
  674. if(empty($data['grade']) || ! is_numeric($data['grade'])) return [false,'请输入分社等级'];
  675. try {
  676. DB::beginTransaction();
  677. Depart::where('id',$data['id'])->update(['grade' => $data['grade']]);
  678. $time = time();
  679. DepartPriceName::where('del_time',0)->where('depart_id',$data['id'])->update([
  680. 'del_time' => $time
  681. ]);
  682. if(! empty($data['title'])){
  683. $insert = [];
  684. foreach ($data['title'] as $value){
  685. $insert[] = [
  686. 'depart_id' => $data['id'],
  687. 'title' => $value,
  688. 'crt_time' => $time,
  689. ];
  690. }
  691. DepartPriceName::insert($insert);
  692. }
  693. DB::commit();
  694. }catch (\Exception $exception){
  695. DB::rollBack();
  696. return [false,$exception->getMessage()];
  697. }
  698. return [true,''];
  699. }
  700. /**
  701. * 班组编辑
  702. * @param $data
  703. * @return array
  704. */
  705. public function teamEdit($data){
  706. list($status,$msg) = $this->teamRule($data,false);
  707. if(!$status) return [$status,$msg];
  708. $model = new Team();
  709. $model = $model->where('id',$data['id'])->first();
  710. $model->title = $data['title'];
  711. $model->code = $data['code'];
  712. $model->save();
  713. return [true,'保存成功!'];
  714. }
  715. /**
  716. * 班组新增
  717. * @param $data
  718. * @param $user
  719. * @return array
  720. */
  721. public function teamAdd($data,$user){
  722. list($status,$msg) = $this->teamRule($data);
  723. if(!$status) return [$status,$msg];
  724. $model = new Team();
  725. $model->title = $data['title'] ;
  726. $model->code = $data['code'];
  727. $model->save();
  728. return [true,'保存成功!'];
  729. }
  730. /**
  731. * 班组删除
  732. * @param $data
  733. * @return array
  734. */
  735. public function teamDel($data){
  736. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  737. Team::where('id',$data['id'])->update([
  738. 'del_time'=>time()
  739. ]);
  740. return [true,'删除成功'];
  741. }
  742. /**
  743. * 班组列表
  744. * @param $data
  745. * @return array
  746. */
  747. public function teamList($data){
  748. $list = Team::where('del_time',0)
  749. ->select('title','id','crt_time','upd_time','code')
  750. ->orderBy('id','desc');
  751. $list = $this->limit($list,'',$data);
  752. return [200,$list];
  753. }
  754. /**
  755. * 班组参数规则
  756. * @param $data
  757. * @param $is_add
  758. * @return array
  759. */
  760. public function teamRule($data,$is_add = true){
  761. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  762. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  763. $model = Team::where('title',$data['title'])
  764. ->where('code',$data['code'])
  765. ->where('del_time',0);
  766. if(! $is_add){
  767. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  768. $model->where('id','<>',$data['id']);
  769. }
  770. $bool = $model->exists();
  771. if($bool) return [false,'名称和编码已存在!'];
  772. return [true,''];
  773. }
  774. /**
  775. * 班组详情
  776. * @param $data
  777. * @return array
  778. */
  779. public function teamDetail($data){
  780. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  781. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  782. ->leftJoin('employee as b','b.id','a.employee_id')
  783. ->where('team_id',$data['id'])
  784. ->select('b.id','b.emp_name','b.number as code')
  785. ->get()->toArray();
  786. return [true,$result];
  787. }
  788. /**
  789. * 人员权限
  790. * @param $data
  791. * @return array
  792. */
  793. public function employeeRole($data){
  794. $role_ids = [];
  795. $employee_ids = [];
  796. foreach ($data as $v){
  797. if(isset($v['role_id'])){
  798. if(!in_array($v['role_id'],$role_ids)){
  799. $role_ids[] = $v['role_id'];
  800. }
  801. }
  802. if(isset($v['employee_id'])){
  803. if(!in_array($v['employee_id'],$employee_ids)){
  804. $employee_ids[] = $v['employee_id'];
  805. }
  806. }
  807. }
  808. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  809. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  810. EmployeeMenuPermission::insert($data);
  811. return [200,'保存成功!'];
  812. }
  813. /**
  814. * 人员部门关系更新
  815. * @param $data
  816. * @return array
  817. */
  818. public function employeeDepart($data){
  819. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  820. DB::beginTransaction();
  821. try {
  822. if($data['type'] == 1){
  823. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  824. }else{
  825. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  826. }
  827. $insert = [];
  828. foreach ($data['insert']['depart_id'] as $t){
  829. foreach ($data['insert']['employee_id'] as $e){
  830. $insert[] = [
  831. 'depart_id' => $t,
  832. 'employee_id' => $e
  833. ];
  834. }
  835. }
  836. EmployeeDepartPermission::insert($insert);
  837. DB::commit();
  838. }catch (\Throwable $exception){
  839. DB::rollBack();
  840. return [false,$exception->getMessage()];
  841. }
  842. return [true,'保存成功!'];
  843. }
  844. /**
  845. * 人员班组关心更新
  846. * @param $data
  847. * @return array
  848. */
  849. public function employeeTeam($data){
  850. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  851. DB::beginTransaction();
  852. try {
  853. if($data['type'] == 1){
  854. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  855. }else{
  856. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  857. }
  858. $insert = [];
  859. foreach ($data['insert']['team_id'] as $t){
  860. foreach ($data['insert']['employee_id'] as $e){
  861. $insert[] = [
  862. 'team_id' => $t,
  863. 'employee_id' => $e
  864. ];
  865. }
  866. }
  867. EmployeeTeamPermission::insert($insert);
  868. DB::commit();
  869. }catch (\Throwable $exception){
  870. DB::rollBack();
  871. return [false,$exception->getMessage()];
  872. }
  873. return [true,'保存成功!'];
  874. }
  875. /**
  876. * 登陆参数规则
  877. * @param $data
  878. * @return array
  879. */
  880. public function loginRule($data){
  881. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  882. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  883. $account = $data['account'];
  884. $res = Employee::where('del_time',0)
  885. ->where(function ($query)use($account) {
  886. $query->where('account', $account)
  887. ->orWhere('mobile', $account);
  888. })
  889. ->get()->toArray();
  890. if(empty($res)) return [false,'账号不存在或已被删除!'];
  891. if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
  892. $res = reset($res);
  893. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  894. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  895. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  896. $is_main = EmployeeService::isMain($res['id']);
  897. $return = EmployeeService::getLoginDepart($res['id']);
  898. $depart_top_title = $return[1] ?? [];
  899. $depart_top_title = $depart_top_title[0]['title'] ?? "";
  900. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main, 'top_depart_title' => $depart_top_title]];
  901. }
  902. /**
  903. * 检查人员信息
  904. * @param $userId
  905. * @return array
  906. */
  907. public static function checkUser($userId){
  908. $res = Employee::where('id', $userId)
  909. ->where('del_time',0)
  910. ->where('is_admin',Employee::IS_ADMIN)
  911. ->where('state',Employee::USE)->get()->first();
  912. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  913. return [true, $res];
  914. }
  915. /**
  916. * 获取登录账号的角色
  917. * @param $employee_id
  918. * @return array
  919. */
  920. public static function getPersonRole($employee_id){
  921. if(empty($employee_id)) return [];
  922. $role = EmployeeRole::where('del_time',0)
  923. ->where('employee_id',$employee_id)
  924. ->select('role_id')
  925. ->get()->toArray();
  926. //组织
  927. $role_id = array_unique(array_column($role,'role_id'));
  928. asort($role_id);
  929. $role_id = array_values($role_id);
  930. return $role_id;
  931. }
  932. /**
  933. * 获取登录账号的角色的菜单
  934. * @param $role_id
  935. * @param $user
  936. * @return array
  937. */
  938. public function getMenuByRole($role_id,$user){
  939. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  940. $button = SysMenuButton::where('del_time',0)->select('id','menu_id')->get()->toArray();
  941. $button_map = [];
  942. foreach ($button as $value){
  943. $button_map[$value['menu_id']][] = $value['id'];
  944. }
  945. $object = [];//返回的模型
  946. if($user['id'] == Employee::SPECIAL_ADMIN || $user['is_manager']){
  947. //超级管理员
  948. foreach ($menu as $value){
  949. $object[] = [
  950. 'type' => 0,//所有权限
  951. 'menu_id' => $value['id'],
  952. 'button' => $button_map[$value['id']] ?? [],
  953. ];
  954. }
  955. return $object;
  956. }
  957. //没绑定角色
  958. if(empty($role_id)) return [];
  959. $search = RoleMenu::whereIn('role_id',$role_id)
  960. ->where('del_time',0)
  961. ->select('menu_id','type')
  962. ->get()->toArray();
  963. $button = $this->fillRoleButton($role_id);
  964. $tmp = [];
  965. foreach ($search as $value){
  966. if(! in_array($value['menu_id'],$tmp)){
  967. $object[] = [
  968. 'menu_id' => $value['menu_id'],
  969. 'type' => $value['type'],
  970. 'button' => $button[$value['menu_id']] ?? [],
  971. ];
  972. $tmp[] = $value['menu_id'];
  973. }
  974. }
  975. unset($tmp);
  976. return $object;
  977. }
  978. /**
  979. * 人员直接绑定部门
  980. * @param $data
  981. * @param $user
  982. * @return array
  983. */
  984. public function employeeManagerDepart($data,$user){
  985. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  986. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  987. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  988. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  989. 'del_time' => time()
  990. ]);
  991. $insert = [];
  992. foreach ($data['depart_id'] as $value){
  993. $insert[] = [
  994. 'employee_id' => $data['employee_id'],
  995. 'depart_id' => $value,
  996. 'crt_time' => time(),
  997. 'upd_time' => time(),
  998. ];
  999. }
  1000. EmployeeManagerDepart::insert($insert);
  1001. return [true,''];
  1002. }
  1003. /**
  1004. * 填充角色下的按钮
  1005. * @param $role_id
  1006. * @return array
  1007. */
  1008. public function fillRoleButton($role_id){
  1009. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1010. ->where('del_time',0)
  1011. ->select('menu_id','button_id')
  1012. ->get()->toArray();
  1013. $button_map = [];
  1014. foreach ($button as $value){
  1015. if(! isset($button_map[$value['menu_id']])){
  1016. $button_map[$value['menu_id']][] = $value['button_id'];
  1017. }else{
  1018. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1019. }
  1020. }
  1021. return $button_map;
  1022. }
  1023. /**
  1024. * 获取登录账号的部门
  1025. * @param $employee_id
  1026. * @return array|string[]
  1027. */
  1028. public static function getLoginDepart($employee_id){
  1029. if(empty($employee_id)) return [];
  1030. //自己绑定的部门 启用的部门
  1031. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1032. ->join('depart as b','b.id','a.depart_id')
  1033. ->where('a.employee_id',$employee_id)
  1034. ->where('b.is_use',Depart::IS_UES)
  1035. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1036. ->orderBy('b.parent_id','asc')
  1037. ->orderBy('b.is_main','desc')
  1038. ->orderBy('a.depart_id','asc')
  1039. ->get()->toArray();
  1040. $top = $map = $rule = [];
  1041. $is_all_depart = 0;
  1042. if(! empty($depart)){
  1043. $list = Depart::where('del_time',0)->get()->toArray();
  1044. $depart_map = array_column($list,null,'id');
  1045. foreach ($depart as $value){
  1046. if($value['parent_id'] == 0){//顶级
  1047. $top[$value['depart_id']] = [
  1048. 'depart_id' => $value['depart_id'],
  1049. 'is_main' => $value['is_main'],
  1050. 'basic_type_id' => $value['basic_type_id'],
  1051. 'title' => $value['title'],
  1052. ];
  1053. $map[$value['depart_id']] = $value['depart_id'];
  1054. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1055. }else{
  1056. $t = self::getTopParentId($value['depart_id'],$list);
  1057. if($t && isset($depart_map[$t])) {
  1058. $t_tmp = $depart_map[$t] ?? [];
  1059. $top[$t_tmp['id']] = [
  1060. 'depart_id' => $t_tmp['id'],
  1061. 'is_main' => $t_tmp['is_main'],
  1062. 'basic_type_id' => $t_tmp['basic_type_id'],
  1063. 'title' => $t_tmp['title'],
  1064. ];
  1065. $map[$value['depart_id']] = $t;
  1066. if(! empty($tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1067. }
  1068. }
  1069. }
  1070. foreach ($depart as $value){
  1071. if(in_array($value['depart_id'],$rule)) continue;
  1072. if(! $value['parent_id']){ //顶级
  1073. if($value['is_main']) {//是总公司
  1074. //所有部门都有
  1075. $rule = array_column($list,'id');
  1076. }else{//不是总公司
  1077. //自己以及子部门
  1078. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1079. $rule = array_merge_recursive($rule,$depart_id);
  1080. }
  1081. }else{//非顶级
  1082. if($value['is_main']) {//是总社
  1083. $top_tmp = $map[$value['depart_id']];
  1084. if(! empty($depart_map[$top_tmp]['is_main'])){
  1085. //顶级公司是总公司 所有部门都有
  1086. $rule = array_column($list,'id');
  1087. }else{
  1088. //顶级公司是分公司 分公司所有部门
  1089. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1090. $rule = array_merge_recursive($rule,$depart_id);
  1091. }
  1092. }else{//不是总社
  1093. $rule = array_merge($rule,[$value['depart_id']]);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. return [$depart, array_values($top), $map, array_unique($rule), $is_all_depart];
  1099. }
  1100. //判断是否总公司
  1101. public static function isMain($employee_id){
  1102. $is_main = 0;
  1103. if(empty($employee_id)) return $is_main;
  1104. //admin账号
  1105. if($employee_id == Employee::SPECIAL_ADMIN) {
  1106. $is_main = 1;
  1107. return $is_main;
  1108. }
  1109. //自己绑定的部门 启用的部门
  1110. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1111. ->join('depart as b','b.id','a.depart_id')
  1112. ->where('a.employee_id',$employee_id)
  1113. ->where('b.is_use',Depart::IS_UES)
  1114. ->select('a.depart_id','b.is_main','b.parent_id')
  1115. ->orderBy('b.parent_id','asc')
  1116. ->orderBy('b.is_main','desc')
  1117. ->orderBy('a.depart_id','asc')
  1118. ->get()->toArray();
  1119. if(! empty($depart)){
  1120. $list = Depart::where('del_time',0)->get()->toArray();
  1121. $depart_map = array_column($list,null,'id');
  1122. foreach ($depart as $value){
  1123. if($value['parent_id'] == 0){//顶级
  1124. if(! empty($value['is_main']) && ! $is_main) $is_main = 1;
  1125. }else{
  1126. $t = self::getTopParentId($value['depart_id'],$list);
  1127. if($t && isset($depart_map[$t])) {
  1128. $tmp_is_main = $depart_map[$t]['is_main'] ?? 0;
  1129. if(! empty($tmp_is_main) && ! $is_main) $is_main = 1;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. return $is_main;
  1135. }
  1136. /**
  1137. * 获取顶级id
  1138. * @param $id
  1139. * @param $data
  1140. * @return int
  1141. */
  1142. public static function getTopParentId($id, $data) {
  1143. foreach ($data as $item) {
  1144. if ($item['id'] == $id) {
  1145. if ($item['parent_id'] == 0) {
  1146. // 找到最顶级的id
  1147. return $item['id'];
  1148. } else {
  1149. // 继续递归查找父级
  1150. return self::getTopParentId($item['parent_id'], $data);
  1151. }
  1152. }
  1153. }
  1154. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1155. return 0;
  1156. }
  1157. /**
  1158. * 递归获取所有id
  1159. * @param $data
  1160. * @param $id
  1161. * @return array
  1162. */
  1163. public static function getAllIds($data, $id) {
  1164. $result = array(); // 存储结果的数组
  1165. foreach ($data as $node) {
  1166. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1167. $result[] = $node['id'];
  1168. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1169. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1170. }
  1171. }
  1172. return $result;
  1173. }
  1174. }