EmployeeService.php 42 KB

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