EmployeeService.php 56 KB

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