SalesmanController.php 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
<?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(){
        $param = $this->request->param();
        $where = [
            'a.status' => 2
        ];
//        if(!empty($param['start_time']) && !empty($param['end_time'])){
//            $start_time = strtotime($param['start_time']);
//            $end_time = strtotime($param['end_time']);
//            $where['audit_time'] = [['>=',$start_time],['<=',$end_time]];
//        }
        if(!empty($param['keyword'])){
            $keyword = trim($param['keyword']);
            $where['b.phone|u.user_nickname'] = ['like','%'.$keyword.'%'];
        }
        if(!empty($param['id'])){
            $where['a.id'] = ['eq',$param['id']];
        }
        $data = Db::name('my_user')->alias('a')
            ->field("a.uid,a.id,a.balance,a.audit_time,a.is_pro,b.phone,u.user_nickname")
            ->join('user u','a.uid = u.id','LEFT')
            ->join('sale_audit b','a.uid = b.uid','LEFT')
            ->where($where)
            ->group('a.id')
            ->order(['a.balance'=>'DESC','a.create_time'=>'DESC'])
            ->paginate(12);
        $data->appends($param);
        $data_arr = $data -> toArray();
        $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'];
            // 获取提现数据
            $ti_count = Db::name('money_expend')->where('uid',$val['uid'])->where('state',1)->sum('money');// 已提金额
            $data_arr['data'][$key]['ti_count'] = $ti_count;
            $data_arr['data'][$key]['all_count'] = Db::name('money_income')->where('uid',$val['uid'])->sum('money');
        }
        $this -> assign('data_arr',$data_arr['data']);
        $this -> assign('data',$data);
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $this->assign('id',!empty($param['id']) ? $param['id'] : '');
        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()){
            if($_POST['money'] > $_POST['price']){
                $this -> error('佣金不能大于售价');
            }
           $goods_data = Db::name('goods') -> where('uid',$_POST['uid']) -> select() -> toArray();
            if(count($goods_data) >= 16){
                $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($_POST['money'] > $_POST['price']){
                $this -> error('佣金不能大于售价');
            }
            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]);
        //删除购物车 浏览记录 收藏表中下架的数据
        Db::name('browsing_history') -> where('goods_id',$id) -> delete();
        Db::name('collect') -> where('goods_id',$id) -> delete();
        Db::name('shopping_cart') -> where('goods_id',$id) -> delete();
        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);
        Db::name('shopping_cart') -> where("goods_id",$id) -> delete();
        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(){
        $param = $this->request->param();
        $where = [
            'status' => 3,
            'pid' => $param['id']
        ];
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where['a.create_time'] = [['>=',$start_time],['<=',$end_time]];
        }
        if(!empty($param['keyword'])){
            $keyword = $param['keyword'];
            $where['b.user_nickname'] = ['like',"%$keyword%"];
        }
        $this -> assign('salesman_id',!empty($param['id']) ? $param['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();

        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('start_time',!empty($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time',!empty($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword',!empty($param['keyword']) ? trim($param['keyword']) : '');
        $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(1000000);
        }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();

    }

    /**
     * 业务员余额明细表
     */
    public function salesman_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();

    }

    /**
     * 将老师和下线设置为业务员
     */
    public function set_free_man(){

        $id = $_POST['id'];
        //删除老师购物车 浏览记录 收藏 订单待付款信息
        //$teacher = Db::name('my_user') -> where('id',$id) -> find();
        /*Db::name('shopping_cart') -> where('uid',$teacher['uid']) -> delete();
        Db::name('browsing_history') -> where('uid',$teacher['uid']) -> delete();
        Db::name('collect') -> where('uid',$teacher['uid']) -> delete();
        Db::name('indent') -> where("uid=".$teacher['uid']." and state=4") -> delete();*/
        //获取这个老师下的所有学生信息
        //$student = Db::name('my_user') -> where('pid',$id) -> select();
        //循环删除学生的购物车 浏览记录 收藏 订单待付款信息
        /*foreach ($student as $key => $val){
            Db::name('shopping_cart') -> where('uid',$val['uid']) -> delete();
            Db::name('browsing_history') -> where('uid',$val['uid']) -> delete();
            Db::name('collect') -> where('uid',$val['uid']) -> delete();
            Db::name('indent') -> where("uid=".$val['uid']." and state=4") -> delete();
        }*/
        //改变老师的身份状态
        $data = Db::name('my_user') -> where('id',$id) -> update(['status'=>0,'pid'=>0,'is_pro'=>0,'bind_status'=>0,'is_withdrawal'=>1]);
        //改变学生的身份状态
        Db::name('my_user') -> where('pid',$id) -> update(['status'=>0,'pid'=>0,'bind_status'=>0]);
        if($data){
            return true;
        }else{
            return false;
        }

    }




























}