Order.php 33.7 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 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/13
 * Time: 11:35
 */

namespace app\api\controller;


use app\common\controller\Api;
use RongCloud\RongCloud;
use think\Db;
use think\Log;
use think\Validate;
use wxapp\pay\WeixinPay;
use wxapp\pay\WeixinRefund;

/**
 * 创建订单/支付接口
 */
class Order extends Api
{
    /**
     * @ApiTitle    (创建订单)
     * @ApiSummary  (创建订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/create_order)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="teacher_id", type="int", required=true, description="老师ID")
     * @ApiParams   (name="territory_id", type="int", required=true, description="咨询领域ID")
     * @ApiParams   (name="is_use", type="int", required=true, description="是否适用优惠劵(0不使用,1使用)")
     * @ApiParams   (name="coupon_id", type="int", required=false, description="优惠劵ID")
     * @ApiParams   (name="money", type="string", required=false, description="金额")
     * @ApiParams   (name="business", type="string", required=false, description="所属行业")
     * @ApiParams   (name="address", type="string", required=false, description="经营地区/职能类别")
     * @ApiParams   (name="product", type="string", required=false, description="产品/职位等级")
     * @ApiParams   (name="scale", type="string", required=false, description="公司规模/下属人数")
     * @ApiParams   (name="content", type="string", required=false, description="问题描述")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        "order_id":"order_id",//订单id
        }
        })
     */
    public function create_order()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->getUserId();
        $validate = new Validate([
            'teacher_id' => 'require',
            'territory_id' => 'require',
            'is_use' => 'require',
            'money'=>'require',
            'business'=>'require',
            'address'=>'require',
            'product'=>'require',
            'scale'=>'require',
            'content'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        if($param['is_use'] == 1){
            if(empty($param['coupon_id'])){
                $this->error('优惠劵ID错误');
            }
            //查看下此订单时候的佣金比例 存入到该订单
            $yong = Db::name('teacher')
                ->where('id',$param['teacher_id'])
                ->find();
            $youhui = Db::name('coupon')->where('id',$param['coupon_id'])->find();
            if($youhui['is_use'] != 0){
                $this->error('优惠劵已过期或者已使用');
            }
            $param['commission'] = $yong['proportion'];
            $param['createtime'] = time();
            $param['num'] = get_order_sn();
            $data = Db::name('order')->insertGetId($param);
            if(empty($data)){
                $this->error('失败');
            }else{
                $this->success('success',['order_id'=>$data]);
            }
        }else{
            $yong = Db::name('commission')
                ->where('id',1)
                ->find();
            $param['commission'] = $yong['proportion'];
            $param['createtime'] = time();
            $param['num'] = get_order_sn();
            $data = Db::name('order')->insertGetId($param);
            $this->success('success',['order_id'=>$data]);
        }
    }

    /**
     * @ApiTitle    (订单详情)
     * @ApiSummary  (订单详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/orderdetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="order_id", type="int", required=true, description="订单ID")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
     "id"://订单id,
          "user_id"://学生ID
          "teacher_id"://老师ID
          "territory_id"://购买的擅长领域ID
          "is_use"://是否使用优惠劵(0不使用1使用
          "coupon_id"://优惠劵ID
          "money"://金额
          "status"://订单状态
          "num"://订单号
          "business"://所属行业
          "address"://经营地区/职能类别
          "product"://产品/职位等级
          "scale"://公司规模/下属人数
          "content"://问题描述
          "commission"://佣金比例
          "finish_status"://完成状态(1进行中2待总结3已总结4售后5已完成
          "order_status"://订单审核(1通过2未通过
          "is_summarize"://是否总结过(1是2否
          "is_complaint"://是否投诉(1是2否3已处理
          "is_chargeback"://是否退单(1是2否
          "expirationtime"://到期时间
          "paytime"://支付时间
          "chat_id"://融云群聊ID
          "chat_name"://群组名称
          "transaction_id"://微信支付订单号
    }
    })
     */
    public function orderdetail()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('order')->where('id',$order_id)->find();
        if(!empty($data['paytime'])){
            $data['paytime'] = date('Y-m-d H:i',$data['paytime']);
        }
        if(!empty($data['expirationtime'])){
            $data['expirationtime'] = date('Y-m-d H:i',$data['expirationtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (续约订单)
     * @ApiSummary  (续约订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/sonorder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="int", required=true, description="订单ID")
     * @ApiParams   (name="is_use", type="int", required=true, description="是否适用优惠劵(0不使用,1使用)")
     * @ApiParams   (name="coupon_id", type="int", required=false, description="优惠劵ID")
     * @ApiParams   (name="money", type="string", required=false, description="金额")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
        "order_id":"order_id",//子订单id
        }
        })
         */
    public function sonorder()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'order_id' => 'require',
            'is_use' => 'require',
            'money'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $teacher = Db::name('order')
            ->where('id',$param['order_id'])
            ->field('id,teacher_id')
            ->find();
        $yong = Db::name('teacher')
            ->where('id',$teacher['teacher_id'])
            ->find();
        $param['commission'] = $yong['proportion'];
        if($param['is_use'] == 1){
            if(empty($param['coupon_id'])){
                $this->error('优惠劵ID错误');
            }
            $youhui = Db::name('coupon')->where('id',$param['coupon_id'])->find();
            if($youhui['is_use'] != 0){
                $this->error('优惠劵已过期或者已使用');
            }
            $param['createtime'] = time();
            $param['num'] = get_order_sn();
            $data = Db::name('sonorder')->insertGetId($param);
            if(empty($data)){
                $this->error('失败');
            }else{
                $this->success('success',['order_id'=>$data]);
            }
        }else{
            $param['createtime'] = time();
            $param['num'] = get_order_sn();
            $data = Db::name('sonorder')->insertGetId($param);
            $this->success('success',['order_id'=>$data]);
        }
    }

    /**
     * @ApiTitle    (延长对话)
     * @ApiSummary  (延长对话)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/lengthen)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="int", required=true, description="订单ID")

     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        })
     */
    public function lengthen()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $res = Db::name('order')
            ->where('id',$order_id)
            ->setInc('expirationtime',86400);
        if(empty($res)){
            $this->error('失败');
        }else{
            $this->success('成功');
        }
    }


    /**
     * @ApiTitle    (续约规则)
     * @ApiSummary  (续约规则)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/rule)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            "content"://续约规则
        }
        })
     */
    public function rule()
    {
        $user_id = $this->getUserId();
        $data = Db::name('textrule')
            ->where('id',1)
            ->field('content')
            ->find();
        $this->success('success',$data['content']);

    }

    /**
     * @ApiTitle    (咨询信息)
     * @ApiSummary  (咨询信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/consult)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="int", required=true, description="订单ID")
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
              "business"://所属行业,
              "address"://经营地区/职能类别,
              "product"://产品/职位等级,
              "scale"://公司规模/下属人数,
              "content"://问题描述
              "type"://1经营管理2职业发展
        }
        })
     */
    public function consult()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('order')
            ->where('id',$order_id)
            ->find();

        $info = Db::name('territory')
            ->alias('a')
            ->join('contype b','a.contype_id = b.id')
            ->where('a.id',$data['territory_id'])
            ->field('a.id,a.contype_id,b.type,b.title')
            ->find();
        $list['business'] = $data['business'];
        $list['address'] = $data['address'];
        $list['product'] = $data['product'];
        $list['scale'] = $data['scale'];
        $list['content'] = $data['content'];
        $list['type'] = $info['type'];
        $this->success('success',$list);
    }


    /**
     * @ApiTitle    (续约订单支付)
     * @ApiSummary  (续约订单支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/sonpay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="续约订单id")
     *
     */
    public function sonpay()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $order = Db::name('sonorder')->where('id',$order_id)->find();
        if(empty($order)){
            $this->error('查询为空');
        }
        if($order['status'] != 1){
            $this->error('订单状态不合法');
        }
        //微信支付
        $openid = $this->getOpenId();
        $pay = new WeixinPay();
        $this->success('SUCCESS',$pay->pay($openid,$order['num'],"小微问问",$order['money'],url('api/pay/sonnotify','','',true)));
    }

    /**
     * @ApiTitle    (支付)
     * @ApiSummary  (支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     */
    public function pay()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $order = Db::name('order')->where(['id'=>$order_id,'user_id'=>$user_id])->find();
        if(empty($order)){
            $this->error('查询为空');
        }
        if($order['status'] != 1){
            $this->error('订单状态不合法');
        }
        //微信支付
        $openid = $this->getOpenId();
        $pay = new WeixinPay();
        $this->success('SUCCESS',$pay->pay($openid,$order['num'],"小微问问",$order['money'],url('api/pay/notify','','',true)));
    }

    /**
     * @ApiTitle    (学生身份我的订单列表)
     * @ApiSummary  (学生身份订单列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/orderList)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type", type="int", required=false, description="类型(如果为空或者为1是进行中 2是已完成3是售后4待总结5已总结)")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            "id": //订单id,
            "num"://订单号,
            "content"://问题描述
            "expirationtime"://到期时间
            "teacher_id"://老师ID
            "name"://老师名称
            "thumbnail"://头像
            "finish_status"://完成状态1=进行中2=待总结3=已总结4=售后5=已完成
            "is_complaint"://是否被投诉(1是2否3已处理
            "is_comment"://是否评价过(1是2否
            "right_status"://学生售后订单中的状态(1已处理2未处理
            "chat_id"://房间id
            "user"用户:{
                "id"://融云id
                "token"://融云token
            },
            "teacher"老师:{
                "id"://融云id
                "token"://融云token
            }
          "xian":是否显示投诉按钮(1显示2不显示
        }
        })
     */
    public function orderList()
    {
        $user_id = $this->getUserId();
        $type = $this->request->param('type');
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
//        类型(如果为空或者为1是进行中 2是已完成3是售后4待总结5已总结
        if($type == '' || $type == 1){
            $data = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.user_id',$user_id)
                ->where('a.finish_status',1)
                ->field('a.*,b.name,b.thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }elseif ($type == 2){
            $data = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.finish_status',5)
                ->where('a.user_id',$user_id)
                ->field('a.*,b.name,b.thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $seventime = $v['expirationtime']+14*86400;
                if($seventime>time()){
                    $v['xian'] = 1;
                }else{
                    $v['xian'] = 2;
                }
            }
        }elseif ($type == 3){
            $data = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.finish_status',4)
//                ->where('a.is_complaint',['=',1],['=',3],'or')
                ->where('a.user_id',$user_id)
                ->field('a.*,b.name,b.thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $detail = Db::name('order')->where('id',$v['id'])->find();
                if($detail['order_status'] == 0){
                    $v['right_status'] = 2;
                }else{
                    $v['right_status'] = 1;
                }
                // 融云账号信息
                $v['user'] = $v['teacher'] = [
                    'id' => '',
                    'token' => ''
                ];
            }
        }elseif ($type == 4){
            $data = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.user_id',$user_id)
                ->where('a.finish_status',2)
                ->field('a.*,b.name,b.thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $seventime = $v['expirationtime']+14*86400;
                if($seventime>time()){
                    $v['xian'] = 1;
                }else{
                    $v['xian'] = 2;
                }
            }
        }elseif ($type == 5){
            $data = Db::name('order')
                ->alias('a')
                ->join('teacher b','a.teacher_id = b.id')
                ->where('a.user_id',$user_id)
                ->where('a.finish_status',3)
                ->field('a.*,b.name,b.thumbnail')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
            foreach ($data as &$v){
                $seventime = $v['expirationtime']+14*86400;
                if($seventime>time()){
                    $v['xian'] = 1;
                }else{
                    $v['xian'] = 2;
                }
            }
        }
        else{
            $this->error('类型有误');
        }
        foreach ($data as &$v){
            $http = get_addon_config('qiniu')['cdnurl'];
            $v['thumbnail'] = $http.$v['thumbnail'];
            $is_have = Db::name('comment')
                ->where('order_id',$v['id'])
                ->find();
            if(empty($is_have)){
                $v['is_comment'] = 2;
            }else{
                $v['is_comment'] = 1;
            }
            $v['expirationtime'] = date('Y-m-d H:i',$v['expirationtime']);
            if($v['status'] == 2) {
                $v['user'] = [
                    'id' => $this->user['rongyun_id'],
                    'token' => $this->user['rongyun_token']
                ];
                $teacher_id = Db::name('teacher')->where('id',$v['teacher_id'])->value('user_id');
                $teacher = Db::name('user')->field('rongyun_id,rongyun_token')->where('id',$teacher_id)->find();
                $v['teacher'] = [
                    'id' => $teacher['rongyun_id'],
                    'token' => $teacher['rongyun_token']
                ];
            }
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (学生身份全部订单列表)
     * @ApiSummary  (学生身份订单列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/quan)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
    "id": //订单id,
    "num"://订单号,
    "content"://问题描述
    "expirationtime"://到期时间
    "teacher_id"://老师ID
    "name"://老师名称
    "thumbnail"://头像
    "finish_status"://完成状态(完成状态1=进行中2=待总结3=已总结4=售后5=已完成
    "is_complaint"://是否被投诉(1是2否3已处理
    "is_comment"://是否评价过(1是2否
    "chat_id"://房间id
    "user"用户:{
    "id"://融云id
    "token"://融云token
    },
    "teacher"老师:{
    "id"://融云id
    "token"://融云token
    }
    }
    })
     */
    public function quan(){
        $user_id = $this->getUserId();
        $data = Db::name('order')
            ->alias('a')
            ->join('teacher b','a.teacher_id = b.id')
            ->where('a.user_id',$user_id)
            ->field('a.*,b.name,b.thumbnail')
            ->order('id desc')
            ->select();
        if(empty($data)){
            $this->success('暂无数据',$data);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (查看总结)
     * @ApiSummary  (查看总结)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/select)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="int", required=true, description="订单ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
            'id'://总结ID
            "background"://背景
            "questions"://问题
            "Research"://调研
            "analyze"://分析
            "conclusion"://结论
            "plana"://方案一
            "planb"://方案二
            "planc"://方案三
        }
        })
     */
    public function select()
    {
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('summarize')
            ->where('order_id',$order_id)
            ->field('order_id,createtime',true)
            ->find();
        $this->success('success',$data);
    }


    /**
     * @ApiTitle    (老师身份我的订单列表)
     * @ApiSummary  (老师身份订单列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/teacherorder)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="type", type="int", required=false, description="类型(如果为空或者为1是进行中2待总结3是结算中4已完成5退款/投诉)")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571492001",
        "data": {
        "id": //订单id,
        "num"://订单号,
        "content"://问题描述
        "expirationtime"://到期时间
        "teacher_id"://老师ID
        "nickname"://学生名称
        "avatar"://学生头像
        "finish_status"://完成状态(完成状态1=进行中2=待总结3=已总结4=售后5=已完成
        "is_complaint"://是否被投诉(1是2否3已处理
        "is_comment"://是否评价过(1是2否
        "is_chargeback"://是否退单(1已退款2未申请退款
        "order_status"://订单审核状态(0待审核1通过2未通过
        "nopass"://未通过的理由
        "chat_id"://房间id
        "user"用户:{
            "id"://融云id
            "token"://融云token
        },
        "teacher"老师:{
            "id"://融云id
            "token"://融云token
        }
        }
        })
         */
    public function teacherorder()
    {
        $user_id = $this->getUserId();
        $teacher_id = Db::name('teacher')->where('user_id',$user_id)->find();
        $type = $this->request->param('type');
        $page = $this->request->param('page', 1, 'intval');
        $pageNum = $this->request->param('pageNum', 10, 'intval');
        if($type == '' || $type == 1){
            //进行中
            $data = Db::name('order')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.teacher_id',$teacher_id['id'])
                ->where('a.finish_status',1)
                ->field('a.*,b.nickname,b.avatar')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }elseif ($type == 2){
            //待总结
            $data = Db::name('order')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.finish_status',2)
                ->where('a.teacher_id',$teacher_id['id'])
                ->field('a.*,b.nickname,b.avatar')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }elseif ($type == 3){
            //结算中
            $data = Db::name('order')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.finish_status',3)
                ->where('a.teacher_id',$teacher_id['id'])
                ->field('a.*,b.nickname,b.avatar')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }elseif ($type == 4){
            //已完成
            $data = Db::name('order')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.finish_status',5)
                ->where('a.teacher_id',$teacher_id['id'])
                ->field('a.*,b.nickname,b.avatar')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }elseif ($type == 5 ){
            //退款/投诉
            $data = Db::name('order')
                ->alias('a')
                ->join('user b','a.user_id = b.id')
                ->where('a.finish_status',4)
//                ->where('a.is_complaint',['=',1],['=',3],'or')
                ->where('a.teacher_id',$teacher_id['id'])
                ->field('a.*,b.nickname,b.avatar')
                ->order('id desc')
                ->page($page,$pageNum)
                ->select();
        }else{
            $this->error('类型有误');
        }
        foreach ($data as &$v){
            $is_have = Db::name('comment')
                ->where('order_id',$v['id'])
                ->find();
            if(empty($is_have)){
                $v['is_comment'] = 2;
            }else{
                $v['is_comment'] = 1;
            }
            $v['expirationtime'] = date('Y-m-d H:i',$v['expirationtime']);
            if($v['status'] == 2) {
                $v['teacher'] = [
                    'id' => $this->user['rongyun_id'],
                    'token' => $this->user['rongyun_token']
                ];
                $user = Db::name('user')->field('rongyun_id,rongyun_token')->where('id',$v['user_id'])->find();
                $v['user'] = [
                    'id' => $user['rongyun_id'],
                    'token' => $user['rongyun_token']
                ];
            }
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (佣金明细中的金额)
     * @ApiSummary  (佣金明细中的金额)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/balance)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    "id"://id
    "balance"://余额
    }
    })
     */
    public function balance()
    {
        $user_id = $this->getUserId();
        $teacher = Db::name('teacher')
            ->where('user_id',$user_id)
            ->field('id,balance')
            ->find();
        if(empty($teacher)){
            $this->error('没有该老师');
        }
        $this->success('success',$teacher);
    }


    /**
     * @ApiTitle    (提现)
     * @ApiSummary  (提现)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/withdraw)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="teacher_id", type="int", required=true, description="老师ID")
     * @ApiParams   (name="money", type="int", required=true, description="提现金额")
     * @ApiParams   (name="card", type="string", required=true, description="卡号")
     * @ApiParams   (name="bank", type="string", required=true, description="开户行")
     * @ApiParams   (name="name", type="string", required=true, description="真实姓名")
     * @ApiParams   (name="phone", type="string", required=true, description="手机号")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    "id"://id
    }
    })
     */
    public function withdraw()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->getUserId();
        $validate = new Validate([
            'teacher_id' => 'require',
            'money'=>'require',
            'card' => 'require',
            'bank' => 'require',
            'name' => 'require',
            'phone'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        //查询提现金额是否满足后台设置允许提现额度
        $money = Db::name('allow')
            ->where('id',1)
            ->find();
        //查询老师余额是否跟提现的金额匹配
        $teacher = Db::name('teacher')
            ->where('id',$param['teacher_id'])
            ->field('id,balance')
            ->find();
        if($param['money'] < $money['money'] || $param['money'] > $teacher['balance']){
            $this->error('提现金额有误,请重新选择');
        }

        //加入佣金明细
        $record['createtime'] = time();
        $record['type'] = 2;
        $record['user_id'] = $param['user_id'];
        $record['teacher_id'] = $param['teacher_id'];
        $record['money'] = $param['money'];
        Db::name('record')->insertGetId($record);

        $param['createtime'] = time();
        Db::startTrans();
        $data = Db::name('withdraw')
            ->insertGetId($param);
        //提现成功将对应的老师余额修改
        $res = Db::name('teacher')->where('id',$param['teacher_id'])->update(['balance'=>$teacher['balance']-$param['money'],'withdraw'=>$param['money']]);
        if($data && $res){
            Db::commit();
            $this->success('成功');
        }else{
            Db::rollback();
            $this->error('失败');
        }
    }

    /**
     * @ApiTitle    (收益记录列表)
     * @ApiSummary  (收益记录列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/record)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
    "code": 1,
    "msg": "成功",
    "time": "1571492001",
    "data": {
        "id": //id,
        "teacher_id"://老师ID,
        "user_id"://用户ID
        "type"://类型(1订单结算2提现审核中3提现已结算
        "money"://金额
        "order_num"://订单号
        "createtime"://创建时间
    }
    })
     */
    public function record()
    {
        $user_id = $this->getUserId();
        $teacher = Db::name('teacher')
            ->where('user_id',$user_id)
            ->find();
        $data = Db::name('record')
            ->where('teacher_id',$teacher['id'])
            ->order('createtime desc')
            ->select();
        foreach ($data as &$v){
            $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (老师退单)
     * @ApiSummary  (老师退单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/order/chargeback)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="int", required=true, description="订单金额ID")
     * @ApiReturn({
    "code": 1,
    "msg": "SUCCESS",
    "time": "1553839125",
    "data": {
    }
    })
     */
    public function chargeback()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new Validate([
            'order_id' => 'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $order = Db::name('order')
            ->where('id',$param['order_id'])
            ->find();
        if(empty($order)){
            $this->error('查询为空');
        }
        if($order['status'] != 2 || $order['finish_status'] != 1){
            $this->error('非法的订单状态');
        }
        if($order['expirationtime'] < time()) {
            $this->error('该订单已到期');
        }
        $pay = new WeixinRefund();
        $openid = Db::name('third')->where('user_id',$order['user_id'])->find();
        $result = $pay->refund($openid['openid'],$order['num'],$order['money'],get_order_sn(),$order['money']);
        if($result['return_code'] == 'FAIL'){
            $this->error($result['return_msg']);
        }else{
            if($result['result_code'] == 'FAIL'){
                $this->error($result['err_code_des']);
            }else{
                Db::name('order')->where('id',$param['order_id'])->update(['status'=>3,'finish_status'=>3,'is_chargeback'=>1]);
                // 群组禁言
                $return = rongyun_ban($order);
                Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . $order['chat_id'] . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');

                $this->success('success');
            }
        }
    }

}