EmployeeService.php 42 KB

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