EmployeeService.php 59 KB

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