OrderController.php 36.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/24
 * Time: 8:54
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use think\Db;
use think\Log;
use wxapp\pay\WeixinPay;

/**
 * @title 订单相关接口
 * @description 订单相关接口
 * @group 订单相关接口
 */
class OrderController extends HomeBaseController{

//    /**
//     * @title 生成订单
//     * @description 生成订单,加入开始时间和用户id
//     * @author 董瑞恩
//     * @url /portal/order/createOrder
//     * @method GET
//     *
//     * @param name:users_id type:String require:1 default:无 other: desc:用户id
//     * @param name:name type:String require:1 default:无 other: desc:设备名称
//     *
//     */
    public function createOrder($users_id,$name){
        $order=[
            'order_no' => cmf_get_order_sn(),
            'eq_name' => $name,
            'users_id' => $users_id,
            'start_time' => time(),
            'state' => 1
        ];
        try{
            Db::startTrans();
            Db::name('users')->where('id',$users_id)->update(['is_use'=>1]);
            Db::name('order')->insert($order);
        }catch (\Exception $exception){
            Db::rollback();
            $data=[
                'state'=>false,
                'message'=>$exception->getMessage()
            ];
            return $data;
        }
        Db::commit();
        $data=[
            'state'=>true,
            'order_no' => $order['order_no']
        ];
        return $data;
    }

//    /**
//     * @title 完成订单
//     * @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
//     * @author 董瑞恩
//     * @url /portal/order/order
//     * @method GET
//     *
//     * @param name:users_id type:String require:1 default:无 other: desc:用户id
//     *
//     * @return data:返回用于调用支付的参数
//     */
    public function order($users_id,$name){
        $order= Db::name('order')->where(['users_id'=>$users_id,'state'=>1])->find();
        $end_time=time();
        $price=$this->getPrice($users_id,$order['start_time'],$end_time);
        $time=ceil(($end_time-$order['start_time'])/3600);
        $data=[
            'end_time'=>$end_time,
            'time' => $time,
            'price' => $price,
            'state' => 2
        ];
        try{
            Db::startTrans();
            Db::name('users')->where('id',$users_id)->update(['is_use'=>0]);
            Db::name('equipment')->where('name',$name)->update(['use'=>0]);
            Db::name('order')->where('order_no',$order['order_no'])->update($data);
        }catch (\Exception $exception){
            Db::rollback();
            $data=[
                'state'=>false,
                'message'=>'数据库操作失败',
                'error' =>$exception->getMessage()
            ];
            $this->apiResponse(200,'success',$data);
        }
        Db::commit();
        //调起支付
        if ($price==0){
            try{
                Db::name('order')->where(['order_no'=>$order['order_no']])->update(['state'=>3]);
            }catch (\Exception $exception){
                $this->apiResponse(301,'error:'.$exception->getMessage());
            }
            $data=[
                'state'=>2,
            ];
            $this->apiResponse(200,'success',$data);
        }
        $this->pay($order['order_no']);

    }




    public function getPriceDRN($users_id,$start_time,$end_time){
        $cost=Db::name('cost')->where('id',1)->find();
        $interval = Db::name('interval')->where('id',1)->find();
        $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
        //一小时免费状态
        $free=$cost['free'];

        //获取整天的价格
        $dayPrice=$this->getDayPrice();

        //用了多少天
        $day=floor(($end_time-$start_time)/86400);
        $dayPrice=$day*$dayPrice;
        //去掉整天的金额
        $new_start_time=$start_time+$day*86400;//时间戳
        $new_end_time= $end_time;//时间戳
        $hours_start_time=date('His',$new_start_time);
        $hours_end_time=date('His',$new_end_time);

        $stateTime=strtotime($interval['start_time']);
        $endTime=strtotime($interval['end_time']);
        //判断预设时间段是否跨天
        $price=0;
        if ($endTime < $stateTime){
            //设备使用时间段是否跨天(不跨天)
            if ($hours_end_time > $hours_start_time){
//                dump(date('Y-m-d H:i:s',$new_start_time));
//                dump(date('Y-m-d H:i:s',$new_end_time));
//                dump(date('Y-m-d H:i:s',$stateTime));
//                dump(date('Y-m-d H:i:s',$endTime));
                //在预设时间段左边并不与之重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $new_start_time && $new_end_time < $endTime)){
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
                }
                //与预设时间段的尾部重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
                    $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] +  ceil(($new_end_time-$endTime)/3600) * $cost['cost']);
                }
                //与预设时间段的头尾都重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && $new_end_time > $stateTime){
                    $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] +  ceil(($new_end_time-$stateTime)/3600) * $interval['price'] + ceil(($stateTime-$endTime)/3600) * $cost['cost']);
                }
                //不与预设时间段重合
                if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $new_start_time && $new_end_time < $stateTime)){
                    //存在首小时免费
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $cost['cost']);
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                        Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
                        Log::init([
                            'type'  =>  'File',
                            'path'  =>  APP_PATH.'logs/'
                        ]);
                        Log::write($price);
                    }
                }
                //与预设时间段的头部重合
                if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $stateTime && $new_end_time < strtotime('24:00:00'))){
                    //存在首小时免费
                    $price += (ceil(($stateTime-$new_start_time)/3600) * $cost['cost'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price']);
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                        Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
                    }
                }
                //在预设时间段右边并不与之重合
                if (($new_start_time > $stateTime && $new_start_time < strtotime('24:00:00')) && ($new_end_time > $new_start_time && $new_end_time < strtotime('24:00:00'))){
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
                }
            //设备使用时间段是否跨天(跨天:开始时间在昨天,结束时间在今天)
            }else{
                $YesterdayStateTime=$stateTime-86400;
                $YesterdayEndTime=$endTime-86400;
                //昨天预设结束前使用,今天预设结束前停止
                if(($new_start_time > (strtotime('00:00:00')-86400) && $new_start_time < $YesterdayEndTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){

                    $price+= ceil(($YesterdayEndTime-$new_start_time)/3600) * $interval['price']
                            +ceil(($YesterdayStateTime-$YesterdayEndTime)/3600) * $cost['cost']
                            +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
                }
                //昨天预设开始前使用,今天预设结束前停止
                if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){
                    //存在首小时免费

                    $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) *  $cost['cost']
                            +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                        Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
                    }
                }
                //昨天预设开始前使用,今天预设结束后停止
                if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
                    //存在首小时免费

                    $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) *  $cost['cost']
                            +ceil(($endTime-$YesterdayStateTime)/3600) *  $interval['price']
                            +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                        Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
                    }
                }
                //昨天预设开始后使用,今天预设结束前停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){

                    $price+= ceil(($new_end_time-$new_start_time)/3600) *  $interval['price'];
                }
                //昨天预设开始后使用,今天预设结束后停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $endTime && $new_end_time < $stateTime)){

                    $price+= ceil(($endTime-$new_start_time)/3600) *  $interval['price']
                            +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
                }
                //昨天预设开始后使用,今天预设开始前停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $stateTime && $new_end_time < ($new_start_time+86400))){

                    $price+= ceil(($endTime-$new_start_time)/3600) *  $interval['price']
                            +ceil(($stateTime-$endTime)/3600) * $cost['cost']
                            +ceil(($new_end_time-$stateTime)/3600) * $interval['price'];
                }
            }
        }
        if ($price > $cost['ceiling']){
            $price = $cost['ceiling'];
        }
        $numPrice = $dayPrice+$price;
        log::write($numPrice);
        return $numPrice;
    }

    /**
     * 只是获取价格信息不会进行操作
     * @param $users_id
     * @param $start_time
     * @param $end_time
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function onlyGetPriceDRN($users_id,$start_time,$end_time){
        $cost=Db::name('cost')->where('id',1)->find();
        $interval = Db::name('interval')->where('id',1)->find();
        $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
        //一小时免费状态
        $free=$cost['free'];

        //获取整天的价格
        $dayPrice=$this->getDayPrice();

        //用了多少天
        $day=floor(($end_time-$start_time)/86400);
        $dayPrice=$day*$dayPrice;
        //去掉整天的金额
        $new_start_time=$start_time+$day*86400;//时间戳
        $new_end_time= $end_time;//时间戳
        $hours_start_time=date('His',$new_start_time);
        $hours_end_time=date('His',$new_end_time);

        $stateTime=strtotime($interval['start_time']);
        $endTime=strtotime($interval['end_time']);
        //判断预设时间段是否跨天
        $price=0;
        if ($endTime < $stateTime){
            //设备使用时间段是否跨天(不跨天)
            if ($hours_end_time > $hours_start_time){
//                dump(date('Y-m-d H:i:s',$new_start_time));
//                dump(date('Y-m-d H:i:s',$new_end_time));
//                dump(date('Y-m-d H:i:s',$stateTime));
//                dump(date('Y-m-d H:i:s',$endTime));
                //在预设时间段左边并不与之重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $new_start_time && $new_end_time < $endTime)){
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
                }
                //与预设时间段的尾部重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
                    $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] +  ceil(($new_end_time-$endTime)/3600) * $cost['cost']);
                }
                //与预设时间段的头尾都重合
                if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && $new_end_time > $stateTime){
                    $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] +  ceil(($new_end_time-$stateTime)/3600) * $interval['price'] + ceil(($stateTime-$endTime)/3600) * $cost['cost']);
                }
                //不与预设时间段重合
                if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $new_start_time && $new_end_time < $stateTime)){
                    //存在首小时免费
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $cost['cost']);
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                        Log::init([
                            'type'  =>  'File',
                            'path'  =>  APP_PATH.'logs/'
                        ]);
                        Log::write($price);
                    }
                }
                //与预设时间段的头部重合
                if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $stateTime && $new_end_time < strtotime('24:00:00'))){
                    //存在首小时免费
                    $price += (ceil(($stateTime-$new_start_time)/3600) * $cost['cost'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price']);
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];

                    }
                }
                //在预设时间段右边并不与之重合
                if (($new_start_time > $stateTime && $new_start_time < strtotime('24:00:00')) && ($new_end_time > $new_start_time && $new_end_time < strtotime('24:00:00'))){
                    $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
                }
            //设备使用时间段是否跨天(跨天:开始时间在昨天,结束时间在今天)
            }else{
                $YesterdayStateTime=$stateTime-86400;
                $YesterdayEndTime=$endTime-86400;
                //昨天预设结束前使用,今天预设结束前停止
                if(($new_start_time > (strtotime('00:00:00')-86400) && $new_start_time < $YesterdayEndTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){

                    $price+= ceil(($YesterdayEndTime-$new_start_time)/3600) * $interval['price']
                            +ceil(($YesterdayStateTime-$YesterdayEndTime)/3600) * $cost['cost']
                            +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
                }
                //昨天预设开始前使用,今天预设结束前停止
                if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){
                    //存在首小时免费

                    $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) *  $cost['cost']
                            +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                    }
                }
                //昨天预设开始前使用,今天预设结束后停止
                if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
                    //存在首小时免费

                    $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) *  $cost['cost']
                            +ceil(($endTime-$YesterdayStateTime)/3600) *  $interval['price']
                            +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
                    if ($free==1 && $is_free==0){
                        $price-=$cost['cost'];
                    }
                }
                //昨天预设开始后使用,今天预设结束前停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){

                    $price+= ceil(($new_end_time-$new_start_time)/3600) *  $interval['price'];
                }
                //昨天预设开始后使用,今天预设结束后停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $endTime && $new_end_time < $stateTime)){

                    $price+= ceil(($endTime-$new_start_time)/3600) *  $interval['price']
                            +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
                }
                //昨天预设开始后使用,今天预设开始前停止
                if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $stateTime && $new_end_time < ($new_start_time+86400))){

                    $price+= ceil(($endTime-$new_start_time)/3600) *  $interval['price']
                            +ceil(($stateTime-$endTime)/3600) * $cost['cost']
                            +ceil(($new_end_time-$stateTime)/3600) * $interval['price'];
                }
            }
        }
        if ($price > $cost['ceiling']){
            $price = $cost['ceiling'];
        }
        $numPrice = $dayPrice+$price;
        log::write($numPrice);
        return $numPrice;
    }

    //获取整天的价格
    public function getDayPrice(){
        $cost=Db::name('cost')->where('id',1)->find();
        $interval = Db::name('interval')->where('id',1)->find();
        $stateTime=strtotime($interval['start_time']);
        $endTime=strtotime($interval['end_time']);
        //预设时间长度
        $time=($stateTime-$endTime)/3600;
        if ($endTime < $stateTime){
            $price=($time *  $cost['cost']) + ((24-$time) *  $interval['price']);
        }else{
            $price=($time *  $interval['price']) + ((24-$time) *  $cost['cost']);
        }

        if ($price > $cost['ceiling']){
            $price = $cost['ceiling'];
        }
        return $price;
    }
    /**
     * 宋国杰
     * @title 计算价格
     * @description 微信统一下单
     * @author 董瑞恩
     * @url /portal/order/getPriceTest
     * @method GET
     *
     * @param name:start_time type:String require:1 default:无 other: desc:起始时间
     * @param name:end_time type:String require:1 default:无 other: desc:结束时间
     *
     * @return data:返回用于调用支付的参数
     */
    public function getPriceTest(){
        /*先获取跨越天数*/

            $users_id=1;
        $start_time=strtotime(input('start_time'));
        $end_time=strtotime(input('end_time'));
//        $end_time=$data['end_time']??time();
        $spend=$this->timediff($start_time,$end_time);
        if ($spend['min']!=0||$spend['sec']!=0){
            $spend['hour']++;
        }
        $interval = Db::name('interval')->where('id',1)->find();
        $cost=Db::name('cost')->where('id',1)->find();
        $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
        $price=0;
        $time=$start_time;
        for ($i=0;$i<$spend['hour'];$i++){
            $price+=$this->getPriceByTime($time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
            $time=$time+3600;
        }

        if ($price>$cost['ceiling']){
            $price=$cost['ceiling'];
            $free=0;
        }else{
            if ($cost['free']==1 && $is_free==0){
                $free=$this->getPriceByTime($start_time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
//                 Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
            }else{
                $free=0;
            }
        }
        $allPirce=$cost['ceiling']*$spend['day']+$price-$free;
        return $allPirce;
    }



  public function getPrice($users_id,$start_time,$end_time){
        /*先获取跨越天数*/


//        $start_time=$data['start_time']??'1545084020';
//        $end_time=$data['end_time']??time();
        $spend=$this->timediff($start_time,$end_time);
        if ($spend['min']!=0||$spend['sec']!=0){
            $spend['hour']++;
        }
        $interval = Db::name('interval')->where('id',1)->find();
        $cost=Db::name('cost')->where('id',1)->find();
        $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
        $price=0;
        $time=$start_time;
        for ($i=0;$i<$spend['hour'];$i++){
              $price+=$this->getPriceByTime($time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
             $time=$time+3600;
        }


         if ($price>$cost['ceiling']){
             $price=$cost['ceiling'];
             $free=0;
         }else{
             if ($cost['free']==1 && $is_free==0){
                 $free=$this->getPriceByTime($start_time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
                 Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
             }else{
                 $free=0;
             }
         }
         $allPirce=$cost['ceiling']*$spend['day']+$price-$free;
         return $allPirce;
    }

    /**
     * 宋国杰
     * @param $users_id
     * @param $start_time
     * @param $end_time
     * @return float|int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function onlyGetPrice($users_id,$start_time,$end_time){
        /*先获取跨越天数*/
//        $users_id='11';
//        $data=input();
//        $start_time=$data['start_time']??'1545084020';
//        $end_time=$data['end_time']??time();
        $spend=$this->timediff($start_time,$end_time);
        if ($spend['min']!=0||$spend['sec']!=0){
            $spend['hour']++;
        }
        $interval = Db::name('interval')->where('id',1)->find();
        $cost=Db::name('cost')->where('id',1)->find();
        $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
        $price=0;
        $time=$start_time;
        for ($i=0;$i<$spend['hour'];$i++){
              $price+=$this->getPriceByTime($time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
            $time=$time+3600;
        }

         if ($price>$cost['ceiling']){
             $price=$cost['ceiling'];
             $free=0;
         }else{
             if ($cost['free']==1 && $is_free==0){
                 $free=$this->getPriceByTime($start_time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
//                 Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
             }else{
                 $free=0;
             }
         }
         $allPirce=$cost['ceiling']*$spend['day']+$price-$free;
         return $allPirce;
    }


    /**
     * 获取当前时间点价格
     * @param $time 开始时间点
     * @param $first_time 一阶段价格
     * @param $second_time 二阶段价格
     * @param $cost1  一阶段金额单价
     * @param $cost2  二阶段金额单价
     * @return mixed
     */
        public function getPriceBytime($time,$first_time,$second_time,$cost1,$cost2){
            $date=date('Y-m-d',$time);
            $first_time=strtotime($date.' '.$first_time);
            $second_time=strtotime($date.' '.$second_time);

            if ($time<$first_time){
              return $cost1;
            }

            if ($time>$first_time && $time<$second_time){
                return $cost2;
            }

             if ($time>$second_time){
                 return $cost1;
             }

        }

    /**
     * @title 统一下单
     * @description 微信统一下单
     * @author 董瑞恩
     * @url /portal/order/pay
     * @method GET
     * @param name:order_no type:String require:1 default:无 other: desc:订单号
     * @return data:返回用于调用支付的参数
     */
    public function pay(){
        $param=$this->request->param();
        $order = Db::name('order')->where(['order_no' => $param['order_no'], 'state' => 2])->find();
        $user = Db::name('users')->where('id', $order['users_id'])->find();
        $price = $order['price'] * 100;//订单价格
        //微信直接支付
//        if ($param['type']==1) {
//            $body = '支付';
//            $notify_url = url('order/notify', '', '', true);//回调地址
//            $out_trade_no = $param['order_no'] . $this->create_noncestr(4);
//            $wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price, $notify_url);
//            $pay = $wxPay->pay();
//            if (isset($pay['package'])) {
//                $data = [
//                    'state' => 1,
//                    'pay' => $pay
//                ];
//                $this->apiResponse(200, 'success', $data);
//            } else {
//                $data = [
//                    'state' => 0,
//                    'message' => '统一下单失败',
//                    'error' => $pay
//                ];
//                $this->apiResponse(200, 'success', $data);
//            }
//        }
        //余额支付
            if ($user['fee']>=$order['price']){
                Db::startTrans();
                try {
                    Db::name('users')->where('id', $user['id'])->setDec('fee',$order['price']);
                    Db::name('order')->where('id',$order['id'])->update(['state'=>3]);
                    Db::name('fee_log')->insert(['user_id'=>$user['id'],'fee'=>$order['price'],'type'=>2,'add_time'=>time()]);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $this->apiResponse(400,'余额支付失败');
                }
                $this->apiResponse(201, '余额支付成功');
            }elseif ($user['fee']<$price){
                Db::name('order')->where('id',$order['id'])->update(['group_money'=>($order['price']-$user['fee'])]);
                $body = '支付';
                $notify_url = url('order/group', '', '', true);//回调地址
                $out_trade_no = $param['order_no'] . $this->create_noncestr(4);
                $wxPay = new WeixinPay($user['open_id'], $out_trade_no, $body, $price-$user['fee'], $notify_url);
                $pay = $wxPay->pay();
                if (isset($pay['package'])) {
                    $data = [
                        'state' => 1,
                        'pay' => $pay
                    ];
                    $this->apiResponse(200, 'success', $data);
                } else {
                    $data = [
                        'state' => 0,
                        'message' => '统一下单失败',
                        'error' => $pay
                    ];
                    $this->apiResponse(200, 'success', $data);
                }
            }
    }
    /**
     * @title 充值生成订单
     * @description
     * @author sgj
     * @url /portal/order/chargeOrder
     * @method GET
     *
     * @param name:fee type:String require:1 default:无 other: desc:金额
     * @param name:user_id  type:String require:1 default:无 other: desc:用户id
     *
     * @return order:订单号
     */
    public function chargeOrder(){
        $fee=input('fee');
        $user_id=input('user_id');
        if (empty($fee)||empty($user_id)){
            $this->error('信息错误!');
        }
        $insert['order_no']=date('YmdHis').rand(1000,9999);
        $insert['eq_name']='';
        $insert['start_time']=time();
        $insert['price']=$fee;
        $insert['state']=2;
        $insert['type']=2;
        $insert['users_id']=$user_id;
        $result=\db('order')->insert($insert);
            if ($result==1){
                $reutrn['order_no']=$insert['order_no'];
                $this->apiResponse(200,'success',$reutrn);
            }else{
                $this->apiResponse(400,'下单失败!');
            }
    }

    /**
     * @title 余额支付
     * @description
     * @author sgj
     * @url /portal/order/chargeOrder
     * @method GET
     *
     * @param name:fee type:String require:1 default:无 other: desc:金额
     * @param name:user_id  type:String require:1 default:无 other: desc:用户id
     *
     * @return order:订单号
     */
    public function payByBalance(){
        $order_no=input('order_no');
        $order_map['order_no']=$order_no;
        $order=\db('order')->where($order_map)->find();
        $user_map['id']=$order['users_id'];
        $user=\db('users')->where($user_map)->find();
        if ($order['price']>$user['fee']){
            $this->apiResponse('400','余额不足!');
        }
        $result=\db('users')->where($user_map)->setDec('fee',$order['price']);
        if ($result==1){
            $this->apiResponse('200','支付成功!');
        }
    }

    //支付回调接口
    public function notify(){
        $param = $this->request->param();
        if ($param == null) {
            $param = file_get_contents("php://input");
            if ($param == null) {
                $param = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        $wxPay=new WeixinPay();
        $data = $wxPay->xmlToArray($param);
        $Sign = $data['sign'];
        //支付成功回调后变更订单状态
        $mySign = $wxPay->getSign($data);
        $order_no =substr($data['out_trade_no'], 0, -4);
        $order=\db('order')->where(['order_no'=>$order_no])->find();
        if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
            try{
                Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
                if ($order['type']==2){
                    /*用户加积分*/
                    \db('users')->where(['id'=>$order['users_id']])->setInc('fee',$order['price']);
                    /*记录信息*/
                    $log_insert['user_id']=$order['users_id'];
                    $log_insert['fee']=$order['price'];
                    $log_insert['type']=1;
                    $log_insert['add_time']=time();
                    \db('fee_log')->insert($log_insert);
                }
            }catch (\Exception $exception){
                $this->apiResponse(301,'error:'.$exception->getMessage());
            }
            return "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>";

        }
    }

    //组合支付回调
    public function group(){
        $param = $this->request->param();
        if ($param == null) {
            $param = file_get_contents("php://input");
            if ($param == null) {
                $param = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        $wxPay=new WeixinPay();
        $data = $wxPay->xmlToArray($param);
        $Sign = $data['sign'];
        //支付成功回调后变更订单状态
        $mySign = $wxPay->getSign($data);
        $order_no =substr($data['out_trade_no'], 0, -4);
        $order=\db('order')->where(['order_no'=>$order_no])->find();
        if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
            try{
                Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
                Db::name('fee_log')->insert(['user_id'=>$order['users_id'],'type'=>2,'fee'=>($order['price']-$order['group_money'])]);
                Db::name('users')->where('id',$order['users_id'])->setDec('fee',($order['price']-$order['group_money']));
            }catch (\Exception $exception){
                $this->apiResponse(301,'error:'.$exception->getMessage());
            }
            return "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>";
        }
    }

    function create_noncestr($length = 4){
        $chars = "0123456789";
        $str = "";
        for($i=0;$i<$length;$i++){
            $str.= substr($chars,mt_rand(0,strlen($chars)-1),1);
        }
        return $str;
    }

    /**
     * 计算两个时间戳戳相差时间
     * @param $begin_time
     * @param $end_time
     * @return array
     */
    public function timediff($begin_time,$end_time)
    {
        if ($begin_time < $end_time) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        //计算小时数
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        //计算分钟数
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        //计算秒数
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
        return $res;
    }
    /**
     * 计算出不同收费时间段的使用时间
     * @param $start  开始时间
     * @param $end    结束时间
     * @param $first  早上计时点
     * @param $second  晚上计时点
     * @return mixed
     */
    public function getTimeOneDay($start,$end,$first,$second){
        $start_date=date('Y-m-d',$start);
        $first=$start_date.' '.$first;
        $first=strtotime($first);
        $second=$start_date.' '.$second;
        $second=strtotime($second);
//        dump($start);
//        dump($end);
//        dump($first);
//        dump($second);
        /*分为六种情况分别处理*/

        if ($end<=$first){
            $time['first']=$start-$end;
            $time['second']=0;
            return $time;
        }

        if ($start<=$first && $end<=$second && $end>$first){
            $time['first']=$first-$start;
            $time['second']=$end-$first;
            return $time;
        }

        if ($start<=$first && $end>$second){
            $time['first']=($first-$start)+($end-$second);
            $time['second']=$second-$first;
            return $time;
        }

        if ($start>$first && $start<=$second  && $end<=$second){
            $time['first']='0';
            $time['second']=$end-$start;
            return $time;
        }

        if ($start>$first && $start<=$second && $end>$second ){
            $time['first']=$second-$start;
            $time['second']=$end-$second;
            return $time;
        }

        if ($start>$second && $end>$second){
            $time['first']=$end-$start;
            $time['second']=0;
            return $time;
        }

    }





}