IntermediaryneedController.php 13.9 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\AgencyModel;
use api\portal\model\OrderModel;
use api\portal\model\UserNeedModel;
use api\portal\model\MemberModel;
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 IntermediaryneedController extends CommonController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }
    /**
     * @title  中介报价时保证金金额(填完房价调用;每次修改重新调用)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediaryneed/payMoney
     * @method POST
     * @param name:housing_price type:int require:1 default: other: desc:房价
     * @param name:token type:int require:1 default: other: desc:token
     */
    public function payMoney($housing_price){
        $where_inteRule['status'] = 1;
        $where_inteRule['low'] = array('elt',$housing_price);
        $where_inteRule['height'] = array('egt',$housing_price);
        $inte_money = Db::name('IntermediaryRule')->where($where_inteRule)->field('money')->find();
        if(!$inte_money){
            $this->apiResponse('0','保证金获取失败');
        }else{
            $this->apiResponse('1','成功',$inte_money['money']);
        }
    }


    /**
     * @title  匹配需求
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediaryneed/sendNeed
     * @method POST
     * @param name:token type:int require:1 default: other: desc:token
     * @param name:page type:int require:1 default: other: desc:分页
     * @return data:''@
     * @data city:'地区'
     * @data quarters:'小区'
     * @data room:'具体房号'
     * @data floor:'楼层'
     * @data house_type:'户型'
     * @data renovation:'装修'
     * @data quotation_time:'报价时间(天)'
     * @data create_time:'发布时间'
     * @data people:'报价人数'
     */
    public function sendNeed(Request $request){
        $data = $request->param();
        $page = $data['page'];
        $size = 4;
        $final_list = array();
        $getPageList = array();
        $is_send = array();
        $where_member['token'] = $data['token'];
//        查询,匹配:省市区县精确匹配,小区名模糊匹配(只要有连续字匹配,就算匹配)
        $member_find = Db::name('Member')->where($where_member)->field("id,city,quarters")->find();
        if(!$member_find){
            $this->apiResponse('0','用户信息错误');
        }
        $where_need['city'] = $member_find['city'];
        $where_need['status'] = 3;
        $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){
            foreach ($need_list as $nek=>$nev){
                $is_match = strpos($member_find['quarters'],$nev['quarters']);
                if($is_match !== false){
                    //            筛除已报价
                    $where_age['need_id'] = $nev['id'];
                    $where_age['agency_id'] = $member_find['id'];
                    $where_age['status'] = array('not in','1,9');
                    $is_agen = Db::name('Agency')->where($where_age)->field('id,need_id')->find();
                    $k[] = $is_agen['need_id'];
                    if(!$is_agen){
                        $final_list[] = $nev;
                    }else{
                        $c[] = $nev['id'];
                    }
                }
            }
            $begin_page = $page*$size-$size;
            $end_page = $page*$size;
            if(!empty($final_list)){
                foreach ($final_list as $fink=>$finv){
                    if($fink >= $begin_page &&  $fink < $end_page){
                        $getPageList[] = $finv;
                    }
                }
            }
            $final['need_list'] = $getPageList;
            $count = count($final_list);
            $final['all_page'] = ceil($count/$size);
            $this->apiResponse('1','成功',$final);
        }else{
            $this->apiResponse('0','该地区暂无匹配需求');
        }
    }

//   匹配需求   按用户匹配(旧版)
    public function sendNeedold(Request $request){
        $data = $request->param();
        $page = $data['page'];
        $size = 4;
        $where_member['token'] = $data['token'];
//        查询,匹配:省市区县精确匹配,小区名模糊匹配(只要有连续字匹配,就算匹配)
        $member_find = Db::name('Member')->where($where_member)->field("id,city,quarters")->find();
        if(!$member_find){
            $this->apiResponse('0','用户信息错误');
        }
        $where_need['city'] = $member_find['city'];
        $where_need['status'] = 3;

        $quarters = explode('+',$member_find['quarters']);
        if($quarters){
            foreach ($quarters as $quartersk=>$quartersv){
                $quarters[$quartersk] = '%'.$quartersv.'%';
            }
        }
        $where_need['quarters'] = array('like',$quarters,'OR');
        $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')->limit($size)->page($page)->select()->toArray();
        $count = count($need_list);
        $final['all_page'] = ceil($count/$size);
        $final_list = array();
        if($need_list){
//            筛除已报价
            foreach ($need_list as $nek=>$nev){
                $where_age['need_id'] = $nev['id'];
                $where_age['agency_id'] = $member_find['id'];
                $where_age['status'] = array('not in','1,9');
                $is_agen = Db::name('Agency')->where($where_age)->field('id')->find();
                if(!$is_agen){
                    $final_list[] = $nev;
                }
            }
            $final['need_list'] = $final_list;
            $this->apiResponse('1','成功',$final);
        }else{
            $this->apiResponse('0','该地区暂无匹配需求');
        }

    }
    /**
     * @title  中介报价(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediaryneed/send
     * @method POST
     * @param name:need_id type:varchar require:1 default: other: desc:需求id
     * @param name:housing_price type:varchar require:1 default: other: desc:房价
     * @param name:assessment_tax type:varchar require:1 default: other: desc:评估税
     * @param name:value_added_tax type:int require:1 default: other: desc:增值税
     * @param name:security_tax type:varchar require:1 default: other: desc:贷款担保税
     * @param name:deed_tax type:varchar require:1 default: other: desc:契税
     * @param name:else_tax type:varchar require:1 default: other: desc:其他税费
     * @param name:personal_income_tax type:tinyint require:1 default: other: desc:个税
     * @param name:service_charge type:tinyint require:1 default: other: desc:居间服务费
     * @param name:else type:int require:1 default: other: desc:备注
     * @param name:token type:varchar require:1 default: other: desc:token
     * @return data:''@
     * @data is_loan:'全款'
     * @data is_contract:'非包干价'
     */
    public function send(Request $request)
    {
//        header('Access-Control-Allow-Origin:*');
        if($request->Post('token') && $request->Post('housing_price')){
//            判空,处理
            Db::startTrans();
            $validate = Loader::validate('IntermediaryNeed');
            if(!$validate->scene('offer')->check($_POST)){
                return json(array('code'=>0,'msg'=>$validate->getError()));
            }
//            加数据库
            $data = $request->Post();
            $token = $data['token'];
            $model = new AgencyModel();
            $where_mem['token'] = $data['token'];
            $mem_id = Db::name('Member')->where($where_mem)->field('id')->find();
            $data['agency_id'] = $mem_id['id'];
//            判断是否已发布过
            $where_isIsset['need_id'] = $data['need_id'];
            $where_isIsset['agency_id'] = $mem_id['id'];
            $where_isIsset['status'] = array('not in','1,9');
            $find_isset = $model->where($where_isIsset)->find();
            if($find_isset){
                Db::rollback();
                $middle_final['need_id'] = $data['need_id'];
                $this->apiResponse('0','该房屋您已参与报价',$middle_final);
            }
//            判断是否存在
            $where_issetNeed['id'] = $data['need_id'];
            $where_issetNeed['status'] = 3;
            $issetNeed = Db::name('UserNeed')->where($where_issetNeed)->find();
            if(!$issetNeed){
                Db::rollback();
                $this->apiResponse('0','需求错误');
            }
            if(empty($data['assessment_tax'])){
                $data['assessment_tax'] = 0;
            }
            if(empty($data['value_added_tax'])){
                $data['value_added_tax'] = 0;
            }
            if(empty($data['security_tax'])){
                $data['security_tax'] = 0;
            }
            if(empty($data['deed_tax'])){
                $data['deed_tax'] = 0;
            }
            if(empty($data['else_tax'])){
                $data['else_tax'] = 0;
            }
            if(empty($data['personal_income_tax'])){
                $data['personal_income_tax'] = 0;
            }
            if(empty($data['service_charge'])){
                $data['service_charge'] = 0;
            }
            $data['all_price'] = $data['housing_price'] + $data['assessment_tax'] + $data['value_added_tax'] + $data['security_tax'] + $data['deed_tax'] + $data['else_tax'] + $data['personal_income_tax'] + $data['service_charge'];
//            查询中介保证金
//            $where_inteRule['status'] = 1;
            $where_inteRule['low'] = array('lt',$data['housing_price']);
            $where_inteRule['height'] = array('egt',$data['housing_price']);
            $inte_money = Db::name('IntermediaryRule')->where($where_inteRule)->field('money')->find();
            if(!$inte_money){
                Db::rollback();
                $this->apiResponse('0','保证金获取失败');
            }else{
                $data['deposit'] = $inte_money['money'];
                $final['pay_money'] = $data['deposit'];
            }
            unset($data['token']);
            $save = $model->save($data);
            $final['intermediaryNeed_id'] = $model->getLastInsID();

            if($save){
                $where_addCount['token'] = $token;
                $add_count = Db::name('member')->where($where_addCount)->setInc('count_agency',1);
                if(!$add_count){
                    Db::rollback();
                }else{
                    Db::commit();
                    $this->apiResponse('1','添加成功',$final);
                }

            }else{
                Db::rollback();
                $this->apiResponse('0','添加失败');
            }
        }else{
//            问答内容
            $where_pro['status'] = 1;
            $where_pro['type'] = 2;
            $list = Db::name('NeedAnswer')->where($where_pro)->order("update_time desc")->field("title,content,id,type")->select()->toArray();
            foreach ($list as $lk=>$lv){
                $list[$lk]['content'] = htmlspecialchars_decode($lv['content']);
            }
            if($list){
                $final['content'] = $list;
//                查询用户发布需求
                if(!$request->param('need_id')){
                    $this->apiResponse('0','请选择报价房屋');
                }
                $where_need['id'] = $request->param('need_id');
                $where_need['status'] = 3;
                $need = Db::name('UserNeed')->where($where_need)->field("loan,contract")->find();
                if(!$need){
                    $this->apiResponse('0','该需求暂不可报价');
                }
//                全款
                if($need['loan'] == 2 ){
                    $final['is_loan'] = 1;
                }
//                包干价
                if($need['contract'] == 2 ){
                    $final['is_contract'] = 1;
                }
                $this->apiResponse('1','成功',$final);
            }else{
                $this->apiResponse('0','暂无内容');
            }
        }
    }

    /**
     * @title  中介报价字段筛选
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/Intermediaryneed/choose
     * @method POST
     * @param name:need_id type:varchar require:1 default: other: desc:需求id
     * @param name:token type:varchar require:1 default: other: desc:token
     * @return data:''@
     * @data is_loan:'全款'
     * @data is_contract:'非包干价'
     */
    public function choose(){
        $data = $this->request->param();
        $where_need['status'] = array('neq',9);
        $where_need['id'] = $data['need_id'];
        $need = Db::name('UserNeed')->where($where_need)->field('loan,contract')->find();
        if($need){
            $final['need'] = $need;
            $this->apiResponse('1','成功',$final);
        }else{
            $this->apiResponse('0','需求错误');
        }
    }




}