EmployeeService.php 42 KB

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