OrderSalesmanController.php 10.0 KB
<?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;

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

    protected function check() {
        $user_id = cmf_get_current_user_id();
        $this->user_id = $user_id;
        $my_user = Db::name('my_user')->where(['uid'=>$user_id])->find();
        if(empty($my_user)){
            $this->error('查无此人','','','');
        }
        if($my_user['status'] != 2){
            $this->error('您还不是业务员那','','','');
        }
    }
    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;
        $where['salesman_uid'] = ['eq',$user_id];
        $where['indent_type'] = ['eq',2];
        $where['state'] = ['neq',4];
        $indentModel = new IndentModel();
        $data = $indentModel->selectData($where);
        $indentGoodsModel = new IndentGoodsModel();
        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();
            }
        }
        $school = Db::name('school')->where('uid',$user_id)->order('create_time desc')->select()->toArray();
        if(count($school)!=0){
            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','');
        }

        $this->assign('data',$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(){
        $param = $this->request->param();
        if(!empty($param['school'])){
            $where['school'] = ['eq',$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'] = ['neq',4];
        }
        if(!empty($param['is_courier'])){
            $where['is_courier'] = ['eq',$param['is_courier']];
        }else{
            $where['is_courier'] = ['eq',1];
        }
        $user_id = $this->user_id;
        $where['salesman_uid'] = ['eq',$user_id];
        $where['indent_type'] = ['eq',2];
        $indentModel = new IndentModel();
        $data = $indentModel->selectData($where);
        $indentGoodsModel = new IndentGoodsModel();
        foreach($data as $key => $vo){
            $indent_goods = $indentGoodsModel->selectData(['indent_id'=>$vo['id']]);
            $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'];
            }
        }
        $this->success('SUCCESS','',$data);
    }
    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()) {
            $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->save('./upload/'.$img_url);
                $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, 0700);
            }
            $new_file = $new_file.cmf_get_current_user_id().".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                return $file_path.cmf_get_current_user_id().".{$type}";
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
}