SalesmanController.php 12.2 KB
<?php
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use function GuzzleHttp\Psr7\str;
use think\Db;
class SalesmanController extends AdminBaseController{

    /**
     * 业务员列表
     */
    public function salesman_list(){

        if($this -> request -> isPost()){
            $where = [
                'status' => 2
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['audit_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['b.phone'] = $keyword;
            }
            $data = Db::name('my_user') -> alias('a') -> field("a.uid,a.id,a.balance,a.audit_time,a.is_pro,b.phone") -> join('sale_audit b','a.uid = b.uid','LEFT') -> where($where) -> paginate(12);
        }else{
            $data = Db::name('my_user') -> alias('a') -> field("a.uid,a.id,a.balance,a.audit_time,a.is_pro,b.phone") -> join('sale_audit b','a.uid = b.uid','LEFT') -> where('status',2) -> paginate(12);
        }

        $data_arr = $data -> toArray();
   /*     array(1) {
            [0] => array(5) {
                ["uid"] => int(1)
                ["balance"] => string(6) "100.00"
                ["audit_time"] => int(1558509018)
                ["is_pro"] => int(1)
                ["phone"] => string(11) "18602699971"
  }
}*/
        $data_money = Db::name('money_ratio') -> find();
        foreach ($data_arr['data'] as $key => $val){
            $data_user = Db::name('user') -> where('id',$val['uid']) -> find();
            $data_arr['data'][$key]['name'] = $data_user['user_nickname'];
            $data_arr['data'][$key]['money'] = $data_money['platform_ratio'];
        }
        $this -> assign('data_arr',$data_arr['data']);
        $this -> assign('data',$data);
        return $this -> fetch();

    }

    /**
     * 业务员编辑查看
     */
    public function salesman_edit(){

        if($this -> request -> isPost()){
            $data['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['phone'] = $_POST['phone'];
            $data['id_number'] = $_POST['id_number'];
            $res = Db::name('sale_audit') -> update($data);
            if($res){
                $this -> success('保存成功',url('Salesman/salesman_edit',array('uid' =>$_POST['uid'] )));
            }else{
                $this -> error('保存失败!');
            }
        }else{
            $uid = $this -> request -> param();
            $data = Db::name('sale_audit') -> where('uid',$uid['uid']) -> find();
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品管理列表
     */
    public function salesman_goods_list(){

        if($this -> request -> isPost()){
            $where = [
                'uid' => ['=',$_POST['uid']]
            ];
            if(!empty($_POST['start_time']) && !empty($_POST['end_time'])){
                $start_time = strtotime($_POST['start_time']);
                $end_time = strtotime($_POST['end_time']);
                $where['create_time'] = [['>=',$start_time],['<=',$end_time]];
            }
            if(!empty($_POST['keyword'])){
                $keyword = $_POST['keyword'];
                $where['book_name'] = ['like',"%$keyword%"];
            }
            $data = Db::name('goods') -> order('sort desc') -> where($where) -> select() -> toArray();
            $this -> assign('good_uid',$_POST['uid']);
            $data_num = Db::name('goods') -> order('sort desc') -> where('uid',$_POST['uid']) -> select() -> toArray();
            for($i=0;$i<count($data_num);$i++){
                $data_num[$i]['wei_sort'] = $i;
            }
            /**
             * 获取假的排序顺序
             */
            for($i=0;$i<count($data_num);$i++){
                for($j=0;$j<count($data);$j++){
                    if($data[$j]['id'] == $data_num[$i]['id']){
                        $data[$j]['wei_sort'] = $data_num[$i]['wei_sort'];
                    }
                }
            }
            $this -> assign('data_num',$data_num);
            $this -> assign('data',$data);
            return $this -> fetch();
        }else{
            $uid = $this -> request -> param();
            $this -> assign('good_uid',$uid['uid']);
            $data = Db::name('goods') -> order('sort desc') -> where('uid',$uid['uid']) -> select() -> toArray();
            for($i=0;$i<count($data);$i++){
                $data[$i]['wei_sort'] = $i;
            }
            $data_num = $data;
            $this -> assign('data_num',$data_num);
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品添加
     */
    public function salesman_goods_add(){

        if($this -> request -> isPost()){
           $goods_data = Db::name('goods') -> where('uid',$_POST['uid']) -> select() -> toArray();
            if(count($goods_data) >= 8){
                $this -> error('添加失败');
            }else{
                if(!empty($_POST['det_img'])){
                    $_POST['det_img'] = json_encode($_POST['det_img']);
                    $_POST['det_name'] = json_encode($_POST['det_name']);
                }
                $_POST['create_time'] = time();
                $data = Db::name('goods') -> insertGetId($_POST);
                Db::name('goods') -> where('id',$data) -> update(['sort'=>$data]);
                if($data){
                    $this -> success('添加成功',url('Salesman/salesman_goods_list',array('uid'=>$_POST['uid'])));
                }else{
                    $this -> error('添加失败');
                }
            }
        }else{
            $uid = $this -> request -> param();
            $this -> assign('good_uid',$uid['uid']);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品管理列表排序替换
     */
    public function salesman_change(){

        $start_id = $_POST['start_id'];
        $start_sort = $_POST['start_sort'];
        $end_id = $_POST['end_id'];
        $end_sort = $_POST['end_sort'];
        Db::name('goods') -> where('id',$start_id) -> update(['sort'=>$end_sort]);
        Db::name('goods') -> where('id',$end_id) -> update(['sort'=>$start_sort]);
        return true;

    }

    /**
     * 业务员商品管理列表编辑
     */
    public function salesman_goods_edit(){

        if($this -> request -> isPost()){
            if(!empty($_POST['det_img'])){
                $_POST['det_img'] = json_encode($_POST['det_img']);
                $_POST['det_name'] = json_encode($_POST['det_name']);
            }
            $data = Db::name('goods') -> update($_POST);
            if($data){
                $this -> success('保存成功',url('Salesman/salesman_goods_edit',array('id'=>$_POST['id'])));
            }else{
                $this -> error('保存失败');
            }
        }else{
            $id = $this -> request -> param();
            $data = Db::name('goods') -> where('id',$id['id']) -> find();
            if(!empty($data['det_img'])){
                $data['det_img'] = json_decode($data['det_img'],true);
                $data['det_name'] = json_decode($data['det_name'],true);
                foreach ($data['det_img'] as $key => $val){
                    $data['more'][$key]['url'] = $data['det_img'][$key];
                    $data['more'][$key]['name'] = $data['det_name'][$key];
                }
            }
            $this -> assign('data',$data);
            return $this -> fetch();
        }

    }

    /**
     * 业务员商品列表下架
     */
    public function salesman_goods_soldout(){

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

    }

    /**
     * 业务员商品列表上架
     */
    public function salesman_goods_putaway(){

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

    }

    /**
     * 业务员商品列表删除
     */
    public function salesman_goods_del(){

        $id = $_POST['id'];
        $data = Db::name('goods') -> delete($id);
        if($data){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 业务员列表取消推广
     */
    public function salesman_list_no_pro(){

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

    }

    /**
     * 业务员列表恢复推广
     */
    public function salesman_list_yes_pro(){

        $id = $_POST['id'];
        $data = Db::name('my_user') -> where('id',$id) -> update(['is_pro'=>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(12);
            $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'];
            }
        }

        $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'];
            $salesman_id = $_POST['salesman_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(12);
        }else{
            $id = $this -> request -> param();
            $teacher_id = $id['teacher_id'];
            $salesman_id = $id['salesman_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('salesman_id',$salesman_id);
        $this -> assign('teacher_id',$teacher_id);
        $this -> assign('data',$data);
        return $this -> fetch();

    }






























}