EmployeeService.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. //通过角色获取菜单
  1015. public function getMenuByRoleInList($user){
  1016. $role_id = $user['role'] ?? [];
  1017. $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
  1018. if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
  1019. //没绑定角色
  1020. if(empty($role_id)) return [];
  1021. $role_menu = RoleMenu::whereIn('role_id',$role_id)
  1022. ->where('del_time',0)
  1023. ->select('menu_id')
  1024. ->get()->toArray();
  1025. return array_column($role_menu,'menu_id');
  1026. }
  1027. //通过角色获取菜单以及按钮
  1028. public function getMenuByRole($user){
  1029. $role_id = $user['role'] ?? [];
  1030. $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
  1031. $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
  1032. $button_map = [];
  1033. foreach ($button as $value){
  1034. $button_map[$value['menu_id']][] = $value;
  1035. }
  1036. $object = [];
  1037. //超级管理员
  1038. if($user['id'] == Employee::SPECIAL_ADMIN){
  1039. foreach ($menu as $value){
  1040. $object[] = [
  1041. 'id' => $value['id'],
  1042. // 'type' => 0,//所有权限
  1043. 'uri' => $value['uri'],
  1044. 'button' => $button_map[$value['id']] ?? [],
  1045. ];
  1046. }
  1047. }else{
  1048. //没绑定角色
  1049. if(empty($role_id)) return [];
  1050. $search = RoleMenu::whereIn('role_id',$role_id)
  1051. ->where('del_time',0)
  1052. ->select('menu_id','type')
  1053. ->get()->toArray();
  1054. $menu_map = array_column($menu,'uri','id');
  1055. //该角色下 菜单里所有按钮
  1056. $button_menu = $this->fillRoleButton($role_id);
  1057. $button_t = array_column($button,null,'id');
  1058. foreach ($search as $value){
  1059. $bt = $button_menu[$value['menu_id']] ?? [];
  1060. $new = [];
  1061. foreach ($bt as $b){
  1062. if(! empty($button_t[$b])) $new[] = $button_t[$b];
  1063. }
  1064. $object[] = [
  1065. 'id' => $value['menu_id'],
  1066. 'uri' => $menu_map[$value['menu_id']] ?? '',
  1067. // 'type' => $value['type'],
  1068. 'button' => $new,
  1069. ];
  1070. }
  1071. }
  1072. return $object;
  1073. }
  1074. /**
  1075. * 人员直接绑定部门
  1076. * @param $data
  1077. * @param $user
  1078. * @return array
  1079. */
  1080. public function employeeManagerDepart($data,$user){
  1081. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  1082. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  1083. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  1084. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  1085. 'del_time' => time()
  1086. ]);
  1087. $insert = [];
  1088. foreach ($data['depart_id'] as $value){
  1089. $insert[] = [
  1090. 'employee_id' => $data['employee_id'],
  1091. 'depart_id' => $value,
  1092. 'crt_time' => time(),
  1093. 'upd_time' => time(),
  1094. ];
  1095. }
  1096. EmployeeManagerDepart::insert($insert);
  1097. return [true,''];
  1098. }
  1099. /**
  1100. * 填充角色下的按钮
  1101. * @param $role_id
  1102. * @return array
  1103. */
  1104. public function fillRoleButton($role_id){
  1105. $button = RoleMenuButton::whereIn('role_id',$role_id)
  1106. ->where('del_time',0)
  1107. ->select('menu_id','button_id')
  1108. ->get()->toArray();
  1109. $button_map = [];
  1110. foreach ($button as $value){
  1111. if(! isset($button_map[$value['menu_id']])){
  1112. $button_map[$value['menu_id']][] = $value['button_id'];
  1113. }else{
  1114. if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
  1115. }
  1116. }
  1117. return $button_map;
  1118. }
  1119. /**
  1120. * 获取登录账号的部门
  1121. * @param $employee_id
  1122. * @return array|string[]
  1123. */
  1124. public static function getLoginDepart($employee_id, $top_depart_id = 0){
  1125. if(empty($employee_id)) return [];
  1126. //自己绑定的部门 启用的部门
  1127. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1128. ->join('depart as b','b.id','a.depart_id')
  1129. ->where('a.employee_id',$employee_id)
  1130. ->where('b.is_use',Depart::IS_UES)
  1131. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1132. ->orderBy('b.parent_id','asc')
  1133. ->orderBy('b.is_main','desc')
  1134. ->orderBy('a.depart_id','asc')
  1135. ->get()->toArray();
  1136. $top = $map = $rule = $head = [];
  1137. $res_data = [];//获取当前所在部门(也可能是门店 为了 depart_id)
  1138. $my_depart = [];//当前门店下 我勾选的门店以及部门信息
  1139. $is_all_depart = $is_behind_main = 0;
  1140. if(! empty($depart)){
  1141. //库存校验
  1142. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1143. //所有部门
  1144. $list = Depart::where('del_time',0)->get()->toArray();
  1145. $depart_map = array_column($list,null,'id');
  1146. foreach ($depart as $key => $value){
  1147. if($value['parent_id'] == 0){
  1148. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1149. $depart[$key]['is_stock'] = $is_stock;
  1150. $top[$value['depart_id']] = [
  1151. 'depart_id' => $value['depart_id'],
  1152. 'is_main' => $value['is_main'],
  1153. 'basic_type_id' => $value['basic_type_id'],
  1154. 'title' => $value['title'],
  1155. 'is_stock' => $is_stock,
  1156. ];
  1157. $map[$value['depart_id']] = $value['depart_id'];
  1158. if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1159. if($value['depart_id'] == $top_depart_id) {
  1160. $my_depart[] = $depart[$key];
  1161. if(empty($res_data)){
  1162. $res_data = $depart[$key];
  1163. }else{
  1164. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1165. }
  1166. }
  1167. }else{
  1168. $t = self::getTopParentId($value['depart_id'],$list);
  1169. if($t && isset($depart_map[$t])) {
  1170. $is_stock = $set_map[$t] ?? 1;
  1171. $depart[$key]['is_stock'] = $is_stock;
  1172. $t_tmp = $depart_map[$t] ?? [];
  1173. if( ! isset($top[$t_tmp['id']])){
  1174. $top[$t_tmp['id']] = [
  1175. 'depart_id' => $t_tmp['id'],
  1176. 'is_main' => $t_tmp['is_main'],
  1177. 'basic_type_id' => $t_tmp['basic_type_id'],
  1178. 'title' => $t_tmp['title'],
  1179. 'is_stock' => $set_map[$t] ?? 1,
  1180. ];
  1181. }
  1182. $map[$value['depart_id']] = $t;
  1183. if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1184. }
  1185. if($t == $top_depart_id){
  1186. $my_depart[] = $depart[$key];
  1187. if(empty($res_data)){
  1188. $res_data = $depart[$key];
  1189. }else{
  1190. if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
  1191. }
  1192. }
  1193. }
  1194. }
  1195. //-------------重组当前所在门店
  1196. $top = array_values($top);
  1197. usort($top, function($a, $b) {
  1198. return $b['is_main'] - $a['is_main'];
  1199. });
  1200. if(! empty($top_depart_id)){
  1201. $targetArray = null;
  1202. foreach ($top as $key => $value) {
  1203. if ($value['depart_id'] == $top_depart_id) {
  1204. $targetArray = $value;
  1205. unset($top[$key]); // 从原数组中移除目标数组
  1206. break; // 找到后跳出循环
  1207. }
  1208. }
  1209. if ($targetArray) array_unshift($top, $targetArray);
  1210. }
  1211. //-------------重组当前所在门店
  1212. //-------------拥有的数据权限(门店、部门)
  1213. $my_top = $top[0] ?? [];
  1214. foreach ($my_depart as $value){
  1215. if(in_array($value['depart_id'],$rule)) continue;
  1216. if(! $value['parent_id']){ //顶级
  1217. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1218. if($value['is_main']) {//是总公司
  1219. //所有部门都有
  1220. $rule = array_column($list,'id');
  1221. }else{//不是总公司
  1222. //自己以及子部门
  1223. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1224. $rule = array_merge_recursive($rule,$depart_id);
  1225. }
  1226. }else{//非顶级
  1227. if(! empty($my_top['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1228. if($value['is_main']) {//是总社
  1229. $top_tmp = $map[$value['depart_id']];
  1230. if(! empty($depart_map[$top_tmp]['is_main'])){
  1231. //顶级公司是总公司 所有部门都有
  1232. $rule = array_column($list,'id');
  1233. }else{
  1234. //顶级公司是分公司 分公司所有部门
  1235. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1236. $rule = array_merge_recursive($rule,$depart_id);
  1237. }
  1238. }else{//不是总社
  1239. $rule = array_merge($rule,[$value['depart_id']]);
  1240. }
  1241. }
  1242. }
  1243. //-------------拥有的数据权限(门店、部门)
  1244. //总店
  1245. foreach ($list as $value){
  1246. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1247. }
  1248. }
  1249. $rule = array_unique($rule);
  1250. return [$depart,$top,$map,$rule,$is_all_depart,$head,$is_behind_main,$res_data];
  1251. }
  1252. //获取用户的所属门店信息
  1253. public static function getLoginMessage($employee_id){
  1254. if(empty($employee_id)) return [];
  1255. //自己绑定的部门 启用的部门
  1256. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1257. ->join('depart as b','b.id','a.depart_id')
  1258. ->where('a.employee_id',$employee_id)
  1259. ->where('b.is_use',Depart::IS_UES)
  1260. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1261. ->orderBy('b.parent_id','asc')
  1262. ->orderBy('b.is_main','desc')
  1263. ->orderBy('a.depart_id','asc')
  1264. ->get()->toArray();
  1265. $top = [];
  1266. if(! empty($depart)){
  1267. //所有部门
  1268. $list = Depart::where('del_time',0)->get()->toArray();
  1269. $depart_map = array_column($list,null,'id');
  1270. foreach ($depart as $value){
  1271. if($value['parent_id'] == 0){//顶级
  1272. if(! isset($top[$value['depart_id']])){
  1273. $top[$value['depart_id']] = [
  1274. 'depart_id' => $value['depart_id'],
  1275. 'is_main' => $value['is_main'],
  1276. 'title' => $value['title'],
  1277. ];
  1278. }
  1279. }else{
  1280. //门店
  1281. $t = self::getTopParentId($value['depart_id'],$list);
  1282. if($t && isset($depart_map[$t])) {
  1283. $t_tmp = $depart_map[$t] ?? [];
  1284. $is_all_depart = 0;
  1285. if(! empty($t_tmp['is_main']) && $value['is_main']) $is_all_depart = 1;
  1286. if(! isset($top[$t_tmp['id']])){
  1287. $top[$t_tmp['id']] = [
  1288. 'depart_id' => $t_tmp['id'],
  1289. 'is_main' => $is_all_depart,
  1290. 'title' => $t_tmp['title'],
  1291. ];
  1292. }else{
  1293. if(! empty($is_all_depart)) $top[$t_tmp['id']]['is_main'] = $is_all_depart;
  1294. }
  1295. }
  1296. }
  1297. }
  1298. }
  1299. $top = array_values($top);
  1300. usort($top, function($a, $b) {
  1301. return $b['is_main'] - $a['is_main'];
  1302. });
  1303. return $top;
  1304. }
  1305. public static function getLoginDepartfq($employee_id){
  1306. if(empty($employee_id)) return [];
  1307. //自己绑定的部门 启用的部门
  1308. $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
  1309. ->join('depart as b','b.id','a.depart_id')
  1310. ->where('a.employee_id',$employee_id)
  1311. ->where('b.is_use',Depart::IS_UES)
  1312. ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
  1313. ->orderBy('b.parent_id','asc')
  1314. ->orderBy('b.is_main','desc')
  1315. ->orderBy('a.depart_id','asc')
  1316. ->get()->toArray();
  1317. $top = $map = $rule = $head = [];
  1318. $is_all_depart = $is_behind_main = 0;
  1319. if(! empty($depart)){
  1320. //库存校验
  1321. $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
  1322. //所有部门
  1323. $list = Depart::where('del_time',0)->get()->toArray();
  1324. $depart_map = array_column($list,null,'id');
  1325. foreach ($depart as $key => $value){
  1326. if($value['parent_id'] == 0){//顶级
  1327. $is_stock = $set_map[$value['depart_id']] ?? 1;
  1328. $depart[$key]['is_stock'] = $is_stock;
  1329. $top[$value['depart_id']] = [
  1330. 'depart_id' => $value['depart_id'],
  1331. 'is_main' => $value['is_main'],
  1332. 'basic_type_id' => $value['basic_type_id'],
  1333. 'title' => $value['title'],
  1334. 'is_stock' => $is_stock,
  1335. ];
  1336. $map[$value['depart_id']] = $value['depart_id'];
  1337. if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
  1338. if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1339. }else{
  1340. $t = self::getTopParentId($value['depart_id'],$list);
  1341. if($t && isset($depart_map[$t])) {
  1342. $is_stock = $set_map[$t] ?? 1;
  1343. $depart[$key]['is_stock'] = $is_stock;
  1344. $t_tmp = $depart_map[$t] ?? [];
  1345. $top[$t_tmp['id']] = [
  1346. 'depart_id' => $t_tmp['id'],
  1347. 'is_main' => $t_tmp['is_main'],
  1348. 'basic_type_id' => $t_tmp['basic_type_id'],
  1349. 'title' => $t_tmp['title'],
  1350. 'is_stock' => $set_map[$t] ?? 1,
  1351. ];
  1352. $map[$value['depart_id']] = $t;
  1353. if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
  1354. if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
  1355. }
  1356. }
  1357. }
  1358. foreach ($depart as $value){
  1359. if(in_array($value['depart_id'],$rule)) continue;
  1360. if(! $value['parent_id']){ //顶级
  1361. if($value['is_main']) {//是总公司
  1362. //所有部门都有
  1363. $rule = array_column($list,'id');
  1364. }else{//不是总公司
  1365. //自己以及子部门
  1366. $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
  1367. $rule = array_merge_recursive($rule,$depart_id);
  1368. }
  1369. }else{//非顶级
  1370. if($value['is_main']) {//是总社
  1371. $top_tmp = $map[$value['depart_id']];
  1372. if(! empty($depart_map[$top_tmp]['is_main'])){
  1373. //顶级公司是总公司 所有部门都有
  1374. $rule = array_column($list,'id');
  1375. }else{
  1376. //顶级公司是分公司 分公司所有部门
  1377. $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
  1378. $rule = array_merge_recursive($rule,$depart_id);
  1379. }
  1380. }else{//不是总社
  1381. $rule = array_merge($rule,[$value['depart_id']]);
  1382. }
  1383. }
  1384. }
  1385. foreach ($list as $value){
  1386. if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
  1387. }
  1388. }
  1389. $top = array_values($top);
  1390. $rule = array_unique($rule);
  1391. return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
  1392. }
  1393. /**
  1394. * 获取顶级id
  1395. * @param $id
  1396. * @param $data
  1397. * @return int
  1398. */
  1399. public static function getTopParentId($id, $data) {
  1400. foreach ($data as $item) {
  1401. if ($item['id'] == $id) {
  1402. if ($item['parent_id'] == 0) {
  1403. // 找到最顶级的id
  1404. return $item['id'];
  1405. } else {
  1406. // 继续递归查找父级
  1407. return self::getTopParentId($item['parent_id'], $data);
  1408. }
  1409. }
  1410. }
  1411. // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
  1412. return 0;
  1413. }
  1414. /**
  1415. * 递归获取所有id
  1416. * @param $data
  1417. * @param $id
  1418. * @return array
  1419. */
  1420. public static function getAllIds($data, $id) {
  1421. $result = array(); // 存储结果的数组
  1422. foreach ($data as $node) {
  1423. if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
  1424. $result[] = $node['id'];
  1425. // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
  1426. $result = array_merge($result, self::getAllIds($data, $node['id']));
  1427. }
  1428. }
  1429. return $result;
  1430. }
  1431. public static function checkWxUser($userId){
  1432. $res = Employee::where('id', $userId)
  1433. ->where('del_time',0)
  1434. ->where('state',Employee::USE)->get()->first();
  1435. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  1436. return [true, $res];
  1437. }
  1438. }