Demo entry 6682309

444

   

Submitted by 44 on Dec 08, 2017 at 03:55
Language: PHP. Code size: 87.5 kB.

<?php

namespace App\Http\Controllers\Api;

use App\Jobs\CarOrderToServer;
use App\Jobs\NoBidCarHandle;
use App\Http\Controllers\Controller;
use App\Jobs\SendCarToSwock;
use App\OrderCar;
use App\SysConfig;
use Illuminate\Http\Request;
use App\Jobs\BidOrder;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use App\Car;
use App\Order;
use Mockery\Exception;
use Validator;
use App\User;
use SimEncrypt;
use App\SecuDeposit;
use App\SecuDepositLog;
use App\ScoreDetails;
use HttpClient;
use App\Information;
use Illuminate\Support\Facades\Cache;
use App\Jobs\UpdateJzStatus;
use App\Jobs\UpdateJzFee;
use App\Jobs\UpdateJzStatusZb;
use App\Jobs\UpdateUserScore;
use App\OrderZb;
use App\Jobs\NoBidCarHandleZb;
use App\PersonRenzLog;
use App\CompanyRenzLog;

class OrderController extends Controller
{
    /**
     * 供运维监控使用
     */
    public function monitor(Request $request)
    {
        $params = $request->all();
        //f_log(__CLASS__.'::'.__FUNCTION__.'::params:',$params);
        return send_back($params, 1, "返回数据成功");
    }

    //矩阵下单
    public function add_order(Request $request)
    {

        $params = $request->all();
        //记录订单请求日志
        w_log(__FUNCTION__ . '#' . $params['jz_order_id'], $params, 'big');
        f_log(__CLASS__ . '::' . __FUNCTION__ . '::params:', $params);
        //格式校验
        $validator = Validator::make($params, [
            'publish_sid' => "alpha_num|size:16",
            'jz_order_id' => "required|integer",
            'jz_order_long' => 'required|alpha_num|size:17',
            'source_type' => 'required|integer',
            'service_type' => 'required|integer',
            'car_name' => 'required|max:50',
            //'car_no' => 'required|between:6,9',
            'car_pic_url' => 'required',
            'out_province' => 'required|integer',
            'out_city' => 'required|integer',
            'out_city_name' => 'required',
            'in_province' => 'required|integer',
            'in_city' => 'required|integer',
            'in_city_name' => 'required',
            'get_car_addr' => 'required|string|max:255',
            'to_addr' => 'required|string|max:255',
            'transfer_fee' => 'required|numeric',
            'info_fee' => 'required|numeric',
            'out_contacts' => 'required|max:30',
            'transfer_time' => 'required|integer',
            'in_contacts' => 'required|max:30',
        ]);
        if ($validator->fails()) {     //格式验证未通过
            return send_back($validator->errors(), 0, $validator->errors()->first());
        }
//      检查车辆唯一
        $car = Car::select("id")
            ->Where(function ($query) use ($params) {
                $query->Where('jz_order_id', $params['jz_order_id'])
                    ->orWhere('jz_order_long', $params['jz_order_long'])
                    ->orWhere('car_no', $params['car_no']);
                if ($params['publish_sid']) {
                    $query->orWhere('publish_sid', $params['publish_sid']);
                }
            })->wherein('car_status', [0, 1, 2])->where('out_city', $params['out_city']);

        $car = $car->first();
        if (sizeof($car) > 0) {
            return send_back([], 0, "不可重复下单");
        }

        $out_name = $params['out_province_name'] == $params['out_city_name'] ? $params['out_city_name'] : $params['out_province_name'] . $params['out_city_name'];
        $in_name = $params['in_province_name'] == $params['in_city_name'] ? $params['in_city_name'] : $params['in_province_name'] . $params['in_city_name'];
        $comments = $out_name . "到" . $in_name . "又有新车源啦~晚了就抢不到喽~";
//        存入数据库
        $order_car = DB::transaction(function () use ($params, $comments) {
            #图片URL处理
            if (isset($params['shop_pic_url']) && !empty($params['shop_pic_url']) && strpos($params['shop_pic_url'], 'http://') !== false) {
                $params['shop_pic_url'] = str_replace('http://', 'https://', $params['shop_pic_url']);
            }
            if (isset($params['car_pic_url']) && !empty($params['car_pic_url']) && strpos($params['car_pic_url'], 'http://') !== false) {
                $params['car_pic_url'] = str_replace('http://', 'https://', $params['car_pic_url']);
            }
            $params['publish_time'] = time();
            $params['create_time'] = time();
            f_log(__CLASS__ . '::' . __FUNCTION__ . '::Carparams0101:', $params);
            $car = Car::create(get_table_data(new Car(), $params));
            $params['car_id'] = $car->id;
            $order = Order::create(get_table_data(new Order(), $params));
            $params['order_id'] = $order->id;
            $order_car = OrderCar::create(get_table_data(new OrderCar(), $params));
            $car->order_id = $order->id;
            $car->save();

            $users = User::select("id")
                ->where("status", 1)->get();

            //发送系统通知
            if (count($users) > 0) {
                $info_data = array();
                foreach ($users as $user) {
                    $info_data[] = [
                        "order_id" => $order->id,
                        "user_id" => $user->id,
                        "car_id" => $car->id,
                        "comments" => $comments,
                        "publish_time" => time(),
                        "type" => 1
                    ];
                    \Predis::setex('have_new_order_' . $user->id, 60 * 60 * 24 * 30, 1);
                }
                Information::insert($info_data);
            }
            return $order_car;
        });

        w_log(__FUNCTION__ . '::order_car::', $order_car, 'big');
        //返回数据
        if (sizeof($order_car) == 1) {
            //发送到socket服务器
            $order_car = $order_car->toArray();
            if ($params['is_test'] == 2) {
                $ressss = $this->dispatch(new \App\Jobs\JpushW(['type' => 'car', 'message' => $comments]));
                w_log('jpush_log', json_encode($ressss), 'big');
            }
            $this->dispatch(new UpdateJzStatus($params['jz_order_id'], 1));
            // 添加延时任务, 6小时
            $job = (new NoBidCarHandle($order_car['order_id']))->delay(SysConfig::get_time('man_plan_endtime') - time());
//            \Log::info('没有人抢单队列下单'.__LINE__);
            $this->dispatch($job);
            $path = '/car/new_car';
            $this->dispatch(new SendCarToSwock($path, $order_car));
            return send_back([], 1, "已经成功下单");
        }
        return send_back([], 0, "下单失败");
    }

    //矩阵攒板下单
    public function add_order_zb(Request $request)
    {
        $params = $request->all();
        $params['is_test'] = isset($params['is_test']) ? intval($params['is_test']) : 2;
        //记录订单请求日志
        f_log(__FUNCTION__, $params);
        //格式校验
        $error = '';
        foreach ($params['cars'] as $one) {
            $res = $this->check_car_params($one, $error);
            if (!$res) {
                return send_back($error->errors(), 0, $error->errors()->first());
            }
        }

        $validator_zb = Validator::make($params['order_zb'], [
            'order_id_zb' => 'required|integer',
            'create_time_zb' => "required|integer",
            'weituo_name' => 'required',
            'weituo_phone' => 'required|digits:11',
            'get_car_name' => 'required',
            'get_car_phone' => 'required|digits:11',
            'get_car_address' => 'required|string|max:255',
            'from_province' => 'required|integer',
            'from_city' => 'required|integer',
            'to_province' => "required|integer",
            'to_city' => "required|integer",
            'fee_per_car' => "required|integer",
            'fee_all_car' => "required|integer",
            'car_num' => 'required|integer'
        ]);
        if ($validator_zb->fails()) {     //攒板数据格式验证未通过
            return send_back($validator_zb->errors(), 0, $validator_zb->errors()->first());
        }
        //检查车辆唯一
        $car = OrderZb::select("order_id_zb")
            ->where('order_id_zb', $params['order_zb']['order_id_zb'])
            ->where('bid_status', '<>', 4)
            ->first();
        if (sizeof($car) > 0) {
            return send_back([], 0, "不可重复下单");
        }
        $out_name = $params['cars'][0]['out_province_name'] == $params['cars'][0]['out_city_name'] ? $params['cars'][0]['out_city_name'] : $params['cars'][0]['out_province_name'] . $params['cars'][0]['out_city_name'];
        $in_name = $params['cars'][0]['in_province_name'] == $params['cars'][0]['in_city_name'] ? $params['cars'][0]['in_city_name'] : $params['cars'][0]['in_province_name'] . $params['cars'][0]['in_city_name'];
        $comments = $out_name . "到" . $in_name . "又有新车源啦~晚了就抢不到喽~";
//        存入数据库
        DB::beginTransaction();

        try {
            $params['order_zb']['in_city_name'] = $params['cars'][0]['in_city_name'];
            $params['order_zb']['out_city_name'] = $params['cars'][0]['out_city_name'];
            $params['order_zb']['info_fee'] = $params['order_zb']['info_fee'];
            $params['order_zb']['bid_status'] = 1;
            $params['order_zb']['fee_all_car'] == 0 && $params['order_zb']['fee_all_car'] = $params['order_zb']['fee_per_car'];
//            $params['order_zb']['create_time_zb'] = time();
            $params['order_zb']['is_test'] = $params['is_test'];
            $order_zb = OrderZb::create(get_table_data(new OrderZb(), $params['order_zb']));
            $order_cars = array();
            $trans_fee = count($params['cars']) >= $params['order_zb']['car_num'] ? $params['order_zb']['fee_all_car'] : $params['order_zb']['fee_per_car'];
            $trans_fee = $params['order_zb']['car_num'] == 0 ? $params['order_zb']['fee_per_car'] : $trans_fee;
            $order_ids = $car_ids = [];
            foreach ($params['cars'] as $one) {
                #图片URL处理
                if (isset($one['shop_pic_url']) && !empty($one['shop_pic_url']) && strpos($one['shop_pic_url'], 'http://') !== false) {
                    $one['shop_pic_url'] = str_replace('http://', 'https://', $one['shop_pic_url']);
                }
                if (isset($one['car_pic_url']) && !empty($one['car_pic_url']) && strpos($one['car_pic_url'], 'http://') !== false) {
                    $one['car_pic_url'] = str_replace('http://', 'https://', $one['car_pic_url']);
                }
                $one['publish_time'] = time();
                $one['buyer_name'] = $params['order_zb']['weituo_name'];
                $one['buyer_mobile'] = $params['order_zb']['weituo_phone'];
                $one['create_time'] = time();
                $one['order_id_zb'] = $params['order_zb']['order_id_zb'];
                $one['transfer_fee'] = $trans_fee;
                $one['total_fee'] = $trans_fee + $params['order_zb']['info_fee'];
                f_log(__CLASS__ . '::' . __FUNCTION__ . '::Carparams02:', $params);
                $car = Car::create(get_table_data(new Car(), $one));
                $one['car_id'] = $car->id;
                $one['is_test'] = intval($params['is_test']);
                array_push($car_ids, $car->id);
                $order = Order::create(get_table_data(new Order(), $one));
                $one['order_id'] = $order->id;
                $one['is_test'] = intval($params['is_test']);
                array_push($order_ids, $order->id);
                $one['is_test'] = intval($params['is_test']);
                $order_car[] = OrderCar::create(get_table_data(new OrderCar(), $one));
                $car->order_id = $order->id;
                $car->save();
            }
            $users = User::select("id")
                ->where("status", 1)
                ->where(function ($sql) {
                    $sql->where("com_is_authed", 1)
                        ->orWhere("is_authed", 1);
                })->get();

            //发送系统通知
            if (count($users) > 0) {
                $info_data = array();
                foreach ($users as $user) {
                    //新需求攒板车辆按车辆推送消息
                    foreach ($order_ids as $key => $val) {
                        $info_data[] = [
                            "order_id" => $val,
                            "user_id" => $user->id,
                            "car_id" => $car_ids[$key],
                            "comments" => $comments,
                            "publish_time" => time(),
                            "type" => 1
                        ];
                    }
                    \Predis::setex('have_new_order_' . $user->id, 60 * 60 * 24 * 30, 1);
                }
                Information::insert($info_data);
            }
            DB::commit();
//            return $order_zb;
        } catch (Exception $e) {
            DB::rollBack();
            return send_back([], 0, "下单失败");
        }
        //返回数据
        //if (sizeof($order_zb) == 1) {
        if ($order_zb) {
            //发送到socket服务器
            $order_zb = $order_zb->toArray();
            $this->dispatchNow(new UpdateJzStatusZb($order_zb['order_id_zb'], 1));
            $path = '/car/new_car_zb';
            $this->dispatchNow(new SendCarToSwock($path, $order_zb));
            // 添加延时任务, 6小时
            $job = (new NoBidCarHandleZb($order_zb['order_id_zb']))->delay(SysConfig::get_time('man_plan_endtime') - time());
//            \Log::info('没有人抢单队列下单'.__LINE__);
            $this->dispatch($job);
//            \Log::info('没有人抢单队列下单'.__LINE__);
            if ($params['is_test'] == 2) {
                w_log('zb_jpush_log_order_id', $order_zb);
                $jpushrs = $this->dispatch(new \App\Jobs\JpushW(['type' => 'car_zb', 'message' => $comments, 'order_zb' => $order_zb]));
                w_log('zb_jpush_log', json_encode($jpushrs), 'big');
            }
            return send_back([], 1, "已经成功下单");
        }
        return send_back([], 0, "下单失败");
    }


    private function check_car_params($params, &$error)
    {
        $validator = Validator::make($params, [
            'publish_sid' => "alpha_num|size:16",
            'jz_order_id' => "required|integer",
            'jz_order_long' => 'required|alpha_num|size:17',
            'source_type' => 'required|integer',
            'service_type' => 'required|integer',
            'car_name' => 'required|max:50',
            //'car_no' => 'required',//邵帅说去掉2017-7-24
            'car_pic_url' => 'required',
            'out_province' => 'required|integer',
            'out_city' => 'required|integer',
            'out_city_name' => 'required',
            'in_province' => 'required|integer',
            'in_city' => 'required|integer',
            'in_city_name' => 'required',
            'get_car_addr' => 'required|string|max:255',
            'to_addr' => 'required|string|max:255',
            'transfer_fee' => 'required|numeric',
            'info_fee' => 'required|numeric',
            'out_contacts' => 'required|max:30',
            'out_telphone' => 'required|digits:11',
            'transfer_time' => 'required|integer',
            'in_contacts' => 'required|max:30',
            'in_telphone' => 'required|digits:11',
            'add_zb_time' => 'required|integer',
        ]);
        if ($validator->fails()) {
            $error = $validator;
            return false; //格式验证未通过
        } else {
            return true;
        }
    }

    //直购下单
    public function add_order_zg(Request $request)
    {
        $params = $request->all();
        //记录订单请求日志
        w_log(__FUNCTION__ . $params['jz_order_id'], $params, 'big');
        //格式校验
        $validator = Validator::make($params, [
            'publish_sid' => "alpha_num|size:16",
            'jz_order_id' => "required|integer",
            'order_id_u2' => "required|integer",
            'jz_order_long' => 'required|alpha_num|size:17',
            'source_type' => 'required|integer',
            'service_type' => 'required|integer',
            'car_name' => 'required|max:50',
            'car_pic_url' => 'required',
            'out_province' => 'required|integer',
            'out_city' => 'required|integer',
            'out_city_name' => 'required',
            'in_province' => 'required|integer',
            'in_city' => 'required|integer',
            'in_city_name' => 'required',
            'get_car_addr' => 'required|string|max:255',//交车地址
            'to_addr' => 'required|string|max:255',//接车地址
            'transfer_fee' => 'required|numeric',
            'out_contacts' => 'required|max:30',
            'out_telphone' => 'required|digits:11',
            'transfer_time' => 'required|integer',
            'in_contacts' => 'required|max:30',
            'in_telphone' => 'required|digits:11',
            'company_account' => 'required',//物流公司账户电话
            'task_get_time' => 'required',//任务分配时间
            'plan_get_car_time' => 'required',//预计提车时间
            'plan_to_time' => 'required',//预计运抵时间
            'trans_per' => 'required',//物流人员
            'trans_per_phone' => 'required',//物流人员电话
        ]);
        if ($validator->fails()) {     //格式验证未通过
            return send_back($validator->errors(), 0, $validator->errors()->first());
        }
//      检查车辆唯一
        $old_car = Car::select("id")->Where('jz_order_id', $params['jz_order_id'])->wherein('car_status', [0, 1, 2])->first();
        if (sizeof($old_car) > 0) {
            return send_back([], 0, "不可重复下单");
        }
        //订单类型为直购,默认为抢单
        $params['order_type'] = 1;
        $params['car_status'] = 1;
        $params['bid_status'] = 3;
        $user_id = User::select('id')->where('telephone', $params['company_account'])->first();
        if (sizeof($user_id) == 0) {
            f_log(__FUNCTION__ . $params['jz_order_id'], '不存在大车司机!');
            return send_back([], 0, "不存在大车司机!");
        }
        $params['user_id'] = $user_id->id;
//        存入数据库
        try {
            #图片URL处理
            if (isset($params['shop_pic_url']) && !empty($params['shop_pic_url']) && strpos($params['shop_pic_url'], 'http://') !== false) {
                $params['shop_pic_url'] = str_replace('http://', 'https://', $params['shop_pic_url']);
            }
            if (isset($params['car_pic_url']) && !empty($params['car_pic_url']) && strpos($params['car_pic_url'], 'http://') !== false) {
                $params['car_pic_url'] = str_replace('http://', 'https://', $params['car_pic_url']);
            }

            DB::beginTransaction();
            $params['end_time'] = time();
            $params['publish_time'] = time();
            $params['create_time'] = time();
            f_log(__CLASS__ . '::' . __FUNCTION__ . '::Carparams03:', $params);
            $car = Car::create(get_table_data(new Car(), $params));
            $params['car_id'] = $car->id;
            $order = Order::create(get_table_data(new Order(), $params));
            $params['order_id'] = $order->id;
            f_log(__CLASS__ . '::' . __FUNCTION__ . '::OrderCarparams:', $params);
            OrderCar::create(get_table_data(new OrderCar(), $params));
            $car->order_id = $order->id;
            $car->save();
            $this->dispatchNow(new UpdateJzStatus($params['jz_order_id'], 1));
            // 添加延时任务, 6小时
            w_log(__FUNCTION__ . $params['jz_order_id'], '下单成功!');
            DB::commit();
            return send_back([], 1, "已经成功下单");
        } catch (Exception $e) {
            w_log(__FUNCTION__ . $params['jz_order_id'], '下单失败');
            return send_back([], 0, "下单失败");
        }
    }


    /**
     * 待抢车列表
     * @param Request $request
     * @return array
     * @author shaogaojie@xin.com
     * @date 2016-05-11 14:21:27
     */
    public function wait_bid_order(Request $request)
    {
        $skip = $request->has('skip') ? $request->skip : 0;
        $take = $request->has('take') ? $request->take : 20;

        f_log('wait_bid_order', $request->all());
        $order_cars = OrderCar::select("order_id", "car_id", "in_city_name", "order_id_zb", "out_city_name", "car_name", "car_no", "car_pic_url", "create_time", "info_fee", "transfer_fee", "bid_status", "start_time")
            ->whereIn('bid_status', [1, 2])
            ->where('order_id_zb', '');
        $this->order_car_query_build($order_cars, $request);

        $order_cars = $order_cars->skip($skip)
            ->take($take)
            ->get();

        if (sizeof($order_cars) > 0) {
            foreach ($order_cars as $key => $item) {
                $item->create_time = date("Y-m-d H:i:s", $item->create_time);
                $item->all_fee = $item->info_fee + $item->transfer_fee;
                $item->car_pic_url = change_pic_url_size($item->car_pic_url, 'middle');
                if ($item->status == 2) {
                    $left_time = time() + config('wuliu.biding_time') - $item->start_time;
                    $left_time > 0 && $item->left_time = $left_time;
                }
                $item->had_bid = OrderCar::had_bid($item->order_id) ? 1 : 0;
            }
        }
        $renz_status = $this->check_renz();
        $this->assign_data(['renz_status' => $renz_status['auth_status']]);
        $this->assign_data(['person_renz_status' => $renz_status['person_renz_status']]);
        $this->assign_data(['comp_renz_status' => $renz_status['comp_renz_status']]);

        if ($renz_status['auth_status'] == 1) {
            $user_info = session('user_info');
            if ($user_info['auth_notice'] == 0) {
                $user = User::find($user_info['id']);
                $user->auth_notice = 1;
                $user->save();
                $this->assign_data(['renz_notice' => 1]);
                session(['user_info' => $user->toArray()]);
            } else {
                $this->assign_data(['renz_notice' => 0]);
            }
        }
        $this->assign_data(['num' => $this->get_num_task()]);
        return $this->send_back($order_cars);
    }

    /**
     * 待抢车列表
     * @param Request $request
     * @return array
     * @author wuhaoliang
     * @date 2016-05-11 14:21:27
     */
    public function wait_bid_order_zb(Request $request)
    {
        $skip = $request->has('skip') ? $request->skip : 0;
        $take = $request->has('take') ? $request->take : 20;

        $order_cars = OrderCar::join('order_zb as oz', 'order_car.order_id_zb', '=', 'oz.order_id_zb')
            ->select("oz.order_id_zb", "oz.create_time_zb", "oz.to_province", "oz.in_city_name", "oz.out_city_name", "oz.fee_per_car", "oz.fee_all_car", "oz.car_num", "oz.bid_status", "oz.info_fee")
            ->whereIn('oz.bid_status', [1, 2]);
        $this->order_car_query_build_zb($order_cars, $request);

        $order_cars = $order_cars->skip($skip)
            ->take($take)
            ->get();

        if (sizeof($order_cars) > 0) {
            foreach ($order_cars as $key => $item) {
                $real_car_num = OrderZb::get_car_num($item->order_id_zb);
                $item->fee_all_car = $real_car_num >= $item->car_num ? $item->fee_all_car * $real_car_num : $item->fee_per_car * $real_car_num;
                $item->fee_all_car = $item->car_num == 0 ? $item->fee_per_car * $real_car_num : $item->fee_all_car;
                $item->info_fee = $item->info_fee * $real_car_num;
                $item->create_time = date("Y-m-d H:i:s", $item->create_time_zb);
                $item->had_bid = OrderCar::had_bid($item->order_id_zb) ? 1 : 0;
                $item->car_num = $real_car_num;
            }
        }
        $renz_status = $this->check_renz();
        $this->assign_data(['renz_status' => $renz_status['auth_status']]);
        $this->assign_data(['person_renz_status' => $renz_status['person_renz_status']]);
        $this->assign_data(['comp_renz_status' => $renz_status['comp_renz_status']]);

        if ($renz_status['auth_status'] == 1) {
            $user_info = session('user_info');
            if ($user_info['auth_notice'] == 0) {
                $user = User::find($user_info['id']);
                $user->auth_notice = 1;
                $user->save();
                $this->assign_data(['renz_notice' => 1]);
                session(['user_info' => $user->toArray()]);
            } else {
                $this->assign_data(['renz_notice' => 0]);
            }
        }
        $this->assign_data(['num' => $this->get_num_task()]);
        return $this->send_back($order_cars);
    }

    /**
     * 检查认证情况
     */
    private function check_renz()
    {
        $user_info = User::get_user_info();
        $person_renz_log = PersonRenzLog::select('auth_status')->where('user_id', $user_info['id'])->orderBy('id', 'desc')->first();
        $comp_renz_log = CompanyRenzLog::select('auth_status')->where('user_id', $user_info['id'])->orderBy('id', 'desc')->first();
        $return['person_renz_status'] = $person_renz_log['auth_status'] ? $person_renz_log['auth_status'] : 0;
        $return['comp_renz_status'] = $comp_renz_log['auth_status'] ? $comp_renz_log['auth_status'] : 0;

        $ren_z = User::select('com_is_authed', 'is_authed', 'applynums')->where('id', $user_info['id'])->first();
//        \Log::info(json_encode($ren_z));
        if ($ren_z) {
            $ren_z = $ren_z->toarray();
        } else {
            $return['auth_status'] = 0;
        }
        if (1 == $ren_z['com_is_authed'] || 1 == $ren_z['is_authed']) {
            $return['auth_status'] = 1;
        } else {
            if (0 == $ren_z['com_is_authed'] && 0 == $ren_z['is_authed'] && $ren_z['applynums'] > 0) {
                $return['auth_status'] = 3;
            } else {
                $return['auth_status'] = max($ren_z['com_is_authed'], $ren_z['is_authed']);
            }
        }
        return $return;
    }

    /**
     * 车辆列表查询条件构造.
     * 不包含 select, 分页, bid_status条件, get结果
     * @param $order_cars
     * @param $request
     * @author shaogaojie@xin.com
     * @date 2016-05-27 18:52:17
     */
    public function order_car_query_build(&$order_cars, $request)
    {
        $order_by = $request->has('order_by') ? $request->order_by : 'desc';
        $order_cars = $order_cars->where('order_status', '1')->where('validity', 1);

        //车牌, 来源订单号等like查询
        $where_like = [];
        $request->has('car_no') && $request->car_no != "" && $where_like['car_no'] = $request->car_no;
        $request->has('publish_sid') && $request->publish_sid != "" && $where_like['publish_sid'] = $request->publish_sid;
        $request->has('jz_order_long') && $request->jz_order_long != "" && $where_like['jz_order_long'] = $request->jz_order_long;
        $request->has('order_id_zb') && $request->order_id_zb != "" && $where_like['order_car.order_id_zb'] = $request->order_id_zb;
        if ($where_like) {
            foreach ($where_like as $key => $val) {
                $order_cars->where($key, 'like', '%' . $val . '%');
            }
        }

        $user_info = session('user_info');
        $user_info['is_test'] = isset($user_info['is_test']) ? intval($user_info['is_test']) : 2;
        if ($user_info['is_test'] == 1) {
            $order_cars->where('is_test', '1');
        } else {
            $order_cars->where('is_test', '<>', '1');
        }

        //如果有高级搜索则不使用简单搜索条件
        if ($request->has('out_city') || $request->has('in_city')) {
            $request->in_place = '';
            $request->out_place = '';
        }

        $where = [];
        $request->has('out_city') && $request->out_city != "" && $where['out_city'] = $request->out_city;
        $request->has('in_city') && $request->in_city != "" && $where['in_city'] = $request->in_city;
        $request->has('brand_id') && $request->brand_id != "" && $where['brand_id'] = $request->brand_id;
        if ($where) {
            $order_cars->where($where);
        }
        //发布时间
        if ($request->has('create_time_start') && $request->create_time_start != '') {
            $order_cars->where('create_time', '>=', strtotime($request->create_time_start));
        }
        if ($request->has('create_time_end') && $request->create_time_end != '') {
            $order_cars->where('create_time', '<', strtotime($request->create_time_end . '+ 1day'));
        }

        //费用区间
        if ($request->has('fee_start') && $request->fee_start != '') {
            $order_cars->where('total_fee', '>=', $request->fee_start);
        }
        if ($request->has('fee_end') && $request->fee_end != '') {
            $order_cars->where('total_fee', '<=', $request->fee_end);
        }

        //迁入地, 迁出地模糊查询
        $where_place = [];
        $request->has('in_place') && $request->in_place != '' && $where_place['in'] = $request->in_place;
        $request->has('out_place') && $request->out_place != '' && $where_place['out'] = $request->out_place;
        if ($where_place) {
            foreach ($where_place as $key => $val) {
                $order_cars->where(function ($query) use ($key, $val) {
                    $query->where($key . '_city_name', 'like', '%' . $val . '%');
                    $query->orWhere($key . '_province_name', 'like', '%' . $val . '%');
                });
            }
        }

        $order_cars = $order_cars
            ->orderBy('id', $order_by);
    }

    /**
     * 车辆列表查询条件构造.
     * 不包含 select, 分页, bid_status条件, get结果
     * @param $order_cars
     * @param $request
     * @author shaogaojie@xin.com
     * @date 2016-05-27 18:52:17
     */
    public function order_car_query_build_zb(&$order_cars, $request)
    {
        f_log(__FUNCTION__ . 'line=' . __LINE__, json_encode($request->all()));
        $order_by = $request->has('order_by') ? $request->order_by : 'desc';
        $order_cars = $order_cars->where('order_car.order_status', '1')->where('order_car.validity', 1);

        $user_info = session('user_info');
        if ($user_info['is_test'] == 1) {
            $order_cars->where('order_car.is_test', '1');
        } else {
            $order_cars->where('order_car.is_test', '<>', '1');
        }

        //车牌, 来源订单号等like查询
        $where_like = [];
        $request->has('car_no') && $request->car_no != "" && $where_like['order_car.car_no'] = $request->car_no;
        $request->has('publish_sid') && $request->publish_sid != "" && $where_like['order_car.publish_sid'] = $request->publish_sid;
        $request->has('jz_order_long') && $request->jz_order_long != "" && $where_like['order_car.jz_order_long'] = $request->jz_order_long;
        $request->has('order_id_zb') && $request->order_id_zb != "" && $where_like['order_car.order_id_zb'] = $request->order_id_zb;

        if ($where_like) {
            foreach ($where_like as $key => $val) {
                $order_cars->where($key, 'like', '%' . $val . '%');
            }
        }

        //如果有高级搜索则不使用简单搜索条件
        if ($request->has('out_city') || $request->has('in_city')) {
            $request->in_place = '';
            $request->out_place = '';
        }

        $where = [];
        $request->has('out_city') && $request->out_city != "" && $where['order_car.out_city'] = $request->out_city;
        $request->has('in_city') && $request->in_city != "" && $where['order_car.in_city'] = $request->in_city;
        $request->has('brand_id') && $request->brand_id != "" && $where['order_car.brand_id'] = $request->brand_id;
        if ($where) {
            $order_cars->where($where);
        }
        //发布时间
        if ($request->has('create_time_start') && $request->create_time_start != '') {
            $order_cars->where('order_car.create_time', '>=', strtotime($request->create_time_start));
        }
        if ($request->has('create_time_end') && $request->create_time_end != '') {
            $order_cars->where('order_car.create_time', '<', strtotime($request->create_time_end . '+ 1day'));
        }

        //费用区间
        if ($request->has('fee_start') && $request->fee_start != '') {
            $order_cars->where('order_car.total_fee', '>=', $request->fee_start);
        }
        if ($request->has('fee_end') && $request->fee_end != '') {
            $order_cars->where('order_car.total_fee', '<=', $request->fee_end);
        }

        //迁入地, 迁出地模糊查询
        $where_place = [];
        $request->has('in_place') && $request->in_place != '' && $where_place['in'] = $request->in_place;
        $request->has('out_place') && $request->out_place != '' && $where_place['out'] = $request->out_place;
        if ($where_place) {
            foreach ($where_place as $key => $val) {
                $order_cars->where(function ($query) use ($key, $val) {
                    $query->where('order_car.' . $key . '_city_name', 'like', '%' . $val . '%');
                    $query->orWhere('order_car.' . $key . '_province_name', 'like', '%' . $val . '%');
                });
            }
        }
        $order_cars->groupBy('order_car.order_id_zb');
        $order_cars = $order_cars->orderBy('order_car.id', $order_by);
    }

    /**
     * 待抢单详情页
     * @param $car_id 车辆id
     * @return \Illuminate\Http\JsonResponse
     * @author shaogaojie@xin.com
     * @date 2016-05-17 20:11:42
     */
    public function wait_bid_order_detail($car_id)
    {
        $car = OrderCar::select("user_id", "order_id", "car_id", "start_time", "jz_order_long", "car_pic_url", "shop_pic_url", "publish_sid", "create_time", "out_city_name", "out_province_name", "in_city_name", "in_province_name", "get_car_addr", "to_addr", "info_fee", "transfer_fee", "car_name", "car_no", "bid_status")
            ->where('car_id', $car_id)->first();
        if (sizeof($car) > 0) {
            $car->create_time = date("Y-m-d H:i:s", $car->create_time);
//            $times = SysConfig::get_config('bid_endtime',false)->times;
            $times = config('wuliu.biding_time');
            $car->have_times = (($car->start_time + $times) - time()) >= 0 ? ($car->start_time + $times) - time() : 0;
            $count = \App\BidLog::where('order_id', $car->order_id)->where('user_id', $this->user_info['id'])->count();
            $had_bid = OrderCar::had_bid($car->order_id) ? 1 : 0;
            if ($count > 0 || $had_bid) {
                $car->had_bid = 1;
            } else {
                $car->had_bid = 0;
            }
            $car->all_times = config('wuliu.biding_time');
            if ($car->user_id != $this->user_info['id'] && $car->bid_status == 3) {
                $car->bid_status_view = 4;
            } else {
                $car->bid_status_view = 3;
            }
        } else {
            return send_back([], 0, '订单已运达失败');
        }
        return send_back($car);
    }

    /**
     * 待抢单详情页攒板
     * @param $order_id_zb 攒板id
     * @return \Illuminate\Http\JsonResponse
     * @author wuhaoliang
     * @date 2016-05-17 20:11:42
     */
    public function wait_bid_order_detail_zb($order_id_zb)
    {
        $cars = OrderCar::select("jz_order_long", "car_pic_url", "car_name", "car_no", "user_id")
            ->where('order_id_zb', $order_id_zb)->get();
        $one_car = OrderCar::select("user_id", "order_id", "start_time", "car_id", "publish_sid", "create_time", "out_city_name", "out_province_name", "in_city_name", "in_province_name", "get_car_addr", "to_addr", "info_fee", "transfer_fee", "car_name", "car_no")
            ->where('order_id_zb', $order_id_zb)->first();
        if (sizeof($cars) > 0) {
            $need['order_zb'] = \App\OrderZb::select("order_id_zb", "bid_status", "create_time_zb", "car_num", "fee_per_car", "fee_all_car")
                ->where('order_id_zb', $order_id_zb)->first();
            $need['order_zb'] = array_merge($need['order_zb']->toarray(), $one_car->toarray());
            $count = \App\BidLog::where('order_id_zb', $order_id_zb)->where('user_id', $this->user_info['id'])->count();
            $had_bid = OrderCar::had_bid($order_id_zb) ? 1 : 0;
            if ($count > 0 || $had_bid) {
                $need['order_zb']['had_bid'] = 1;
            } else {
                $need['order_zb']['had_bid'] = 0;
            }
            $need['order_zb']['create_time_zb'] = date("Y-m-d H:i:s", $need['order_zb']['create_time_zb']);
            $times = config('wuliu.biding_time');
            $need['order_zb']['have_times'] = (($need['order_zb']['start_time'] + $times) - time()) >= 0 ? ($need['order_zb']['start_time'] + $times) - time() : 0;
            $need['order_zb']['all_times'] = config('wuliu.biding_time');
            $real_car_num = OrderZb::get_car_num($need['order_zb']['order_id_zb']);
            $need['order_zb']['fee_all_car'] = $real_car_num >= $need['order_zb']['car_num'] ? $need['order_zb']['fee_all_car'] * $real_car_num : $need['order_zb']['fee_per_car'] * $real_car_num;
            $need['order_zb']['fee_all_car'] = $need['order_zb']['car_num'] == 0 ? $need['order_zb']['fee_per_car'] * $real_car_num : $need['order_zb']['fee_all_car'];
            $need['order_zb']['info_fee'] = $need['order_zb']['info_fee'] * $real_car_num;
            $need['order_zb']['car_num'] = $real_car_num;
            foreach ($cars as $key => $one_car) {
                $one_car->create_time = date("Y-m-d H:i:s", $one_car->create_time);
            }
            if ($one_car->user_id != $this->user_info['id'] && $need['order_zb']['bid_status'] == 3) {
                $need['order_zb']['bid_status_view'] = 4; //抢单失败!
            } else {
                $need['order_zb']['bid_status_view'] = 3; //抢单成功!
            }
            $need['cars'] = $cars;
        } else {
            return send_back([], 0, '订单已人工预约分配物流,请刷新批量抢单列表');
        }
        return send_back($need);
    }

    /**
     * 抢单
     * @param \Illuminate\Http\Request $request
     * @return type
     * @author wuhaoliang
     */
    /**
     * 20170929  限制重复提交  设置默认时间为180秒
     */
    public function bid_car(Request $request)
    {
        $redis = \Predis::connection();
        $req_data = $request->all();

        $log_key_prefix = sprintf("log_bid_car_%s_%s",__CLASS__,__FUNCTION__);
        $log_key_pattern = $log_key_prefix.'_%s';
        f_log(sprintf($log_key_pattern,'req_data'),var_export($req_data,true));
        $lock_key = sprintf("lock_bid_car_order_id_%d_user_id_%d",$req_data['order_id'],$this->user_info['id']);

        try{
            //判断是否抢过,如果取不到锁,则上锁
            if (!get_lock($lock_key,180)) {
                throw new Exception('请求过于密集,请耐心等待!');
            }

            //获取用户信息
            $user_info = \App\User::where('id', $this->user_info['id'])->select('id','score', 'com_is_authed', 'is_authed', 'auth_type','forbidden_module')->first()->toarray();
            //获取当前订单信息
            $order_info = OrderCar::get_order_info($req_data['order_id']);
            //对抢单操作进行校验
            $check_status = $this->bid_check($user_info,$order_info,$req_data['order_id'], 1);
            if($check_status['status'] == 0){
                throw new Exception($check_status['msg']);
            }

            //冻结保证金
            $freeze_res = SecuDeposit::freeze_secu_deposit_car($this->user_info['id'],$req_data['order_id'],true);
            if($freeze_res['code'] == 0){
                throw new Exception($freeze_res['msg']);
            }

            Cache::increment('frozen_cars_' . $this->user_info['id']);  //个人中心保证金主页冻结车辆数量
            $redis->setex(config('wuliu.order_had_bid') . $req_data['order_id'], config('wuliu.biding_time') + 120, 1);

            $bidjob = (new BidOrder(['order_id' => $req_data['order_id'], 'user_id' => $this->user_info['id'], 'score' => $user_info['score']]));
            $this->dispatchNow($bidjob);
            $this->dispatch(new UpdateJzStatus($order_info['jz_order_id'], 2));
            \App\BidLog::create(['order_id' => $req_data['order_id'], 'user_id' => $this->user_info['id'],
                'user_name' => $this->user_info['realname'],
                'user_phone' => $this->user_info['telephone'],
                'user_score' => $user_info['score'],
                'bid_time' => time()]);
            #更新用户单车参与竞抢量
            $this->dispatch(new UpdateUserScore(['fun' => 'update_bid_num', 'user_id' => $this->user_info['id']]));
            release_lock($lock_key);
            return send_back([], 1, "提交成功!");
        }catch(Exception $e){
            f_log("bid_car_exception_order_id_{$req_data['order_id']}","user_id:{$this->user_info['id']}".$e->getMessage().var_export($req_data,true));
            w_log("bid_car_exception_order_id_{$req_data['order_id']}","user_id:{$this->user_info['id']}".$e->getMessage().var_export($req_data,true));
            isset($lock_key) && release_lock($lock_key);
            return $this->send_back([], 0, $e->getMessage());
        }

    }

    /**
     * 批量抢单
     * @param \Illuminate\Http\Request $request
     * @return type
     * @author wuhaoliang
     */
    /**
     * 20170929  限制重复提交  设置默认时间为180秒
     */
    public function bid_car_zb(Request $request)
    {
        $redis = \Predis::connection();
        $req_data = $request->all();
        $order_id_zb = isset($req_data['order_id_zb']) ? intval($req_data['order_id_zb']) : 0;
        $log_key_prefix = sprintf("log_bid_car_zb_%s",__FUNCTION__);
        $log_key_pattern = $log_key_prefix.'_%s';
        f_log(sprintf($log_key_pattern,'req_data'),$req_data);
        $lock_key = sprintf("lock_zb_bid_car_order_id_%d_user_id_%d",$order_id_zb,$this->user_info['id']);
        try {
            if (!get_lock($lock_key, 180)) {
                throw new Exception('请求过于密集,请耐心等待!');
            }

            //获取用户信息
            $user_info = \App\User::where('id', $this->user_info['id'])->select('id','score', 'com_is_authed', 'is_authed', 'auth_type','forbidden_module')->first()->toarray();
            //获取当前订单信息
            $order_info_zb = OrderZb::get_order_info_zb($order_id_zb);
            //对抢单操作进行校验
            $check_status = $this->bid_check($user_info,$order_info_zb,$order_id_zb, 2);
            if($check_status['status'] == 0){
                throw new Exception($check_status['msg']);
            }
            $zb_order_lists = DB::table('order_car')->where('order_id_zb', $order_id_zb)->where('order_status', '<>', 3)->lists('order_id');

            $cold_res = SecuDeposit::cold_money_zb($this->user_info['id'],$zb_order_lists,true);
            if($cold_res['code'] == 0){
                throw new Exception($cold_res['msg']);
            }

            Cache::increment('frozen_cars_' . $this->user_info['id'], $order_info_zb['car_num']);  //个人中心保证金主页冻结车辆数
            $redis->setex(config('wuliu.order_had_bid') . $order_id_zb, config('wuliu.biding_time') + 120, 1);

            #开启抢单流程
            $bidjob = (new \App\Jobs\BidOrderZb(['order_id_zb' => $order_id_zb, 'user_id' => $this->user_info['id'], 'score' => $user_info['score']]));
            $this->dispatchNow($bidjob);
            #更改矩阵订单状态为竞抢中
            $this->dispatch(new UpdateJzStatusZb($order_info_zb['order_id_zb'], 2));
            #记录抢单日志
            \App\BidLog::create(['order_id_zb' => $order_id_zb, 'user_id' => $this->user_info['id'],
                'user_name' => $this->user_info['realname'],
                'user_phone' => $this->user_info['telephone'],
                'user_score' => $user_info['score'],
                'bid_time' => time()]);
            DB::commit();
            #更新用户积分
            $this->dispatch(new UpdateUserScore(['fun' => 'update_batch_bid_num', 'user_id' => $this->user_info['id']]));

            release_lock($lock_key);

            return send_back([], 1, "提交成功!");
        }catch(Exception $e){
            f_log(sprintf($log_key_pattern,'exception_msg'),"user_id:{$this->user_info['id']}".$e->getMessage().var_export($req_data,true));
            w_log(sprintf($log_key_pattern,'exception_msg'),"user_id:{$this->user_info['id']}".$e->getMessage().var_export($req_data,true));
            isset($lock_key) && release_lock($lock_key);

            return $this->send_back([], 0, $e->getMessage());
        }
    }
    
    /*
     * 抢单检测
     * @params $user_info用户信息 $order_info订单id $order_id 订单号/攒板号 $type 订单类型(1单车 2攒板)
     * @return array
     */
    public function bid_check($user_info, $order_info, $order_id, $type){
        $redis = \Predis::connection();
        $return = ['status'=>1, 'msg'=>'验证通过'];
        #参数校验
        if($order_id <= 0 ){
            return ['status'=>0, 'msg'=>'订单ID无效!'];
        }

        $user_id = $user_info['id'];
        if($user_id <= 0){
            return ['status'=>0, 'msg'=>'用户未登陆'];
        }

        $user_check_res = \App\User::check_qiang_authed($user_id,$user_info);
        if(empty($user_check_res['code'])){
            return ['status'=>0, 'msg'=>$user_check_res['msg']];
        }
        if($type == 1){  //单车
            //订单已竞抢结束
            $has_stop = $redis->get('bid_has_stop_' . $order_id);
            $show_str = '单车抢单';
        }else{  //攒板车
            //订单已竞抢结束
            $has_stop = $redis->get('bid_zb_has_stop_' . $order_id);
            $show_str = '批量抢单';
        }
        if (empty($order_info)) {
            return ['status'=>0, 'msg'=>'订单已人工预约分配物流,请刷新'.$show_str.'列表'];
        }
        if ($has_stop) {
            return ['status'=>0, 'msg'=>'竞抢已结束,不可抢'];
        }
        if ($order_info['bid_status'] == 3) {
            return ['status'=>0, 'msg'=>'订单已被抢,请刷新'.$show_str.'列表'];
        }
        if ($order_info['bid_status'] == 4) {
            return ['status'=>0, 'msg'=>'订单已人工预约分配物流,请刷新'.$show_str.'列表'];
        }
        if ($order_info['bid_status'] > 2) {
            return ['status'=>0, 'msg'=>'订单已经分配,不可抢'];
        }
        $had_bid = OrderCar::had_bid($order_id);
        if ($had_bid) {
            return ['status'=>0, 'msg'=>'您已经抢过这辆车,请您耐心等待'];
        }
        if($type == 2){  //攒板车
            $zb_order_lists = DB::table('order_car')->where('order_id_zb', $order_id)->where('order_status', '<>', 3)->lists('order_id');
        }        
        //对个人级进行检验
        if ($user_info['auth_type'] == 0) {
            $in_city = $type == 1 ? $order_info['in_city'] : $order_info['to_city'];
            $out_city = $type == 1 ? $order_info['out_city'] : $order_info['from_city'];
            $user_id = $this->user_info['id'];
            //其他线路未完成订单数
            $other_line_order_nums = DB::table('order_car')
                ->where('user_id', $user_id)
                ->whereIn('car_status', [1, 2])
                ->where('order_status', 1)
                ->where('validity', 1)
                ->where(function ($query) use ($in_city, $out_city) {
                    $query->where('in_city', '!=', $in_city)->orWhere('out_city', '!=', $out_city);
                })
                ->count();
            w_log('other_line_order_nums',DB::getQueryLog());
            if ($other_line_order_nums > 0) {
                return ['status'=>0, 'msg'=>'尚有未完成的订单,不能抢其他线路的订单'];
            }
            //获取当前竞抢中的订单
            $order_lists = DB::table('order_car')->where('bid_status', 2)->lists('order_id');
            $biding_nums = 0;
            foreach ($order_lists as $one_order_id) {
                $i = OrderCar::had_bid($one_order_id) ? 1 : 0;
                if ($i == 1 && $biding_nums == 0) {  //参与了竞抢
                    $biding_order_info = OrderCar::get_order_info($one_order_id);
                }
                $biding_nums = $biding_nums + $i;  //竞抢中的数量
            }
            if (!empty($biding_order_info) && ($biding_order_info['out_city'] != $out_city || $biding_order_info['in_city'] != $in_city)) {
                return ['status'=>0, 'msg'=>'尚有未完成的订单,不能抢其他线路的订单'];
            }
            \Log::info(__CLASS__ . ',line=' . __LINE__ . ',nums=' . $biding_nums);
            //同一线路其他待运达车辆
            $one_line_other_order_nums = OrderCar::where('user_id', $this->user_info['id'])
                ->whereIn('car_status', [1, 2])
                ->where('out_city', $out_city)
                ->where('in_city', $in_city)
                ->count();
            \Log::info(__CLASS__ . ',line=' . __LINE__ . ',user_id=' . $this->user_info['id'] . ',nums=' . ($one_line_other_order_nums + $biding_nums));
            if (($one_line_other_order_nums + $biding_nums >= 14) || ($type == 2 && ($one_line_other_order_nums + $biding_nums + count($zb_order_lists) > 14))) {
                if ($biding_nums != 0) {  //未完成订单超过14台
                    return ['status'=>0, 'msg'=>'您是个人级用户,您未完成订单达到限制14单,请尽快完成订单后再进行抢单'];
                } else {
                    return ['status'=>0, 'msg'=>'个人级资质用户未完成运达的车辆(包含竟抢中的)以达到14台,请尽快完成订单后再进行抢单'];
                }
            }
        }
        return $return;
    }

    //待提车列表
    public function wait_get_car_list(Request $request)
    {
        $skip = $request->has('skip') ? $request->skip : 0;
        $take = $request->has('take') ? $request->take : 20;
        $order_by = $request->has('order_by') ? $request->order_by : 'desc';
        $order_cars = DB::table('order_car')
            ->select('order_id', 'order_id_zb', 'car_pic_url', 'in_city_name', 'out_city_name', 'car_name', 'the_last_get_time', 'add_score_get_time', 'out_telphone', 'order_type', 'task_get_time', 'plan_get_car_time')
            ->where('user_id', $this->user_info['id'])
            ->where('car_status', 1)
            ->orderby('end_time', $order_by);
        $this->order_car_query_build($order_cars, $request);
        $order_cars = $order_cars->skip($skip)
            ->take($take)
            ->get();
        foreach ($order_cars as $key => $val) {
            $order_cars[$key]->the_last_get_time = todate($val->the_last_get_time);
            $order_cars[$key]->add_score_get_time = todate($val->add_score_get_time);
            $order_cars[$key]->task_get_time = todate($val->task_get_time);
            $order_cars[$key]->plan_get_car_time = todate($val->plan_get_car_time);
        }
        $this->assign_data(['num' => $this->get_num_task()]);
        return $this->send_back($order_cars);
    }

    /**
     * 生成车的二维码
     * @author wuhaoliang
     */
    public function make_code(Request $request)
    {
        //检查车的中标人是不是自己
        $size = $request->size ? $request->size : 300;
        $order_id = $request->order_id ? $request->order_id : -1;
        $car_num = OrderCar::where('order_id', '=', $order_id)
            ->where('user_id', '=', $this->user_info['id'])
            ->count();
        if ($car_num == 0) {
            return send_back('', 0, '车辆已运达失败,请刷新待提车列表');
        }
        $p = generate_qrcode(SimEncrypt::encrypt('car:' . $request->order_id), $size);
        return send_back(base64_encode($p));
    }

    public function wait_get_car_route($order_id)
    {
//        $order_info = OrderCar::get_order_info($order_id);
        $order_info = OrderCar::where('order_id', $order_id)->first();
        if (empty($order_info)) {
            return $this->send_back([], 'redirect_car_info', '重定向到工作汇总');
        }
        if ($order_info['car_status'] == 2) {
            return $this->send_back([], 'redirect_wait_car_to', '重定向到待运达接口');
        }
        if ($order_info['car_status'] > 2) {
            return $this->send_back([], 'redirect_car_info', '重定向到工作汇总');
        }
        return $this->send_back([], 1, '无重定向');
    }

    /**
     * 待提车详情页
     * @param \Illuminate\Http\Request $request
     * @return type
     * @author wuhaoliang
     */
    public function wait_get_car_detail($order_id)
    {
        if (!$order_id) {
            return $this->send_back('', 0, '缺少订单号!');
        }
        $size = 600;
        $p = generate_qrcode(SimEncrypt::encrypt('car:' . $order_id), $size);
        $data = OrderCar::select("buyer_name", "buyer_mobile", "in_contacts", "in_telphone", "jz_order_long", "order_id_zb", "order_id", "car_id", "out_contacts", "out_telphone", "car_pic_url", "add_score_get_time", "the_last_get_time", "shop_pic_url", "publish_sid", "create_time", "out_city_name", "out_province_name", "in_city_name", "in_province_name", "get_car_addr", "to_addr", "info_fee", "transfer_fee", "transfer_fee_zg", "car_name", "car_no", "bid_status", "order_type", "task_get_time", "plan_get_car_time")
            ->where('order_id', $order_id)
            ->where('user_id', '=', $this->user_info['id'])
            ->first();
        if (sizeof($data) > 0) {
            $data->create_time = date("Y-m-d H:i:s", $data->create_time);
            $data->add_score_get_time = date("Y-m-d H:i:s", $data->add_score_get_time);
            $data->the_last_get_time = date("Y-m-d H:i:s", $data->the_last_get_time);
            $data->task_get_time = date("Y-m-d H:i:s", $data->task_get_time);
            $data->plan_get_car_time = date("Y-m-d H:i:s", $data->plan_get_car_time);
            $data->car_no = $data->car_no ? $data->car_no : '--';
            $data->code = base64_encode($p);
        } else {
            return send_back('', 0, '订单已运达失败,请刷新待提车列表');
        }
        return $this->send_back($data);
    }

    /**
     * 提车出库
     * @author wuhaoliang
     */
    public function get_car_act(Request $request, $order_id)
    {
        return send_back([], 0, '没有此功能!');
        $pics = $request->get('pics');
        w_log(__FUNCTION__, $pics);
        $order_car = OrderCar::get_order_info($order_id);
        if (empty($order_car)) {
            return send_back([], 0, '订单已运达失败,请刷新待提车列表!');
        }
        if ($order_car['jz_confirm_out'] != 1) {
            return $this->send_back([], 0, '请让矩阵工作人员扫码确认!');
        }
        if ($order_car['car_status'] == 2) {  //车辆状态已经提车
            return send_back([], 0, '订单已提车完成,请勿重复操作!');
        }
        if ($order_car['car_status'] == 3) {  //车辆状态已经运达完成
            return send_back([], 0, '订单已运达成功,请刷新待提车列表!');
        }
        if ($order_car['car_status'] == 4) {  //车辆状态已经运达失败
            return send_back([], 0, '订单已运达失败,请刷新待提车列表!');
        }
        DB::beginTransaction();
        //判断提车时间是否满足加分
        $time = time();
        $score_details = new ScoreDetails();
        if($time <= $order_car['add_score_get_time']){  //当前时间满足加分提车,加积分
            $score_details->score_operation(3, SysConfig::get_config('on_time_get_car'), $order_car['user_id'], $order_id);
        }
        if($time <= $order_car['the_last_get_time']){  //当前时间满足超时提车,减积分
            $score_details->score_operation(4, SysConfig::get_config('not_on_time_get_car'), $order_car['user_id'], $order_id);
        }
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_id), ['car_status' => 2], $order_id);
        $car_s = Car::where('order_id', $order_id)->update(['car_status' => 2]);
        //更新提车时间
        if ($order_car_s && $car_s) {
            OrderCar::send_to_jz(array_merge($order_car, $this->user_info), 4);
            DB::commit();
            return send_back([], 1, '提车成功!');
        } else {
            DB::rollback();
            return send_back([], 0);
        }
    }

    /**
     * 直购订单接车完成
     * @author wuhaoliang
     */
    public function get_car_finish(Request $request, $order_id)
    {
        //判断提车时间是否满足加分
        $order_car = OrderCar::get_order_info($order_id);
        if (empty($order_car)) {
            return send_back([], 0, '订单已运达失败,请刷新待提车列表!');
        }
        if ($order_car['order_type'] != 1) {
            return send_back([], 0, '不是直购订单!');
        }
        if ($order_car['car_status'] == 2) {  //车辆状态已经提车
            return send_back([], 0, '订单已提车完成,请勿重复操作!');
        }
        if ($order_car['car_status'] == 3) {  //车辆状态已经运达完成
            return send_back([], 0, '订单已运达成功,请刷新待提车列表!');
        }
        if ($order_car['car_status'] == 4) {  //车辆状态已经运达失败
            return send_back([], 0, '订单已运达失败,请刷新待提车列表!');
        }
        DB::beginTransaction();
        $time = time();
        $trans_need_time = strtotime("+" . $order_car['transfer_time'] . "day", $time);
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_id), ['car_status' => 2, 'get_car_time' => $time], $order_id);
        $car_s = Car::where('order_id', $order_id)->update(['car_status' => 2, 'get_car_time' => $time]);
        f_log(__FUNCTION__ . 'order_id=' . $order_id, 'order_car_s=' . json_encode($order_car_s) . 'car_s=' . $car_s);
        //更新提车时间
        if ($order_car_s && $car_s) {
            OrderCar::send_to_jz(array_merge($order_car, $this->user_info, array('get_car_time' => $time, 'plan_to_time' => $order_car['plan_to_time'])), 11);
            DB::commit();
            return send_back([], 1, '提车成功!');
        } else {
            DB::rollback();
            return send_back([], 0, '提车失败!');
        }
    }

    /**
     * 取消订单:扣除保证金、扣信誉积分、解冻保证金、订单转为矩阵的人工预约分配流程、取消的订单可在工作汇总模块的失败订单中查看
     * @author wuhaoliang
     */
    public function fail_car_act($order_id)
    {
        $order_car_info = \App\OrderCar::get_order_info($order_id);
        if (empty($order_car_info)) {
            return send_back([], 0, '订单已运达失败,请刷新待提车列表!');
        }
        if ($order_car_info['bid_status'] == 4) {
            if ($order_car_info['fail_type'] == 7) {
                return send_back([], 0, '不可以重复取消订单!');
            } else {
                return send_back([], 0, '订单已运达失败,请刷新待提车列表');
            }
        }
        if ($order_car_info['user_id'] != $this->user_info['id']) {
            return send_back([], 0, '不可以取消别人的车!');
        }
        if ($order_car_info['car_status'] == 2) {
            return $this->send_back([], 0, '订单已出库,不可取消订单!');
        }
        //更新订单状态
        DB::beginTransaction();
//        $car_s = \App\Car::where('order_id',$order_id)->update(['car_out_store_time' => time()]);
        $order_s = Order::where('id', $order_id)->update(['order_status' => 3,
            'fail_type' => 7, 'bid_status' => 4, 'order_fail_time' => time()]);
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_id), ['bid_status' => 4, 'order_status' => 3, 'fail_type' => 7, 'car_status' => 4, 'order_fail_time' => time()]);
        $car_s = Car::where('order_id', $order_id)->update(['car_status' => 4]);
        //扣除保证金
        $red_s = SecuDeposit::reduce_code_money(12, $order_car_info['info_fee'], $order_id);
        //解冻保证金
        $hot_s = SecuDeposit::hot_money(7, config('wuliu.baozj_num') - $order_car_info['info_fee'], $order_id); //12表示手动取消订单
        //扣信誉积分
        $score_details = new ScoreDetails(); 
        $score_red_s = $score_details->score_operation(2, SysConfig::get_config('cancel_order'), $this->user_info['id'], $order_id); //取消订单
        //订单转为矩阵的人工预约分配流程

        $data['order_id_jz'] = $order_car_info['jz_order_id'];
        $data['info_fee'] = $order_car_info['info_fee'];
        $data['content_type'] = 1;
        $data['sn'] = create_sn($data, config('sn.juzhen'));
        $request = array(
            'url' => env('JZ_DOMAIN') . '/api/qd_get/activation_plan_trans',
            'params' => $data
        );
        $return_s = HttpClient::post($request)->content();
        $return_s = json_decode($return_s);

        f_log('fail_car_act_'.$order_id,var_export(compact('order_id','order_s','order_car_s','hot_s','red_s','score_red_s','car_s','request'),true));

        if ($order_s && $order_car_s && $hot_s && $red_s && $score_red_s && $car_s && $return_s->status) {
            $this->dispatch(new UpdateUserScore(['fun' => 'update_fail_order', 'user_id' => $order_car_info['user_id']]));
            $this->dispatch(new UpdateUserScore(['fun' => 'cancel_order_lv', 'user_id' => $order_car_info['user_id']]));
            $this->dispatchNow(new UpdateJzStatus($order_car_info['jz_order_id'], 5));
            if ($order_car_info['order_id_zb']) {
                $oz = new OrderZb();
                $oz->change_transport_fee($order_car_info['order_id_zb']);
            }
            DB::commit();
            return $this->send_back([], 1, '取消成功,信誉积分' . SysConfig::get_config('cancel_order') . '分,保证金扣款' . $order_car_info['info_fee'] . '元');
        } else {
            DB::rollback();
            if ($return_s->status == 0) {
                return send_back([], 0, $return_s->message);
            }
            return send_back([], 0, '取消提车失败!');
        }
    }

    /**
     * 直购取消订单
     * @author wuhaoliang
     */
    public function fail_car_zhigou_act($order_id_jz)
    {
        $orderinfo = OrderCar::where('jz_order_id', $order_id_jz)->first();
        if (empty($orderinfo)) {
            return send_back([], 0, '没有这辆车!');
        }
        if ($orderinfo['bid_status'] == 4) {
            return send_back([], 0, '不可以重复取消订单!');
        }
        $order_id = $orderinfo->order_id;
        //更新订单状态
        DB::beginTransaction();
        $order_s = Order::where('id', $order_id)->update(['order_status' => 3,
            'fail_type' => 7, 'bid_status' => 4, 'order_fail_time' => time()]);
        OrderCar::stop_order($order_id);
        $car_s = Car::where('order_id', $order_id)->update(['car_status' => 4]);
        //订单转为矩阵的人工预约分配流程

        if ($order_s && $car_s) {
            DB::commit();
            return $this->send_back([], 1, '取消成功');
        } else {
            DB::rollback();
            return send_back([], 0, '取消提车失败!');
        }
    }

    public function fail_car_warning($order_id)
    {
        $order_car_info = \App\OrderCar::get_order_info($order_id);
        if (empty($order_car_info)) {
            return send_back([], 0, '没有这辆车!');
        }
        return $this->send_back([], 1, '取消成功,信誉积分' . SysConfig::get_config('cancel_order') . '分,保证金扣款' . number_format($order_car_info['info_fee'], 2, '.', '') . '元');
    }

    /**
     *  待送达列表
     * @param \Illuminate\Http\Request $request
     * @return type json
     * @author wuhaoliang
     */
    public function wait_car_to_list(Request $request)
    {
        $skip = $request->has('skip') ? $request->skip : 0;
        $take = $request->has('take') ? $request->take : 20;
        $order_cars = DB::table('order_car')
            ->select('car_no', 'order_id', 'order_id_zb', 'site_times', 'car_pic_url', 'in_city_name', 'out_city_name', 'car_name', 'get_car_time', 'add_score_to_time', "order_type", "plan_to_time")
            ->where('user_id', $this->user_info['id'])
            ->where('car_status', 2);
        $order_by = $request->has('order_by') ? $request->order_by : 'desc';
        $order_cars->orderBy('get_car_time', $order_by);

        $this->order_car_query_build($order_cars, $request);

        $order_cars = $order_cars->skip($skip)
            ->take($take)
            ->get();
        foreach ($order_cars as $key => $val) {
            $order_cars[$key]->get_car_time = todate($val->get_car_time);
            $order_cars[$key]->add_score_to_time = todate($val->add_score_to_time);
            $order_cars[$key]->plan_to_time = todate($val->plan_to_time);
            $order_cars[$key]->times = SysConfig::get_config('send_site_times') - $val->site_times;
        }
        $this->assign_data(['num' => $this->get_num_task()]);
        return $this->send_back($order_cars);
    }

    public function wait_car_to_route($order_id)
    {
        $order_info = OrderCar::get_order_info($order_id);

        if ($order_info['car_status'] > 2) {
            return $this->send_back([], 'redirect_car_info', '重定向到工作汇总');
        }
        return $this->send_back([], 1, '无重定向');
    }

    /**
     * 待送达详情页
     * @param \Illuminate\Http\Request $request
     * @return type
     * @author wuhaoliang
     */
    public function wait_car_to_detail($order_id)
    {
        if (!$order_id) {
            return $this->send_back('', 0, '缺少订单号!');
        }
        $data = OrderCar::select("jz_order_long", "order_id_zb", "order_id", "car_id", "in_contacts",
            "in_telphone", "out_contacts", "out_telphone", "car_pic_url", "get_car_time", "the_last_to_time",
            "shop_pic_url", "publish_sid", "create_time", "out_city_name", "out_province_name", "in_city_name",
            "in_province_name", "get_car_addr", "to_addr", "info_fee", "transfer_fee", "car_name", "car_no",
            "bid_status", "site_times", 'the_last_get_time', 'add_score_get_time', 'the_last_to_time',
            "order_type", "task_get_time", "plan_get_car_time", "plan_to_time", "add_score_to_time", "transfer_fee_zg")
            ->where('order_id', $order_id)->first();
        if (sizeof($data) > 0) {
            $data->create_time = date("Y-m-d H:i:s", $data->create_time);
            $data->get_car_time = date("Y-m-d H:i:s", $data->get_car_time);
            $data->the_last_get_time = date("Y-m-d H:i:s", $data->the_last_get_time);
            $data->add_score_get_time = date("Y-m-d H:i:s", $data->add_score_get_time);
            $data->the_last_to_time = date("Y-m-d H:i:s", $data->the_last_to_time);
            $data->task_get_time = date("Y-m-d H:i:s", $data->task_get_time);
            $data->plan_get_car_time = date("Y-m-d H:i:s", $data->plan_get_car_time);
            $data->add_score_to_time = date("Y-m-d H:i:s", $data->add_score_to_time);
            $data->plan_to_time = $data->plan_to_time ? date("Y-m-d H:i:s", $data->plan_to_time) : '--';
            $data->times = SysConfig::get_config('send_site_times') - $data->site_times;
            $data->car_no = $data->car_no ? $data->car_no : '--';
        } else {
            return $this->send_back('', 0, '订单已运达失败,请刷新待运达列表');
        }
        return $this->send_back($data);
    }

    /**
     * 获取定位信息
     * @param \Illuminate\Http\Request $request
     * @author wuhaoliang
     */
    public function get_site(Request $request, $order_id)
    {
        $location = $request->all();
        f_log('get_site', $location);
        $order_info = OrderCar::get_order_info($order_id);
        if (empty($order_info)) {
            return send_back([], 0, '车辆已运达失败,请刷新待运达列表');
        }
        $key = __FUNCTION__ . $order_id . $this->user_info['id'];
        if (\Predis::get($key)) {
            return send_back([], 0, '定位过于密集!');
        }
//        \Predis::setex($key,30*60,1);
        \Predis::setex($key, 3, 1);
        $location = $location['location'];
        $site_info = HttpClient::get("http://api.map.baidu.com/geocoder/v2/?ak=9zSCh0xebW7CnPFm7yIM2r0D&location=$location&output=json")->content();
        $site_info = json_decode($site_info, true);
        $city_name = $site_info['result']['addressComponent']['city'];
        $need_data['order_id'] = $order_id;
        $need_data['user_id'] = $this->user_info['id'];
        $need_data['city_name'] = $city_name;
        $need_data['do_time'] = time();
        $need_data['ip'] = $_SERVER['REMOTE_ADDR'];
        $need_data['location'] = $location;
        DB::beginTransaction();
        $res = \App\SiteInfo::create($need_data);
        $status = DB::table('order_car')->where('order_id', $order_id)->increment('site_times');
        $score_s = true;
        $site_times = SysConfig::get_config('send_site_times');
        if ($order_info['site_times'] < $site_times && $order_info['order_type'] == 0) {
            $score_details = new ScoreDetails();
            $score_s = $score_details->score_operation(10, SysConfig::get_config('send_location'), $this->user_info['id'], $order_id);
        }
        f_log(__FUNCTION__ . $order_id, "status=$status;score_s=$score_s,res=" . json_encode($res));
        if ($res && $status && $score_s) {
            DB::commit();
            $user_info = User::find($this->user_info['id']);
            $user_name = $user_info->auth_type == 1 ? $user_info->comp_name : $user_info->realname;
            OrderCar::send_to_jz(array('jz_order_id' => $order_info['jz_order_id'], 'city' => $city_name, 'user_name' => $user_name), 5);
            \Predis::del(config('wuliu.order_info') . $order_id);
            $this->assign_data(['times' => ($site_times - $order_info['site_times']) - 1]);
            $con = '发送定位成功!';
            return $this->send_back([], $status, $con);
        } else {
            DB::rollback();
            return send_back([], 0, '定位失败!');
        }
    }

    /**
     * 完成运输
     * @author wuhaoliang
     * @param type $order_id订单号
     */
    public function finish_trans($order_id)
    {
        $lock = get_lock($order_id . __FUNCTION__, 3);
        if (!$lock) {
            return send_back([], 0, "正在完成,请稍等!");
        }
        $to_time = time();
        $order_info = OrderCar::get_order_info($order_id);
        if (empty($order_info)) {
            return send_back([], 0, "订单已运达失败,请刷新待运达列表");
        }
        //判断订单状态
        if ($order_info['car_status'] == 3) {  //车辆已运达完成
            return send_back([], 0, '订单已运达成功,请勿重复操作');
        }
        if ($order_info['car_status'] == 4) {  //车辆已运达失败
            return send_back([], 0, '订单已运达失败,请刷新待运达列表');
        }
        if ($order_info['order_type'] == 1) {  //直购订单
            $status_zg = $this->finish_zg($order_info, $to_time);
            if ($status_zg) {
                return send_back([], 1, '运达成功!');
            } else {
                return send_back([], 0, '操作失败!');
            }
        }

        if ($order_info['car_status'] != 2) {
            return send_back([], 0, "不在运达状态!");
        }
        $user_info = User::where('id', $this->user_info['id'])->first()->toarray();
        
        DB::beginTransaction();
        //更新车辆状态
        $num = SecuDepositLog::where('order_id', $order_id)->where('oper_type', 10)->count();
        $sec_red_s = 1;
        if ($num == 0) {
            $sec_red_s = SecuDeposit::reduce_code_money(10, $order_info['info_fee'], $order_id);
        }
        if ($sec_red_s == 0) {
            return $this->send_back([], 0, '客户保证金不足,请提醒充值!');
        }
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_id), ['car_to_time' => $to_time, 'car_status' => 3], $order_id);
        $car_s = Car::where('order_id', $order_id)->update(['car_to_time' => time(), 'car_status' => 3]);
        $score_s = 1;
//        $trans_type = '';
        $score_details = new ScoreDetails();
        if ($to_time < $order_info['add_score_to_time']) {
            $score_s = $score_details->score_operation(5, SysConfig::get_config('on_time_trans'), $order_info['user_id'], $order_id);
        } elseif ($to_time > $order_info['add_score_to_time']) {
            $score_s = $score_details->score_operation(6, SysConfig::get_config('out_time_trans'), $order_info['user_id'], $order_id);
        }
        //todo 更新矩阵中抢单物流状态!
        $cost_bzj = SecuDepositLog::get_deduction_freeze_deposit($this->user_info['id'], $order_id);
        $hot_money = config('wuliu.baozj_num') - $cost_bzj;
        $sec_s = SecuDeposit::hot_money(7, $hot_money, $order_id);


        f_log('finish_trans_'.$order_id,var_export(compact('order_id','score_s','sec_s','order_car_s','car_s','sec_red_s'),true));

        if ($score_s && $order_car_s && $car_s && $sec_s && $sec_red_s) {
            DB::commit();
            $this->dispatch(new UpdateUserScore(['fun' => 'update_time_out', 'user_id' => $this->user_info['id']]));
            $this->dispatch(new UpdateJzStatus($order_info['jz_order_id'], 7));
            OrderCar::send_to_jz(array_merge(['jz_order_id' => $order_info['jz_order_id']], ['realname' => $user_info['realname'], 'auth_type' => $user_info['auth_type'],
                'comp_name' => $user_info['comp_name'], 'transfer_fee' => $order_info['transfer_fee'],
                'info_fee' => $order_info['info_fee'],
                'telephone' => $user_info['telephone'], 'contact_phone' => $user_info['contact_phone'],
                'contact_people' => $user_info['contact_people']]), 6);
            $content = '运达成功';
            return send_back([], 1, $content);
        } else {
            DB::rollback();
            return send_back([], 0, '操作失败!');
        }
    }

    private function finish_zg($order_info, $to_time)
    {
        DB::beginTransaction();
        $user_info = User::where('id', $this->user_info['id'])->first()->toarray();
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_info['order_id']), ['car_to_time' => $to_time, 'car_status' => 3], $order_info['order_id']);
        $car_s = Car::where('order_id', $order_info['order_id'])->update(['car_to_time' => $to_time, 'car_status' => 3]);
        $jz_s = OrderCar::send_to_jz(array_merge(['jz_order_id' => $order_info['jz_order_id']], ['realname' => $user_info['realname'], 'auth_type' => $user_info['auth_type'],
            'comp_name' => $user_info['comp_name'], 'transfer_fee' => $order_info['transfer_fee'],
            'info_fee' => $order_info['info_fee'], 'car_to_time' => $to_time,
            'telephone' => $user_info['telephone'], 'contact_phone' => $user_info['contact_phone'],
            'contact_people' => $user_info['contact_people']]), 12);
        if ($order_car_s && $car_s && $jz_s->status == 1) {
            DB::commit();
            return true;
        } else {
            DB::rollback();
            return false;
        }
    }

    /**
     * 矩阵扫码出库
     * @author wuhaoliang
     */
    public function jz_car_out_store(Request $request)
    {
        $order_id = $this->check_order_id($request);
        \Log::info(__FUNCTION__ . __LINE__ . '#' . $order_id);
        if ($request->jz_order_id) {
            $order_info = OrderCar::where('jz_order_id', $request->jz_order_id)->first();
            $order_id = $order_info['order_id'];
        } else {
            $order_info = OrderCar::get_order_info($order_id);
        }
        if (empty($order_info)) {
            return $this->send_back([], 0, '订单不存在!');
        }
        if ($order_info['jz_confirm_out'] == 1) {
            return $this->send_back([], 0, '请不要重复操作出库!');
        }
        if ($order_info['car_status'] != 1) {
            return $this->send_back([], 0, '此车不在出库状态!');
        }
        \Log::info(__FUNCTION__ . '#' . __LINE__ . "order_id = $order_id");
        DB::beginTransaction();
        //更新车辆出库时间
        $time = time();
        $trans_need_time = strtotime("+" . $order_info['transfer_time'] . "day", $time);
        $the_last_to_time = $trans_need_time + SysConfig::get_time('auto_to_time');
        $car_s = \App\Car::where('order_id', $order_id)->update(['car_out_store_time' => $time,
            'add_score_to_time' => $trans_need_time, 'the_last_to_time' => $the_last_to_time,
            'get_car_time' => $time, 'car_status' => 2]);
        $order_car_s = OrderCar::update_order(OrderCar::where('order_id', $order_id), ['car_out_store_time' => time(), 'add_score_to_time' => $trans_need_time,
            'the_last_to_time' => $the_last_to_time, 'get_car_time' => $time, 'jz_confirm_out' => 1, 'car_status' => 2], $order_id);
        if ($request->has('scan_phone')) {
            $order_s = Order::where('id', $order_id)->update(['scan_person' => $request->scan_person, 'scan_phone' => $request->scan_phone]);
        } else {
            $order_s = 1;
        }
        $score_details = new ScoreDetails();
        if (time() < $order_info['add_score_get_time']) {
            //准时提车
            $score_s =$score_details->score_operation(3, SysConfig::get_config('on_time_get_car'), $order_info['user_id'], $order_id);
        } else {
            //非准时提车
            $score_s = $score_details->score_operation(4, SysConfig::get_config('not_on_time_get_car'), $order_info['user_id'], $order_id);
        }
        $data = OrderCar::select('jz_order_long', 'jz_order_id', 'publish_sid', 'create_time', 'end_time', 'add_score_get_time', 'the_last_get_time', 'out_province_name', 'in_province_name', 'in_city_name', 'out_city_name', 'trans_company_name', 'info_fee', 'trans_company_id', 'trans_per', 'trans_per_phone', 'transfer_time', 'user_id', 'add_score_to_time')
            ->where('order_id', $order_id)
            ->first()->toarray();
        $user_info = User::select('telephone', 'auth_type', 'realname', 'comp_name', 'contact_people', 'contact_phone')
            ->where('id', $data['user_id'])->first()->toarray();
        if ($order_car_s && $order_s && $car_s && $score_s) {
            DB::commit();
            $this->dispatch(new UpdateJzStatus($order_info['jz_order_id'], 4));
            $this->dispatch(new UpdateUserScore(['fun' => 'update_get_car_time', 'user_id' => $data['user_id']]));
            $add_score_to_car_job = (new \App\Jobs\WarningJob($order_id, 3))
                ->delay($trans_need_time - $time - (SysConfig::get_config('add_score_arrive_sys_notice') * 3600));
            $this->dispatch($add_score_to_car_job);
            $auto_finish = (new \App\Jobs\AutoFinish($order_id))->delay($the_last_to_time - $time);
            //手动操作出库,不写日志

            if ($request->has('jz_order_id') && !$request->has('no_write_pro')) {
                OrderCar::send_to_jz(array_merge($data, $user_info), 4);
            }
            $this->dispatch($auto_finish);
            $state = 1;
        } else {
            w_sql(__FUNCTION__);
            DB::rollback();
            $state = 0;
        }
        return $this->send_back(array_merge($data, $user_info), $state);
    }

    /**
     * 矩阵扫码批量出库
     * @author wuhaoliang
     */
    public function jz_some_car_out_store(Request $request)
    {
        $user_id = $request->has('user_id') ? $request->user_id : -1;
        if ($user_id < 0) {
            return $this->send_back('', 0, '没有这个用户!');
        }

        $user_info = User::find($user_id);
        $all_order = OrderCar::where('user_id', $user_id)
            ->select('jz_order_id', 'jz_order_long', 'order_id', 'car_name', 'car_no', 'publish_sid', 'add_score_get_time', 'the_last_get_time', 'trans_company_name', 'trans_per', 'trans_per_phone', 'transfer_time', 'add_score_to_time', 'info_fee', 'car_pic_url', 'out_city_name', 'in_city_name')
            ->where('car_status', 1)
            ->where('order_type', 0)
            ->orderby('end_time', 'asc')
            ->where('validity', 1)
            ->get()->toarray();
        return $this->send_back(array('orders' => $all_order, 'user_id' => $user_id,
            'auth_type' => $user_info->auth_type, 'realname' => $user_info->realname, 'telephone' => $user_info->telephone,
            'comp_name' => $user_info->comp_name, 'contact_people' => $user_info->contact_people,
            'contact_phone' => $user_info->contact_phone));
    }

    /**
     * 矩阵扫码批量出库处理
     * @author wuhaoliang
     */
    public function jz_some_car_out_store_act(Request $request)
    {
        $orders = $request->has('orders') ? $request->orders : array();
        $user_id = $request->has('user_id') ? $request->user_id : -1;
        $time = time();
        if ($user_id < 0) {
            return $this->send_back('', 0, '没有这个用户!');
        }
        $orders = json_decode($orders, true);
        if (empty($orders)) {
            return $this->send_back('', 0, '未选择订单!');
        }
        w_log(__FUNCTION__, $orders, 'big');
        DB::beginTransaction();
        $order_info_list = OrderCar::select('user_id', 'order_id', 'info_fee', 'add_score_get_time', 'transfer_time')->whereIn('order_id', $orders)->get();
        w_log(__FUNCTION__, $order_info_list, 'big');
        foreach ($order_info_list as $one_order) {
            $trans_need_time = strtotime("+" . $one_order['transfer_time'] . "day", $time);
            f_log($one_order['order_id'], $trans_need_time);
            $the_last_to_time = $trans_need_time + SysConfig::get_time('auto_to_time');
            $order_car_s = OrderCar::where('user_id', $user_id)
//                ->where('car_out_store_time', 0)
                ->where('order_id', $one_order['order_id'])
                ->where('validity', 1)
//                ->update(['car_out_store_time' => $time,'get_car_time'=>$time,'jz_confirm_out'=>1])
                ->update(['car_out_store_time' => time(), 'add_score_to_time' => $trans_need_time,
                    'the_last_to_time' => $the_last_to_time, 'get_car_time' => $time,
                    'jz_confirm_out' => 1, 'car_status' => 2, 'scan_person' => $request->scan_person, 'scan_phone' => $request->scan_phone]);
            $car_s = \App\Car::where('order_id', $one_order['order_id'])
                ->update(['car_out_store_time' => $time,
                    'add_score_to_time' => $trans_need_time, 'the_last_to_time' => $the_last_to_time, 'get_car_time' => $time, 'car_status' => 2]);
            $order_s = Order::where('id', $one_order['order_id'])->update(['scan_person' => $request->scan_person, 'scan_phone' => $request->scan_phone]);
            $score_details = new ScoreDetails();
            if (time() < $one_order['add_score_get_time']) {
                //准时提车
                $score_s = $score_details->score_operation(3, SysConfig::get_config('on_time_get_car'), $one_order['user_id'], $one_order['order_id']);
            } else {
                //非准时提车
                $score_s = $score_details->score_operation(4, SysConfig::get_config('not_on_time_get_car'), $one_order['user_id'], $one_order['order_id']);
            }
            if (!$score_s || !$order_s) {
                break;
            }
            \Predis::del(config('wuliu.order_info') . $one_order['order_id']);
        }
        \Log::info(__CLASS__ . 'line=' . __LINE__ . "order_car_s=$order_car_s,car_s=$car_s,score_s=$score_s");
        $nedd_data = array();
        if ($order_car_s && $order_s && $car_s && $score_s) {
            DB::commit();
            foreach ($order_info_list as $one_order) {
                $this->dispatch(new UpdateJzStatus($one_order['jz_order_id'], 4));
                $this->dispatch(new UpdateUserScore(['fun' => 'update_get_car_time', 'user_id' => $one_order['user_id']]));
            }
            $order_info_list = OrderCar::select('order_id', 'add_score_to_time', 'the_last_to_time')->whereIn('order_id', $orders)->get();
            $nedd_data = array();
            foreach ($order_info_list as $order_one) {
                $add_score_to_car_job = (new \App\Jobs\WarningJob($order_one['order_id'], 3))
                    ->delay($order_one['add_score_to_time'] - $time - (SysConfig::get_config('add_score_arrive_sys_notice') * 3600));
                $this->dispatch($add_score_to_car_job);
                $auto_finish = (new \App\Jobs\AutoFinish($order_one['order_id']))->delay($order_one['the_last_to_time'] - $time);
                $this->dispatch($auto_finish);
                $nedd_data[$order_one['order_id']] = $order_one['add_score_to_time'];
            }

            $state = 1;
        } else {
            DB::rollback();
            $state = 0;
        }
        $this->w_log(__CLASS__ . __FUNCTION__, array('order' => json_encode($orders), 'user_id' => $user_id, 'status' => $state));
        return $this->send_back($nedd_data, $state);
    }

    public function get_num_task()
    {

        $order_car = OrderCar::select(DB::raw('count(*) as num, car_status'))
            ->where('validity', 1)
            ->where(function ($query) {
                $query->where(function ($query) {
                    $query->where('user_id', $this->user_info['id'])
                        ->whereIn('car_status', [1, 2]);
                })->orWhere('car_status', 0)->where('order_id_zb', '');
            });

        $this->order_car_query_build($order_car, app('request'));
        $data = $order_car->groupBy('car_status')
            ->get();
        $res = [0, 0, 0];
        foreach ($data as $val) {
            $res[$val->car_status] = $val->num;
        }
        $res[3] = $this->get_zb_num(app('request'));
        return $res;
    }

    /**
     * 订单数量
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author shaogaojie@xin.com
     * @date 2016-08-08 14:57:49
     */
    public function get_order_num(Request $request)
    {
        $user_info = session('user_info');
        if (!$request->has('car_status') || !in_array($request->car_status, [0, 1, 2])) {
            return send_back(0, 1);
        }

        $order_car = OrderCar::where('validity', 1)
            ->where('car_status', $request->car_status);
        if ($request->car_status != 0) {
            $order_car->where('user_id', $user_info['id']);
        }

        $this->order_car_query_build($order_car, $request);
        $count = $order_car->count();
        return send_back(['count' => $count], 1);
    }

    public function get_zb_num($request)
    {
        $order_cars = OrderCar::join('order_zb as oz', 'order_car.order_id_zb', '=', 'oz.order_id_zb')
            ->wherein('oz.bid_status', [1, 2]);
        $data = $this->order_car_query_build_zb($order_cars, $request);
        $data = $order_cars->get()->count();
        return $data;
    }

    //更新运输信息
    public function update_trans_info(Request $request)
    {
        $params = $request->all();

        $validator = Validator::make($params, [
            'order_id_jz' => 'required',//物流公司账户电话
        ]);
        //格式验证未通过
        if ($validator->fails()) {
            return send_back($validator->errors(), 0, $validator->errors()->first());
        }
        f_log(__FUNCTION__ . __LINE__ . 'order_id=' . $params['order_id_jz'], $params);
        $request->has('company_account') && $update_data['company_account'] = $params['company_account'];
        $request->has('get_car_addr') && $update_data['get_car_addr'] = $params['get_car_addr'];
        $request->has('to_addr') && $update_data['to_addr'] = $params['to_addr'];
        $request->has('task_get_time') && $update_data['task_get_time'] = $params['task_get_time'];
        $request->has('plan_get_car_time') && $update_data['plan_get_car_time'] = $params['plan_get_car_time'];
        $request->has('plan_to_time') && $update_data['plan_to_time'] = $params['plan_to_time'];
        $request->has('trans_per') && $update_data['trans_per'] = $params['trans_per'];
        $request->has('trans_per_phone') && $update_data['trans_per_phone'] = $params['trans_per_phone'];
        $request->has('out_contacts') && $update_data['out_contacts'] = $params['out_contacts'];//提车联系人
        $request->has('out_telphone') && $update_data['out_telphone'] = $params['out_telphone'];//提车联系人电话
        $request->has('in_contacts') && $update_data['in_contacts'] = $params['in_contacts'];//接车联系人
        $request->has('in_telphone') && $update_data['in_telphone'] = $params['in_telphone'];//接车联系人电话
        $request->has('buyer_name') && $update_data['buyer_name'] = $params['buyer_name'];// 接车人
        $request->has('buyer_mobile') && $update_data['buyer_mobile'] = $params['buyer_mobile'];// 接车人
        $order_info = OrderCar::select('order_id')->where('jz_order_id', $params['order_id_jz'])->first();
        DB::beginTransaction();
        try {
            $oc_s = OrderCar::where('order_id', $order_info['order_id'])
                ->update(get_table_data(new OrderCar(), $update_data));
            $o_s = Order::where('id', $order_info['order_id'])
                ->update(get_table_data(new Order(), $update_data));
            $c_s = Car::where('order_id', $order_info['order_id'])
                ->update(get_table_data(new Car(), $update_data));
            DB::commit();
            f_log(__FUNCTION__ . __LINE__ . 'order_id=' . $params['order_id_jz'], '成功!');
//            return $order_zb;
        } catch (Exception $e) {
            DB::rollBack();
            Mail::raw('直购更新失败!矩阵订单号为' . $params['order_id_jz'], function ($message) {
                $message->from($_SERVER['MAIL_SMTP_USER'], '直购更新失败!');
                $message->to('shaogaojie@xin.com');
            });
            f_log(__FUNCTION__ . __LINE__ . 'order_id=' . $params['order_id_jz'], '失败!oc=' . $oc_s . 'o=' . $o_s . 'c=' . $c_s);
            return send_back([], 0, "更新失败!");
        }
        return send_back([], 1, "更新成功!");
    }

}

This snippet took 0.12 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).