1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
<?php

namespace app\api\controller;

use app\api\controller\v1\Base;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Car extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 查询常用路线
     * @return void
     */
    public function selectroute(){
        $res=Db::name("route")->field("id,name,start_address,end_address")->limit(2)->select();
        return $this->success($res);
    }

    /**
     * 通过线路查询车辆
     * @return void
     */
    public function selectcarbyroute(){
        $route_id = $this->request->param("route_id");
        $time = $this->request->param("time");
        $res=Db::name("car")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->join("driver c","a.driver_id=c.id")
            ->field("a.id,b.id as route_id,c.id as driver_id,DATE_FORMAT(FROM_UNIXTIME(a.reservation_time), '%H:%i') AS reservation_time ,DATE_FORMAT(FROM_UNIXTIME(a.start_time), '%H:%i') AS start_time ,b.price,a.seat,a.license_plate")
            ->where("route_id",$route_id)
            ->where("scheduling_type",1)
            ->group("start_time")
            ->select();

        // 查询临时线路车次
        $statr_time=strtotime(date("Y-m-d",time()));
        $end_time=$statr_time+86400;
        $temporary_route=Db::name("temporary_route")
            ->where("create_time",">",$statr_time)
            ->where("create_time","<",$end_time)
            ->where("route_id",$route_id)
            ->select();

        $data=[];
        foreach ($temporary_route as $k=>$v){
            $data[]=Db::name("car")
                ->alias("a")
                ->join("route b","a.route_id=b.id")
                ->join("driver c","a.driver_id=c.id")
                ->field("a.id,b.id as route_id,c.id as driver_id,DATE_FORMAT(FROM_UNIXTIME(a.reservation_time), '%H:%i') AS reservation_time ,DATE_FORMAT(FROM_UNIXTIME(a.start_time), '%H:%i') AS start_time ,b.price,a.seat,a.license_plate")
                ->where("route_id",$route_id)
                ->where("a.id",$temporary_route[$k]['car_id'])
                ->group("start_time")
                ->find();
        }
        $res=array_merge($data,$res);

        $start_time=strtotime(date("Y-m-d",$time));
        $end_time=$start_time+86400;
        $newtime=time();
        foreach ($res as $k=>$v){
            $res[$k]['order']=Db::name("order")
                ->where("car_id",$res[$k]['id'])
                ->where("is_pay",1)
                ->where("reservation_time",">",$start_time)
                ->where("reservation_time","<",$end_time)
                ->count();

        }
        return $this->success("请求成功",$res);
    }


    /**
     * 创建订单
     * @return void
     */
    public function createorder(){
        $number=$this->request->param("number");//乘车人数
        $phone = $this->request->param("phone");//联系手机号码
        $user_name = $this->request->param("user_name");//联系人
        $position = $this->request->param("position");//上车地址
        $lat = $this->request->param("lat");//经度
        $lng = $this->request->param("lng");//维度
        $remarks = $this->request->param("remarks");//备注
        $intended_driver_id = $this->request->param("intended_driver_id");//意向司机
        $route_id = $this->request->param("route_id");//线路
        $car_id = $this->request->param("car_id");//车辆
        if($number<=0){
            $this->error("人数错误");
        }
       //查询线路
        $route=Db::name("route")->where("id",$route_id)->find();
        //查询车辆
        $car=Db::name("car")->where("id",$car_id)->find();
        //查询司机
        $driver=Db::name("driver")->where("id",$car['driver_id'])->find();
        //判断总表
        $time=strtotime(date("Y-m-d",time()));

        $order_review=Db::name("order_review")
            ->where("car_id",$car_id)
            ->where("car_id",$car_id)
            ->where("route_id",$route_id)
            ->where("driver_id",$driver['id'])
            ->where("createtime",">",$time)
            ->find();

        if(!$order_review){

            $order_review_id=Db::name("order_review")
                ->insertGetId([
                    "car_id"=>$car_id,
                    "route_id"=>$route_id,
                    "driver_id"=>$driver['id'],
                    "order_status"=>1,
                    "createtime"=>time()
                ]);

        }else{
            $order_review_id=$order_review['id'];
        }
        $data=[
            "order_no"=>getOrderSn(),
            "price"=>bcmul($route['price'],$number,2),
            "is_pay"=>"2",//未支付
            "car_id"=>$car_id,
            "order_review_id"=>$order_review_id,
            "route_id"=>$route_id,
            "user_id"=>$this->auth->id,
            "phone"=>$phone,
            "user_name"=>$user_name,
            "driver_id"=>$driver['id'],
            "driver_name"=>$driver['name'],
            "create_time"=>time(),
            "reservation_time"=>time(),
            "position"=>$position,
            "lat"=>$lat,
            "lng"=>$lng,
            "number"=>$number,
            "remarks"=>$remarks,
            "intended_driver_id"=>$intended_driver_id,
        ];
        $res=Db::name("order")->insertGetId($data);
        $userinfo = Db::name('user')
            ->where(['id' => $this->auth->id])
            ->field('id,wx_xcx_openid')
            ->find();
        $notifyURI = $this->doman . '/addons/epay/api/OrderPayNtf';
        $params = [
            'amount' => $data['price'],
            'orderid' => $data['order_no'],
            'type' => 'wechat',
            'notifyurl' => $notifyURI,
            'method' => 'miniapp',
            'openid' => $userinfo['wx_xcx_openid'],
        ];
        $f = \addons\epay\library\Service::submitOrder($params);
        $this->success("请求成功",$f);


    }


    /**
     * 查询乘车人信息
     * @return void
     */
    public function passengerlist(){
        $res=Db::name("passenger")->where("user_id",$this->auth->id)->select();
        return $this->success("请求成功",$res);
    }
    /**
     * 增加乘车人信息
     * @return void
     */
    public function addpassenger(){
        $name=$this->request->param("name");
        $IDcard=$this->request->param("IDcard");
        $phone=$this->request->param("phone");
        $is_adult=$this->request->param("is_adult");
        $res=Db::name("passenger")->insert([
            "name"=>$name,
            "IDcard"=>$IDcard,
            "phone"=>$phone,
            "user_id"=>$this->auth->id,
            "is_adult"=>$is_adult
        ]);
        return $this->success("添加成功",$res);
    }

    /**
     * 修改乘车人信息
     * @return void
     */
    public function updatepassenger(){
        $name=$this->request->param("name");
        $IDcard=$this->request->param("IDcard");
        $phone=$this->request->param("phone");
        $is_adult=$this->request->param("is_adult");
        $id=$this->request->param("id");
        $res=Db::name("passenger")->where("id",$id)->update([
            "name"=>$name,
            "IDcard"=>$IDcard,
            "phone"=>$phone,
            "is_adult"=>$is_adult
        ]);
        return $this->success("修改成功",$res);
    }
    /**
     * 删除乘车人信息
     * @return void
     */
    public function deletepassenger(){
        $id=$this->request->param("id");
        $res=Db::name("passenger")->where("id",$id)->delete();
        if($res){
            return $this->success("删除成功",$res);

        }else{
            return $this->error("删除失败");
        }
    }

    /**
     * 查询订单
     * @return void
     */
    public function selectorder(){
        $is_pay=$this->request->param("is_pay"); //是否支付:1=已支付,2=未支付,3=已退款,4=已取消
        $page=$this->request->param("page");
        $total=$this->request->param("total");
        $w['user_id'] = $this->auth->id;
        if ($is_pay!=0){
            $w['is_pay'] = $is_pay;
        }
        $res=Db::name("order")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->page($page,$total)
            ->select();
        $amount=Db::name("order")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->count();
        $data=["data"=>$res,"amount"=>$amount];
        return $this->success("请求成功",$data);
    }


    /**
     * 切换司机
     * @return void
     */
    public function switching_driver(){
        $res=Db::name("driver")->where("user_id",$this->auth->id)->find();
        $car=Db::name("car")->where("driver_id",$res['id'])->find();
        if($car){
            //本月收入
            $revenue_mon=sprintf('%.2f',Db::name("order")
                ->whereTime('create_time', 'month')
                ->where("is_pay",1)
                ->where("driver_id",$res['id'])
                ->sum("price"));

            //累计收入
            $all_order=sprintf('%.2f',Db::name("order")
                ->where("is_pay",1)
                ->where("driver_id",$res['id'])
                ->sum("price"));

            //本月订单
            $order_mon=Db::name("order")
                ->where("is_pay",1)
                ->where("driver_id",$res['id'])
                ->count();
            $res['license_plate']=$car['license_plate'];
            $res['car_model']=$car['car_model'];
            $res['revenue_mon']=$revenue_mon;
            $res['all_order']=$all_order;
            $res['order_mon']=$order_mon;
        }else{
            return $this->error("请求失败");
        }

        if($res){
            return $this->success("请求成功",$res);
        }else{
            return $this->error("请求失败");
        }
    }
    /**
     * 切换司机
     * @return void
     */
    public function switching_user(){
        $res=Db::name("user")->where("id",$this->auth->id)->find();
        if($res){
            return $this->success("请求成功",$res);
        }else{
            return $this->error("请求失败");
        }
    }

    /**
     * 通过位置查询线路
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectroutebyname(){
        $start_address=$this->request->param("start_address");
        $end_address=$this->request->param("end_address");
        $route=Db::name("route")->where("end_address",$end_address)->where("start_address",$start_address)->find();
        return $this->success("请求成功",$route);
    }

    /**
     * 开始接送
     * @return void
     */
    public function connect_order(){
        $id=$this->request->param("id");
        $order=Db::name("order_review")->where("id",$id)->find();
        if($order['order_status']==1){
            $order=Db::name("order")->where("id",$id)->update(['order_status'=>2]);
            return $this->success("请求成功");
        }else{
            return $this->error("请求失败");
        }
    }
    /**
     * 订单进行
     * @return void
     */
    public function have_order(){
        $id=$this->request->param("id");
        $order=Db::name("order_review")->where("id",$id)->find();
        if($order['order_status']==2){
            $order=Db::name("order")->where("id",$id)->update(['order_status'=>3]);
            return $this->success("请求成功");
        }else{
            return $this->error("请求失败");
        }
    }

    /**
     * 订单完成
     * @return void
     */
    public function complete_order(){
        $id=$this->request->param("id");
        $order=Db::name("order_review")->where("id",$id)->find();
        if($order['order_status']==3){
            $order=Db::name("order")->where("id",$id)->update(['order_status'=>4]);
            return $this->success("请求成功");
        }else{
            return $this->error("请求失败");
        }
    }

    /**
     * 查询订单总览
     * @return void
     */
    public function selectorderreview(){
        $order_status=$this->request->param("order_status"); //接送状态:1=未开始,2=接送中,3=进行中,4=已完成
        $page=$this->request->param("page");
        $total=$this->request->param("total");
        $driver=Db::name("driver")->where("user_id",$this->auth->id)->find();
        $w["a.driver_id"] = $driver["id"];
        if ($order_status!=0){
            $w["a.order_status"] = $order_status;
        }
        $res=Db::name("order_review")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->page($page,$total)
            ->select();
        foreach ($res as $k=>$v){
            $res[$k]['price']=sprintf('%.2f',Db::name("order")->sum("price"));
            $res[$k]['number']=Db::name("order")->sum("number");
        }
        $amount=Db::name("order_review")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->count();
        $data=["data"=>$res,"amount"=>$amount];

        return $this->success("请求成功",$data);
    }
    /**
     *司机查询订单
     * @return void
     */
    public function selectbydriver(){
        //$order_status=$this->request->param("order_status"); //接送状态:1=未开始,2=接送中,3=进行中,4=已完成
        $page=$this->request->param("page");
        $total=$this->request->param("total");
        $order_review_id=$this->request->param("order_review_id");
        $driver=Db::name("driver")->where("user_id",$this->auth->id)->find();
        $w['driver_id'] = $driver["id"];
            $w['order_review_id'] = $order_review_id;
        $res=Db::name("order")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->page($page,$total)
            ->select();
        $amount=Db::name("order")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.*,b.start_address,b.end_address")
            ->where($w)
            ->count();
        $data=["data"=>$res,"amount"=>$amount];
        return $this->success("请求成功",$data);
    }

    /**
     *认证声明
     * @return void
     */
    public function authentication_statement(){
        $content = config("site.content");//微信小程序AppID
        return $this->success($content);
    }

    /**
     *紧急手机号
     * @return void
     */
    public function alarm_phone(){
        $content = config("site.alarm_phone");//微信小程序AppID
        return $this->success("请求成功",$content);
    }

    /**
     * 退款接口
     * @param $id
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function refund(){
        $id=$this->request->param("id");
        //查询订单
        $order=Db::name("order")->where("id",$id)->find();
        if($order['is_pay']==1){
            $this->error("该订单无法退款");
        //拼接退款参数
        $pay_fee = $order['price'];
        $refund_fee = $pay_fee;
        $order_sn = $order['order_no'];
        $pay_type = 'wechat';
        $reason = '订单退款';
        $notifyurl = 'https://wyc.tenyes.cn/api/index/refundNotifyx';//退款回调地址
        //直接调用退款方法传参即可
        $response = \addons\epay\library\Service::submitRefund($pay_fee, $refund_fee, $order_sn, getRefundSn($order['user_id']), $pay_type, $reason, $notifyurl, '', 'miniapp');
        $response = json_encode($response);
        $response = json_decode($response, true);
        file_put_contents("pcl_repay_v2.log", date("Y-m-d H:i:s") . "::" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
        if (!empty($response['return_code'] == 'SUCCESS')) {
            //退款成功,更新退款记录
            $relogs['update_time'] = time();
            $relogs['is_pay'] = 3;
            $relogs['out_refund_no'] = $response['out_refund_no'];
            $relogs['refund_time'] = time();
            $rs2 = Db::name('order')
                ->where(['id' => $order['id']])
                ->update($relogs);
        } else {
            $this->error('退款失败');
        }
        $this->success("退款成功");
    }else{
            $relogs['is_pay'] = 4;
            $rs2 = Db::name('order')
                ->where(['id' => $order['id']])
                ->update($relogs);
            $this->success("取消成功");
        }
    }

    /**
     * 添加临时车辆路线
     * @return void
     */
    public function addtemporaryroute(){
        $user_id=$this->auth->id;
        $driver=Db::name("driver")->where("user_id",$user_id)->find();
        $car=Db::name("car")->where("driver_id",$driver['id'])->find();
        $data=[
            'user_id'=>$user_id,
            'car_id'=>$car['id'],
            'route_id'=>$car['route_id'],
            'driver_id'=>$driver['id'],
            'create_time'=>time(),
        ];
        //判断是否重复
        //开始时间
        $statr_time=strtotime(date("Y-m-d",time()));
        $end_time=$statr_time+86400;
        $is_repeat=Db::name("temporary_route")
            ->where("user_id",$user_id)
            ->where("car_id",$car['id'])
            ->where("create_time",">",$statr_time)
            ->where("create_time","<",$end_time)
            ->where("driver_id",$driver['id'])
            ->find();
        if($is_repeat){
            return $this->error("添加失败");
        }
        $res=Db::name("temporary_route")->insert($data);
        return $this->success("添加成功",$res);
    }

    /**
     * 意向司机
     * @return void
     */
    public function intention_driver(){
        $route_id=$this->request->param("route_id");
        $car=Db::name("car")
            ->alias("a")
            ->join("driver b","a.driver_id=b.id")
            ->where("route_id",$route_id)
            ->field("b.id,b.name")
            ->select();
        return $this->success("请求成功",$car);
    }

    /**
     * 添加紧急联系人手机
     * @return void
     */
    public function emergency_phone(){
        $emergency_phone=$this->request->param("emergency_phone");
        $user_id=$this->auth->id;
        $res=Db::name("user")
            ->where("id",$user_id)
            ->update(['emergency_phone'=>$emergency_phone]);
        return $this->success("修改成功",$res);
    }

    /**
     * 获取轮播图
     * @return void
     */
    public function getimage(){
        $res=Db::name("image")->where("is_show",1)->select();
        foreach ($res as $k=>$v){
            $res[$k]['image']=full_image($res[$k]['image']);
        }
        return $this->success("请求成功",$res);
    }
}