IntermediaryController.php 33.9 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
<?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;
    }

    //    登录,注册
//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: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)){
                return json(array('code'=>0,'msg'=>$validate->getError()));
            }
            $data['password'] = $this->md5($data['password']);
            $sure_password = $this->md5($data['sure_password']);
//            确认密码
            if($data['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['create_time'] = array('lt',(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{
                Db::name('Code')->where($where_code)->delete();
            }
//            $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['token'] = $this->md5($str);
            $data['type'] = 2;
            $data['city'] = $request->param('city');
            $data['company'] = $request->param('company');
            $data['store'] = $request->param('store');
            $data['quarters'] = $request->param('quarters');
            $add_first = $user->allowField(true)->save($data);
            $uid = $user->id;

            //                中介推荐,注册成功,增加中介信誉分
            if(!empty($getParam['parent_id'])){
//                    中介被拉黑情况操作(未完)
                $where_parentAdd['id'] = $getParam['parent_id'];
                $where_parentAdd['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'] = $getParam['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'] = "推荐".$getParam['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://bifangjia.cn?uid='.$uid;
            $get_url = $this->shot($url);
            $update_first['link'] = $get_url;
            $update_first['id'] = $uid;
            $add = $user->allowField(true)->save($update_first);
            if($add_first && $add){
                $this->apiResponse('1','注册成功');
            }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')->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();
        $quarters = explode('+',$member['quarters']);
        $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){
                $where_agency['need_id'] = $neev['id'];
                $where_agency['agency_id'] = $member['id'];
                $agency_id = Db::name('Agency')->where($where_agency)->field('id')->find();
                if(!$agency_id){
                    $else_arr[] = $neev;
                }
            }
//            $array =array (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,21,22,23,24,25);
            //            分页
            $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;
                    }
                }
            }

            $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['agency_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'] = $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'] = $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();
        if($agency_list){
            foreach ($agency_list as $agencyk=>$agencyv){
                $final_list[$agencyk]['need_id'] = $need_id;
                $final_list[$agencyk]['agency_id'] = $agencyv['id'];
                $final_list[$agencyk]['user_id'] = $user_id;
                $money = $agencyv['housing_price']*10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'] ;
                $money_arr[] = $money;
                $fianlMoney = substr($money, 0,-4);
                $final_list[$agencyk]['true_money'] = $money;
                $final_list[$agencyk]['money'] = $fianlMoney;
            }
            sort($money_arr);
            foreach ($final_list 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'];
        $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,
             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
             ")
            ->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){
                $changeInt = (int)$nv['all_price'];
                $need[$nk]['intPrice'] = substr($changeInt,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'];
        $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.*,
                    u.status as need_status,u.else as need_else ,u.pay_time as need_pay_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:中介头像
     * @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'])){
            $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/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('start',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_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_need['id'] = $agency['need_id'];
            $where_need['status'] = 7;
            $need = Db::name('UserNeed')->where($where_need)->find();
            if($need){
                Db::startTrans();
                $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['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')->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){
                            $add_detail['user_id'] = $need_referee;
                            $add_detail['title'] = '推荐'.$need['amid'];;
                            $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', '确认失败');
                                }
                            }else{
                                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){
                        $add_detailNext['user_id'] = $agency_referee;
                        $add_detailNext['title'] = '推荐'.$agency['amid'];;
                        $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','确认失败');
                            }
                        }


                    // 退回需求用户保证金
                    $need['user_type'] = Db::name('Member')->where(['id'=>$need['user_id']])->value('type');
                    $thirdsend = controller('Thirdsend','controller');
                    $thirdsend->need_order_refund($need,'中介确认成交退款');
//                    给中介发送信息
                    $sendResult = $this->sendLoginAgency($agency['tel'],$agency['name']);
                    if($sendResult != 'OK'){
                        $this->apiResponse('0',$sendResult);
                    }

                    Db::commit();
                    $this->apiResponse('1','确认成功');
                }else{
                    Db::rollback();
                    $this->apiResponse('0','确认失败');
                }
            }else{
                $this->apiResponse('0','需求状态错误');
            }
        }else{
            $this->apiResponse('0','报价状态错误');
        }




    }







}