Team.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\user;
  3. use think\Db;
  4. use Exception;
  5. use app\admin\model\UsersPath;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use app\common\controller\Backend;
  9. use think\exception\ValidateException;
  10. /**
  11. * 会员团队
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Team extends Backend
  16. {
  17. /**
  18. * @var \app\admin\model\UsersPath
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('User');
  25. $user_id = $this->request->param('ids/d', 0);
  26. $this->assignconfig('user_id', $user_id);
  27. $this->view->assign("typeList", UsersPath::where('parent_id', $user_id)->order('distance', 'asc')->column('distance'));
  28. }
  29. /**
  30. * 查看
  31. * @return string|Json
  32. * @throws \think\Exception
  33. * @throws DbException
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if (false === $this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. //如果发送的来源是 Selectpage,则转发到 Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. [, $sort, $order, $offset, $limit] = $this->buildparams();
  47. $where = self::_where(json_decode($this->request->get("filter"), true));
  48. $mapType = [];
  49. $type = $this->request->param('distance/d');
  50. if($type > 0) $mapType['distance'] = $type;
  51. $user_id = $this->request->param('user_id/d');
  52. $list = $this->model->alias('a')
  53. ->join('users b','a.parent_id = b.id', 'LEFT')
  54. ->join('users_path c','a.id = c.user_id and c.parent_id ='. $user_id, 'LEFT')
  55. ->where('a.id','IN',function($query) use($user_id){
  56. $query->table('ta_users_path')->where('parent_id', $user_id)->order('distance', 'desc')->field('user_id');
  57. })
  58. ->where($where)->where($mapType)->where('a.is_delete', 0)
  59. ->field('a.id,a.is_lock,a.mobile,a.balance,a.bonus_sum,a.create_time,b.mobile as parent_name,c.distance')
  60. ->order($sort, $order)
  61. ->paginate($limit);
  62. if($list->total() > 0){
  63. $in = model('Mongyin');
  64. $out = model('Mongyout');
  65. foreach ($list as &$item) {
  66. $item->recharge = $in::where('status', 1)->where('user_id', $item->id)->sum('amount');
  67. $item->withdraw = $out::where('status', 1)->where('user_id', $item->id)->sum('amount');
  68. }
  69. }
  70. $result = ['total' => $list->total(), 'rows' => $list->items()];
  71. return json($result);
  72. }
  73. /**
  74. * 封禁
  75. * @param $ids
  76. * @return string
  77. * @throws DbException
  78. * @throws \think\Exception
  79. */
  80. public function lock($ids = null)
  81. {
  82. $row = $this->model->get($ids);
  83. if (!$row) {
  84. $this->error(__('No Results were found'));
  85. }
  86. $adminIds = $this->getDataLimitAdminIds();
  87. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  88. $this->error(__('You have no permission'));
  89. }
  90. $result = false;
  91. try {
  92. $result = $row->allowField(true)->save(['is_lock'=>1]);
  93. } catch (ValidateException|PDOException|Exception $e) {
  94. $this->error($e->getMessage());
  95. }
  96. if (false === $result) {
  97. $this->error(__('No rows were updated'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 封禁团队
  103. * @param $ids
  104. * @return string
  105. * @throws DbException
  106. * @throws \think\Exception
  107. */
  108. public function lockAll($ids = null)
  109. {
  110. $rows = UsersPath::where('parent_id', $ids)->column('distance');
  111. if (!$rows) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $result = false;
  115. try {
  116. $result = $this->model->where('id', 'in', $rows)->update(['is_lock'=>1]);
  117. } catch (ValidateException|PDOException|Exception $e) {
  118. $this->error($e->getMessage());
  119. }
  120. if (false === $result) {
  121. $this->error(__('No rows were updated'));
  122. }
  123. $this->success();
  124. }
  125. //搜索条件
  126. private static function _where($filter = array()): array
  127. {
  128. $where = [];
  129. if(isset($filter['mobile'])) $where['a.mobile'] = $filter['mobile'];
  130. if(isset($filter['create_time'])) {
  131. $arr = explode(' - ', $filter['create_time']);
  132. $where['a.create_time']= ['between', [strtotime($arr[0]), strtotime($arr[1])]];
  133. }
  134. return $where;
  135. }
  136. }