ThirdsendController.php 47.2 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 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;


use api\portal\model\AgencyModel;
use api\portal\model\OrderModel;
use api\portal\model\RefundModel;
use api\portal\model\UserNeedModel;
use cmf\controller\HomeBaseController;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;
use think\Loader;
use think\Config;
use think\captcha\Captcha;
use think\Url;
use SmsDemo;
//use think\Route;
/**
 * @title 订单修改
 * @description 接口说明
 * @group 接口分组
 */

class ThirdsendController extends HomeBaseController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    /**
     *  API返回信息格式函数 ;0失败,1成功,-1需要登录
     * @param string $code
     * @param string $message
     * @param array $data
     */
    public function apiResponse($code = '0', $message = '',$data = array(),$nums =0){
        header('Access-Control-Allow-Origin: *');
        header('Content-Type:application/json; charset=utf-8');
        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'=>$data,
            'nums'=>''.$nums
        );
        die(json_encode($result,JSON_UNESCAPED_UNICODE));
    }



//    查询中介报价(暂未用)
//  $need_id 需求id
    public function Offer($user_id,$need_id){
        $where_agency['status'] = 2;
        $where_agency['create_time'] = array('lt',time());
        $where_agency['need_id'] = $need_id;
        $agency_list = Db::name('Agency')
            ->where($where_agency)
//            ->field("id,housing_price,assessment_tax,security_tax,else_tax,service_charge")
            ->select()->toArray();
        if($agency_list){
            foreach ($agency_list as $agencyk=>$agencyv){
//                $final_list[$agencyk] = $agencyv;
//                $final_list[$agencyk]['agency_id'] = $agencyv['id'];
//                $final_list[$agencyk]['user_id'] = $user_id;
                $money = $agencyv['housing_price']*10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'] ;
                $money_arr[] = $money;
                $fianlMoney = substr($money, 0,-4);
                $agency_list[$agencyk]['true_money'] = $money;
//                $final_list[$agencyk]['money'] = $fianlMoney;
            }
            sort($money_arr);
            if(count($money_arr) > 3 ){
                foreach ($agency_list as $finalk=>$finalv){
                    foreach ($money_arr as $monryk=>$moneyv){
                        if($finalv['true_money'] == $moneyv){
                            $return[] =  $finalv;
                        }
                    }
                }
            }else{
                $save['status'] = 8;
            }

            return $return;
        }
    }

    public function isLogin($token){
        $where_member['token'] = $token;
//        $where_member['status'] = 1;
        $member = Db::name('Member')->where($where_member)->field('id,status')->find();
        if($member){
            if($member['status'] == 9){
                $this->apiResponse('0','您已被拉入黑名单');
            }else{
                return $member;
            }
        }else{
            $this->apiResponse('-1','请重新登录');
        }
    }



    /**
     * @title  30分钟后发布(sure)(时间为0时调用)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Thirdsend/thirdChange
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:need_id type:int require:1 default: other: desc:需求id
     */
//    30分钟后发布(sure)(时间为0时调用)
//    need_id   需求id
    public function thirdChange(){
        Db::startTrans();
        $data = $this->request->param();
        $this->isLogin($data['token']);
        if(empty($data['need_id'])){
            $this->apiResponse('0','需求id不能为空');
        }
        $where_need['status'] = 2;
        $where_need['id'] = $data['need_id'];
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,pay_time')->find();
        $time = time();
        if($time < $userNeedList['pay_time']+1800){
            $this->apiResponse('0','未到发布时间');
        }else{
//            查询父级


            $where_save['id'] = $data['need_id'];
            $save['status'] = 3;
            $save['update_time'] = time();
            $fianl_save = Db::name('UserNeed')->where($where_save)->update($save);
            if($fianl_save){
                Db::commit();
                $where_return['id'] = $data['need_id'];
                $return = Db::name('UserNeed')->where($where_return)->find();
                $this->apiResponse('1','成功',$return);
            }else{
                Db::rollback();
                $this->apiResponse('0','失败');
            }
        }
    }


    /**
     * @title  规定时间结束报价(sure)(时间为0时调用)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Thirdsend/timeOver
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:need_id type:int require:1 default: other: desc:需求id
     */
//    规定时间结束报价(sure)(时间为0时调用)
//      need_id 需求id
    public function timeOver()
    {
        Db::startTrans();
        $data = $this->request->param();
        $this->isLogin($data['token']);
        if(empty($data['need_id'])){
            $this->apiResponse('0','需求id不能为空');
        }
//        查询用户发布需求信息
        $where_need['status'] = 3;
        $where_need['id'] = $data['need_id'];
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time,pay_time,user_id,pay_money')->find();
        if(!$userNeedList){
            $this->apiResponse('0','需求状态不正确');
        }
        $userNeedList['user_type'] = Db::name('Member')->where(['id'=>$userNeedList['user_id']])->value('type');
        $time = time();
//        查询相应报价
        $final_time = $userNeedList['pay_time'] + $userNeedList['quotation_time'] * 86400;
//            判断时间是否正确
        if ($time >= $final_time) {
//                $us_middleid = $uNeedv['id'];
            $where_agency['a.status'] = 2;
            $where_agency['a.pay_time'] = array('lt', time());
            $where_agency['a.need_id'] = $userNeedList['id'];
            $where_agency['m.type'] = 2;
            $where_agency['m.status'] = 1;
            $agency_list = Db::name('Agency')->alias('a')
                ->where($where_agency)
                ->join("hp_Member m", 'm.id = a.agency_id')
                ->field('a.*,m.company,m.type as user_type')
                ->select()->toArray();
//                查询品牌企业
            $where_brand['status'] = 1;
            $band_list = Db::name('Brand')->where($where_brand)->column('name');
            $agen_arr = $is_trueMoney = array();
            if ($agency_list) {
                $get_list = array();
//                    删除十万以内报价
                foreach ($agency_list as $tenk=>$tenv){
                    $ten_money = $tenv['housing_price'] * 10000 + $tenv['assessment_tax'] + $tenv['security_tax'] + $tenv['else_tax'] + $tenv['service_charge'];
                    if($ten_money > 100000){
                        $is_trueMoney[] = $tenv;
                    }
                }
                //        判断是否超过三个报价人
                if (count($is_trueMoney) >= 3) {
//                    查询三人中是否有品牌中介
//                        循环匹配品牌企业
                    foreach ($is_trueMoney as $agencyk => $agencyv) {
                        foreach ($band_list as $brak => $brav) {
//                            判断是否在品牌企业中
                            if (strstr($brav, $agencyv['company']) !== false) {
//                                存在
                                $agen_money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                                if (empty($agen_arr)) {
                                    $agen_arr[0]['money'] = $agen_money;
                                    $agen_arr[0]['id'] = $agencyv['id'];
                                } else {
//                                    判断大小
                                    if ($agen_money < $agen_arr[0]['money']) {
                                        $agen_arr[0]['money'] = $agen_money;
                                        $agen_arr[0]['id'] = $agencyv['id'];

                                    }
                                }
                            }
                        }
//                        取出前三报价
                        $money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                        $money_list[$agencyv['id']] = $money;
                    }
                    asort($money_list);
                    $k = 0;
                    foreach ($money_list as $mok => $mov) {
                        $else_changeList[] = $mok;
                        if ($k < 3) {
                            $fin_list[$k]['id'] = $mok;
                            $fin_list[$k]['money'] = $mov;
                            $fin_arr[] = $mok;
                        }
                        $k += 1;
                    }
                    if($agen_arr){
                        if(!in_array($agen_arr[0]['id'],$fin_arr)){
//                            品牌中介不在取出三个最低价中
                            $middle_agen[0] = $agen_arr[0]['id'];
                            $get_list = array_merge($fin_arr,$middle_agen);
                        }else{
                            $get_list = $fin_arr;
                        }
                    }else{
                        $get_list = $fin_arr;
                    }
                    foreach ($else_changeList as $getk=>$getv){
                        if(in_array($getv,$get_list)){
                            $change_final['id'] = $getv;
                            $change_final['status'] = 4;
                        }else{
                            $change_final['id'] = $getv;
                            $change_final['status'] = 3;
                            // 中介报价未进入决赛,执行订单退款
                            $agencyInfo = Db::name('Agency')->alias('a')
                                ->where(['a.id'=>$getv])
                                ->join("hp_Member m", 'm.id = a.agency_id')
                                ->field('a.*,m.company,m.type as user_type')
                                ->find();
                            $refund_result = $this->agency_order_refund($agencyInfo,'中介报价未进入决赛圈退款',2);
                        }
                        $agencyModel = new  AgencyModel();
                        $fianl_AgsaveFirst = $agencyModel->isUpdate(true)->save($change_final);
                        if(!$fianl_AgsaveFirst){
                            Db::rollback();
                        }
                    }
//                        报价成功,更改状态
                    $userSave_statusFirst['id'] = $userNeedList['id'];
                    $userSave_statusFirst['status'] = 5;
                    $userSave_statusFirst['choose_time'] = time();
                    $userNeedModel = new UserNeedModel();
                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_statusFirst);
                    if(!$fianl_Ussave){
                        Db::rollback();
                    }
                } else {
//                报价小于3人,更改订单状态,报价失败
                    foreach ($agency_list as $changeka=>$changeva){
                        $change_final['id'] = $changeva['id'];
                        $change_final['status'] = 7;
                        $agencyModel = new  AgencyModel();
                        $fianl_AgsaveNext = $agencyModel->isUpdate(true)->save($change_final);

                        if(!$fianl_AgsaveNext){
                            Db::rollback();
                        }
                        // 中介报价订单退款
                        $refund_result = $this->agency_order_refund($changeva,'中介报价少于3人退款',1);
                    }
                    $userSave_status['id'] = $userNeedList['id'];
                    $userSave_status['status'] = 4;
                    $userNeedModel = new UserNeedModel();
                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                    if(!$fianl_Ussave){
                        Db::rollback();
                    }

                    // 用户需求订单退款
                    $refund_result = $this->need_order_refund($userNeedList,'报价人数不足',1);
                }
            } else {
//                没有报价
                $userSave_status['id'] = $userNeedList['id'];
                $userSave_status['status'] = 4;
                $userNeedModel = new UserNeedModel();
                $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                if(!$fianl_Ussave){
                    Db::rollback();
                }
                // 用户需求订单退款
                $refund_result = $this->need_order_refund($userNeedList,'报价人数不足',1);
            }
//            插入数据库
//            用户需求表
//            中介报价表
            Db::commit();
            $where_return['id'] = $data['need_id'];
            $return = Db::name('UserNeed')->where($where_return)->find();
            $this->apiResponse('1','成功',$return);
        }else{
            $this->apiResponse('0','时间不正确');
        }
//        }
    }

    /**
     * @title  三天用户选择(sure)(时间为0时调用)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Thirdsend/choose
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:need_id type:int require:1 default: other: desc:需求id
     */
//    三天用户选择(sure)(时间为0时调用)
//      need_id 需求id
    public function choose(){
        Db::startTrans();
        $data = $this->request->param();
        $this->isLogin($data['token']);
        if(empty($data['need_id'])){
            $this->apiResponse('0','需求id不能为空');
        }
//        查询用户发布需求信息
        $where_need['status'] = 5;
        $where_need['id'] = $data['need_id'];
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,choose_time,user_id,pay_money,choose_time')->find();
        if($userNeedList){
//            foreach ($userNeedList as $needk=>$needv) {
            if (time() >= ($userNeedList['choose_time'] + 259200)) {
                $where_agency['a.need_id'] = $userNeedList['id'];
                $where_agency['a.status'] = 4;
                $agency_list = Db::name('Agency')->alias('a')
                    ->where($where_agency)
                    ->join("hp_Member m", 'm.id = a.agency_id')
                    ->field('a.*,m.company,m.type as user_type')
                    ->select()->toArray();
                if ($agency_list) {
                    $where_agencyUpdate['need_id'] = $userNeedList['id'];
                    $where_agencyUpdate['status'] = 4;
                    $agency_save['status'] = 8;
                    $agency_save['update_time'] = time();
                    $agency_result = Db::name('Agency')->where($where_agencyUpdate)->update($agency_save);
                    if (!$agency_result) {
                        Db::rollback();
                    }
                }
                $where_needUpdate['id'] = $userNeedList['id'];
                $needUpdate['status'] = 6;
                $need_result = Db::name('UserNeed')->where($where_needUpdate)->update($needUpdate);
                if (!$need_result) {
                    Db::rollback();
                } else {
                    if ($agency_list) {
                        foreach ($agency_list as $agency_key => $agency_val) {
                            // 中介报价订单退款
                            $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                        }
                    }
                }
            }
//            }
            Db::commit();
            $where_return['id'] = $data['need_id'];
            $return = Db::name('UserNeed')->where($where_return)->find();
            $this->apiResponse('1','成功',$return);
        }else{
            $this->apiResponse('0','需求错误');
        }
    }

    /**
     * @title  三天中介确认(时间为0时调用)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Thirdsend/threeChoose
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:need_id type:int require:1 default: other: desc:需求id
     */
//    三天中介确认(时间为0时调用)
//      need_id 需求id
    public function threeChoose(){
        Db::startTrans();
        $data = $this->request->param();
        $this->isLogin($data['token']);
        if(empty($data['need_id'])){
            $this->apiResponse('0','需求id不能为空');
        }
        $where_need['status'] = 7;
        $where_need['id'] = $data['need_id'];
        $need_list = Db::name('UserNeed')->where($where_need)->field('id,choose_time')->find();
        if($need_list){
//            foreach ($need_list as $needk=>$needv){
            if(time() >= ($need_list['choose_time']+259200)){
                $where_agency['status'] = 5;
                $where_agency['need_id'] = $need_list['id'];
                $agency = Db::name('Agency')->where($where_agency)->find();
//                    修改中介报价状态
                if($agency){
                    $where_changeAgency['id'] = $agency['id'];
                    $changeAgency['status'] = 10;
                    $changeAgency['update_time'] = time();
                    $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                    if(!$update_a){
                        Db::rollback();
                    }
                }
//                    修改用户需求状态
                $where_needChange['id'] = $need_list['id'];
                $update_need['status'] = 8;
                $update_need['update_time'] = time();
                $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                if (!$change_needFinal) {
                    Db::rollback();
                }
                $need_list['user_type'] = Db::name('Member')->where(['id'=>$need_list['user_id']])->value('type');
                $this->need_order_refund($agency,'中介未确认成交退款',5);
            }
//            }
            Db::commit();
            $where_return['id'] = $data['need_id'];
            $return = Db::name('UserNeed')->where($where_return)->find();
            $this->apiResponse('1','成功',$return);
        }else{
            $this->apiResponse('0','需求错误');
        }
    }





//    30分钟后发布(sure)(分钟执行)
    public function thirdSend(){
        Db::startTrans();
        $where_need['status'] = 2;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,pay_time')->select()->toArray();
        $time = time();
        $change_arr = array();
        foreach ($userNeedList as $userk=>$userv){
            if($time >= $userv['pay_time']+1800){
                $change_arr[] = $userv['id'];
            }
        }
        if($change_arr){
            $where_save['id'] = array('in',$change_arr);
            $save['status'] = 3;
            $save['update_time'] = time();
            $fianl_save = Db::name('UserNeed')->where($where_save)->update($save);
            if($fianl_save){
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

//    规定时间结束报价(sure)(分钟执行)
    public function over()
    {
        Db::startTrans();
//        查询用户发布需求信息
        $where_need['status'] = 3;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time,pay_time,user_id')->select()->toArray();
        $userNeedList['user_type'] = Db::name('Member')->where(['id'=>$userNeedList['user_id']])->value('type');
        $time = time();
//        循环需求,查询相应报价
        foreach ($userNeedList as $uNeedk => $uNeedv) {
            $final_time = $uNeedv['pay_time'] + $uNeedv['quotation_time'] * 86400;
            if ($time >= $final_time) {
                $us_middleid = $uNeedv['id'];
                $where_agency['a.status'] = 2;
                $where_agency['a.pay_time'] = array('lt', time());
                $where_agency['a.need_id'] = $uNeedv['id'];
                $where_agency['m.type'] = 2;
                $where_agency['m.status'] = 1;
                $agency_list = Db::name('Agency')->alias('a')
                    ->where($where_agency)
                    ->join("hp_Member m", 'm.id = a.agency_id')
                    ->field('a.*,m.company')
                    ->select()->toArray();
//                查询品牌企业
                $where_brand['status'] = 1;
                $band_list = Db::name('Brand')->where($where_brand)->column('name');
                $agen_arr = array();
                $money_arr = array();
                if ($agency_list) {
//                    删除十万以内报价
                    foreach ($agency_list as $tenk=>$tenv){
                        $ten_money = $tenv['housing_price'] * 10000 + $tenv['assessment_tax'] + $tenv['security_tax'] + $tenv['else_tax'] + $tenv['service_charge'];
                        if($ten_money > 100000){
                            $is_trueMoney[] = $tenv;
                        }
                    }


                    //        判断是否超过三个报价人
                    if (count($is_trueMoney) >= 3) {

//                    查询三人中是否有品牌中介
//                        循环匹配品牌企业
                        foreach ($is_trueMoney as $agencyk => $agencyv) {
                            foreach ($band_list as $brak => $brav) {
//                            判断是否在品牌企业中
                                if (strstr($brav, $agencyv['company']) !== false) {
//                                存在
                                    $agen_money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                                    if (empty($agen_arr)) {
                                        $agen_arr[0]['money'] = $agen_money;
                                        $agen_arr[0]['id'] = $agencyv['id'];
                                    } else {
//                                    判断大小
                                        if ($agen_money < $agen_arr[0]['money']) {
                                            $agen_arr[0]['money'] = $agen_money;
                                            $agen_arr[0]['id'] = $agencyv['id'];

                                        }
                                    }
                                }
                            }
//                        取出前三报价
                            $money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                            $money_list[$agencyv['id']] = $money;
                        }
                        asort($money_list);
                        $k = 0;
                        foreach ($money_list as $mok => $mov) {
                            $else_changeList[] = $mok;
                            if ($k < 3) {
                                $fin_list[$k]['id'] = $mok;
                                $fin_list[$k]['money'] = $mov;
                                $fin_arr[] = $mok;
                            }
                            $k += 1;
                        }
                        if($agen_arr){
                            if(!in_array($agen_arr[0]['id'],$fin_arr)){
//                            品牌中介不在取出三个最低价中
                                $middle_agen[0] = $agen_arr[0]['id'];
                                $get_list = array_merge($fin_arr,$middle_agen);
                            }else{
                                $get_list = $fin_arr;
                            }
                        }else{
                            $get_list = $fin_arr;
                        }
                        foreach ($else_changeList as $getk=>$getv){
                            if(in_array($getv,$get_list)){
                                $change_final['id'] = $getv;
                                $change_final['status'] = 4;
                            }else{
                                $change_final['id'] = $getv;
                                $change_final['status'] = 3;
                                // 中介报价未进入决赛,执行订单退款
                                $agencyInfo = Db::name('Agency')->alias('a')
                                    ->where(['a.id'=>$getv])
                                    ->join("hp_Member m", 'm.id = a.agency_id')
                                    ->field('a.*,m.company,m.type as user_type')
                                    ->find();
                                $refund_result = $this->agency_order_refund($agencyInfo,'中介报价未进入决赛圈退款',2);
                            }
                            $agencyModel = new  AgencyModel();
                            $fianl_AgsaveFirst = $agencyModel->isUpdate(true)->save($change_final);
                            if(!$fianl_AgsaveFirst){
                                Db::rollback();
                            }
                        }
//                        报价成功,更改状态
                        $userSave_statusFirst['id'] = $uNeedv['id'];
                        $userSave_statusFirst['status'] = 5;
                        $userSave_statusFirst['choose_time'] = time();
                        $userNeedModel = new UserNeedModel();
                        $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_statusFirst);
                        if(!$fianl_Ussave){
                            Db::rollback();
                        }
                    } else {
//                报价小于3人,更改订单状态,报价失败
                        foreach ($agency_list as $changeka=>$changeva){
                            $change_final['id'] = $changeva['id'];
                            $change_final['status'] = 7;
                            $agencyModel = new  AgencyModel();
                            $fianl_AgsaveNext = $agencyModel->isUpdate(true)->save($change_final);
                            if(!$fianl_AgsaveNext){
                                Db::rollback();
                            }
                            // 中介报价订单退款
                            $refund_result = $this->agency_order_refund($changeva,'中介报价少于3人退款',1);

                        }
                        $userSave_status['id'] = $uNeedv['id'];
                        $userSave_status['status'] = 4;
                        $userNeedModel = new UserNeedModel();
                        $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                        if(!$fianl_Ussave){
                            Db::rollback();
                        }
                        // 用户需求订单退款
                        $refund_result = $this->need_order_refund($userNeedList,'报价人数不足',1);

                    }
                } else {
//                没有报价
                    $userSave_status['id'] = $uNeedv['id'];
                    $userSave_status['status'] = 4;
                    $userNeedModel = new UserNeedModel();
                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                    if(!$fianl_Ussave){
                        Db::rollback();
                    }
                    // 用户需求订单退款
                    $refund_result = $this->need_order_refund($userNeedList,'报价人数不足',1);

                }
//            插入数据库
//            用户需求表
//                $userNeedModel = new UserNeedModel();
//                foreach ($userSave_status as $fck=>$fcv){
//                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($fcv);
//                    if(!$fianl_Ussave){
//                        Db::rollback();
//                    }
//                }
//            中介报价表
                Db::commit();
            }

        }
    }

//    三天用户选择(sure)(天执行)
    public function userChoose(){
        Db::startTrans();
//        查询用户发布需求信息
        $where_need['status'] = 5;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,choose_time,user_id,pay_money,choose_time')->select()->toArray();
        if($userNeedList){
            foreach ($userNeedList as $needk=>$needv) {
                if (time() >= ($needv['choose_time'] + 259200)) {
                    $where_agency['need_id'] = $needv['id'];
                    $where_agency['status'] = 4;
                    $agency_list = Db::name('Agency')->alias('a')
                        ->where($where_agency)
                        ->join("hp_Member m", 'm.id = a.agency_id')
                        ->field('a.*,m.company,m.type as user_type')
                        ->select()->toArray();
                    if ($agency_list) {
                        $where_agencyUpdate['need_id'] = $needv['id'];
                        $where_agencyUpdate['status'] = 4;
                        $agency_save['status'] = 8;
                        $agency_save['update_time'] = time();
                        $agency_result = Db::name('Agency')->where($where_agencyUpdate)->update($agency_save);
                        if (!$agency_result) {
                            Db::rollback();
                        }
                    }
                    $where_needUpdate['id'] = $needv['id'];
                    $needUpdate['status'] = 6;
                    $need_result = Db::name('UserNeed')->where($where_needUpdate)->update($needUpdate);
                    if (!$need_result) {
                        Db::rollback();
                    } else {
                        if ($agency_list) {
                            foreach ($agency_list as $agency_key => $agency_val) {
                                // 中介报价订单退款
                                $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                            }
                        }
                    }
                }
            }
        }
        Db::commit();


    }


//    三天中介确认
    public function intermediaryChoose(){
        Db::startTrans();
        $where_need['status'] = 7;
        $need_list = Db::name('UserNeed')->where($where_need)->field('id,choose_time')->select()->toArray();
        if($need_list){
            foreach ($need_list as $needk=>$needv){
                if(time() >= ($needv['choose_time']+259200)){
                    $where_agency['status'] = 5;
                    $where_agency['need_id'] = $needv['id'];
                    $agency = Db::name('Agency')->where($where_agency)->find();
//                    修改中介报价状态
                    if($agency){
                        $where_changeAgency['id'] = $agency['id'];
                        $changeAgency['status'] = 10;
                        $changeAgency['update_time'] = time();
                        $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                        if(!$update_a){
                            Db::rollback();
                        }
                    }
//                    增加中介保证金变化
                    $add_a['ag_id'] = $agency['id'];
                    $add_a['user_id'] = $agency['agency_id'];
                    $add_a['title'] = '中介未确认';
                    $add_a['type'] = 2;
                    $add_a['money'] = $agency['deposit'];
                    $add_a['create_time'] = time();
                    $add_a['update_time'] = time();
                    $amoney_id = Db::name('AmoenyDetail')->insertGetId($add_a);
                    if (!$amoney_id) {
                        Db::rollback();
                    }
//                    修改用户需求状态
                    $where_needChange['id'] = $needv['id'];
                    $update_need['status'] = 8;
                    $update_need['update_time'] = time();
                    $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                    if (!$change_needFinal) {
                        Db::rollback();
                    }
                    $need_list['user_type'] = Db::name('Member')->where(['id'=>$need_list['user_id']])->value('type');
                    $this->need_order_refund($agency,'中介未确认成交退款',5);
                }
            }
            Db::commit();
        }
    }


    /**
     * 需求订单退款
     * @param needInfo 用户需求信息
     * @param reason 退款原因
     */
    public function need_order_refund($needInfo,$reason,$reason_id) {
        $order_model = new OrderModel();
        $where = [
            'user_id' => $needInfo['user_id'],
            'type' => 1,
            'need_id' => $needInfo['id'],
            'is_get' => 2,
            'status' => 2
        ];
        // 根据不同订单类型执行退款操作
        $orderInfo = $order_model->where($where)->find();
        $orderInfo['user_type'] = $needInfo['user_type'];
        $transaction_id = $orderInfo['transaction_id'];
        $order_sn = $orderInfo['order_sn'];
//        $money = $orderInfo['money'];
        $money = 0.01;
        $out_refund_no = cmf_get_order_sn();
        // 微信
        $refund_result = false;
        if($orderInfo['pay_type'] == 1 || $orderInfo['pay_type'] == 2) {
            $refund_result = $this->wxpay_refund($orderInfo, $transaction_id,$out_refund_no,$money*100,$money*100,$reason,$reason_id);
        }
        // 支付宝
        if($orderInfo['pay_type'] == 3 || $orderInfo['pay_type'] == 4) {
            $refund_result = $this->alipay_refund($orderInfo, $transaction_id,$order_sn,$money,$reason,$reason_id);
        }
        return $refund_result;
    }

    /**
     * 中介报价退款
     * @param agencyInfo 中介报价信息
     * @param reason 退款原因
     */
    public function agency_order_refund($agencyInfo,$reason,$reason_id) {
        $order_model = new OrderModel();
        $where = [
            'user_id' => $agencyInfo['agency_id'],
            'type' => 2,
            'need_id' => $agencyInfo['id'],
            'is_get' => 2,
            'status' => 2
        ];
        // 根据不同订单类型执行退款操作
        $orderInfo = $order_model->where($where)->find();
        $orderInfo['user_type'] = $agencyInfo['user_type'];
        $transaction_id = $orderInfo['transaction_id'];
        $order_sn = $orderInfo['order_sn'];
//        $money = $orderInfo['money'];
        $money = 0.01;
        $out_refund_no = cmf_get_order_sn();
        $refund_result = false;
        // 微信
        if($orderInfo['pay_type'] == 1 || $orderInfo['pay_type'] == 2) {
            $refund_result = $this->wxpay_refund($orderInfo, $transaction_id,$out_refund_no,$money*100,$money*100,$reason,$reason_id);
        }
        // 支付宝
        if($orderInfo['pay_type'] == 3 || $orderInfo['pay_type'] == 4) {
            $refund_result = $this->alipay_refund($orderInfo, $transaction_id,$order_sn,$money,$reason,$reason_id);
        }
        return $refund_result;
    }

    /**
     * 支付宝退款接口(两个订单号不能同时为空)
     * @param trade_no 支付宝交易号
     * @param out_trade_no 商户支付订单号
     * @param refund_amount 退款金额
     * @param refund_reason 退款原因
     * @param out_request_no 如需部分退款,传此参数
     */
    private function alipay_refund($orderInfo, $trade_no = null, $out_trade_no = null, $refund_amount, $refund_reason,$reason_id, $out_request_no = null) {
        $refund = controller('Alipay','controller');
        $data = [
            'trade_no' => $trade_no,
            'out_trade_no' => $out_trade_no,
            'refund_amount' => $refund_amount,
            'refund_reason' => $refund_reason,
            'out_request_no' => $out_request_no
        ];
        $result = $refund->refund($data);
        $refund_model = new RefundModel();
        if($result->code == '10000' && $result->msg == 'Success') {
            $insert = [
                'order_id' => $orderInfo['id'],
                'order_sn' => $result->out_trade_no,
                'user_id' => $orderInfo['user_id'],
                'user_type' => $orderInfo['user_type'],
                'refund_id' => $result->trade_no,
                'pay_type' => 2,
                'status' => 2,
                'reason' => $reason_id,
                'create_time' => time(),
                'update_time' => time(),
                'refund_time' => strtotime($result->gmt_refund_pay),
                'more' => json_encode($result)
            ];
            $refund_result = $refund_model->insertGetId($insert);
            if(!$refund_result) {
                return false;
            }
            return true;
        } else {
            $insert = [
                'order_id' => $orderInfo['id'],
                'user_id' => $orderInfo['user_id'],
                'user_type' => $orderInfo['user_type'],
                'pay_type' => 2,
                'status' => 4,
                'reason' => $reason_id,
                'create_time' => time(),
                'update_time' => time(),
                'refund_time' => time(),
                'more' => json_encode($result)
            ];
            $refund_model->insertGetId($insert);
            return false;
        }
    }

    /**
     * 支付宝退款查询接口(两个订单号不能同时为空)
     * @param trade_no 支付宝交易号
     * @param out_trade_no 商户支付订单号
     * @param refund_amount 退款金额
     * @param refund_reason 退款原因
     * @param out_request_no 如需部分退款,传此参数
     */
    private function alipay_refundquery($trade_no = null, $out_trade_no = null, $refund_amount, $refund_reason, $out_request_no = null) {
        $refund = controller('Alipay','controller');
        $data = [
            'trade_no' => $trade_no,
            'out_trade_no' => $out_trade_no,
            'refund_amount' => $refund_amount,
            'refund_reason' => $refund_reason,
            'out_request_no' => $out_request_no
        ];
        return $refund->refund($data);
    }

    /**
     * 微信退款接口(两个订单号不能同时为空)
     * @param orderInfo 支付订单信息
     * @param transaction_id 微信业务订单号
     * @param out_refund_no 商户退款订单号
     * @param total_fee 订单金额
     * @param refund_fee 退款金额
     * @param refund_desc 退款原因
     */
    private function wxpay_refund($orderInfo, $transaction_id = null, $out_refund_no = null, $total_fee, $refund_fee, $refund_desc,$reason_id) {
        require_once VENDOR_PATH.'WxpayAPI/WxRefund.php';
        $refund = new \WxRefund(Config::get('wx_appid'),Config::get('wx_mch_id'),Config::get('wx_key'),$transaction_id,
            $out_refund_no,$total_fee,$refund_fee,$refund_desc);
        $result = $refund->pay();
        if($result['return_code'] == 'SUCCESS' && $result['return_msg'] == 'OK') {
            $refund_model = new RefundModel();
//            $refund = $refund_model->where(['order_sn'=>$result['out_refund_no_0']])->find();
//            if(!empty($refund)) {
//                return $this->wxpay_refundquery($transaction_id, $out_refund_no, $out_refund_no, $refund['refund_id']);
//            }
            if($result['result_code'] == 'SUCCESS') {
                $insert = [
                    'order_id' => $orderInfo['id'],
                    'order_sn' => $result['out_refund_no'],
                    'user_id' => $orderInfo['user_id'],
                    'user_type' => $orderInfo['user_type'],
                    'refund_id' => $result['refund_id'],
                    'pay_type' => 1,
                    'reason' => $reason_id,
                    'create_time' => time(),
                    'update_time' => time()
                ];
                $refund_result = $refund_model->insertGetId($insert);
                if(!$refund_result) {
                    return false;
                }
            }
//            return $this->wxpay_refundquery($transaction_id, $out_refund_no, $result['out_refund_no'], $insert['refund_id']);
        } else {
            Db::name('Test')->insert(['data'=>$result['return_msg']]);
            return false;
        }
    }

    /**
     * 微信退款查询接口(四个订单号不能同时为空)
     * @param transaction_id 微信业务订单号
     * @param out_trade_no 商户退款订单号
     * @param out_refund_no 商户退款订单号
     * @param refund_id 微信退款单号
     */
    private function wxpay_refundquery($transaction_id = null, $out_trade_no = null, $out_refund_no = null, $refund_id = null) {
        require_once VENDOR_PATH.'WxpayAPI/WxRefundQuery.php';
        $refund = new \WxRefundQuery(Config::get('wx_appid'),Config::get('wx_mch_id'),Config::get('wx_key'),$transaction_id,
            $out_trade_no,$out_refund_no,$refund_id);
        $result = $refund->pay();
        $refund_model = new RefundModel();
        $refund = $refund_model->where(['refund_id'=>$result['refund_id_0']])->find();
        if($refund['status'] == 2) {
            return true;
        }
        if($result['return_code'] == 'SUCCESS' && $result['return_msg'] == 'OK') {
            Db::startTrans();
            $refund_success_time = 0;
            if(!empty($result['refund_success_time_0'])) {
                $refund_success_time = strtotime($result['refund_success_time_0']);
            }
            $refund_update = [
                'status' => 2,
                'update_time' => time(),
                'refund_time' => $refund_success_time
            ];
            $refund_result = $refund_model->where(['id'=>$refund['id']])->update($refund_update);
            if(!$refund_result) {
                Db::rollback();
                return false;
            }
            $order_model = new OrderModel();
            $order_update = [
                'status' => 4,
                'update_time' => time(),
                'refund_time' => $refund_success_time
            ];
            $order_result = $order_model->where(['id'=>$refund['order_id']])->update($order_update);
            if(!$order_result) {
                Db::rollback();
                return false;
            }
            Db::commit();
        } else {
            $refund_update = [
                'status' => 5,
                'update_time' => time(),
                'remark' => $result['return_msg']
            ];
            $refund_result = $refund_model->where(['id'=>$refund['id']])->update($refund_update);
            return false;
        }
    }





//    发布需求,匹配中介(暂未调用)
    public function sendNeed($need_id){
        header("Content-Type: text/html;charset=utf-8");
        $where_need['id'] = $need_id;
        $where_need['status'] = 3;
        $need_list = Db::name('UserNeed')->where($where_need)->field("id,city,quarters")->find();
        $final_arr = array();
        $code = 'SMS_137674888';
        $where_member['type'] = 2;
        $where_member['status'] = array('neq','8,9');
//        查询,匹配:省市区县精确匹配,小区名模糊匹配(只要有连续字匹配,就算匹配)
        $member_find = Db::name('Member')->where($where_member)->field("id,name,tel,city,quarters")->select()->toArray();
        foreach ($member_find as $mek=>$mev){
            if($need_list['city'] == $mev['city']){
                $quarters = explode('+',$mev['quarters']);
                foreach ($quarters as $quak=>$quav){
                    if(strstr($need_list['quarters'],$quav) !== false){
                        $final_arr[] =  $mev;
                        $final_tel[] = $mev['tel'];
                        $middle_arr['name'] = $mev['name'];
                        $middle_arr['name2'] = $need_list['quarters'];
                        $final_mes[] = $middle_arr;
                        $final_autograph[] = '比房价';
                    }
                }
            }
        }
        $json_tel = json_encode($final_tel,JSON_UNESCAPED_UNICODE);
        $json_mes = json_encode($final_mes,JSON_UNESCAPED_UNICODE);
        $json_autograph = json_encode($final_autograph,JSON_UNESCAPED_UNICODE);
        $res_mes = $this->sendLogin($json_tel,$code,$json_mes,$json_autograph);
        if($res_mes != 'OK'){
            $this->apiResponse('0',$res_mes);
        }

    }



    //  短信验证码
    public function sendLogin($tel,$code,$modelVal,$json_autograph){
        require_once VENDOR_PATH."aliyun-dysms-php-sdk/api_demo/SmsDemo.php";
        set_time_limit(0);
        header('Content-Type: text/plain; charset=utf-8');
        $response = SmsDemo::sendBatchSms($tel,$code,$modelVal,$json_autograph);
        return $response;
    }

    /**
     * 人机验证接口
     */
    public function luosimao() {
        $data = $this->request->param();
        $return = $this->curl('129be932c145c773f436eeb263465339',$data['response']);
        $this->apiResponse(1,'成功',$return);
    }

    //人机验证提交curl
    public function curl($api_key,$response) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://captcha.luosimao.com/api/site_verify");

        curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-'.$api_key);

        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('api_key' => $api_key,'response' => $response));

        $res = curl_exec( $ch );
        curl_close( $ch );
//$res  = curl_error( $ch );
//        var_dump($res);
        return $res;
    }
}