UsersController.php 9.3 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use Dompdf\Image\Cache;
use think\Db;
class UsersController extends AdminBaseController{

    public function get_over(){
        $order = Db::name('indent')->whereIn('state',[2,3,5,6])
            ->whereBetween('pay_time',[1567958400,1568044799])
            ->sum('money');
        echo $order.'--';
        $zhichu = Db::name('money_expend')->where('state',1)
            ->whereBetween('create_time',[1567958400,1568044799])
            ->sum('money');
        echo $zhichu;
        $zhichu1 = Db::name('money_expend')->where('state',1)
            ->whereBetween('create_time',[1567958400,1568044799])
            ->column('id');
        var_dump($zhichu1);
//        $list = Db::name('money_income')->alias('mi')
//            ->field('mi.*,i.uid as iuid,i.money as imoney,indent_type,salesman_uid')
//            ->join('__INDENT__ i','mi.indent_id = i.id')
//            ->where('i.state',2)
//            ->select(false);
//        var_dump($list);
    }

    public function over_count() {
        $income = Db::name('money_income')->where('money','>',0)->select();
        $income = collection($income)->toArray();
        $ids = $user_ids = [];
        $money = 0;
        foreach ($income as $k=>$v) {
            $indent = Db::name('indent')->where('id',$v['indent_id'])->find();
            if(!$indent) {
                $ids[] = $v['id'];
                $user_ids[] = $v['uid'];
                $money += $v['money'];
            }
        }
        echo implode(',',$ids).'<br/>';
        echo implode(',',$user_ids).'<br/>';
        echo $money;
    }

    /**
     * 显示用户列表
     */
    public function user_list(){
        $param = $this->request->param();
        $where = [
            'a.create_time' => ['>=', 0],
        ];
        if(!empty($param['keyword'])){
            $keyword = trim($param['keyword']);
            $where['b.user_nickname|a.phone'] = ['like',"%$keyword%"];
        }
        if(!empty($param['id'])){
            $where['a.id'] = ['eq',$param['id']];
        }
        if(!empty($param['identity'])){
            if($param['identity'] == -1){
                $where['a.status'] = ['eq',0];
            }else{
                $where['a.status'] = ['eq',$param['identity']];
            }
        }
        $data = Db::name('my_user') -> alias('a')
            ->field('a.*,b.user_nickname')
            ->join('user b','a.uid = b.id','LEFT')
            ->where($where)
            ->order(['a.balance'=>'DESC','a.create_time'=>'DESC'])
            ->paginate(12);
        $data->appends($param);
        $list = $data->items();
        foreach ($list as $k=>$v) {
            // 获取邀请人昵称
            $invite_nickname = '';
            if($v['pid']) {
                $invite = Db::name('my_user')->alias('m')
                    ->field('u.user_nickname')
                    ->join('user u','m.uid = u.id')
                    ->where('m.id',$v['pid'])
                    ->find();
                $invite_nickname = $invite['user_nickname'];
            }
            $list[$k]['invite_nickname'] = $invite_nickname;
            // 获取提现数据
            $ti_count = Db::name('money_expend')->where('uid',$v['uid'])->where('state',1)->sum('money');// 已提金额
            $list[$k]['ti_count'] = $ti_count;
            $list[$k]['all_count'] = Db::name('money_income')->where('uid',$v['uid'])->sum('money');
            // 获取邀请人数
            $invite_count = 0;
            if(in_array($v['status'],[2,3])) {
                $invite_count = Db::name('my_user')
                    ->alias('a')
                    ->field('a.*,b.user_nickname')
                    ->join('user b','a.uid = b.id','left')
                    ->where('status',$v['status']+1)
                    ->where('pid',$v['id'])
                    ->count();
            }
            $list[$k]['invite_count'] = $invite_count;
        }
//        echo Db::name('my_user')->sum('balance');
//        echo Db::name('indent')->where('indent_type',1)->where('state',5)->sum('money');
        $this->assign('list',$list);
        $this->assign('data',$data);
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $this->assign('status',!empty($param['identity']) ? $param['identity'] : '');
        $this->assign('id',!empty($param['id']) ? $param['id'] : '');
        return $this->fetch();

    }

    /**
     * 设置为业务员
     */
    public function set_salesman(){

        $id = $_POST['id'];
        $data = Db::name('my_user') -> where('id',$id) -> update(['status'=>1]);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 下线老师列表
     */
    public function teacher_list(){

        if($this -> request -> isPost()){
            $where = [
                'status' => 3,
                'pid' => $_POST['salesman_id']
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['user_nickname'] = ['like',"%$keyword%"];
            }
            $this -> assign('salesman_id',$_POST['salesman_id']);
            $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where($where) -> paginate(1000000);
            $data_arr = $data -> toArray();
        }else{
            $id = $this -> request -> param();
            $this -> assign('salesman_id',$id['id']);
            $data = Db::name('my_user') -> where("status = 3 and pid =".$id['id']) -> paginate(12);
            $data_arr = $data -> toArray();
            foreach($data_arr['data'] as $key => $val){
                $data_nick = Db::name('user') -> where('id',$data[$key]['uid']) -> find();
                $data_arr['data'][$key]['user_nickname'] = $data_nick['user_nickname'];
            }
        }

        foreach ($data_arr['data'] as $key => $val){
            $money_income = Db::name('money_income') -> where('uid',$val['uid']) -> select();
            $m_money = 0;
            foreach ($money_income as $key1 => $val1){
                $m_money += $val1['money'];
            }
            $data_arr['data'][$key]['money'] = $m_money;
        }

        $this -> assign('data',$data);
        $this -> assign('data_arr',$data_arr['data']);
        return $this -> fetch();

    }

    /**
     * 下线学生列表
     */
    public function student_list(){

        if($this -> request -> isPost()){
            $teacher_id = $_POST['teacher_id'];
            $where = [
                'a.status' => 4,
                'a.pid' => $teacher_id
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['b.user_nickname'] = ['like',"%$keyword%"];
            }
            $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where($where) -> paginate(1000000);
        }else{
            $id = $this -> request -> param();
            $teacher_id = $id['teacher_id'];
            $data = Db::name('my_user') -> alias('a') -> field('a.*,b.user_nickname') -> join('user b','a.uid = b.id','left') -> where("a.status = 4 and a.pid =".$teacher_id) -> paginate(12);
        }
        $this -> assign('teacher_id',$teacher_id);
        $this -> assign('data',$data);
        return $this -> fetch();

    }


    /**
     * 显示余额明细页
     */
    public function users_money(){

        $uid = $this -> request -> param();
        $data = Db::name('money_income')->where('uid',$uid['uid'])->order('create_time','DESC')->select()->toArray();
        if(!empty($data)){
            foreach ($data as $key => $val){
                if($val['type'] == 2){
                    $data[$key]['indent'] = Db::name('indent') -> where('id',$val['indent_id']) -> find();
                    $indent_good = Db::name('indent_goods') -> where('indent_id',$val['indent_id']) -> select() -> toArray();
                    $indent_goods = '';
                    foreach ($indent_good as $key1 => $val1){
                        $indent_goods .= $val1['book_name'].',';
                    }
                    $data[$key]['indent_good'] = $indent_goods;
                }else{
                    $data[$key]['indent'] = Db::name('indent') -> where('id',$val['indent_id']) -> find();
                    $data[$key]['indent_good'] = $val['book_name'];
                }
            }
        }
        $this -> assign('data',$data);
        return $this -> fetch();

    }

}