EmployeeService.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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. if($model->is_admin == 1){
  79. $model->account = $data['number'];
  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. if($model->is_admin == 1){
  139. $model->account = $data['number'];
  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->save();
  517. $depart_id = $model->id;
  518. if(empty($depart_id)) {
  519. DB::rollBack();
  520. return [false,'部门新建失败'];
  521. }
  522. if(empty($value['parent_id'])){
  523. $m = new Storehouse();
  524. $m->title = $value['title'];
  525. $m->depart_id = $depart_id;
  526. $m->top_depart_id = $depart_id;
  527. $m->crt_id = $user['id'];
  528. $m->save();
  529. if(empty($m->id)) {
  530. DB::rollBack();
  531. return [false,'仓库生成失败'];
  532. }
  533. $employee = new Employee();
  534. $number = "admin" . $value['code'];
  535. $employee->number = $number;
  536. $employee->emp_name = $value['title'] . "管理员账号";
  537. $employee->entry_time = date('Y-m-d');
  538. $employee->state = 1;
  539. $employee->crt_id = $user['id'];
  540. $employee->is_admin = 1;
  541. $employee->account = $number;
  542. $employee->password = Hash::make("password");
  543. $employee->is_manager = 1;
  544. $employee->save();
  545. if(empty($employee->id)) {
  546. DB::rollBack();
  547. return [false,'管理员账号生成失败'];
  548. }
  549. $depart = new EmployeeDepartPermission();
  550. $depart->employee_id = $employee->id;
  551. $depart->depart_id = $depart_id;
  552. $depart->save();
  553. if(empty($depart->id)) {
  554. DB::rollBack();
  555. return [false,'管理员账号部门关联生成失败'];
  556. }
  557. //公司系统设置
  558. ProductInventorySet::insert(['top_depart_id' => $depart_id, 'param_one' => 1, 'crt_time' => $time]);
  559. // if(! empty($value['is_main'])){
  560. // //总供应商 所有分社可见
  561. // Supplier::insert([
  562. // 'title' => $value['title'],
  563. // 'crt_id' => $user['id'],
  564. // 'crt_time' => $time,
  565. // 'depart_id' => $depart_id,
  566. // 'top_depart_id' => $depart_id,
  567. // 'is_main' => Supplier::is_main,
  568. // ]);
  569. // }
  570. }
  571. }
  572. DB::commit();
  573. }catch (\Exception $exception){
  574. DB::rollBack();
  575. return [false,$exception->getMessage()];
  576. }
  577. return [true,'保存成功!'];
  578. }
  579. /**
  580. * 部门删除
  581. * @param $data
  582. * @return array
  583. */
  584. public function departDel($data){
  585. list($status,$msg) = $this->checkDepartDel($data);
  586. if(! $status) return [false, $msg];
  587. Depart::whereIn('id',$data['id'])->update([
  588. 'del_time'=>time()
  589. ]);
  590. return [true,'删除成功'];
  591. }
  592. /**
  593. * 判断部门是否可以删除
  594. * @param $data
  595. * @return array
  596. */
  597. public function checkDepartDel($data){
  598. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  599. $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
  600. if($bool) return [false,'部门下有子部门!'];
  601. if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
  602. $bool = ProductInventory::where('top_depart_id',$data['id'])->where('del_time',0)->exists();
  603. if($bool) return [false,'部门下的仓库有产品信息!'];
  604. return [true, ''];
  605. }
  606. /**
  607. * 部门列表
  608. * @param $data
  609. * @param $user
  610. * @return array
  611. */
  612. public function departList($data,$user){
  613. $model = Depart::where('del_time',0)
  614. ->select('title','id','code','parent_id','is_main','basic_type_id','rate')
  615. ->orderby('code', 'asc');
  616. if(! empty($data['get_top']) && $data['get_top'] == 1){
  617. //指派销售
  618. $depart_id = $this->getDepartIdListOfMySales($user);
  619. $model->whereIn('id', $depart_id);
  620. }else{
  621. if($user['id'] != Employee::SPECIAL_ADMIN && ! $user['is_all_depart'] && empty($data['get_top'])){
  622. $depart_id = $this->getDepartIdList($user);
  623. $model->whereIn('id',$depart_id);
  624. }
  625. }
  626. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  627. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  628. $list = $model->get()->toArray();
  629. $list = $this->fillDepartList($list, $user);
  630. $list_tree = $list;
  631. if(! empty($list_tree)) {
  632. $list_tree = $this->makeTree(0,$list_tree);
  633. $list_tree = $this->set_sort_circle($list_tree);
  634. }
  635. return [200,['data' => $list,'tree' => $list_tree]];
  636. }
  637. public function fillDepartList($list,$user){
  638. if(empty($list)) return $list;
  639. $basic = BasicType::where('del_time',0)
  640. ->whereIn('id', array_unique(array_column($list,'basic_type_id')))
  641. ->pluck('title','id')->toArray();
  642. foreach ($list as $key => $value){
  643. $list[$key]['basic_type_title'] = $basic[$value['basic_type_id']] ?? '';
  644. $list[$key]['is_show_basic_type'] = $user['is_all_depart'];
  645. }
  646. return $list;
  647. }
  648. //获取可见的部门范围
  649. public function getDepartIdList($user){
  650. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  651. $result = [];
  652. foreach ($user['depart_range'] as $v){
  653. // 查找所有父级id
  654. $parentIds = $this->findParentIds($v, $list);
  655. // 查找所有子级id
  656. $childIds = $this->findChildIds($v, $list);
  657. // 合并父级和子级id
  658. $tmp = array_merge($parentIds, $childIds, [$v]);
  659. $result = array_merge($result,$tmp);
  660. }
  661. return array_unique($result);
  662. }
  663. //获取指派销售时的部门
  664. public function getDepartIdListOfMySales($user){
  665. $top = $user['depart_top'][0] ?? [];
  666. $top = $top['depart_id'] ?? 0;
  667. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  668. // 查找所有子级id
  669. $childIds = $this->findChildIds($top, $list);
  670. $result = array_merge($childIds, [$top]);
  671. return $result;
  672. }
  673. /**
  674. * 部门参数规则
  675. * @param $data
  676. * @param $is_check
  677. * @return array
  678. */
  679. public function departRule($data,$user, $is_check = true){
  680. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  681. $code = array_column($data['data'],'code');
  682. $title = array_column($data['data'],'title');
  683. $code = array_map(function($val) {
  684. return $val !== null ? $val : 0;
  685. }, $code);
  686. $title = array_map(function($val) {
  687. return $val !== null ? $val : 0;
  688. }, $title);
  689. $code_count = array_count_values($code);
  690. $title_count = array_count_values($title);
  691. foreach ($code as $value){
  692. if(empty($value)) return [false,'编码不能为空!'];
  693. if($code_count[$value] > 1) return [false,'编码不能重复'];
  694. }
  695. foreach ($title as $value){
  696. if(empty($value)) return [false,'名称不能为空!'];
  697. if($title_count[$value] > 1) return [false,'名称不能重复'];
  698. }
  699. $count = 0;
  700. foreach ($data['data'] as $value){
  701. if(empty($value['parent_id']) && ! empty($value['is_main'])) $count ++;
  702. }
  703. if($count > 1) return [false,'顶级总社只允许存在一个!'];
  704. if($count == 1){
  705. $id = $data['id'] ?? 0;
  706. $bool = Depart::where('del_time',0)
  707. ->where('parent_id',0)
  708. ->where('is_main',1)
  709. ->when(! empty($id), function ($query) use ($id) {
  710. return $query->where('id', '<>',$id);
  711. })
  712. ->exists();
  713. if($bool) return [false,'顶级总社只允许存在一个!'];
  714. }
  715. foreach ($data['data'] as $key => $value){
  716. if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
  717. $data['data'][$key]['upd_time'] = time();
  718. //Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
  719. if($is_check){
  720. if(empty($user['is_all_depart']) && empty($value['parent_id'])) return [false,'上级部门必须选择'];
  721. $data['data'][$key]['crt_time'] = time();
  722. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  723. ->where('del_time',0)
  724. ->exists();
  725. }else{
  726. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  727. $bool = Depart::whereRaw("binary code = '{$value['code']}'")
  728. ->where('id','<>',$data['id'])
  729. ->where('del_time',0)
  730. ->exists();
  731. }
  732. if($bool) return [false,'编码不能重复'];
  733. }
  734. return [true, $data];
  735. }
  736. /**
  737. * 检测部门下是否存在人员
  738. * @param $depart_id
  739. * @return false
  740. */
  741. public function checkDepartHasPerson($depart_id = []){
  742. if(empty($depart_id)) return false;
  743. $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
  744. ->leftJoin('employee as b','b.id','a.employee_id')
  745. ->where('b.del_time',0)
  746. ->whereIn('a.depart_id',$depart_id)
  747. ->exists();
  748. return $bool;
  749. }
  750. /**
  751. * 班组编辑
  752. * @param $data
  753. * @return array
  754. */
  755. public function teamEdit($data){
  756. list($status,$msg) = $this->teamRule($data,false);
  757. if(!$status) return [$status,$msg];
  758. $model = new Team();
  759. $model = $model->where('id',$data['id'])->first();
  760. $model->title = $data['title'];
  761. $model->code = $data['code'];
  762. $model->save();
  763. return [true,'保存成功!'];
  764. }
  765. /**
  766. * 班组新增
  767. * @param $data
  768. * @param $user
  769. * @return array
  770. */
  771. public function teamAdd($data,$user){
  772. list($status,$msg) = $this->teamRule($data);
  773. if(!$status) return [$status,$msg];
  774. $model = new Team();
  775. $model->title = $data['title'] ;
  776. $model->code = $data['code'];
  777. $model->save();
  778. return [true,'保存成功!'];
  779. }
  780. /**
  781. * 班组删除
  782. * @param $data
  783. * @return array
  784. */
  785. public function teamDel($data){
  786. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  787. Team::where('id',$data['id'])->update([
  788. 'del_time'=>time()
  789. ]);
  790. return [true,'删除成功'];
  791. }
  792. /**
  793. * 班组列表
  794. * @param $data
  795. * @return array
  796. */
  797. public function teamList($data){
  798. $list = Team::where('del_time',0)
  799. ->select('title','id','crt_time','upd_time','code')
  800. ->orderBy('id','desc');
  801. $list = $this->limit($list,'',$data);
  802. return [200,$list];
  803. }
  804. /**
  805. * 班组参数规则
  806. * @param $data
  807. * @param $is_add
  808. * @return array
  809. */
  810. public function teamRule($data,$is_add = true){
  811. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  812. if($this->isEmpty($data,'code')) return [false,'编码不存在'];
  813. $model = Team::where('title',$data['title'])
  814. ->where('code',$data['code'])
  815. ->where('del_time',0);
  816. if(! $is_add){
  817. if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
  818. $model->where('id','<>',$data['id']);
  819. }
  820. $bool = $model->exists();
  821. if($bool) return [false,'名称和编码已存在!'];
  822. return [true,''];
  823. }
  824. /**
  825. * 班组详情
  826. * @param $data
  827. * @return array
  828. */
  829. public function teamDetail($data){
  830. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  831. $result = EmployeeTeamPermission::from('employee_team_permission as a')
  832. ->leftJoin('employee as b','b.id','a.employee_id')
  833. ->where('team_id',$data['id'])
  834. ->select('b.id','b.emp_name','b.number as code')
  835. ->get()->toArray();
  836. return [true,$result];
  837. }
  838. /**
  839. * 人员权限
  840. * @param $data
  841. * @return array
  842. */
  843. public function employeeRole($data){
  844. $role_ids = [];
  845. $employee_ids = [];
  846. foreach ($data as $v){
  847. if(isset($v['role_id'])){
  848. if(!in_array($v['role_id'],$role_ids)){
  849. $role_ids[] = $v['role_id'];
  850. }
  851. }
  852. if(isset($v['employee_id'])){
  853. if(!in_array($v['employee_id'],$employee_ids)){
  854. $employee_ids[] = $v['employee_id'];
  855. }
  856. }
  857. }
  858. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  859. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  860. EmployeeMenuPermission::insert($data);
  861. return [200,'保存成功!'];
  862. }
  863. /**
  864. * 人员部门关系更新
  865. * @param $data
  866. * @return array
  867. */
  868. public function employeeDepart($data){
  869. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  870. DB::beginTransaction();
  871. try {
  872. if($data['type'] == 1){
  873. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  874. }else{
  875. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  876. }
  877. $insert = [];
  878. foreach ($data['insert']['depart_id'] as $t){
  879. foreach ($data['insert']['employee_id'] as $e){
  880. $insert[] = [
  881. 'depart_id' => $t,
  882. 'employee_id' => $e
  883. ];
  884. }
  885. }
  886. EmployeeDepartPermission::insert($insert);
  887. DB::commit();
  888. }catch (\Throwable $exception){
  889. DB::rollBack();
  890. return [false,$exception->getMessage()];
  891. }
  892. return [true,'保存成功!'];
  893. }
  894. /**
  895. * 人员班组关系更新
  896. * @param $data
  897. * @return array
  898. */
  899. public function employeeTeam($data){
  900. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  901. DB::beginTransaction();
  902. try {
  903. if($data['type'] == 1){
  904. EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
  905. }else{
  906. EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  907. }
  908. $insert = [];
  909. foreach ($data['insert']['team_id'] as $t){
  910. foreach ($data['insert']['employee_id'] as $e){
  911. $insert[] = [
  912. 'team_id' => $t,
  913. 'employee_id' => $e
  914. ];
  915. }
  916. }
  917. EmployeeTeamPermission::insert($insert);
  918. DB::commit();
  919. }catch (\Throwable $exception){
  920. DB::rollBack();
  921. return [false,$exception->getMessage()];
  922. }
  923. return [true,'保存成功!'];
  924. }
  925. /**
  926. * 登陆参数规则
  927. * @param $data
  928. * @return array
  929. */
  930. public function loginRule($data){
  931. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  932. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  933. $account = $data['account'];
  934. $res = Employee::where('del_time',0)
  935. ->where(function ($query)use($account) {
  936. $query->where('account', $account)
  937. ->orWhere('mobile', $account);
  938. })
  939. ->get()->toArray();
  940. if(empty($res)) return [false,'账号不存在或已被删除!'];
  941. if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
  942. $res = reset($res);
  943. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  944. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  945. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  946. //总社分社信息
  947. $return = EmployeeService::getLoginDepart($res['id']);
  948. $is_main = $return[4] ?? 0;
  949. $depart_top = $return[1] ?? [];
  950. $depart_top_title = $depart_top[0]['title'] ?? "";
  951. $depart_top_id = $depart_top[0]['depart_id'] ?? 0;
  952. 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]];
  953. }
  954. /**
  955. * 检查人员信息
  956. * @param $userId
  957. * @return array
  958. */
  959. public static function checkUser($userId){
  960. $res = Employee::where('id', $userId)
  961. ->where('del_time',0)
  962. ->where('is_admin',Employee::IS_ADMIN)
  963. ->where('state',Employee::USE)->get()->first();
  964. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  965. return [true, $res];
  966. }
  967. /**
  968. * 获取登录账号的角色
  969. * @param $employee_id
  970. * @return array
  971. */
  972. public static function getPersonRole($employee_id){
  973. if(empty($employee_id) || $employee_id == Employee::SPECIAL_ADMIN) return [];
  974. $role = EmployeeRole::where('del_time',0)
  975. ->where('employee_id',$employee_id)
  976. ->select('role_id')
  977. ->get()->toArray();
  978. //组织
  979. $role_id = array_unique(array_column($role,'role_id'));
  980. asort($role_id);
  981. $role_id = array_values($role_id);
  982. return $role_id;
  983. }
  984. public static function getPersonRoleQx($role_id){
  985. if(empty($role_id)) return [];
  986. $role = RoleMenu::where('del_time',0)
  987. ->whereIn('role_id',$role_id)
  988. ->select('menu_id','type')
  989. ->get()->toArray();
  990. $sysmenu = SysMenu::where('del_time',0)
  991. ->where('is_authority','>',0)
  992. ->select('id')
  993. ->get()->toArray();
  994. $sysmenu = array_column($sysmenu,'id');
  995. $return = [];
  996. foreach ($role as $value){
  997. if(! in_array($value['menu_id'],$sysmenu)) continue;
  998. if(isset($return[$value['menu_id']]) && $return[$value['menu_id']] < $value['type']){
  999. $return[$value['menu_id']] = $value['type'];
  1000. }else{
  1001. $return[$value['menu_id']] = $value['type'];
  1002. }
  1003. }
  1004. return $return;
  1005. }
  1006. //通过角色获取菜单
  1007. public function getMenuByRoleInList($user){
  1008. $role_id = $user['role'] ?? [];
  1009. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  1010. if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
  1011. //没绑定角色
  1012. if(empty($role_id)) return [];
  1013. $role_menu = RoleMenu::whereIn('role_id',$role_id)
  1014. ->where('del_time',0)
  1015. ->select('menu_id')
  1016. ->get()->toArray();
  1017. return array_column($role_menu,'menu_id');
  1018. }
  1019. //通过角色获取菜单以及按钮
  1020. public function getMenuByRole($user){
  1021. $role_id = $user['role'] ?? [];
  1022. $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
  1023. $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
  1024. $button_map = [];
  1025. foreach ($button as $value){
  1026. $button_map[$value['menu_id']][] = $value;
  1027. }
  1028. $object = [];
  1029. //超级管理员
  1030. if($user['id'] == Employee::SPECIAL_ADMIN){
  1031. foreach ($menu as $value){
  1032. $object[] = [
  1033. 'id' => $value['id'],
  1034. // 'type' => 0,//所有权限
  1035. 'uri' => $value['uri'],
  1036. 'button' => $button_map[$value['id']] ?? [],
  1037. ];
  1038. }
  1039. }else{
  1040. //没绑定角色
  1041. if(empty($role_id)) return [];
  1042. $search = RoleMenu::whereIn('role_id',$role_id)
  1043. ->where('del_time',0)
  1044. ->select('menu_id','type')
  1045. ->get()->toArray();
  1046. $menu_map = array_column($menu,'uri','id');
  1047. //该角色下 菜单里所有按钮
  1048. $button_menu = $this->fillRoleButton($role_id);
  1049. $button_t = array_column($button,null,'id');
  1050. foreach ($search as $value){
  1051. $bt = $button_menu[$value['menu_id']] ?? [];
  1052. $new = [];
  1053. foreach ($bt as $b){
  1054. if(! empty($button_t[$b])) $new[] = $button_t[$b];
  1055. }
  1056. $object[] = [
  1057. 'id' => $value['menu_id'],
  1058. 'uri' => $menu_map[$value['menu_id']] ?? '',
  1059. // 'type' => $value['type'],
  1060. 'button' => $new,
  1061. ];
  1062. }
  1063. }
  1064. return $object;
  1065. }
  1066. /**
  1067. * 人员直接绑定部门
  1068. * @param $data
  1069. * @param $user
  1070. * @return array
  1071. */
  1072. public function employeeManagerDepart($data,$user){
  1073. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  1074. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  1075. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  1076. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  1077. 'del_time' => time()
  1078. ]);
  1079. $insert = [];
  1080. foreach ($data['depart_id'] as $value){
  1081. $insert[] = [
  1082. 'employee_id' => $data['employee_id'],
  1083. 'depart_id' => $value,
  1084. 'crt_time' => time(),
  1085. 'upd_time' => time(),
  1086. ];
  1087. }
  1088. EmployeeManagerDepart::insert($insert);
  1089. return [true,''];
  1090. }
  1091. /**
  1092. * 填充角色下的按钮
  1093. * @param $role_id
  1094. * @return array
  1095. */
  1096. public function fillRoleButton($role_id){
  1097. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1098. ->where('del_time',0)
  1099. ->select('menu_id','button_id')
  1100. ->get()->toArray();
  1101. $button_map = [];
  1102. foreach ($button as $value){
  1103. if(! isset($button_map[$value['menu_id']])){
  1104. $button_map[$value['menu_id']][] = $value['button_id'];
  1105. }else{
  1106. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1107. }
  1108. }
  1109. return $button_map;
  1110. }
  1111. /**
  1112. * 获取登录账号的部门
  1113. * @param $employee_id
  1114. * @return array|string[]
  1115. */
  1116. public static function getLoginDepart($employee_id){
  1117. if(empty($employee_id)) return [];
  1118. //自己绑定的部门 启用的部门
  1119. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1120. ->join('depart as b','b.id','a.depart_id')
  1121. ->where('a.employee_id',$employee_id)
  1122. ->where('b.is_use',Depart::IS_UES)
  1123. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1124. ->orderBy('b.parent_id','asc')
  1125. ->orderBy('b.is_main','desc')
  1126. ->orderBy('a.depart_id','asc')
  1127. ->get()->toArray();
  1128. $top = $map = $rule = $head = [];
  1129. $is_all_depart = $is_behind_main = 0;
  1130. if(! empty($depart)){
  1131. //库存校验
  1132. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1133. //所有部门
  1134. $list = Depart::where('del_time',0)->get()->toArray();
  1135. $depart_map = array_column($list,null,'id');
  1136. foreach ($depart as $key => $value){
  1137. if($value['parent_id'] == 0){//顶级
  1138. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1139. $depart[$key]['is_stock'] = $is_stock;
  1140. $top[$value['depart_id']] = [
  1141. 'depart_id' => $value['depart_id'],
  1142. 'is_main' => $value['is_main'],
  1143. 'basic_type_id' => $value['basic_type_id'],
  1144. 'title' => $value['title'],
  1145. 'is_stock' => $is_stock,
  1146. ];
  1147. $map[$value['depart_id']] = $value['depart_id'];
  1148. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1149. if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1150. }else{
  1151. $t = self::getTopParentId($value['depart_id'],$list);
  1152. if($t && isset($depart_map[$t])) {
  1153. $is_stock = $set_map[$t] ?? 1;
  1154. $depart[$key]['is_stock'] = $is_stock;
  1155. $t_tmp = $depart_map[$t] ?? [];
  1156. $top[$t_tmp['id']] = [
  1157. 'depart_id' => $t_tmp['id'],
  1158. 'is_main' => $t_tmp['is_main'],
  1159. 'basic_type_id' => $t_tmp['basic_type_id'],
  1160. 'title' => $t_tmp['title'],
  1161. 'is_stock' => $set_map[$t] ?? 1,
  1162. ];
  1163. $map[$value['depart_id']] = $t;
  1164. if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1165. if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1166. }
  1167. }
  1168. }
  1169. foreach ($depart as $value){
  1170. if(in_array($value['depart_id'],$rule)) continue;
  1171. if(! $value['parent_id']){ //顶级
  1172. if($value['is_main']) {//是总公司
  1173. //所有部门都有
  1174. $rule = array_column($list,'id');
  1175. }else{//不是总公司
  1176. //自己以及子部门
  1177. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1178. $rule = array_merge_recursive($rule,$depart_id);
  1179. }
  1180. }else{//非顶级
  1181. if($value['is_main']) {//是总社
  1182. $top_tmp = $map[$value['depart_id']];
  1183. if(! empty($depart_map[$top_tmp]['is_main'])){
  1184. //顶级公司是总公司 所有部门都有
  1185. $rule = array_column($list,'id');
  1186. }else{
  1187. //顶级公司是分公司 分公司所有部门
  1188. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1189. $rule = array_merge_recursive($rule,$depart_id);
  1190. }
  1191. }else{//不是总社
  1192. $rule = array_merge($rule,[$value['depart_id']]);
  1193. }
  1194. }
  1195. }
  1196. foreach ($list as $value){
  1197. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1198. }
  1199. }
  1200. $top = array_values($top);
  1201. $rule = array_unique($rule);
  1202. return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
  1203. }
  1204. //判断是否总公司
  1205. public static function isMain($employee_id){
  1206. $is_main = 0;
  1207. if(empty($employee_id)) return $is_main;
  1208. //admin账号
  1209. if($employee_id == Employee::SPECIAL_ADMIN) {
  1210. $is_main = 1;
  1211. return $is_main;
  1212. }
  1213. //自己绑定的部门 启用的部门
  1214. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1215. ->join('depart as b','b.id','a.depart_id')
  1216. ->where('a.employee_id',$employee_id)
  1217. ->where('b.is_use',Depart::IS_UES)
  1218. ->select('a.depart_id','b.is_main','b.parent_id')
  1219. ->orderBy('b.parent_id','asc')
  1220. ->orderBy('b.is_main','desc')
  1221. ->orderBy('a.depart_id','asc')
  1222. ->get()->toArray();
  1223. if(! empty($depart)){
  1224. $list = Depart::where('del_time',0)->get()->toArray();
  1225. $depart_map = array_column($list,null,'id');
  1226. foreach ($depart as $value){
  1227. if($value['parent_id'] == 0){//顶级
  1228. if(! empty($value['is_main']) && ! $is_main) $is_main = 1;
  1229. }else{
  1230. $t = self::getTopParentId($value['depart_id'],$list);
  1231. if($t && isset($depart_map[$t])) {
  1232. $tmp_is_main = $depart_map[$t]['is_main'] ?? 0;
  1233. if(! empty($tmp_is_main) && ! $is_main) $is_main = 1;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. return $is_main;
  1239. }
  1240. /**
  1241. * 获取顶级id
  1242. * @param $id
  1243. * @param $data
  1244. * @return int
  1245. */
  1246. public static function getTopParentId($id, $data) {
  1247. foreach ($data as $item) {
  1248. if ($item['id'] == $id) {
  1249. if ($item['parent_id'] == 0) {
  1250. // 找到最顶级的id
  1251. return $item['id'];
  1252. } else {
  1253. // 继续递归查找父级
  1254. return self::getTopParentId($item['parent_id'], $data);
  1255. }
  1256. }
  1257. }
  1258. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1259. return 0;
  1260. }
  1261. /**
  1262. * 递归获取所有id
  1263. * @param $data
  1264. * @param $id
  1265. * @return array
  1266. */
  1267. public static function getAllIds($data, $id) {
  1268. $result = array(); // 存储结果的数组
  1269. foreach ($data as $node) {
  1270. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1271. $result[] = $node['id'];
  1272. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1273. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1274. }
  1275. }
  1276. return $result;
  1277. }
  1278. public static function checkWxUser($userId){
  1279. $res = Employee::where('id', $userId)
  1280. ->where('del_time',0)
  1281. ->where('state',Employee::USE)->get()->first();
  1282. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  1283. return [true, $res];
  1284. }
  1285. }