ThirdsendController.php 79.5 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 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
<?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\MoneyDetailModel;
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;
    protected $refund;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    public function _initialize()
    {
        $this->refund = controller('Alipay','controller');
    }

    /**
     *  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'] + $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();
        if(!empty($userNeedList)) {
            $time = time();
            $sendTime = intval($userNeedList['pay_time']+1780);
            if($time < $sendTime){
                $this->apiResponse('0','未到发布时间',['now'=>$time,'pay'=>$sendTime]);
            }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->sendNeed($data['need_id']);
                    $this->apiResponse('1','成功123',$return);
                }else{
                    Db::rollback();
                    $this->apiResponse('0','失败');
                }
            }
        }
        else{
            $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();
        $res_middle_arr = array();
        $this->isLogin($data['token']);
        if(empty($data['need_id'])){
            $this->apiResponse('0','需求id不能为空');
        }
//        查询用户发布需求信息
        $where_need['status'] = array('neq',9);
        $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,status')
            ->find();
        if(!$userNeedList){
            $this->apiResponse('0','需求状态不正确');
        }
//        已发布,待报价, 修改状态
        if($userNeedList['status'] == 3){
            $userNeedList['user_type'] = Db::name('Member')->where(['id'=>$userNeedList['user_id']])->value('type');
            $time = time();
//        查询相应报价
            $final_time = $userNeedList['pay_time'] + $userNeedList['quotation_time'] * 86400 + 1790;
//            判断时间是否正确
            if ($time >= $final_time) {
                // 处理未支付报价
                $not_pay = Db::name('Agency')->where(['need_id'=>$userNeedList['id'],'status'=>1])->column('id');
                if($not_pay) {
                    $update = [
                        'status' => 3
                    ];
                    $change = Db::name('Agency')->where(['id'=>['in',$not_pay]])->update($update);
                    if(!$change) {
                        Db::rollback();
                        $this->apiResponse(0,'报价处理失败');
                    }
                }
//                $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,m.token as token,m.id as res_mid')
                    ->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){
                        if($tenv['token'] == $data['token']){
                            $res_mid['all_price'] = $tenv['all_price'];
                            $res_mid['id'] = $tenv['id'];
                            $res_middle_arr[] = $res_mid;
                        }
                        $ten_money = $tenv['housing_price'] + $tenv['assessment_tax'] + $tenv['security_tax'] + $tenv['else_tax'] + $tenv['service_charge'];
                        if($ten_money > 100000){
                            $is_trueMoney[] = $tenv;
                        } else {
                            $up_agen['id'] = $tenv['id'];
                            $up_agen_res['status'] = 3;
                            $res_up = Db::name('Agency')->where($up_agen)->update($up_agen_res);
                            if(!$res_up){
                                Db::rollback();
                            }
                            $this->agency_order_refund($tenv,'中介报价未进入决赛圈退款',2);
                        }
                    }
                    //        判断是否超过三个报价人
                    if (count($is_trueMoney) >= 3) {
//                    查询三人中是否有品牌中介
//                        循环匹配品牌企业
                        foreach ($is_trueMoney as $agencyk => $agencyv) {
                            foreach ($band_list as $brak => $brav) {
//                            判断是否在品牌企业中
                                if (strpos($agencyv['company'],$brav) !== false) {
//                                存在
                                    $agen_money = $agencyv['housing_price']  + $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']  + $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();
//                $return['agency_status'] = 4;
                $return['agency_id'] = $res_middle_arr[0]['id'];
                $return['all_price'] = $res_middle_arr[0]['all_price'];
                $return['agency_status'] = 4;
                $this->apiResponse('1','成功123',$return);
            }else{
                $this->apiResponse('0','时间不正确');
            }
        }else if($userNeedList['status'] == 4 || $userNeedList['status'] == 5){
            $return = $userNeedList;
            $where_a['need_id'] = $userNeedList['id'];
            $where_a['agency_id'] = $userNeedList['user_id'];
            $all = Db::name('Agency')->where($where_a)->field('all_price,status')->find();
            $return['all_price'] = $all['all_price'];
            $return['agency_status'] = $all['status'];
            $return['agency_id'] = $all['id'];
            $this->apiResponse('1','成功',$return);
        }
    }

    /**
     * @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'] = array('neq',9);
        $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($userNeedList['status'] == 5){
                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 {
//                        增加平台收益
                    $add_get['type'] = 1;
                    $add_get['agency_id'] = 1;
                    foreach ($agency_list as $agencyk=>$agencyv){
                        $agency_idar[] = $agencyv['id'];
                    }
                    $add_get['agency_id'] = implode(',',$agency_idar);
                    $add_get['create_time'] = time();
                    $add_get['update_time'] = time();
                    $res_get = Db::name('Get')->insertGetId($add_get);
                    if (!$res_get) {
                        Db::rollback();
                    }
                    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_needs['n.id'] = $data['need_id'];
        $where_needs['n.status'] = 7;
        $need_list = Db::name('UserNeed')->alias('n')
            ->join('Member m','m.id = n.user_id')
            ->field('n.*,m.referee_id,m.name,m.tel,m.id as amid,m.money as amoney')
            ->where($where_needs)
            ->find();
        if($need_list){
            if($need_list['is_refuse'] == 4){
//                拒绝投诉
                if(time() >= $need_list['refuse_complaint_time']){
                    $where_agency['a.status'] = 5;
                    $where_agency['a.need_id'] = $need_list['id'];
                    $agency = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')->field(['a.*,m.type as user_type,m.referee_id,m.id as amid'])->where($where_agency)->find();
//                    修改中介报价状态
                    if($agency){
                        $where_changeAgency['id'] = $agency['id'];
                        $changeAgency['status'] = 11;
                        $changeAgency['update_time'] = time();
                        $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                        if(!$update_a){
                            Db::rollback();
                            $this->apiResponse('0','修改中介状态失败');
                        }
                    }
//                    修改用户需求状态
                    $where_needChange['id'] = $need_list['id'];
                    $update_need['status'] = 10;
                    $update_need['update_time'] = time();
                    $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                    if (!$change_needFinal) {
                        Db::rollback();
                        $this->apiResponse('0','修改需求状态失败');
                    }else{
                        //                    分别查询用户,中介推荐人
                        $where_m['id'] = $change_needFinal['user_id'];
                        $refereeFirst = Db::name('Member')->where($where_m)->field('referee_id,money')->find();
                        $need_referee = $refereeFirst['referee_id'];

                        $agency_referee = $agency['referee_id'];
//                        诚意金金额获取
                        $where_reward['type'] = 5;
                        $where_reward['status'] = 1;
                        $reward = Db::name('Reward')->where($where_reward)->find();
//                    查询推荐人是否已获得奖励
//                    需求用户
                        $where_nref['id'] = $need_referee;
                        $is_needReferee = Db::name('Member')->where($where_nref)->field('is_getmoney,money')->find();
                        if($is_needReferee['is_getmoney'] == 0 && !empty($need_referee)){
                            $add_detail['user_id'] = $need_referee;
                            $add_detail['title'] = '推荐'.$change_needFinal['tel'];;
                            $add_detail['money'] = $reward['number'];
                            $add_detail['order_type'] = 7;
                            $add_detail['type'] = 1;
                            $add_detail['status'] = 1;
                            $add_detail['create_time'] = time();
                            $add_detail['update_time'] = time();
                            $add_res = Db::name('MoneyDetail')->insertGetId($add_detail);
                            if($add_res) {
                                $where_addParent['id'] = $need_referee;
                                $needReferee_add['money'] = $reward['number']+$is_needReferee['money'];
                                $needReferee_add['getmoney_id'] = $change_needFinal['user_id'];
                                $needReferee_add['is_getmoney'] = 1;
                                $needReferee_add['update_time'] = time();
                                $add_parent = Db::name('Member')->where($where_addParent)->update($needReferee_add);
                                if (!$add_parent) {
                                    Db::rollback();
                                    $this->apiResponse('0', '确认失败');
                                }
                            }else{
                                Db::rollback();
                                $this->apiResponse('0','确认失败');
                            }
                        }
                    }
                    //                增加平台收益
                    $add_get['type'] = 4;
                    $add_get['agency_id'] = $agency['id'];
                    $add_get['money'] = $agency['deposit'];
                    $add_get['create_time'] = time();
                    $add_get['update_time'] = time();
                    $res_get = Db::name('Get')->insertGetId($add_get);
                    if(!$res_get){
                        Db::rollback();
                    }

                    $this->handle($need_list,$agency);
                    $need_list['user_type'] = Db::name('Member')->where(['id'=>$need_list['user_id']])->value('type');
                    $this->need_order_refund($need_list,'中介未确认成交退款',5);
                    $agency_list = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')
                        ->field(['a.*,m.type as user_type'])->where(['a.need_id'=>$data['need_id'],'status'=>6])->select()->toArray();
                    if (!empty($agency_list)) {
                        foreach ($agency_list as $agency_key => $agency_val) {
                            // 中介报价订单退款
                            $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                        }
                    }
                }

            }else{
//                正常流程
                if(time() >= ($need_list['next_choose_time']+259200)){
                    $where_agency['a.status'] = 5;
                    $where_agency['a.need_id'] = $need_list['id'];
                    $agency = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')->field(['a.*,m.type as user_type,m.referee_id,m.id as amid'])->where($where_agency)->find();
//                    修改中介报价状态
                    if($agency){
                        $where_changeAgency['id'] = $agency['id'];
                        $changeAgency['status'] = 11;
                        $changeAgency['update_time'] = time();
                        $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                        if(!$update_a){
                            Db::rollback();
                            $this->apiResponse('0','修改中介状态失败');
                        }
                    }
//                    修改用户需求状态
                    $where_needChange['id'] = $need_list['id'];
                    $update_need['status'] = 10;
                    $update_need['update_time'] = time();
                    $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                    //                增加平台收益
                    $add_get['type'] = 4;
                    $add_get['agency_id'] = $agency['id'];
                    $add_get['money'] = $agency['deposit'];
                    $add_get['create_time'] = time();
                    $add_get['update_time'] = time();
                    $res_get = Db::name('Get')->insertGetId($add_get);
                    if(!$res_get){
                        Db::rollback();
                    }
                    if (!$change_needFinal) {
                        Db::rollback();
                        $this->apiResponse('0','修改需求状态失败');
                    }else{
                        //                    分别查询用户,中介推荐人
                        $where_m['id'] = $change_needFinal['user_id'];
                        $refereeFirst = Db::name('Member')->where($where_m)->field('referee_id,money')->find();
                        $need_referee = $refereeFirst['referee_id'];

                        $agency_referee = $agency['referee_id'];
//                        诚意金金额获取
                        $where_reward['type'] = 5;
                        $where_reward['status'] = 1;
                        $reward = Db::name('Reward')->where($where_reward)->find();
//                    查询推荐人是否已获得奖励
//                    需求用户
                        $where_nref['id'] = $need_referee;
                        $is_needReferee = Db::name('Member')->where($where_nref)->field('is_getmoney')->find();
                        if($is_needReferee['is_getmoney'] == 0 && !empty($need_referee)){
                            $add_detail['user_id'] = $need_referee;
                            $add_detail['title'] = '推荐'.$change_needFinal['tel'];;
                            $add_detail['money'] = $reward['number'];
                            $add_detail['order_type'] = 7;
                            $add_detail['type'] = 1;
                            $add_detail['status'] = 1;
                            $add_detail['create_time'] = time();
                            $add_detail['update_time'] = time();
                            $add_res = Db::name('MoneyDetail')->insertGetId($add_detail);
                            if($add_res) {
                                $where_addParent['id'] = $need_referee;
                                $needReferee_add['money'] = $reward['number']+$need_referee['money'];
                                $needReferee_add['getmoney_id'] = $change_needFinal['user_id'];
                                $add_parent = Db::name('Member')->where($where_addParent)->update($needReferee_add);
                                if (!$add_parent) {
                                    Db::rollback();
                                    $this->apiResponse('0', '确认失败');
                                }
                            }else{
                                Db::rollback();
                                $this->apiResponse('0','确认失败');
                            }
                        }
                    }

                    $this->handle($need_list,$agency);
                    $need_list['user_type'] = Db::name('Member')->where(['id'=>$need_list['user_id']])->value('type');
                    $this->need_order_refund($need_list,'中介未确认成交退款',5);
                    $agency_list = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')
                        ->field(['a.*,m.type as user_type'])->where(['a.need_id'=>$data['need_id'],'status'=>6])->select()->toArray();
                    if (!empty($agency_list)) {
                        foreach ($agency_list as $agency_key => $agency_val) {
                            // 中介报价订单退款
                            $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                        }
                    }
                }

            }

            Db::commit();
            $where_return['n.id'] = $need_list['id'];
            $where_return['a.id'] = $agency['id'];
            $return = Db::name('Agency')->alias('a')
                ->where($where_return)
                ->join("UserNeed n",'n.id = a.need_id')
                ->field("n.city,n.quarters,n.room,n.floor,n.house_type,n.renovation,n.status as need_status,n.create_time as need_time,n.quotation_time,n.id as need_id,n.next_choose_time,
             a.all_price,a.create_time,a.status,a.id as intermediaryNeed_id,a.pay_time,a.id ,a.refuse_complaint_time,a.choose_time
             ")
                ->find();
            $changeInt = (int)$return['all_price'];
            $return['intPrice'] = substr($changeInt,0,-4);
            $this->apiResponse('1','成功',['need'=>$return]);
        }else{
            $where_return['n.id'] = $need_list['id'];
            $where_return['a.status'] = 11;
            $return = Db::name('Agency')->alias('a')
                ->where($where_return)
                ->join("UserNeed n",'n.id = a.need_id')
                ->field("n.city,n.quarters,n.room,n.floor,n.house_type,n.renovation,n.status as need_status,n.create_time as need_time,n.quotation_time,n.id as need_id,n.next_choose_time,
             a.all_price,a.create_time,a.status,a.id as intermediaryNeed_id,a.pay_time,a.id ,a.refuse_complaint_time,a.choose_time
             ")
                ->find();
            $changeInt = (int)$return['all_price'];
            $return['intPrice'] = substr($changeInt,0,-4);
            if(!empty($return['status']) && $return['status'] == 11) {
                $this->apiResponse('1','成功',['need'=>$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();
        if(!empty($userNeedList)) {
            $time = time();
            $change_arr = array();
            foreach ($userNeedList as $userk=>$userv){
                if($time >= $userv['pay_time']+1800){
                    $this->sendNeed($userv['id']);
                    $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'] = array('neq',9);
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time,pay_time,user_id,status')->select()->toArray();
        $time = time();
//        循环需求,查询相应报价
        foreach ($userNeedList as $uNeedk => $uNeedv) {
            if($uNeedv['status'] == 3){
                $uNeedv['user_type'] = Db::name('Member')->where(['id'=>$uNeedv['user_id']])->value('type');
                $final_time = $uNeedv['pay_time'] + $uNeedv['quotation_time'] * 86400 + 1800;
                if ($time >= $final_time) {
                    // 处理未支付报价
                    $not_pay = Db::name('Agency')->where(['need_id'=>$uNeedv['id'],'status'=>1])->column('id');
                    if($not_pay) {
                        $update = [
                            'status' => 3
                        ];
                        $change = Db::name('Agency')->where(['id'=>['in',$not_pay]])->update($update);
                        if(!$change) {
                            Db::rollback();
                            $this->apiResponse(0,'报价处理失败');
                        }
                    }
                    $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,m.type as user_type')
                        ->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']  + $tenv['assessment_tax'] + $tenv['security_tax'] + $tenv['else_tax'] + $tenv['service_charge'];
                            if($ten_money > 100000){
                                $is_trueMoney[] = $tenv;
                            } else {
                                $up_agen['id'] = $tenv['id'];
                                $up_agen_res['status'] = 3;
                                $res_up = Db::name('Agency')->where($up_agen)->update($up_agen_res);
                                if(!$res_up){
                                    Db::rollback();
                                }
                                $this->agency_order_refund($tenv,'中介报价未进入决赛圈退款',2);
                            }
                        }
                        //        判断是否超过三个报价人
                        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'] + $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'] + $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($uNeedv,'报价人数不足',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($uNeedv,'报价人数不足',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'] = array('neq',9);
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,choose_time,user_id,pay_money,choose_time,status')->select()->toArray();
        if($userNeedList){
            foreach ($userNeedList as $needk=>$needv) {
                if($needv['status'] == 5){
                    if (time() >= ($needv['choose_time'] + 259200)) {
                        $where_agency['a.need_id'] = $needv['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'] = $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();
                            }
//                        增加平台收益
                            $add_get['type'] = 1;
                            $add_get['agency_id'] = 1;
                            foreach ($agency_list as $agencyk=>$agencyv){
                                $agency_idar[] = $agencyv['id'];
                            }
                            $add_get['agency_id'] = implode(',',$agency_idar);
                            $add_get['create_time'] = time();
                            $add_get['update_time'] = time();
                            $res_get = Db::name('Get')->insertGetId($add_get);
                            if (!$res_get) {
                                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::name('Test')->insert(['data'=>4]);
        Db::startTrans();
        $where_needs['n.status'] = 7;
        $need_list = Db::name('UserNeed')->alias('n')
            ->join('Member m','m.id = n.user_id')
            ->field('n.*,m.referee_id,m.name,m.tel,m.id as amid,m.money as amoney')
            ->select()->toArray();
        if($need_list){
            foreach ($need_list as $needk=>$needv){
                if($need_list['is_refuse'] == 4){
//                拒绝投诉
                    if(time() >= $needv['refuse_complaint_time']){
                        $where_agency['a.status'] = 5;
                        $where_agency['a.need_id'] = $needv['id'];
                        $agency = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')->field(['a.*,m.type as user_type,m.referee_id,m.id as amid'])->where($where_agency)->find();
//                    修改中介报价状态
                        if($agency){
                            $where_changeAgency['id'] = $agency['id'];
                            $changeAgency['status'] = 11;
                            $changeAgency['update_time'] = time();
                            $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                            if(!$update_a){
                                Db::rollback();
                                $this->apiResponse('0','修改中介状态失败');
                            }
                        }
//                    修改用户需求状态
                        $where_needChange['id'] = $needv['id'];
                        $update_need['status'] = 10;
                        $update_need['update_time'] = time();
                        $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                        if (!$change_needFinal) {
                            Db::rollback();
                            $this->apiResponse('0','修改需求状态失败');
                        }else{
                            //                    分别查询用户,中介推荐人
                            $where_m['id'] = $change_needFinal['user_id'];
                            $refereeFirst = Db::name('Member')->where($where_m)->field('referee_id,money')->find();
                            $need_referee = $refereeFirst['referee_id'];

                            $agency_referee = $agency['referee_id'];
//                        诚意金金额获取
                            $where_reward['type'] = 5;
                            $where_reward['status'] = 1;
                            $reward = Db::name('Reward')->where($where_reward)->find();
//                    查询推荐人是否已获得奖励
//                    需求用户
                            $where_nref['id'] = $need_referee;
                            $is_needReferee = Db::name('Member')->where($where_nref)->field('is_getmoney,money')->find();
                            if($is_needReferee['is_getmoney'] == 0 && !empty($need_referee)){
                                $add_detail['user_id'] = $need_referee;
                                $add_detail['title'] = '推荐'.$change_needFinal['tel'];;
                                $add_detail['money'] = $reward['number'];
                                $add_detail['order_type'] = 7;
                                $add_detail['type'] = 1;
                                $add_detail['status'] = 1;
                                $add_detail['create_time'] = time();
                                $add_detail['update_time'] = time();
                                $add_res = Db::name('MoneyDetail')->insertGetId($add_detail);
                                if($add_res) {
                                    $where_addParent['id'] = $need_referee;
                                    $needReferee_add['money'] = $reward['number']+$is_needReferee['money'];
                                    $needReferee_add['getmoney_id'] = $change_needFinal['user_id'];
                                    $needReferee_add['is_getmoney'] = 1;
                                    $needReferee_add['update_time'] = time();
                                    $add_parent = Db::name('Member')->where($where_addParent)->update($needReferee_add);
                                    if (!$add_parent) {
                                        Db::rollback();
                                        $this->apiResponse('0', '确认失败');
                                    }
                                }else{
                                    Db::rollback();
                                    $this->apiResponse('0','确认失败');
                                }
                            }
                        }
                        //                增加平台收益
                        $add_get['type'] = 4;
                        $add_get['agency_id'] = $agency['id'];
                        $add_get['money'] = $agency['deposit'];
                        $add_get['create_time'] = time();
                        $add_get['update_time'] = time();
                        $res_get = Db::name('Get')->insertGetId($add_get);
                        if(!$res_get){
                            Db::rollback();
                        }

//                        这里是退款
                        $this->handle($needv,$agency);
                        $need_list['user_type'] = Db::name('Member')->where(['id'=>$needv['user_id']])->value('type');
                        $this->need_order_refund($need_list,'中介未确认成交退款',5);
                        $agency_list = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')
                            ->field(['a.*,m.type as user_type'])->where(['a.need_id'=>$needv['need_id'],'status'=>6])->select()->toArray();
                        if (!empty($agency_list)) {
                            foreach ($agency_list as $agency_key => $agency_val) {
                                // 中介报价订单退款
                                $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                            }
                        }
                    }

                }else{
                    if(time() >= ($needv['choose_time']+259200)){
                        $where_agency['status'] = 5;
                        $where_agency['need_id'] = $needv['id'];
                        $agency = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')->field(['a.*,m.type as user_type,m.referee_id,m.id as amid'])->where($where_agency)->find();
//                    修改中介报价状态
                        if($agency){
                            $where_changeAgency['id'] = $agency['id'];
                            $changeAgency['status'] = 11;
                            $changeAgency['update_time'] = time();
                            $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                            if(!$update_a){
                                Db::rollback();
                            }
                        }
                        //                增加平台收益
                        $add_get['type'] = 4;
                        $add_get['agency_id'] = $agency['id'];
                        $add_get['money'] = $agency['deposit'];
                        $add_get['create_time'] = time();
                        $add_get['update_time'] = time();
                        $res_get = Db::name('Get')->insertGetId($add_get);
                        if(!$res_get){
                            Db::rollback();
                        }
//                        修改用户需求状态
                        $where_needChange['id'] = $needv['id'];
                        $update_need['status'] = 10;
                        $update_need['update_time'] = time();
                        $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                        if (!$change_needFinal) {
                            Db::rollback();
                        }else{
                            //                    分别查询用户,中介推荐人
                            $where_m['id'] = $change_needFinal['user_id'];
                            $refereeFirst = Db::name('Member')->where($where_m)->field('referee_id,money')->find();
                            $need_referee = $refereeFirst['referee_id'];

                            $agency_referee = $agency['referee_id'];
//                        诚意金金额获取
                            $where_reward['type'] = 5;
                            $where_reward['status'] = 1;
                            $reward = Db::name('Reward')->where($where_reward)->find();
//                    查询推荐人是否已获得奖励
//                    需求用户
                            $where_nref['id'] = $need_referee;
                            $is_needReferee = Db::name('Member')->where($where_nref)->field('is_getmoney')->find();
                            if($is_needReferee['is_getmoney'] == 0 && !empty($need_referee)){
                                $add_detail['user_id'] = $need_referee;
                                $add_detail['title'] = '推荐'.$change_needFinal['tel'];;
                                $add_detail['money'] = $reward['number'];
                                $add_detail['order_type'] = 7;
                                $add_detail['type'] = 1;
                                $add_detail['status'] = 1;
                                $add_detail['create_time'] = time();
                                $add_detail['update_time'] = time();
                                $add_res = Db::name('MoneyDetail')->insertGetId($add_detail);
                                if($add_res) {
                                    $where_addParent['id'] = $need_referee;
                                    $needReferee_add['money'] = $reward['number']+$need_referee['money'];
                                    $needReferee_add['getmoney_id'] = $change_needFinal['user_id'];
                                    $add_parent = Db::name('Member')->where($where_addParent)->update($needReferee_add);
                                    if (!$add_parent) {
                                        Db::rollback();
//                                        $this->apiResponse('0', '确认失败');
                                    }
                                }else{
                                    Db::rollback();
//                                    $this->apiResponse('0','确认失败');
                                }
                            }
                        }



//                        这里是退款
                        $this->handle($needv,$agency);
                        $needv['user_type'] = Db::name('Member')->where(['id'=>$needv['user_id']])->value('type');
                        $this->need_order_refund($needv,'中介未确认成交退款',5);
                        $agency_list = Db::name('Agency')->alias('a')->join('__MEMBER__ m','a.agency_id = m.id')
                            ->field(['a.*,m.type as user_type'])->where(['a.need_id'=>$needv['need_id'],'status'=>6])->select()->toArray();
                        if (!empty($agency_list)) {
                            foreach ($agency_list as $agency_key => $agency_val) {
                                // 中介报价订单退款
                                $this->agency_order_refund($agency_val, '用户未选择中标中介退款',3);
                            }
                        }
                    }



                }

            }
            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();
        if(empty($orderInfo) || $orderInfo['status'] == 1) {
            $this->apiResponse(0,'订单状态错误');
        }
        $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();
        if(empty($orderInfo) || $orderInfo['status'] == 1) {
            $this->apiResponse(0,'订单状态错误');
        }
        $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) {
        $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 = $this->refund->refund($data);
        $refund_model = new RefundModel();
        $money_detail_model = new MoneyDetailModel();
        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,
                'money' => $orderInfo['money'],
                '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);
            $money_insert = [
                'order_id' => $orderInfo['id'],
                'user_id' => $orderInfo['user_id'],
                'user_type' => $orderInfo['user_type'],
                'title' => '退款',
                'money' => $orderInfo['money'],
                'order_type' => $orderInfo['type'],
                'type' => 1,
                'reason' => $reason_id,
                'create_time' => time(),
                'update_time' => time()
            ];
            $money_result = $money_detail_model->insertGetId($money_insert);
            $update = [
                'status' => 4,
                'refund_time' => strtotime($result->gmt_refund_pay)
            ];
            $order_result = Db::name('Order')->where(['id'=>$orderInfo['id']])->update($update);
            if(!$refund_result || !$order_result || !$money_result) {
                Db::rollback();
                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'],
                    'money' => $orderInfo['money'],
                    '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 {
            cache('refund',$result);
            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();
        if($need_list){
            $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(strpos($mev['quarters'],$need_list['quarters']) !== 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[] = '比房价';
                        }
//                    }
                }
            }
            if(!empty($final_tel)){
                $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);
                $mes = $res_mes->Message;
                if($mes != 'OK'){
                    if($mes == '触发分钟级流控Permits:1'){
                        $this->apiResponse('0','请在一分钟后获取验证码');
                    }else if($mes == '触发小时级流控Permits:5'){
                        $this->apiResponse('0', '请在一小时后获取验证码');
                    }else{
                        $this->apiResponse('0', $mes);
                    }
                }
            }
        }

    }




    // 添加推荐人余额
    public function handle($need,$agency)
    {
        //                    分别查询用户,中介推荐人
        $where_m['id'] = $need['user_id'];
        $refereeFirst = Db::name('Member')->where($where_m)->field('id,referee_id,money')->find();
        if(!empty($refereeFirst['referee_id'])) {
            $need_referee = $refereeFirst['referee_id'];
//                        诚意金金额获取
            $where_reward['type'] = 5;
            $where_reward['status'] = 1;
            $reward = Db::name('Reward')->where($where_reward)->find();
//                    查询推荐人是否已获得奖励
//                    需求用户
            $where_nref['id'] = $need_referee;
            $is_needReferee = Db::name('Member')->where($where_nref)->field('is_getmoney')->find();
            if ($is_needReferee['is_getmoney'] == 0) {
                $add_detail['user_id'] = $need_referee;
                $add_detail['title'] = '推荐'.$refereeFirst['tel'];
                $add_detail['money'] = $reward['number'];
                $add_detail['order_type'] = 7;
                $add_detail['type'] = 1;
                $add_detail['status'] = 1;
                $add_detail['create_time'] = time();
                $add_detail['update_time'] = time();
                $add_res = Db::name('MoneyDetail')->insertGetId($add_detail);
                if ($add_res) {
                    $where_addParent['id'] = $need_referee;
                    $needReferee_add['money'] = $reward['number'] + $need_referee['money'];
                    $needReferee_add['getmoney_id'] = $need['user_id'];
                    $add_parent = Db::name('Member')->where($where_addParent)->update($needReferee_add);
                    if (!$add_parent) {
                        Db::rollback();
                        $this->apiResponse('0', '确认失败',$refereeFirst);
                    }
                } else {
                    Db::rollback();
                    $this->apiResponse('0', '确认失败');
                }
            }
        }

        if(!empty($agency['referee_id'])) {
            $agency_referee = $agency['referee_id'];
//                    中介用户
            $where_aref['id'] = $agency_referee;
            $is_agencyReferee = Db::name('Member')->where($where_aref)->field('is_getmoney')->find();
            if($is_agencyReferee['is_getmoney'] == 0){
                $add_detailNext['user_id'] = $agency_referee;
                $add_detailNext['title'] = '推荐'.$agency['tel'];;
                $add_detailNext['money'] = $reward['number'];
                $add_detailNext['order_type'] = 7;
                $add_detailNext['type'] = 1;
                $add_detailNext['status'] = 1;
                $add_detailNext['create_time'] = time();
                $add_detailNext['update_time'] = time();
                $add_resNext = Db::name('MoneyDetail')->insertGetId($add_detailNext);
                if($add_resNext) {
                    $where_addParentNext['id'] = $agency_referee;
                    $needReferee_addNext['money'] = $reward['number']+$agency['amoney'];
                    $needReferee_addNext['getmoney_id'] = $agency['amid'];
                    $add_parentNext = Db::name('Member')->where($needReferee_addNext)->update($needReferee_addNext);
                    if (!$add_parentNext) {
                        Db::rollback();
                        $this->apiResponse('0', '确认失败');
                    }
                }else{
                    Db::rollback();
                    $this->apiResponse('0','确认失败');
                }
            }
        }

    }



    //  短信验证码
    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;
    }
}