IntermediaryneedController.php 11.3 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 = 2;
        $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")->limit($size)->page($page)->select()->toArray();
        $count = count($need_list);
        $final['all_page'] = ceil($count/$size);

        if($need_list){
//            筛除已报价
            foreach ($need_list as $nek=>$nev){
                $where_age['need_id'] = $nev['id'];
                $where_age['agency_id'] = $member_find['id'];
                $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'];
            $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','需求错误');
            }
            $data['all_price'] = $data['housing_price']*10000 + $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'] = 2;
                $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','暂无内容');
            }
        }
    }


//    报价支付(付款成功,报价人数+1)
    public function needPay(Request $request){
//        添加订单
        Db::startTrans();
        $data = $request->param();
        if(!$data['need_id']){
            $this->apiResponse('0','需求不能为空');
        }
//        查询金额
        $where_need['id'] = $data['need_id'];
        $where_need['status'] = 1;
        $should_pay = Db::name('UserNeed')->where($where_need)->field('pay_money')->find();
        if(!$should_pay){
            $this->apiResponse('0','需求错误');
        }else{
            if(!$should_pay['pay_money']){
                $this->apiResponse('0','诚意金金额有误');
            }
        }
//        拼字度
        $add['need_id'] = $data['need_id'];
        $add['money'] = $should_pay['pay_money'];
        $add['name'] = '诚意金缴纳';
        $add['type'] = 1;
        $add['is_get'] = 2;
        $add['status'] = 1;
//        加入数据库
        $model = new OrderModel();
        $save = $model->save($add);
        if($save){
            Db::commit();
        }else{
            Db::rollback();
        }
//        支付(未完)
echo "<pre/>";
print_r('3333');
die;
//        更改订单状态
//        成功
        $update['pay_time'] = time();
        $update['status'] = time();
        $update = $model->isUpdate(true)->allowField(true)->save($update);
        if($update){
            $this->apiResponse('1','支付成功');
        }

    }






}