EmployeeService.php 51 KB

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