EmployeeService.php 59 KB

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