IntermediaryController.php 45.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 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
<?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\OrderModel;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use api\portal\model\MemberModel;
use think\Db;
use think\Request;
use think\Loader;

/**
 * @title 中介接口
 * @description 接口说明
 * @group 接口分组
 */

class IntermediaryController extends CommonController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }
    //    获取ip地址
    function getIP(){
        global $ip;
        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else $ip = "Unknow";
        return $ip;
    }

    //    登录,注册
//tel 手机号
//password  密码
//sure_password  确认密码
//name 真实姓名
    /**
     * @title  房产中介注册(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/join
     * @method POST
     * @param name:name type:int require:1 default: other: desc:姓名
     * @param name:tel type:int require:1 default: other: desc:手机号
     * @param name:password type:int require:1 default: other: desc:密码
     * @param name:sure_password type:int require:1 default: other: desc:确认密码
     * @param name:code type:int require:1 default: other: desc:验证码
     * @param name:city type:int require:1 default: other: desc:城市
     * @param name:company type:int require:1 default: other: desc:所属公司
     * @param name:store type:int require:1 default: other: desc:门店
     * @param name:quarters type:int require:1 default: other: desc:服务小区
     * @param name:parent_id type:int require:1 default: other: desc:推荐人id
     * @param name:parent_tel type:int require:1 default: other: desc:推荐人手机号
     * @param name:uid type:int require:1 default: other:一开始传的推荐人id desc:推荐人id
     * @param name:connect   type:int require:1 default: other:1 登录注册 ,2其他 desc:1
     */

    public function join(Request $request)
    {
        $data = $request->Post();
        if(!empty($data['name'])){
//            验证
            $validate = Loader::validate('User');
            if(!$validate->scene('intermediary')->check($data)){
                $this->apiResponse('0',$validate->getError());
            }
            //                省市区不能为空判断
            $num = 0;
            if(empty($data['city'])){
                $this->apiResponse('0','城市不能为空');
            }else{
                $expFirst = explode('-',$data['city']);
                if(count($expFirst) < 3){
                    $this->apiResponse('0','省市区不能为空');
                }else{
                    foreach ($expFirst as $ek=>$ev){
                        if(empty($ev)){
                            $num +=1;
                        }else{
                            $num +=0;
                        }
                    }
                    if($num >0){
                        $this->apiResponse('0','省市区不能为空');
                    }
                }
            }
            $data_add['password'] = $this->md5($data['password']);
            $sure_password = $this->md5($data['sure_password']);
//            确认密码
            if($data_add['password'] != $sure_password){
                $this->apiResponse('0','两次密码不一致');
            }

//           验证码
//            $data['tel'] = $_POST['tel'];
            $code = $request->param('code');
            $where_code['tel'] = $data['tel'];
            $where_code['type'] = 1;
            $where_code['status'] = 1;
            $where_code['create_time'] = array('egt',(time()-300));
            $find_code = Db::name('Code')->where($where_code)->order('create_time desc')->field('code')->find();
            if(!$find_code){
                $this->apiResponse('0','验证码错误,请重新获取');
            }
            if($code != $find_code['code']){
                $this->apiResponse('0','验证码错误,请重新获取');
            }else{
                $update_code['status'] = 9;
                $update_code['update_time'] = time();
                Db::name('Code')->where($where_code)->update($update_code);
            }
//            $data['name'] = $_POST['name'];

//            判断手机号是否已注册
            $user = new MemberModel();
            $where_user['tel'] = $data['tel'];
            $is_isset = $user->where($where_user)->find();
            if($is_isset){
                $this->apiResponse('0','您已注册过,请直接登录');
            }

            $str = rand(1000,9999).time().rand(100,999);
            $data_add['token'] = $this->md5($str);
            $data_add['name'] = $data['name'];
            $data_add['tel'] = $data['tel'];
            $data_add['type'] = 2;
            $data_add['reputation'] = 50;
            $data_add['start'] = 50;
            $data_add['city'] = $request->param('city');
            $data_add['company'] = $request->param('company');
            $data_add['store'] = $request->param('store');
            $data_add['quarters'] = $request->param('quarters');
            $data_add['create_ip'] = $this->getIP();
            if(!empty($data['parent_id'])){
                $data_add['referee_id'] = $data['parent_id'];
                $data_add['referee_tel'] = $data['parent_tel'];
            }
            $add_first = $user->allowField(true)->save($data_add);
            $uid = $user->id;
            //                中介推荐,注册成功,增加中介信誉分
            if(!empty($data['parent_id'])){
                $where_parentAdd['id'] = $data['parent_id'];
//                $where_parentAdd['type'] = 2;
                $choose_type = $user->where($where_parentAdd)->field('type')->find();
                if($choose_type['type'] == 2){
                    $where_reward['type'] = 2;
                    $where_reward['status'] = 1;
                    $reward = Db::name('Reward')->where($where_reward)->order('create_time')->field('number')->find();
                    if($reward){
                        $add_agencyScore = $user->where($where_parentAdd)->setInc('reputation',$reward['number']);
                        if(!$add_agencyScore){
                            $this->apiResponse('0','推荐奖励增加失败');
                        }else{
//                           推荐记录表
                            $add_log['type'] = 2;
                            $add_log['number'] = $reward['number'];
                            $add_log['referee'] = $data['parent_id'];
                            $add_log['recommen'] = $uid;
                            $add_log['create_time'] = time();
                            $add_log['update_time'] = time();
                            $log = Db::name('RewardLog')->insertGetId($add_log);
                            if(!$log){
                                Db::rollback();
                            }
//                           信誉记录表
                            $add_startlog['detail'] = "推荐".$data['parent_tel'].'注册';
                            $add_startlog['star'] = $reward['number'];
                            $add_startlog['type'] = 3;
                            $add_startlog['create_time'] = time();
                            $add_startlog['update_time'] = time();
                            $startLog = Db::name('StartDetail')->insertGetId($add_startlog);
                            if(!$startLog){
                                Db::rollback();
                            }
                        }
                    }else{
                        Db::rollback();
                        $this->apiResponse('0','推荐奖励获取失败');
                    }
                }
            }

            $url = 'http://www.bifangjia.com?uid='.$uid;
            $get_url = $this->shot($url);
            $update_first['link'] = $get_url;
            $update_first['id'] = $uid;
            //                手机端
            $urlphone = 'http://m.bifangjia.com?uid='.$uid;
            $get_urlphone = $this->shot($urlphone);
            $update_first['phone_link'] = $get_urlphone;
            $add = $user->allowField(true)->save($update_first);
            if($add_first && $add){
                $final['token'] = $data_add['token'];
                $this->apiResponse('1','注册成功',$final);
            }else{
                $this->apiResponse('0','注册失败');
            }

        }else{
//            服务协议
//            $service = new PostService();
//            $list = $service->publishedArticle(1,1)->toArray();
            $where_pro['status'] = 1;
            $where_pro['type'] = 2;
            $list = Db::name('Protocol')->where($where_pro)->order("update_time desc")->field("title,content")->find();
//              推荐人(未完)
            $getdata = $this->request->param();
            if(!empty($getdata['uid'])){
                $where_member['id'] = $getdata['uid'];
                $find_parent = Db::name('Member')->where($where_member)->field('id,tel')->find();
                if($find_parent){
                    $list['parent'] = $find_parent['tel'];
                    $list['parent_id'] = $find_parent['id'];
                }
            }
            if($list){
                $this->apiResponse('1','成功',$list);
            }else{
                $this->apiResponse('0','暂无内容');
            }

        }
    }

    //    短网址
    function shot($url){
        $jump = "http://suo.im/api.php?url=".$url;
        $shot_val = file_get_contents($jump);
        return $shot_val;
    }

//    中介个人中心左侧信息
    public function left($token){
        $where_member['token'] = $token;
        $where_member['status'] = 1;
        $member = Db::name('Member')->where($where_member)->field('name,id,start,reputation,company,store,quarters,tel,city,pic,money,get_money,referee_tel,referee_id,phone_link')->find();
        if(!$member){
            $this->apiResponse('0','用户信息错误');
        }else{
            if(empty($member['pic'])){
                $member['pic'] = "../themes/simpleboot3/public/assets/images/pic.png";
            }
        }
//        查询签到信息
        $member['pic'] = cmf_get_image_url($member['pic']);
        $where_checkin['agency_id'] = $member['id'];
        $tme_str = date('Y-m-d',time());
        $where_checkin['create_time'] = strtotime($tme_str);
        $is_check = Db::name('Checkin')->where($where_checkin)->find();
        if($is_check){
            $member['is_checkin'] = 1;
        }else{
            $member['is_checkin'] = 0;
        }
        return $member;
    }


    /**
     * @title  中介个人中心首页(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/myIndex
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:page type:int require:1 default: other: desc:页码
     */
    public function myIndex(Request $request){
        $token = $request->param('token');
        $pageInt = $request->param('page');
        $member = $this->left($token);
        $size = 2;
        $all_page = 0;
        $res = array();
        $final['intermediary'] = $member;
        $where_need['city'] = $member['city'];
        $where_need['status'] = 3;
//        if($quarters){
//            foreach ($quarters as $quartersk=>$quartersv){
//                $quarters[$quartersk] = '%'.$quartersv.'%';
//            }
//        }
//        $where_need['quarters'] = array('like',$quarters,'OR');
        $page = $request->param('page');
        $need_list = Db::name('UserNeed')->where($where_need)
            ->field("id,city,quarters,room,floor,house_type,renovation,quotation_time,create_time,people,pay_time")
            ->order('pay_time desc')
            ->select()->toArray();

        if($need_list){
            $else_arr = [];
            foreach ($need_list as $neek=>$neev){
                $is_match = strpos($member['quarters'],$neev['quarters']);
                if($is_match !== false) {
                    $where_agency['need_id'] = $neev['id'];
                    $where_agency['agency_id'] = $member['id'];
                    $where_agency['status'] = array('not in', '1,9');
                    $agency_id = Db::name('Agency')->where($where_agency)->field('id')->find();
                    if (!$agency_id) {
                        $else_arr[] = $neev;
                    }
                }
            }
            //            分页
            $all_page = count($else_arr);
            $page = ceil($all_page/$size);
            $page = $page?$page:1;
            if($pageInt <1 || $pageInt >$page ){
                $this->apiResponse('0','页码错误');
            }else{
                $start = $pageInt*$size-$size;
                $end = $pageInt*$size-1;
                foreach ($else_arr as $elsek=>$elsev){
                    if($elsek >= $start && $elsek <= $end){
                        $res[] = $elsev;
                    }
                }
            }
            if($all_page == 0){
                $final['is_sendneed'] = 0;
            }else{
                $final['is_sendneed'] = 1;
            }

            $final['intermediary_need'] = $res;
        }else{
            $final['intermediary_need'] = array();
        }
        $final['all_page'] = ceil($all_page/$size);
        $this->apiResponse('1','成功',$final);
    }


    /**
     * @title  中介个人中心匹配需求详情(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/mySend
     * @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
     */
    public function mySend(Request $request){
        $token = $request->param('token');
        $member = $this->left($token);
        $final['intermediary'] = $member;
        $final['money'] = 0;
        if(!$request->param('need_id')){
            $this->apiResponse('0','请选择需求id');
        }else{
            $need_id = $request->param('need_id');
        }
        $where_need['id'] = $need_id;
        $where_need['status'] = 3;
        $need_list = Db::name('UserNeed')->where($where_need)
            ->field("id,user_id,city,quarters,room,floor,house_type,renovation,area,is_first,loan,contract,quotation_time,create_time,people,else,pay_time")
            ->find();
        if($need_list){
//            判断是否已购买透视卡
//            $where_order['type'] = 3;
            $where_order['need_id'] = $need_id;
            $where_order['user_id'] = $member['id'];
            $is_order = Db::name('See')->where($where_order)->field('id,money,create_time')->find();
            if($is_order){
                $final['is_see'] = 2;
                $final['money'] = $this->getLowStr($is_order['money']);
//                $final['money'] = $is_order['money'];
                $final['pay_time'] = $is_order['create_time'];
            }else{
                $where_agency['need_id'] = $request->param('need_id');
                $where_agency['status'] = 2;
                $agency_middle = Db::name('Agency')->where($where_agency)->field('id,agency_id')->select()->toArray();
                $agency_count = count($agency_middle);
                if($agency_count >=3){
                        $money = $this->getOrderInfo($token,$need_id);
                        $final['is_see'] = 1;
                        $final['money'] = $this->getLowStr($money['money']);
//                        $final['money'] = $money['money'];
                        $final['pay_time'] = $money['pay_time'];
                }else{
                    $final['is_see'] = 0;
                }
                foreach ($agency_middle as $midk=>$midv){
                    if($member['id'] == $midv['agency_id']){
                        $final['is_see'] = 0;
                    }
                }
            }
            $final['intermediary_need'] = $need_list;

        }else{
            $final['intermediary_need'] = array();
            $final['is_see'] = 0;

        }

        $this->apiResponse('1','成功',$final);
    }


//     * @param name:token type:char require:1 default: other: desc:用户token
//     * @param name:need_id type:int require:1 default: other: desc:需求id

    public function getOrderInfo($token,$need_id) {
        $member_model = new MemberModel();
        $user = $member_model->field(['id,money'])->where(['token'=>$token])->find();
        $order_model = new OrderModel();
        $price = $order_model->where(['need_id'=>$need_id,'user_id'=>$user['id'],'type'=>3,'status'=>2])->field('money,pay_time')->find();
        if(empty($price)) {
            return false;
        }
        return $price;
    }



//    查询中介报价
//  $need_id 需求id
    public function agencyOffer($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,refuse_complaint_time")
            ->select()->toArray();
        $final_list = array();
        $add_arr = array();
        $is_trueMoney = array();
        if($agency_list){
            foreach ($agency_list as $agencyk=>$agencyv){
                $money = $agencyv['housing_price'] + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'] ;
                if($money > 100000){
                    $agencyv['need_id'] = $need_id;
                    $agencyv['agency_id'] = $agencyv['id'];
                    $agencyv['user_id'] = $user_id;
                    $money_arr[] = $money;
                    $fianlMoney = substr($money, 0,-4);
                    $agencyv['true_money'] = $money;
                    $agencyv['money'] = $money;
                    $is_trueMoney[] = $agencyv;
                }
            }
            sort($money_arr);
            foreach ($is_trueMoney as $finalk=>$finalv){
                if($finalv['true_money'] == $money_arr[0]){
                    $add_arr['need_id'] = $finalv['need_id'];
                    $add_arr['agency_id'] = $finalv['agency_id'];
                    $add_arr['user_id'] = $finalv['user_id'];
                    $add_arr['money'] = $finalv['money'];
                    $add_arr['create_time'] = time();
                    $add_arr['update_time'] = time();
                }
            }
            $save = Db::name('See')->insertGetId($add_arr);
            $return['id'] = $save;
            $return['money'] = $add_arr['money'];
            $return['time'] = time();
            return $return;
        }
    }


//    透视卡
//token
//need_id
//    public function see(Request $request){
//        $data = $request->param();
//        $user = $this->left($data['token']);
//        $user_id = $user['id'];
////        购买支付
//
//
//
////        成功加入记录
////        加订单
//        $add = array();
//        $add['name'] = '购买透视卡';
//        $add['order_sn'] = $this->getmath();
//        $add['type'] = 3;
//        $add['need_id'] = $data['need_id'];
//        $add['is_get'] = 2;
////        查询透视卡金额
//        $add['money'] = 2;
//
//        $add['status'] = 1;
//        $add['create_time'] = time();
//        $add['update_time'] = time();
//        $order_id = Db::name('Order')->insertGetId($add);
//        if(!$order_id){
//            $this->apiResponse('0','购买失败');
//        }
//
////        支付成功
////        修改订单状态
//        $where_order['order_sn'] = $data['order_sn'];
//        $save['update_time'] = time();
//        $save['pay_time'] = time();
//        $is_save = Db::name('Order')->where($where_order)->update($save);
//        if($is_save){
//            return true;
//        }else{
//            return false;
//        }
////         查询报价
//        $money = $this->agencyOffer($user_id,$data['need_id']);
//
//    }

    /**
     * @title  中介个人中心我的报价(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/myMoney
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:page type:int require:1 default: other: desc:页码
     */
    public function myMoney(Request $request){
        $data = $request->param();
        $size = 2;
        $my = $this->left($data['token']);
        $final['intermediary'] = $my;
        $where_need['a.agency_id'] = $my['id'];
        $where_need['a.status'] = array('not in','1,9,15');
        $page = $data['page'];
        $need = Db::name('Agency')->alias('a')
            ->where($where_need)
            ->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,n.refuse_complaint_time,n.is_refuse,n.pay_time,
             a.all_price,a.create_time,a.status as status,a.id as intermediaryNeed_id,a.pay_time as agency_pay_time,a.id ,a.refuse_complaint_time,a.choose_time
             ")
            ->order('create_time desc')
            ->limit($size)->page($page)
            ->select()->toArray();

        $all_page = Db::name('Agency')->alias('a')
            ->where($where_need)
            ->join("UserNeed n",'n.id = a.need_id')
            ->count();
        if($need){
            $number = Db::name('Agency')->alias('a')
                ->where($where_need)
                ->join("UserNeed n",'n.id = a.need_id')
                ->count();
            $final['all_page'] = ceil($number/$size);
            foreach ($need as $nk=>$nv){
//                $str = (string)$nv['all_price'];
                $changeInt = explode('.',$nv['all_price']);
                $need[$nk]['intPrice'] = substr($changeInt[0],0,-4);
            }
            $final['need'] = $need;
        }else{
            $final['need'] = array();
        }
        $final['all_page'] = ceil($all_page/$size);
        $this->apiResponse('1','成功',$final);
    }

    /**
     * @title  中介个人中心我的报价详情(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/myDetail
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:intermediary_id type:int require:1 default: other: desc:报价id
     */
    public function myDetail(Request $request){
        $data = $request->param();
        $my = $this->left($data['token']);
        $final['intermediary'] = $my;
        $where_need['agency_id'] = $my['id'];
        $where_need['i.id'] = $data['intermediary_id'];
        $where_need['i.status'] = array('not in','1,9');
        $need = Db::name('Agency')->alias('i')
            ->where($where_need)
            ->join("hp_user_need u",'u.id = i.need_id')
            ->field("i.id as intermediaryNeed_id ,i.else as agency_else, i.pay_time as agency_pay_time,i.status as agency_status,i.*,
                    u.status as need_status,u.else as need_else ,u.pay_time as need_pay_time,u.refuse_complaint_time as refuse_complaint_time,u.*
            ")
            ->find();
        if($need){
            $final['need'] = $need;
        }else{
            $final['need'] = array();
        }
        $this->apiResponse('1','成功',$final);
    }

    /**
     * @title  中介个人中心查看评价(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/comment
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:intermediary_id type:int require:1 default: other: desc:报价id
     */
    public function comment(Request $request){
        $data = $request->param();
        $my = $this->left($data['token']);
        if(empty($data['intermediary_id'])){
            $this->apiResponse('0','报价id不能为空');
        }
        $where_agency['id'] = $data['intermediary_id'];
        $agency = Db::name('Agency')->where($where_agency)->find();
        if($agency){
            $where_comment['need_id'] = $agency['need_id'];
            $where_comment['agency_id'] = $agency['id'];
            $where_comment['intermediary_id'] = $agency['agency_id'];
            $where_comment['status'] = 1;
            $comment = Db::name('Comment')->where($where_comment)->find();
            if($comment){
                $final['comment'] = $comment;
                $final['agency'] = $my;
                $this->apiResponse('1','成功',$final);
            }else{
                $this->apiResponse('0','暂无评价');
            }
        }else{
            $this->apiResponse('0','状态错误');
        }
    }


    /**
     * @title  中介个人中心我的资料(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/mySelf
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     */
    public function mySelf(Request $request){
        $data = $request->param();
        $my = $this->left($data['token']);
        $final['intermediary'] = $my;
        $where_card['c.user_id'] = $my['id'];
        $where_card['c.status'] = 1;
        $card = Db::name('Card')->alias('c')
            ->where($where_card)
            ->join("Bank b",'b.id=c.bank_id')
            ->field('c.id,b.name,c.card_num')
            ->select()->toArray();
        $final['card'] = $card;
        $this->apiResponse('1','成功',$final);
    }

    /**
     * @title  中介资料修改(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/myChange
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:company type:int require:1 default: other: desc:所属公司
     * @param name:store type:int require:1 default: other: desc:门店
     * @param name:quarters type:int require:1 default: other: desc:服务小区
     * @param name:pic type:varchar require:1 default: other: desc:中介头像
     * @param name:city type:varchar require:1 default: other: desc:城市
     * @return data:''@
     * @data status:'状态(1->待发布,2->已发布,待报价,3->已成交,4->未交保证金,5->已过期,6->未选择中介,7->中介未确认,8->报价人数不足,9->删除)'
     */
    public function myChange(Request $request){
        $data = $request->param();
        $where_member['token'] = $data['token'];
        $where_member['status'] = 1;
        $member = Db::name('Member')->where($where_member)->find();
        if(!empty($data['company'])){
            if(empty($data['city'])){
                $this->apiResponse('0','请选择所在城市');
            }
            if(empty($data['store'])){
                $this->apiResponse('0','请输入所在门店');
            }
            if(empty($data['quarters'])){
                $this->apiResponse('0','请输入服务小区');
            }else{
                $exp_quar = explode('+',$data['quarters']);
                foreach ($exp_quar as $quark=>$quarv){
                    $qua_len = mb_strlen($quarv,'utf-8');
                    if($qua_len > 12){
                        $this->apiResponse('0','服务小区最多12个字符');
                    }
                }
            }
            $update_member['city'] = $data['city'];
            $update_member['company'] = $data['company'];
            $update_member['id'] = $member['id'];
            $update_member['store'] = $data['store'];
            $update_member['quarters'] = $data['quarters'];
            if(!empty($data['pic'])){
                $update_member['pic'] = $data['pic'];
            }
            $model = new MemberModel();
            $member = $model->isUpdate(true)->save($update_member);
            if(!$member){
                $this->apiResponse('0','中介信息有误');
            }else{
                $this->apiResponse('1','成功');
            }
        }else{
            if(!$member){
                $this->apiResponse('0','中介信息有误');
            }else{
                $member['pic'] = cmf_get_image_url($member['pic']);
                $this->apiResponse('1','成功',$member);
            }
        }
    }

    /**
     * @title  中介资料头像修改(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/changePic
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:pic type:varchar require:1 default: other: desc:中介头像
     * @return data:''
     */
    public function changePic(){
        $data = $this->request->param();
        if(empty($data['pic'])){
            $this->apiResponse('0','头像不能为空');
        }
        $where_member['token'] = $data['token'];
        $where_member['status'] = 1;
        $member = Db::name('Member')->where($where_member)->find();
        if(!$member){
            $this->apiResponse('0','用户信息错误');
        }
        $update_member['id'] = $member['id'];
        $update_member['pic'] = $data['pic'];
        $model = new MemberModel();
        $member = $model->isUpdate(true)->save($update_member);
        if($member){
            $this->apiResponse('1','上传成功');
        }else{
            $this->apiResponse('0','头像上传失败');
        }
    }


    /**
     * @title  中介签到(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/checkIn
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     */
    public function checkIn(Request $request){
        $data = $request->param();
        $mes = $this->left($data['token']);
        if($mes['is_checkin'] == 0){
            $add['agency_id'] = $mes['id'];
            $time = date('Y-m-d',time());
            $add['create_time'] = strtotime($time);
            $add['update_time'] = time();
            $result = Db::name('Checkin')->insertGetId($add);
            if($request){
                $where_addStart['id'] = $mes['id'];
                $addStart = Db::name('Member')->where($where_addStart)->setInc('reputation',1);
                if($addStart){
                    $this->apiResponse('1','签到成功');
                }else{
                    $this->apiResponse('0','签到失败');
                }
            }else{
                $this->apiResponse('0','签到失败');
            }
        }else{
            $this->apiResponse('0','您已签到');
        }
    }

    /**
     * @title  我的账户(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/orderDetail
     * @method POST
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @return data:'type.1增加,2减少,3提现'
     */
    public function orderDetail(Request $request){
        $data = $request->param();
        $user = $this->left($data['token']);
        $where_card['user_id'] = $user['id'];
        $where_card['status'] = array('neq',9);
        $card = Db::name('Card')->where($where_card)->find();
        if($card){
            $final['is_card'] = 1;
        }else{
            $final['is_card'] = 0;
        }
        $where_umoney['user_id'] = $user['id'];
        $where_umoney['status'] = array('neq',9);
        $order_list = Db::name('MoneyDetail')->where($where_umoney)->select()->toArray();
        $final['user'] = $user;
        if($order_list){
            foreach ($order_list as $ordk=>$ordv){
                $order_list[$ordk]['update_time'] = date('Y-m-d H:i:s',$ordv['update_time']);
            }
            $final['detail'] = $order_list;
        }else{
            $final['detail'] = array();
        }
        if($final){
            $this->apiResponse('1','成功',$final);
        }else{
            $this->apiResponse('0','失败');
        }
    }

    /**
     * @title  中介确认成交(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/sure
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:intermediary_id type:int require:1 default: other: desc:中介报价id
     * @return data:''
     */
    public function sure(Request $request){
        $data = $request->param();
        $where_agency['a.id'] = $data['intermediary_id'];
        $where_agency['a.status'] = 5;
        $agency = Db::name('Agency')->alias('a')
            ->join('Member m','m.id = a.agency_id')
            ->where($where_agency)
            ->field('a.*,m.referee_id,m.name,m.tel,m.id as amid,m.money as amoney')
            ->find();
        if($agency){
//            增加中介成交数
            $where_count_deal['id'] = $agency['amid'];
            $save_count_deal = Db::name('Member')->where($where_count_deal)->setInc('count_deal',1);
            if(!$save_count_deal){
                Db::rollback();
                $this->apiResponse('0', '确认失败');
            }
            $where_needs['n.id'] = $agency['need_id'];
            $where_needs['n.status'] = 7;
            $need = 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){
                Db::startTrans();
                $where_need['id'] = $agency['need_id'];
                $where_need['status'] = 7;
                $update_agency['status'] = 11;
                $update_agency['update_time'] = time();
                $add_agency = Db::name('Agency')->alias('a')->where($where_agency)->update($update_agency);
                $update_need['status'] = 10;
                $update_need['update_time'] = time();
                $update_need['sure_time'] = time();
                $add_need = Db::name('UserNeed')->where($where_need)->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($add_agency && $add_need){
//                    分别查询用户,中介推荐人
                    $where_m['id'] = $need['user_id'];
                    $refereeFirst = Db::name('Member')->where($where_m)->field('referee_id,money,type')->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['user_type'] = $refereeFirst['type'];
                            $add_detail['title'] = '推荐'.$need['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'] = $need['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_rewResAgency = $this->addReward($agency_referee,$agency['amid']);
                    if($add_rewResAgency == false){
                        Db::rollback();
                        $this->apiResponse('0','增加信誉失败');
                    }
//                    用户
                    $add_rewResUser = $this->addReward($need_referee,$need['user_id']);
                    if($add_rewResUser == false){
                        Db::rollback();
                        $this->apiResponse('0','增加信誉失败');
                    }
//                    中介用户
//                    $where_aref['id'] = $agency_referee;
//                    $is_agencyReferee = Db::name('Member')->where($where_aref)->field('is_getmoney')->find();
//                    if($is_agencyReferee['is_getmoney'] == 0 && !empty($agency_referee)){
//                        $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($where_addParentNext)->update($needReferee_addNext);
//                            if (!$add_parentNext) {
//                                    Db::rollback();
//                                    $this->apiResponse('0', '确认失败');
//                                }
//                            }else{
//                                Db::rollback();
//                                $this->apiResponse('0','确认失败');
//                            }
//                        }


                    // 退回需求用户保证金
                    $need['user_type'] = Db::name('Member')->where(['id'=>$need['user_id']])->value('type');
                    $thirdsend = controller('Thirdsend','controller');
                    $thirdsend->need_order_refund($need,'中介确认成交退款',6);
                    $where_agencyNext['a.need_id'] = $need['id'];
                    $where_agencyNext['a.status'] = 6;
                    $agency_refund = Db::name('Agency')->alias('a')
                        ->join('Member m','m.id = a.agency_id')
                        ->where($where_agencyNext)
                        ->field('a.*,m.referee_id,m.name,m.type as user_type')
                        ->select();
                    foreach ($agency_refund as $ka=>$kv) {
                        $thirdsend->agency_order_refund($kv,'中介确认成交退款',6);
                    }
                    Db::commit();
                    $final['need'] = $agency;
                    $this->apiResponse('1','确认成功',$final);
                }else{
                    Db::rollback();
                    $this->apiResponse('0','确认失败');
                }
            }else{
                $this->apiResponse('0','需求状态错误');
            }
        }else{
            $this->apiResponse('0','报价状态错误');
        }

    }



//增加信誉
//$id  => 中介id,推荐人id
//$recommen => 被推荐人id
    public function addReward($id,$recommen){
        $where_reNex['type'] = 4;
        $where_reNex['status'] = array('neq',9);
        $rewardNex = Db::name('Reward')->where($where_reNex)->find();
        if($rewardNex){
            $where_memNex['id'] = $id;
            $where_memNex['type'] = 2;
            $save_memNex = Db::name('Member')->where($where_memNex)->setInc('reputation',$rewardNex['number']);
            if($save_memNex){
                $saveLog['type'] = 4;
                $saveLog['number'] = $rewardNex['number'];
                $saveLog['referee'] = $id;
                $saveLog['recommen'] = $recommen;
                $saveLog['create_time'] = time();
                $saveLog['update_time'] = time();
                $resSaveLog = Db::name('RewardLog')->insertGetId($saveLog);
                if($resSaveLog){
                    return true;
                }else{
                    return false;
                }

            }
        }
        return true;
    }


    /**
     * @title  中介投诉(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediary/complaint
     * @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
     * @param name:intermediary_id type:int require:1 default: other: desc:中介报价id
     * @param name:content type:int require:1 default: other: desc:投诉内容
     */
//* @param name:gener type:int require:1 default: other: desc:1->用户投诉,2->中介投诉
    public function complaint(Request $request){
        $token = $request->param('token');
        $member = $this->left($token);
        $data = $request->param();
        if(empty($data['need_id']) && empty($data['intermediary_id'])){
            $this->apiResponse('0','投诉id不能为空');
        }
        $where_find['n.id'] = $data['need_id'];
        $where_find['a.id'] = $data['intermediary_id'];
        $where_find['n.status'] = array('in','5,7,10,11');
        $where_find['a.status'] = 5;
        $find = Db::name('UserNeed')->alias('n')
            ->where($where_find)
            ->join("Agency a",'a.need_id = n.id')
            ->field("a.id as agency_id")
            ->find();
        if($find){
//            查询是否已投诉
            $where_isset['user_id'] = $member['id'];
            $where_isset['need_id'] = $data['need_id'];
            $where_isset['agency_id'] = $find['agency_id'];
            $where_isset['intermediary_id'] = $data['intermediary_id'];
            $where_isset['type'] = 2;
            $where_isset['status'] = 1;
            $is_isset= Db::name('Complaint')->where($where_isset)->field('id')->find();
            if($is_isset){
                $this->apiResponse('0','您已投诉');
            }
//            添加投诉
            $add['user_id'] = $member['id'];
            $add['need_id'] = $data['need_id'];
            $add['agency_id'] = $find['agency_id'];
            $add['intermediary_id'] = $data['intermediary_id'];
            $add['content'] = $data['content'];
            $add['type'] = 2;
            $add['create_time'] = time();
            $add['update_time'] = time();
            $is_add = Db::name('Complaint')->insertGetId($add);
            if($is_add){
                $where_changeNeed['id'] = $data['need_id'];
                $save_need['status'] = 12;
                $save_need['complaint_time'] = time();
                $changeNeed = Db::name('UserNeed')->where($where_changeNeed)->update($save_need);
                if(!$changeNeed){
                    $this->apiResponse('0','投诉失败');
                }
                $where_changeAgency['id'] = $data['intermediary_id'];
                $save_agency['status'] = 12;
                $save_agency['complaint_time'] = time();
                $changeAgency = Db::name('Agency')->where($where_changeAgency)->update($save_agency);
                if(!$changeAgency){
                    $this->apiResponse('0','投诉失败');
                }
                $final['complaint_id'] = $is_add;
                $this->apiResponse('1','投诉成功',$final);
            }else{
                $this->apiResponse('0','投诉失败');
            }
        }else{
            $this->apiResponse('0','当前状态不支持投诉');
        }
        $final['user'] = $member;

    }






}