Team.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. *
  4. * User: anyluck
  5. * Date: 2020/6/4
  6. * Time: 10:43
  7. */
  8. namespace app\web\model;
  9. use app\common\model\User;
  10. class Team
  11. {
  12. // 团队
  13. // 给团队加人数
  14. public static function all_tem($user_id)
  15. {
  16. $user = User::cache(true, 60)->field("id,pid")->select()->toArray();
  17. $GetTeamMember = self::get_downline($user, $user_id);
  18. $count = 0;
  19. if ($GetTeamMember) {
  20. foreach ($GetTeamMember as $k => $v) {
  21. $count += 1;
  22. }
  23. }
  24. return $count;
  25. }
  26. public static function father($pid, $id)
  27. {
  28. $get_fu= self::get_fu($id);
  29. if ($get_fu){
  30. $get_id=array_column($get_fu,"id");
  31. $in_array=in_array($pid,$get_id);
  32. if ($in_array){
  33. return true;
  34. }else{// 没有不存在查看一下下级
  35. $user=User::field("id,pid")->cache(true,600)->select();
  36. $get_xia=self::get_downline($user->toArray(),$id);
  37. $get_xia_id=array_column($get_xia,"id");
  38. $in_arrays=in_array($pid,$get_xia_id);
  39. if ($in_arrays){
  40. return true;
  41. }else{
  42. return false;
  43. }
  44. }
  45. }
  46. }
  47. // 查找无限上级
  48. public static function get_fu($id)
  49. {
  50. static $data=[];
  51. if ($id){
  52. $where["id"]=$id;
  53. $user=User::where($where)->field("id,pid")->cache(true,600)->find();
  54. if ($user){
  55. $data[]=$user->toArray();
  56. if ($user->pid){
  57. self::get_fu($user->pid);
  58. }
  59. }
  60. }
  61. return $data;
  62. }
  63. //获取用户的所有下级ID
  64. public static function get_downline($data, $mid, $level = 0)
  65. {
  66. $arr = array();
  67. foreach ($data as $key => $v) {
  68. if ($v['pid'] == $mid) { //pid为0的是顶级分类
  69. $v['level'] = $level + 1;
  70. $arr[] = $v;
  71. $arr = array_merge($arr, self::get_downline($data, $v['id'], $level + 1));
  72. }
  73. }
  74. return $arr;
  75. }
  76. }