EmployeeService.php 56 KB

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