EmployeeService.php 42 KB

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