EmployeeService.php 50 KB

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