EmployeeService.php 59 KB

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