OrderSalesmanController.php 19.1 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
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/9
 * Time: 15:34
 */

namespace app\portal\controller;


use app\portal\model\AddressModel;
use app\portal\model\IndentGoodsModel;
use app\portal\model\IndentModel;
use cmf\controller\WeChatBaseController;
use think\Db;
use think\Image;
use think\Session;

class OrderSalesmanController extends WeChatBaseController
{
    protected $user_id;
    protected $my_user;
    protected $status;
    protected $pid;
    protected $beforeActionList = [
        'check'             => ['except' => 'getGradeClass'],
    ];

    protected function check() {
        $user_id = cmf_get_current_user_id();
        $this->user_id = $user_id;
        $this->my_user = Db::name('my_user')->where(['uid'=>$user_id])->find();
        if(empty($this->my_user)){
            $this->error('查无此人','','','');
        }
        $this->status = $this->my_user['status'];
        $this->pid = $this->my_user['id'];
        if($this->my_user['status'] != 2 && $this->my_user['status'] != 3){
            $this->error('您还没有权限查看','','','');
        }
        $this->assign('my_user',$this->my_user);
    }
    public function get_all(){
        $param = $this->request->param();
        if(!empty($param['is_courier'])){
            $where['is_courier'] = ['eq',$param['is_courier']];
        }
        $user_id = $this->user_id;
        if($this->status == 2){
            $where['salesman_uid'] = ['eq',$user_id];
            //获取当前业务员下级的所有老师
            $user_ids = Db::name('my_user')->where(['pid'=>$this->pid])->column('uid');
        }else{
            //获取当前用户的所有下级
            $user_child = Db::name('my_user')->where(['pid'=>$this->pid])->select();
            $user_ids = [];
            foreach($user_child as $key => $u_c){
                $user_ids[] = $u_c['uid'];
            }
            $where['uid'] = ['in',$user_ids];
        }
        $where['indent_type'] = ['eq',2];
        $where['state'] = ['eq',2];
        $indentModel = new IndentModel();
        $data = $indentModel->selectData($where);
        $indentGoodsModel = new IndentGoodsModel();
        $indent_ids = [];
        foreach($data as $key => $vo){
            $indent_goods = $indentGoodsModel->selectData(['indent_id'=>$vo['id']]);
            $data[$key]['indent_goods'] = $indent_goods;
            //判断订单是不是平台订单
            if($vo['indent_type'] == 1){
                $data[$key]['dizhi'] = Db::name('address') -> where('id',$vo['indent_address']) -> find();
            }
            $indent_ids[] = $vo['id'];
        }
        $goods_count = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids]])->group('goods_id')->order('indent_id desc')->select()->toArray();
        foreach($goods_count as $key => $g_c){
            $number_sum = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids],'goods_id'=>$g_c['goods_id']])->sum('number');
            $goods_count[$key]['number_sum'] = $number_sum;
        }
        $this->assign('goods_count',$goods_count);
        // 身份为老师,获取上级业务员的id
        $school = [];
        if($this->my_user['status'] == 2) {
            $user_ids[] = $user_id;
            $school = Db::name('school')->whereIn('uid',$user_ids)->order('create_time desc')->select()->toArray();
        } elseif($this->my_user['status'] == 3) {
            $school_uid = Db::name('my_user')->where('id',$this->my_user['pid'])->value('uid');
            $school = Db::name('school')->whereIn('uid',[$user_id,$school_uid])->order('create_time desc')->select()->toArray();
        }
        if(count($school)!=0){
            $schools = [];
            foreach ($school as $key => $val){
                $schools[$key]['value'] = $val['id'];
                $schools[$key]['text'] = $val['school'];
            }
            $this->assign('school',json_encode($schools));
        }else{
            $this->assign('school','');
        }
        foreach ($data as $k=>$v) {
            foreach ($v['indent_goods'] as $ki=>$vi) {
                $data[$k]['indent_goods'][$ki]['thumbnail'] = cmf_get_image_url($vi['thumbnail']);
            }
        }
        $this->assign('data',$data);
        $this->assign('count',count($data));
        return $this->fetch();
    }
    public function getGradeClass(){
        $school_id = $this->request->param('school_id',0,'intval');
        if(empty($school_id)){
            $this->error('缺少必要参数');
        }
        $grade = Db::name('grade_class')->where(['school_id'=>$school_id])->select()->toArray();
        foreach($grade as $key => $g){
            $class = explode('-',$g['class']);
            $grade[$key]['start'] = $class[0];
            $grade[$key]['end'] = $class[1];
        }
        $this->success('SUCCESS','',$grade);
    }
    public function getOrderSalesman(){
        set_time_limit(0);
        ini_set('memory_limit','512M');
        $param = $this->request->param();
        if(!empty($param['school'])){
            $where['school'] = ['like',"%$param[school]%"];
        }
        if(!empty($param['grade'])){
            $param['grade'] = explode('-',$param['grade']);
            $where['grade'] = ['eq',$param['grade'][0]];
            $where['class'] = ['eq',$param['grade'][1]];
        }
        if(!empty($param['state'])){
            $where['state'] = ['eq',$param['state']];
        }else{
            $where['state'] = ['eq',2];
        }
        if(!empty($param['is_courier'])){
            $where['is_courier'] = ['eq',$param['is_courier']];
        }
//        else{
//            $where['is_courier'] = ['eq',1];
//        }//
        $user_id = $this->user_id;
        // 身份为老师,获取上级业务员的id
        $school_uid = $user_id;
//        if($this->my_user['status'] == 3) {
//            $school_uid = Db::name('my_user')->where('id',$this->my_user['pid'])->value('uid');
//        }
        if($this->status == 2){
            $where['salesman_uid'] = ['eq',$school_uid];
        }else{
            //获取当前用户的所有下级
            $user_child = Db::name('my_user')->where(['pid'=>$this->pid])->select();
            $user_ids = [];
            foreach($user_child as $key => $u_c){
                $user_ids[] = $u_c['uid'];
            }
            $where['uid'] = ['in',$user_ids];
        }
        $where['indent_type'] = ['eq',2];
        $indentModel = new IndentModel();
        $data = $indentModel->selectData($where);
        $indentGoodsModel = new IndentGoodsModel();
        $indent_ids = [];
        foreach($data as $key => $vo){
            $indent_goods = $indentGoodsModel->selectData(['indent_id'=>$vo['id']]);
            foreach ($indent_goods as &$vi) {
                $vi['thumbnail'] = cmf_get_image_url($vi['thumbnail']);
            }
            $data[$key]['indent_goods'] = $indent_goods;
            if($vo['is_courier'] == 2){
                $address = Db::name('address')->where(['id'=>$vo['indent_address']])->find();
                $data[$key]['region'] = $address['region'];
                $data[$key]['detailed'] = $address['detailed'];
                $data[$key]['phone'] = $address['phone'];
            }
            $indent_ids[] = $vo['id'];
        }
        $goods_count = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids]])->group('goods_id')->order('indent_id desc')->select()->toArray();
        foreach($goods_count as $key => $g_c){
            $number_sum = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids],'goods_id'=>$g_c['goods_id']])->sum('number');
            $goods_count[$key]['number_sum'] = $number_sum;
        }
        $result['data'] = $data;
        $result['goods_count'] = $goods_count;
        $this->success('SUCCESS','',$result);
    }
    public function send(){
        $param = $this->request->param();
        if(empty($param['is_courier'])){
            $this->error('缺少必要参数1');
        }
        if($param['is_courier'] == 1){
            if(empty($param['start_time'])){
                $this->error('缺少必要参数2');
            }
//            if(empty($param['end_time'])){
//                $this->error('缺少必要参数3');
//            }
        }else if($param['is_courier'] == 2){
            if(empty($param['logistic_name'])){
                $this->error('缺少必要参数4');
            }
            if(empty($param['logistic_code'])){
                $this->error('缺少必要参数5');
            }
        }
        if(empty($param['indent_id'])){
            $this->error('缺少必要参数6');
        }
        $where['id'] = ['eq',$param['indent_id']];
        $indentModel = new IndentModel();
        $indent = $indentModel->findData($where);
        if(empty($indent)){
            $this->error('查询为空');
        }
        if($indent['state'] != 2){
            $this->error('订单不是待支付状态');
        }
        $arr = [];
        if($param['is_courier'] == 1){
            $arr['start_time'] = $param['start_time'];
//            $arr['end_time'] = $param['end_time'];
        }else if($param['is_courier'] == 2){
            $arr['logistic_name'] = $param['logistic_name'];
            $arr['logistic_code'] = $param['logistic_code'];
        }
        $arr['state'] = 5;
        $result = $indentModel->updateData($where,$arr);////
        $this->success('发货成功');
    }
    public function send_all(){
        $param = $this->request->param();
        if(empty($param['start_time2']) || empty($param['indent_ids'])){
            $this->error('缺少必要参数');
        }
        $indent_ids = explode(',',$param['indent_ids']);
        $data['start_time'] = $param['start_time2'];
//        $data['end_time'] = $param['end_time2'];
        $data['state'] = 5;
        $indentModel = new IndentModel();
        $result = $indentModel->updateData(['id'=>['in',$indent_ids]],$data);
        if(empty($result)){
            $this->error('更新失败');
        }else{
            $this->success('发货成功');
        }
    }
    public function get_one(){
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this->error('缺少必参数','','','');
        }
        $indentModel = new IndentModel();
        $indent = $indentModel->findData(['id'=>$id])->toArray();
        if(empty($indent)){
            $this->error('查询为空');
        }
        $indentGoodsModel = new IndentGoodsModel();
        $indent_goods = $indentGoodsModel->selectData(['indent_id'=>$indent['id']]);
        if(!empty($indent['indent_address'])){
            $addressModel = new AddressModel();
            $indent_address = $addressModel->findData(['id'=>$indent['indent_address']])->toArray();
            $indent['indent_address'] = $indent_address;
        }
        $indent['indent_goods'] = $indent_goods;
        $this->assign('data',$indent);
        return $this->fetch();
    }
    public function take_all(){
        $indent_ids = $this->request->param('indent_ids');
        $data['state'] = 3;
        $indent_ids = explode(',',$indent_ids);
        $where['id'] = ['in',$indent_ids];
        $indentModel = new IndentModel();
        $result = $indentModel->updateData($where,$data);
        if(empty($result)){
            $this->error('更新失败');
        }else{
            $this->success('更新成功');
        }
    }
    public function take_one(){
        $indent_id = $this->request->param('indent_id');
        $data['state'] = 3;
        $where['id'] = ['eq',$indent_id];
        $indentModel = new IndentModel();
        $result = $indentModel->updateData($where,$data);
        if(empty($result)){
            $this->error('更新失败');
        }else{
            $this->success('更新成功');
        }
    }

    public function upload_img() {
        if($this->request->isAjax()) {
            set_time_limit(0);
            ini_set('memory_limit', '2048M');
            ini_set("max_execution_time", 0);
            $img = $this->request->param('img');
            $top = $this->request->param('top');
            if($img) {
                $img_url = $this->base64_image_content($img,'./upload');
                $new_img = Image::open('./upload/'.$img_url);
                $width = $new_img->width();
                $height = $new_img->height();
                $new_img->thumb($width,$height - intval($top) * 2,5);
                $new_img->thumb($width/2,($height - intval($top) * 2)/2);
                $new_img->save('./upload/'.$img_url,'jpg',100);
                $this->success('成功','',cmf_get_asset_url($img_url));
            }
        }
    }

    /**
     * [将Base64图片转换为本地图片并保存]
     * @E-mial wuliqiang_aa@163.com
     * @TIME   2017-04-07
     * @WEB    http://blog.iinu.com.cn
     * @param  [Base64] $base64_image_content [要保存的Base64]
     * @param  [目录] $path [要保存的路径]
     */
    private function base64_image_content($base64_image_content,$path){
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            $file_path = "order_img/";
            $new_file = $path."/".$file_path;
            if(!file_exists($new_file)){
                //检查是否有该文件夹,如果没有就创建,并给予最高权限
                mkdir($new_file, 0777,true);
            }
            $time = time();
            $new_file = $new_file.cmf_get_current_user_id().$time.".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                $file = $file_path.cmf_get_current_user_id().$time.".{$type}";
                $session_img = Session::get('img_url');
                if(file_exists('./upload/'.$session_img)) {
                    @unlink('./upload/'.$session_img);
                }
                Session::set('img_url',$file);
                return $file;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    public function updatePost(){
        $param = $this->request->param();
        $indent_id = $this->request->param('indent_id',0,'intval');
        if(empty($indent_id)){
            $this->error('缺少必要参数123');
        }
        $indentModel = new IndentModel();
        $data = $indentModel->findData(['id'=>$indent_id]);
        if($data['is_courier'] == 1){
            if(empty($param['name']) || empty($param['region']) || empty($param['school']) || empty($param['grade_class']) || empty($param['phone']) || empty($param['beiyong_address'])){
                $this->error('缺少必要参数');
            }
            $grade_class = explode('-',$param['grade_class']);
            $arr['name'] = $param['name'];
            $arr['phone'] = $param['phone'];
            $arr['region'] = $param['region'];
            $arr['school'] = preg_replace('# #','',$param['school']);
            $arr['grade'] = $grade_class[0];
            $arr['class'] = $grade_class[1];
            $arr['beiyong_address'] = $param['beiyong_address'];
            $result = $indentModel ->updateData(['id'=>$indent_id],$arr);
        }else if($data['is_courier'] == 2){
            if(empty($param['name']) || empty($param['phone']) || empty($param['region']) || empty($param['region_detail'])){
                $this->error('缺少必要参数123');
            }
            $arr['name'] = $param['name'];
            $arr['phone'] = $param['phone'];
            $arr['region'] = $param['region'];
            $arr['region_detail'] = $param['region_detail'];
            $result = $indentModel ->updateData(['id'=>$indent_id],$arr);
        }
        $this->success('SUCCESS');
    }

    // 订单截图
    public function get_jietu(){
        $param = $this->request->param();
        if(!empty($param['is_courier'])){
            $where['is_courier'] = ['eq',$param['is_courier']];
        }
        $user_id = $this->user_id;
        if($this->status == 2){
            $where['salesman_uid'] = ['eq',$user_id];
            //获取当前业务员下级的所有老师
            $user_ids = Db::name('my_user')->where(['pid'=>$this->pid])->column('uid');
        }else{
            //获取当前用户的所有下级
            $user_child = Db::name('my_user')->where(['pid'=>$this->pid])->select();
            $user_ids = [];
            foreach($user_child as $key => $u_c){
                $user_ids[] = $u_c['uid'];
            }
            $where['uid'] = ['in',$user_ids];
        }
        $where['indent_type'] = ['eq',2];
        $where['state'] = ['eq',2];
        $indentModel = new IndentModel();
        $data = $indentModel->selectData($where);
        $indentGoodsModel = new IndentGoodsModel();
        $indent_ids = [];
        foreach($data as $key => $vo){
            $indent_goods = $indentGoodsModel->selectData(['indent_id'=>$vo['id']]);
            $data[$key]['indent_goods'] = $indent_goods;
            //判断订单是不是平台订单
            if($vo['indent_type'] == 1){
                $data[$key]['dizhi'] = Db::name('address') -> where('id',$vo['indent_address']) -> find();
            }
            $indent_ids[] = $vo['id'];
        }
        $goods_count = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids]])->group('goods_id')->order('indent_id desc')->select()->toArray();
        foreach($goods_count as $key => $g_c){
            $number_sum = $indentGoodsModel->where(['indent_id'=>['in',$indent_ids],'goods_id'=>$g_c['goods_id']])->sum('number');
            $goods_count[$key]['number_sum'] = $number_sum;
        }
        $this->assign('goods_count',$goods_count);
        // 身份为老师,获取上级业务员的id
        $school = [];
        if($this->my_user['status'] == 2) {
            $user_ids[] = $user_id;
            $school = Db::name('school')->whereIn('uid',$user_ids)->order('create_time desc')->select()->toArray();
        } elseif($this->my_user['status'] == 3) {
            $school_uid = Db::name('my_user')->where('id',$this->my_user['pid'])->value('uid');
            $school = Db::name('school')->whereIn('uid',[$user_id,$school_uid])->order('create_time desc')->select()->toArray();
        }
        if(count($school)!=0){
            $schools = [];
            foreach ($school as $key => $val){
                $schools[$key]['value'] = $val['id'];
                $schools[$key]['text'] = $val['school'];
            }
            $this->assign('school',json_encode($schools));
        }else{
            $this->assign('school','');
        }
        foreach ($data as $k=>$v) {
            foreach ($v['indent_goods'] as $ki=>$vi) {
                $data[$k]['indent_goods'][$ki]['thumbnail'] = cmf_get_image_url($vi['thumbnail']);
            }
        }
        $this->assign('data',$data);
        $this->assign('count',count($data));
        return $this->fetch();
    }
}