EmployeeService.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Depart;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeDepartPermission;
  7. use App\Model\EmployeeManagerDepart;
  8. use App\Model\EmployeeMenuPermission;
  9. use App\Model\EmployeeRole;
  10. use App\Model\EmployeeTeamPermission;
  11. use App\Model\ProductInventory;
  12. use App\Model\ProductInventorySet;
  13. use App\Model\Role;
  14. use App\Model\RoleMenu;
  15. use App\Model\RoleMenuButton;
  16. use App\Model\Storehouse;
  17. use App\Model\Supplier;
  18. use App\Model\SysMenu;
  19. use App\Model\SysMenuButton;
  20. use App\Model\Team;
  21. use App\Model\WxEmployeeOfficial;
  22. use App\Service\Weixin\WeixinService;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Hash;
  25. use Mockery\Exception;
  26. /**
  27. * 人员相关
  28. * @package App\Models
  29. */
  30. class EmployeeService extends Service
  31. {
  32. public function employeeEditOther($data,$user){
  33. list($status,$msg) = $this->employeeOtherRule($data,$user);
  34. if(!$status) return [$status,$msg];
  35. try {
  36. DB::beginTransaction();
  37. $model = new Employee();
  38. $model = $model->where('id',$user['id'])->first();
  39. $model->password = Hash::make($data['new_password']);
  40. $model->save();
  41. DB::commit();
  42. }catch (\Exception $exception){
  43. DB::rollBack();
  44. return [false, $exception->getMessage()];
  45. }
  46. return [true,''];
  47. }
  48. public function employeeOtherRule($data,$user){
  49. if(! isset($data['old_password'])) return [false,'请输入原密码'];
  50. if($data['old_password'] == "") return [false,'原密码不能为空'];
  51. if(! isset($data['new_password'])) return [false,'请输入新密码'];
  52. if($data['new_password'] == "") return [false,'新密码不能为空'];
  53. if(! isset($data['re_password'])) return [false,'请输入确认密码'];
  54. if($data['re_password'] == "") return [false,'确认密码不能为空'];
  55. if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
  56. if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
  57. if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
  58. return [true,''];
  59. }
  60. /**
  61. * 用户编辑
  62. * @param $data
  63. * @param $user
  64. * @return array
  65. */
  66. public function employeeEdit($data,$user){
  67. list($status,$msg) = $this->employeeRule($data,false);
  68. if(!$status) return [$status,$msg];
  69. try {
  70. DB::beginTransaction();
  71. $model = new Employee();
  72. $model = $model->where('id',$data['id'])->first();
  73. $model->number = $data['number'];
  74. $model->emp_name = $data['emp_name'];
  75. $model->mobile = $data['mobile'] ?? '';
  76. $model->leave_time = $data['leave_time'] ?? '';
  77. $model->entry_time = $data['entry_time'] ?? '';
  78. $model->birth_date = $data['birth_date']??'';
  79. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  80. $model->is_admin = $data['is_admin'];
  81. $model->account = $data['number'];
  82. if($model->pic != $data['pic']){
  83. // 使用正则表达式匹配特定路径部分并替换为空
  84. $pattern = '/^https?:\/\/[^\/]+\/image\//';
  85. $replacedUrl = preg_replace($pattern, '', $data['pic']);
  86. (new FileUploadService())->delLocalPublicFile($replacedUrl);
  87. }
  88. $model->pic = $data['pic'] ?? "";
  89. if($model->is_admin == 1){
  90. if($data['password'] !== '******'){
  91. $model->password = Hash::make($data['password']);
  92. }
  93. }
  94. $model->save();
  95. EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
  96. if(isset($data['depart'])){
  97. $insert = [];
  98. foreach ($data['depart'] as $value){
  99. $insert[] = [
  100. 'employee_id' => $model->id,
  101. 'depart_id' => $value,
  102. ];
  103. }
  104. EmployeeDepartPermission::insert($insert);
  105. }
  106. EmployeeRole::where('employee_id',$data['id'])->update([
  107. 'del_time' => time()
  108. ]);
  109. if(isset($data['role'])){
  110. $insert = [];
  111. foreach ($data['role'] as $value){
  112. $insert[] = [
  113. 'employee_id' => $model->id,
  114. 'role_id' => $value,
  115. 'crt_time' => time(),
  116. 'upd_time' => time(),
  117. ];
  118. }
  119. EmployeeRole::insert($insert);
  120. }
  121. DB::commit();
  122. }catch (\Exception $exception){
  123. DB::rollBack();
  124. return [false, $exception->getMessage()];
  125. }
  126. return [true,''];
  127. }
  128. /**
  129. * 用户新增
  130. * @param $data
  131. * @param $user
  132. * @return array
  133. */
  134. public function employeeAdd($data,$user){
  135. list($status,$msg) = $this->employeeRule($data);
  136. if(!$status) return [$status,$msg];
  137. try{
  138. DB::beginTransaction();
  139. $model = new Employee();
  140. $model->number = $data['number'];
  141. $model->emp_name = $data['emp_name'];
  142. $model->mobile = $data['mobile'] ?? '';
  143. $model->leave_time = $data['leave_time'] ?? '';
  144. $model->entry_time = $data['entry_time'] ?? '';
  145. $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
  146. $model->crt_id = $user['id'];
  147. $model->is_admin = $data['is_admin'];
  148. $model->account = $data['number'];
  149. $model->sex = $data['sex']??'男';
  150. $model->birth_date = $data['birth_date']??'';
  151. $model->pic = $data['pic'] ?? '';
  152. if($model->is_admin == 1){
  153. if($data['password'] !== '********'){
  154. $model->password = Hash::make($data['password']);
  155. }
  156. }
  157. $model->save();
  158. if(isset($data['depart'])){
  159. $insert = [];
  160. foreach ($data['depart'] as $value){
  161. $insert[] = [
  162. 'employee_id' => $model->id,
  163. 'depart_id' => $value,
  164. ];
  165. }
  166. EmployeeDepartPermission::insert($insert);
  167. }
  168. if(isset($data['role'])){
  169. $insert = [];
  170. foreach ($data['role'] as $value){
  171. $insert[] = [
  172. 'employee_id' => $model->id,
  173. 'role_id' => $value,
  174. 'crt_time' => time(),
  175. 'upd_time' => time(),
  176. ];
  177. }
  178. EmployeeRole::insert($insert);
  179. }
  180. DB::commit();
  181. }catch (Exception $e){
  182. DB::rollBack();
  183. return [false, $e->getMessage()];
  184. }
  185. return [true,''];
  186. }
  187. /**
  188. * 用户删除
  189. * @param $data
  190. * @return array
  191. */
  192. public function employeeDel($data){
  193. if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
  194. try{
  195. DB::beginTransaction();
  196. $pic = Employee::whereIn('id',$data['id'])
  197. ->select('pic')
  198. ->get()->toArray();
  199. $pic = array_filter(array_column($pic,'pic'));
  200. Employee::whereIn('id',$data['id'])->update([
  201. 'del_time'=>time()
  202. ]);
  203. EmployeeRole::where('del_time',0)->whereIn('employee_id',$data['id'])->update([
  204. 'del_time'=>time()
  205. ]);
  206. if(! empty($pic)){
  207. foreach ($pic as $value){
  208. // 使用正则表达式匹配特定路径部分并替换为空
  209. $pattern = '/^https?:\/\/[^\/]+\/image\//';
  210. $replacedUrl = preg_replace($pattern, '', $value);
  211. (new FileUploadService())->delLocalPublicFile($replacedUrl);
  212. }
  213. }
  214. DB::commit();
  215. }catch (\Throwable $exception){
  216. DB::rollBack();
  217. return [false, $exception->getMessage()];
  218. }
  219. return [true,'删除成功'];
  220. }
  221. /**
  222. * 用户列表
  223. * @param $data
  224. * @param $user
  225. * @return array
  226. */
  227. public function employeeList($data,$user){
  228. $model = Employee::where('del_time',0)
  229. ->select('number','mobile','emp_name','id','entry_time','leave_time','is_admin','state','is_device','sex','birth_date','pic')
  230. ->orderBy('number','asc');
  231. if(! empty($data['depart'])) {
  232. $depart = Depart::where('del_time',0)
  233. ->select('id','parent_id')
  234. ->get()->toArray();
  235. $result = array_merge($this->getAllDescendants($depart,$data['depart']),[$data['depart']]);
  236. $employee_id = DB::table('employee_depart_permission')
  237. ->whereIn("depart_id", $result)
  238. ->select("employee_id")
  239. ->get()->toArray();
  240. $employee_id = array_column($employee_id,'employee_id');
  241. $model->whereIn("id", $employee_id);
  242. }
  243. if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
  244. if(! empty($data['emp_name'])) $model->where('emp_name', 'LIKE', '%'.$data['emp_name'].'%');
  245. if(! empty($data['state'])) $model->where('state',$data['state']);
  246. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  247. if(! isset($data['all_emp'])) $model->where('id','<>',Employee::SPECIAL_ADMIN);
  248. if(! empty($data['role'])) {
  249. $emp = EmployeeRole::where('role_id',$data['role'])
  250. ->where('del_time',0)
  251. ->select('employee_id')->get()->toArray();
  252. $model->whereIn('id',array_column($emp,'employee_id'));
  253. }
  254. if($user['id'] != Employee::SPECIAL_ADMIN) $model->where('is_manager',0);
  255. $list = $this->limit($model,'',$data);
  256. //组织数据
  257. $list = $this->organizationEmployeeData($list);
  258. return [true , $list];
  259. }
  260. /**
  261. * 用户数据组装
  262. * @param $data
  263. * @return array
  264. */
  265. public function organizationEmployeeData($data) {
  266. if (empty($data['data'])) return $data;
  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",array_column($data['data'],'id'))
  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. $res = DB::table('employee_depart_permission as a')
  284. ->select('a.employee_id','b.title','b.id')
  285. ->join('depart as b','a.depart_id','=','b.id')
  286. ->whereIn("a.employee_id",array_column($data['data'],'id'))
  287. ->orderBy('b.id')
  288. ->get()->toArray();
  289. $depart_title = $depart_id = [];
  290. foreach ($res as $value){
  291. if(isset($depart_title[$value->employee_id])){
  292. $depart_title[$value->employee_id] .= ',' . $value->title;
  293. }else{
  294. $depart_title[$value->employee_id] = $value->title;
  295. }
  296. $depart_id[$value->employee_id][] = $value->id;
  297. }
  298. foreach ($data['data'] as $key => $value){
  299. $data['data'][$key]['role'] = $role2[$value['id']] ?? [];
  300. $data['data'][$key]['role_name'] = $role[$value['id']] ?? '';
  301. $data['data'][$key]['depart'] = $depart_id[$value['id']] ?? [];
  302. $data['data'][$key]['depart_title'] = $depart_title[$value['id']] ?? '';
  303. }
  304. return $data;
  305. }
  306. //获取当前顶级部门下人员id
  307. public function getEmployee($user){
  308. $top_depart_id = $user['depart_top'][0] ?? [];
  309. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  310. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  311. // 查找所有子级id
  312. $childIds = $this->findChildIds($top_depart_id, $list);
  313. $childIds[] = $top_depart_id;
  314. $employee_id = EmployeeDepartPermission::whereIn('depart_id',$childIds)
  315. ->select("employee_id")
  316. ->get()->toArray();
  317. return array_unique(array_column($employee_id,'employee_id'));
  318. }
  319. /**
  320. * 用户参数规则
  321. * @param $data
  322. * @param $is_add
  323. * @return array
  324. */
  325. public function employeeRule($data,$is_add = true){
  326. if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
  327. if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
  328. if(empty($data['depart'])) return [false,'部门不能为空'];
  329. $mobile = $data['mobile'] ?? "";
  330. $number = $data['number'] ?? "";
  331. if(! $is_add){
  332. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  333. $bool = Employee::where('del_time',0)
  334. ->where('id','<>',$data['id'])
  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. }else{
  342. $bool = Employee::where('del_time',0)
  343. ->where(function ($query) use ($mobile, $number){
  344. $query->where('number', $number);
  345. $query->when(! empty($mobile), function ($query) use ($mobile) {
  346. return $query->orWhere('mobile', $mobile);
  347. });
  348. })->exists();
  349. }
  350. if($bool) return [false,'工号或手机号码已存在!'];
  351. return [true,''];
  352. }
  353. /**
  354. * 角色编辑
  355. * @param $data
  356. * @return array
  357. */
  358. public function roleEdit($data,$user){
  359. list($status,$msg) = $this->roleRule($data,$user, false);
  360. if(!$status) return [$status,$msg];
  361. $model = new Role();
  362. $model = $model->where('id',$data['id'])->first();
  363. $model->title = $data['title'];
  364. $model->save();
  365. return [true, ''];
  366. }
  367. /**
  368. * 角色新增
  369. * @param $data
  370. * @param $user
  371. * @return array
  372. */
  373. public function roleAdd($data,$user){
  374. list($status,$msg) = $this->roleRule($data,$user);
  375. if(!$status) return [$status,$msg];
  376. $model = new Role();
  377. $model->title = $data['title'] ;
  378. $model->save();
  379. return [true, ''];
  380. }
  381. /**
  382. * 角色删除
  383. * @param $data
  384. * @return array
  385. */
  386. public function roleDel($data){
  387. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  388. $bool = EmployeeRole::where('del_time',0)
  389. ->whereIn('role_id',$data['id'])
  390. ->exists();
  391. if($bool) return [false,'角色已绑定人员!'];
  392. try{
  393. DB::beginTransaction();
  394. Role::where('id',$data['id'])->update([
  395. 'del_time' => time()
  396. ]);
  397. RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
  398. 'del_time' => time()
  399. ]);
  400. RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
  401. 'del_time' => time()
  402. ]);
  403. DB::commit();
  404. }catch (\Throwable $exception){
  405. DB::rollBack();
  406. return [false, $exception->getMessage()];
  407. }
  408. return [true, ''];
  409. }
  410. /**
  411. * 角色列表
  412. * @param $data
  413. * @return array
  414. */
  415. public function roleList($data,$user){
  416. $model = Role::where('del_time',0)
  417. ->select('title','crt_time','id','upd_time')
  418. ->orderBy('id','desc');
  419. if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
  420. $list = $this->limit($model,'',$data);
  421. return [true, $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->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. $list = $model->get()->toArray();
  592. $list_tree = $list;
  593. if(! empty($list_tree)) {
  594. $list_tree = $this->makeTree(0,$list_tree);
  595. $list_tree = $this->set_sort_circle($list_tree);
  596. }
  597. return [200,['data' => $list,'tree' => $list_tree]];
  598. }
  599. /**
  600. * 部门参数规则
  601. * @param $data
  602. * @param $is_check
  603. * @return array
  604. */
  605. public function departRule($data,$user, $is_check = true){
  606. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  607. $code = array_column($data['data'],'code');
  608. $title = array_column($data['data'],'title');
  609. $code = array_map(function($val) {
  610. return $val !== null ? $val : 0;
  611. }, $code);
  612. $title = array_map(function($val) {
  613. return $val !== null ? $val : 0;
  614. }, $title);
  615. $code_count = array_count_values($code);
  616. $title_count = array_count_values($title);
  617. foreach ($code as $value){
  618. if(empty($value)) return [false,'编码不能为空!'];
  619. if($code_count[$value] > 1) return [false,'编码不能重复'];
  620. }
  621. foreach ($title as $value){
  622. if(empty($value)) return [false,'名称不能为空!'];
  623. if($title_count[$value] > 1) return [false,'名称不能重复'];
  624. }
  625. foreach ($data['data'] as $key => $value){
  626. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  627. $data['data'][$key]['upd_time'] = time();
  628. if($is_check){
  629. $data['data'][$key]['crt_time'] = time();
  630. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  631. ->where('del_time',0)
  632. ->exists();
  633. }else{
  634. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  635. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  636. ->where('id','<>',$data['id'])
  637. ->where('del_time',0)
  638. ->exists();
  639. }
  640. if($bool) return [false,'编码不能重复'];
  641. }
  642. return [true, $data];
  643. }
  644. /**
  645. * 检测部门下是否存在人员
  646. * @param $depart_id
  647. * @return false
  648. */
  649. public function checkDepartHasPerson($depart_id = []){
  650. if(empty($depart_id)) return false;
  651. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  652. ->leftJoin('employee as b','b.id','a.employee_id')
  653. ->where('b.del_time',0)
  654. ->whereIn('a.depart_id',$depart_id)
  655. ->exists();
  656. return $bool;
  657. }
  658. /**
  659. * 班组编辑
  660. * @param $data
  661. * @return array
  662. */
  663. public function teamEdit($data){
  664. list($status,$msg) = $this->teamRule($data,false);
  665. if(!$status) return [$status,$msg];
  666. $model = new Team();
  667. $model = $model->where('id',$data['id'])->first();
  668. $model->title = $data['title'];
  669. $model->code = $data['code'];
  670. $model->save();
  671. return [true,'保存成功!'];
  672. }
  673. /**
  674. * 班组新增
  675. * @param $data
  676. * @param $user
  677. * @return array
  678. */
  679. public function teamAdd($data,$user){
  680. list($status,$msg) = $this->teamRule($data);
  681. if(!$status) return [$status,$msg];
  682. $model = new Team();
  683. $model->title = $data['title'] ;
  684. $model->code = $data['code'];
  685. $model->save();
  686. return [true,'保存成功!'];
  687. }
  688. /**
  689. * 班组删除
  690. * @param $data
  691. * @return array
  692. */
  693. public function teamDel($data){
  694. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  695. Team::where('id',$data['id'])->update([
  696. 'del_time'=>time()
  697. ]);
  698. return [true,'删除成功'];
  699. }
  700. /**
  701. * 班组列表
  702. * @param $data
  703. * @return array
  704. */
  705. public function teamList($data){
  706. $list = Team::where('del_time',0)
  707. ->select('title','id','crt_time','upd_time','code')
  708. ->orderBy('id','desc');
  709. $list = $this->limit($list,'',$data);
  710. return [200,$list];
  711. }
  712. /**
  713. * 班组参数规则
  714. * @param $data
  715. * @param $is_add
  716. * @return array
  717. */
  718. public function teamRule($data,$is_add = true){
  719. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  720. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  721. $model = Team::where('title',$data['title'])
  722. ->where('code',$data['code'])
  723. ->where('del_time',0);
  724. if(! $is_add){
  725. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  726. $model->where('id','<>',$data['id']);
  727. }
  728. $bool = $model->exists();
  729. if($bool) return [false,'名称和编码已存在!'];
  730. return [true,''];
  731. }
  732. /**
  733. * 班组详情
  734. * @param $data
  735. * @return array
  736. */
  737. public function teamDetail($data){
  738. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  739. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  740. ->leftJoin('employee as b','b.id','a.employee_id')
  741. ->where('team_id',$data['id'])
  742. ->select('b.id','b.emp_name','b.number as code')
  743. ->get()->toArray();
  744. return [true,$result];
  745. }
  746. /**
  747. * 人员权限
  748. * @param $data
  749. * @return array
  750. */
  751. public function employeeRole($data){
  752. $role_ids = [];
  753. $employee_ids = [];
  754. foreach ($data as $v){
  755. if(isset($v['role_id'])){
  756. if(!in_array($v['role_id'],$role_ids)){
  757. $role_ids[] = $v['role_id'];
  758. }
  759. }
  760. if(isset($v['employee_id'])){
  761. if(!in_array($v['employee_id'],$employee_ids)){
  762. $employee_ids[] = $v['employee_id'];
  763. }
  764. }
  765. }
  766. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  767. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  768. EmployeeMenuPermission::insert($data);
  769. return [200,'保存成功!'];
  770. }
  771. /**
  772. * 人员部门关系更新
  773. * @param $data
  774. * @return array
  775. */
  776. public function employeeDepart($data){
  777. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  778. DB::beginTransaction();
  779. try {
  780. if($data['type'] == 1){
  781. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  782. }else{
  783. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  784. }
  785. $insert = [];
  786. foreach ($data['insert']['depart_id'] as $t){
  787. foreach ($data['insert']['employee_id'] as $e){
  788. $insert[] = [
  789. 'depart_id' => $t,
  790. 'employee_id' => $e
  791. ];
  792. }
  793. }
  794. EmployeeDepartPermission::insert($insert);
  795. DB::commit();
  796. }catch (\Throwable $exception){
  797. DB::rollBack();
  798. return [false,$exception->getMessage()];
  799. }
  800. return [true,'保存成功!'];
  801. }
  802. /**
  803. * 人员班组关系更新
  804. * @param $data
  805. * @return array
  806. */
  807. public function employeeTeam($data){
  808. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  809. DB::beginTransaction();
  810. try {
  811. if($data['type'] == 1){
  812. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  813. }else{
  814. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  815. }
  816. $insert = [];
  817. foreach ($data['insert']['team_id'] as $t){
  818. foreach ($data['insert']['employee_id'] as $e){
  819. $insert[] = [
  820. 'team_id' => $t,
  821. 'employee_id' => $e
  822. ];
  823. }
  824. }
  825. EmployeeTeamPermission::insert($insert);
  826. DB::commit();
  827. }catch (\Throwable $exception){
  828. DB::rollBack();
  829. return [false,$exception->getMessage()];
  830. }
  831. return [true,'保存成功!'];
  832. }
  833. /**
  834. * 登陆参数规则
  835. * @param $data
  836. * @return array
  837. */
  838. public function loginRule($data){
  839. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  840. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  841. $account = $data['account'];
  842. $res = Employee::where('del_time',0)
  843. ->where(function ($query)use($account) {
  844. $query->where('account', $account)
  845. ->orWhere('mobile', $account);
  846. })
  847. ->get()->toArray();
  848. if(empty($res)) return [false,'账号不存在或已被删除!'];
  849. if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
  850. $res = reset($res);
  851. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  852. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  853. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  854. return [true, ['id'=>$res['id'],'name'=>$res['emp_name']]];
  855. }
  856. /**
  857. * 检查人员信息
  858. * @param $userId
  859. * @return array
  860. */
  861. public static function checkUser($userId){
  862. $res = Employee::where('id', $userId)
  863. ->where('del_time',0)
  864. ->where('is_admin',Employee::IS_ADMIN)
  865. ->where('state',Employee::USE)->get()->first();
  866. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  867. return [true, $res];
  868. }
  869. /**
  870. * 获取登录账号的角色
  871. * @param $employee_id
  872. * @return array
  873. */
  874. public static function getPersonRole($employee_id){
  875. if(empty($employee_id) || $employee_id == Employee::SPECIAL_ADMIN) return [];
  876. $role = EmployeeRole::where('del_time',0)
  877. ->where('employee_id',$employee_id)
  878. ->select('role_id')
  879. ->get()->toArray();
  880. //组织
  881. $role_id = array_unique(array_column($role,'role_id'));
  882. asort($role_id);
  883. $role_id = array_values($role_id);
  884. return $role_id;
  885. }
  886. public static function getPersonRoleQx($role_id = []){
  887. if(empty($role_id)) return [];
  888. $role = RoleMenu::where('del_time',0)
  889. ->whereIn('role_id',$role_id)
  890. ->select('menu_id','type')
  891. ->get()->toArray();
  892. $sysmenu = SysMenu::where('del_time',0)
  893. ->where('is_authority','>',0)
  894. ->select('id')
  895. ->get()->toArray();
  896. $sysmenu = array_column($sysmenu,'id');
  897. $return = [];
  898. foreach ($role as $value){
  899. if(! in_array($value['menu_id'],$sysmenu)) continue;
  900. if(isset($return[$value['menu_id']])){
  901. if($return[$value['menu_id']] < $value['type']) $return[$value['menu_id']] = $value['type'];
  902. }else{
  903. $return[$value['menu_id']] = $value['type'];
  904. }
  905. }
  906. return $return;
  907. }
  908. public static function getSpecialButton($role_id,$user){
  909. $return = [];
  910. $special_button = config('specialButton');
  911. if($user == Employee::SPECIAL_ADMIN) {
  912. foreach ($special_button as $value){
  913. $return[] = $value['id'];
  914. }
  915. return $return;
  916. }
  917. $role_button = RoleMenuButton::where('del_time',0)
  918. ->where('button_id','<',0)
  919. ->whereIn('role_id',$role_id)
  920. ->select('menu_id','button_id')
  921. ->get()->toArray();
  922. foreach ($role_button as $value){
  923. $return[] = $value['button_id'];
  924. }
  925. return $return;
  926. }
  927. //通过角色获取菜单
  928. public function getMenuByRoleInList($user){
  929. $role_id = $user['role'] ?? [];
  930. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  931. if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
  932. //没绑定角色
  933. if(empty($role_id)) return [];
  934. $role_menu = RoleMenu::whereIn('role_id',$role_id)
  935. ->where('del_time',0)
  936. ->select('menu_id')
  937. ->get()->toArray();
  938. return array_column($role_menu,'menu_id');
  939. }
  940. //通过角色获取菜单以及按钮
  941. public function getMenuByRole($user){
  942. $role_id = $user['role'] ?? [];
  943. $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
  944. $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
  945. $button_map = [];
  946. foreach ($button as $value){
  947. $button_map[$value['menu_id']][] = $value;
  948. }
  949. $object = [];
  950. //超级管理员
  951. if($user['id'] == Employee::SPECIAL_ADMIN){
  952. foreach ($menu as $value){
  953. $object[] = [
  954. 'id' => $value['id'],
  955. // 'type' => 0,//所有权限
  956. 'uri' => $value['uri'],
  957. 'button' => $button_map[$value['id']] ?? [],
  958. ];
  959. }
  960. }else{
  961. //没绑定角色
  962. if(empty($role_id)) return [];
  963. $search = RoleMenu::whereIn('role_id',$role_id)
  964. ->where('del_time',0)
  965. ->select('menu_id','type')
  966. ->get()->toArray();
  967. $menu_map = array_column($menu,'uri','id');
  968. //该角色下 菜单里所有按钮
  969. $button_menu = $this->fillRoleButton($role_id);
  970. $button_t = array_column($button,null,'id');
  971. foreach ($search as $value){
  972. $bt = $button_menu[$value['menu_id']] ?? [];
  973. $new = [];
  974. foreach ($bt as $b){
  975. if(! empty($button_t[$b])) $new[] = $button_t[$b];
  976. }
  977. $object[] = [
  978. 'id' => $value['menu_id'],
  979. 'uri' => $menu_map[$value['menu_id']] ?? '',
  980. // 'type' => $value['type'],
  981. 'button' => $new,
  982. ];
  983. }
  984. }
  985. return $object;
  986. }
  987. /**
  988. * 人员直接绑定部门
  989. * @param $data
  990. * @param $user
  991. * @return array
  992. */
  993. public function employeeManagerDepart($data,$user){
  994. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  995. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  996. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  997. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  998. 'del_time' => time()
  999. ]);
  1000. $insert = [];
  1001. foreach ($data['depart_id'] as $value){
  1002. $insert[] = [
  1003. 'employee_id' => $data['employee_id'],
  1004. 'depart_id' => $value,
  1005. 'crt_time' => time(),
  1006. 'upd_time' => time(),
  1007. ];
  1008. }
  1009. EmployeeManagerDepart::insert($insert);
  1010. return [true,''];
  1011. }
  1012. /**
  1013. * 填充角色下的按钮
  1014. * @param $role_id
  1015. * @return array
  1016. */
  1017. public function fillRoleButton($role_id){
  1018. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1019. ->where('del_time',0)
  1020. ->select('menu_id','button_id')
  1021. ->get()->toArray();
  1022. $button_map = [];
  1023. foreach ($button as $value){
  1024. if(! isset($button_map[$value['menu_id']])){
  1025. $button_map[$value['menu_id']][] = $value['button_id'];
  1026. }else{
  1027. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1028. }
  1029. }
  1030. return $button_map;
  1031. }
  1032. /**
  1033. * 获取登录账号的部门
  1034. * @param $employee_id
  1035. * @return array|string[]
  1036. */
  1037. public static function getLoginDepart($employee_id, $top_depart_id = 0){
  1038. if(empty($employee_id)) return [];
  1039. //自己绑定的部门 启用的部门
  1040. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1041. ->join('depart as b','b.id','a.depart_id')
  1042. ->where('a.employee_id',$employee_id)
  1043. ->where('b.is_use',Depart::IS_UES)
  1044. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1045. ->orderBy('b.parent_id','asc')
  1046. ->orderBy('b.is_main','desc')
  1047. ->orderBy('a.depart_id','asc')
  1048. ->get()->toArray();
  1049. $top = $map = $rule = $head = [];
  1050. $res_data = [];//获取当前所在部门(也可能是门店 为了 depart_id)
  1051. $my_depart = [];//当前门店下 我勾选的门店以及部门信息
  1052. $is_all_depart = 0;
  1053. if(! empty($depart)){
  1054. //库存校验
  1055. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1056. //所有部门
  1057. $list = Depart::where('del_time',0)->get()->toArray();
  1058. $depart_map = array_column($list,null,'id');
  1059. foreach ($depart as $key => $value){
  1060. if($value['parent_id'] == 0){
  1061. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1062. $depart[$key]['is_stock'] = $is_stock;
  1063. $top[$value['depart_id']] = [
  1064. 'depart_id' => $value['depart_id'],
  1065. 'is_main' => $value['is_main'],
  1066. 'basic_type_id' => $value['basic_type_id'],
  1067. 'title' => $value['title'],
  1068. 'is_stock' => $is_stock,
  1069. ];
  1070. $map[$value['depart_id']] = $value['depart_id'];
  1071. if($value['depart_id'] == $top_depart_id) {
  1072. $my_depart[] = $depart[$key];
  1073. if(empty($res_data)){
  1074. $res_data = $depart[$key];
  1075. }else{
  1076. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1077. }
  1078. }
  1079. }else{
  1080. $t = self::getTopParentId($value['depart_id'],$list);
  1081. if($t && isset($depart_map[$t])) {
  1082. $is_stock = $set_map[$t] ?? 1;
  1083. $depart[$key]['is_stock'] = $is_stock;
  1084. $t_tmp = $depart_map[$t] ?? [];
  1085. if( ! isset($top[$t_tmp['id']])){
  1086. $top[$t_tmp['id']] = [
  1087. 'depart_id' => $t_tmp['id'],
  1088. 'is_main' => $t_tmp['is_main'],
  1089. 'basic_type_id' => $t_tmp['basic_type_id'],
  1090. 'title' => $t_tmp['title'],
  1091. 'is_stock' => $set_map[$t] ?? 1,
  1092. ];
  1093. }
  1094. $map[$value['depart_id']] = $t;
  1095. }
  1096. if($t == $top_depart_id){
  1097. $my_depart[] = $depart[$key];
  1098. if(empty($res_data)){
  1099. $res_data = $depart[$key];
  1100. }else{
  1101. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1102. }
  1103. }
  1104. }
  1105. }
  1106. //-------------重组当前所在门店
  1107. $top = array_values($top);
  1108. usort($top, function($a, $b) {
  1109. return $b['is_main'] - $a['is_main'];
  1110. });
  1111. if(! empty($top_depart_id)){
  1112. $targetArray = null;
  1113. foreach ($top as $key => $value) {
  1114. if ($value['depart_id'] == $top_depart_id) {
  1115. $targetArray = $value;
  1116. unset($top[$key]); // 从原数组中移除目标数组
  1117. break; // 找到后跳出循环
  1118. }
  1119. }
  1120. if ($targetArray) array_unshift($top, $targetArray);
  1121. }
  1122. //-------------重组当前所在门店
  1123. //-------------拥有的数据权限(门店、部门)
  1124. $my_top = $top[0] ?? [];
  1125. foreach ($my_depart as $value){
  1126. if(in_array($value['depart_id'],$rule)) continue;
  1127. if(! $value['parent_id']){ //顶级
  1128. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1129. if($value['is_main']) {//是总公司
  1130. //所有部门都有
  1131. $rule = array_column($list,'id');
  1132. }else{//不是总公司
  1133. //自己以及子部门
  1134. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1135. $rule = array_merge_recursive($rule,$depart_id);
  1136. }
  1137. }else{//非顶级
  1138. if(! empty($my_top['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1139. if($value['is_main']) {//是总社
  1140. $top_tmp = $map[$value['depart_id']];
  1141. if(! empty($depart_map[$top_tmp]['is_main'])){
  1142. //顶级公司是总公司 所有部门都有
  1143. $rule = array_column($list,'id');
  1144. }else{
  1145. //顶级公司是分公司 分公司所有部门
  1146. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1147. $rule = array_merge_recursive($rule,$depart_id);
  1148. }
  1149. }else{//不是总社
  1150. $rule = array_merge($rule,[$value['depart_id']]);
  1151. }
  1152. }
  1153. }
  1154. //-------------拥有的数据权限(门店、部门)
  1155. //总店
  1156. foreach ($list as $value){
  1157. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1158. }
  1159. }
  1160. $rule = array_unique($rule);
  1161. return [$depart,$top,$map,$rule,$is_all_depart,$head,$res_data];
  1162. }
  1163. //获取用户的所属门店信息
  1164. public static function getLoginMessage($employee_id){
  1165. if(empty($employee_id)) return [];
  1166. //自己绑定的部门 启用的部门
  1167. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1168. ->join('depart as b','b.id','a.depart_id')
  1169. ->where('a.employee_id',$employee_id)
  1170. ->where('b.is_use',Depart::IS_UES)
  1171. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1172. ->orderBy('b.parent_id','asc')
  1173. ->orderBy('b.is_main','desc')
  1174. ->orderBy('a.depart_id','asc')
  1175. ->get()->toArray();
  1176. $top = [];
  1177. if(! empty($depart)){
  1178. //所有部门
  1179. $list = Depart::where('del_time',0)->get()->toArray();
  1180. $depart_map = array_column($list,null,'id');
  1181. foreach ($depart as $value){
  1182. if($value['parent_id'] == 0){//顶级
  1183. if(! isset($top[$value['depart_id']])){
  1184. $top[$value['depart_id']] = [
  1185. 'depart_id' => $value['depart_id'],
  1186. 'is_main' => $value['is_main'],
  1187. 'title' => $value['title'],
  1188. ];
  1189. }
  1190. }else{
  1191. //门店
  1192. $t = self::getTopParentId($value['depart_id'],$list);
  1193. if($t && isset($depart_map[$t])) {
  1194. $t_tmp = $depart_map[$t] ?? [];
  1195. $is_all_depart = 0;
  1196. if(! empty($t_tmp['is_main']) && $value['is_main']) $is_all_depart = 1;
  1197. if(! isset($top[$t_tmp['id']])){
  1198. $top[$t_tmp['id']] = [
  1199. 'depart_id' => $t_tmp['id'],
  1200. 'is_main' => $is_all_depart,
  1201. 'title' => $t_tmp['title'],
  1202. ];
  1203. }else{
  1204. if(! empty($is_all_depart)) $top[$t_tmp['id']]['is_main'] = $is_all_depart;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. $top = array_values($top);
  1211. usort($top, function($a, $b) {
  1212. return $b['is_main'] - $a['is_main'];
  1213. });
  1214. return $top;
  1215. }
  1216. /**
  1217. * 获取顶级id
  1218. * @param $id
  1219. * @param $data
  1220. * @return int
  1221. */
  1222. public static function getTopParentId($id, $data) {
  1223. foreach ($data as $item) {
  1224. if ($item['id'] == $id) {
  1225. if ($item['parent_id'] == 0) {
  1226. // 找到最顶级的id
  1227. return $item['id'];
  1228. } else {
  1229. // 继续递归查找父级
  1230. return self::getTopParentId($item['parent_id'], $data);
  1231. }
  1232. }
  1233. }
  1234. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1235. return 0;
  1236. }
  1237. /**
  1238. * 递归获取所有id
  1239. * @param $data
  1240. * @param $id
  1241. * @return array
  1242. */
  1243. public static function getAllIds($data, $id) {
  1244. $result = array(); // 存储结果的数组
  1245. foreach ($data as $node) {
  1246. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1247. $result[] = $node['id'];
  1248. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1249. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1250. }
  1251. }
  1252. return $result;
  1253. }
  1254. public static function checkWxUser($userId){
  1255. $res = Employee::where('id', $userId)
  1256. ->where('del_time',0)
  1257. ->where('state',Employee::USE)->get()->first();
  1258. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  1259. return [true, $res];
  1260. }
  1261. }