ThirdsendController.php 8.7 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 cmf\controller\HomeBaseController;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;
use think\Loader;
use think\Config;
use think\captcha\Captcha;
use think\Url;
use SmsDemo;
//use think\Route;
/**
 * @title 30分钟后发布
 * @description 接口说明
 * @group 接口分组
 */

class ThirdsendController extends HomeBaseController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

//    查询中介报价
//  $need_id 需求id
    public function Offer($user_id,$need_id){
        $where_agency['status'] = 2;
        $where_agency['create_time'] = array('lt',time());
        $where_agency['need_id'] = $need_id;
        $agency_list = Db::name('Agency')
            ->where($where_agency)
//            ->field("id,housing_price,assessment_tax,security_tax,else_tax,service_charge")
            ->select()->toArray();
        if($agency_list){
            foreach ($agency_list as $agencyk=>$agencyv){
//                $final_list[$agencyk] = $agencyv;
//                $final_list[$agencyk]['agency_id'] = $agencyv['id'];
//                $final_list[$agencyk]['user_id'] = $user_id;
                $money = $agencyv['housing_price']*10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'] ;
                $money_arr[] = $money;
                $fianlMoney = substr($money, 0,-4);
                $agency_list[$agencyk]['true_money'] = $money;
//                $final_list[$agencyk]['money'] = $fianlMoney;
            }
            sort($money_arr);
            if(count($money_arr) > 3 ){
                foreach ($agency_list as $finalk=>$finalv){
                    foreach ($money_arr as $monryk=>$moneyv){
                        if($finalv['true_money'] == $moneyv){
                           $return[] =  $finalv;
                        }
                    }
                }
            }else{
                $save['status'] = 8;
            }

            return $return;
        }
    }



//    30分钟后发布
    public function thirdSend(){
        Db::startTrans();
        $where_need['status'] = 1;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,create_time')->select()->toArray();
        $time = time();
        $change_arr = array();
        foreach ($userNeedList as $userk=>$userv){
            if($userv['create_time'] <= ($time+60) &&  $userv['create_time'] >= $time){
                $change_arr[] = $userv['id'];
            }
        }
        if($change_arr){
            $where_save['id'] = array('in',$change_arr);
            $save['status'] = 2;
            $save['update_time'] = time();
            $fianl_save = Db::name('UserNeed')->where($where_save)->update($save);
            if($fianl_save){
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

//    规定时间结束报价
    public function over()
    {
        Db::startTrans();
//        查询用户发布需求信息
        $where_need['status'] = 2;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time')->select()->toArray();
        $change_arr = array();
        $time = time();
        $time = '1529734985';
//        循环需求,查询相应报价
        foreach ($userNeedList as $uNeedk => $uNeedv) {
            $final_time = $uNeedv['create_time'] + $uNeedv['quotation_time'] * 86400;
            if ($final_time <= ($time + 60) && $final_time >= $time) {
                $us_middleid = $uNeedv['id'];
                $where_agency['a.status'] = 2;
                $where_agency['a.create_time'] = array('lt', time());
                $where_agency['a.need_id'] = $uNeedv['id'];
                $where_agency['m.type'] = 2;
                $where_agency['m.status'] = 1;
                $agency_list = Db::name('Agency')->alias('a')
                    ->where($where_agency)
                    ->join("hp_Member m", 'm.id = a.agency_id')
                    ->field('a.*,m.company')
                    ->select()->toArray();
//                查询品牌企业
                $where_brand['status'] = 1;
                $band_list = Db::name('Brand')->where($where_brand)->column('name');
                $agen_arr = array();
                $money_arr = array();
                if ($agency_list) {
                    //        判断是否超过三个报价人
                    if (count($agency_list) >= 3) {
//                    查询三人中是否有品牌中介
//                        循环匹配品牌企业
                        foreach ($agency_list as $agencyk => $agencyv) {
                            foreach ($band_list as $brak => $brav) {
//                            判断是否在品牌企业中
                                if (strstr($brav, $agencyv['company']) !== false) {
//                                存在
                                    $agen_money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                                    if (empty($agen_arr)) {
                                        $agen_arr[0]['money'] = $agen_money;
                                        $agen_arr[0]['id'] = $agencyv['id'];
                                    } else {
//                                    判断大小
                                        if ($agen_money < $agen_arr[0]['money']) {
                                            $agen_arr[0]['money'] = $agen_money;
                                            $agen_arr[0]['id'] = $agencyv['id'];

                                        }
                                    }
                                }
                            }
//                        取出前三报价
                            $money = $agencyv['housing_price'] * 10000 + $agencyv['assessment_tax'] + $agencyv['security_tax'] + $agencyv['else_tax'] + $agencyv['service_charge'];
                            $money_list[$agencyv['id']] = $money;
                        }
                        asort($money_list);
                        $k = 0;
                        foreach ($money_list as $mok => $mov) {
                            if ($k < 3) {
                                $fin_list[$k]['id'] = $mok;
                                $fin_list[$k]['money'] = $mov;
                                $fin_arr[] = $mok;
                            }
                            $k += 1;
                        }
                        $ag_id = implode(',', $fin_arr);
                        $userSave_status['status'] = 5;
                        $agencySave_status['status'] = 4;

                    } else {
//                报价小于3人,更改订单状态,报价失败
                        $userSave_status['status'] = 4;
                        $agencySave_status['status'] = 7;
                    }
                } else {
//                没有报价
                    $userSave_status['status'] = 4;
                    $agencySave_status['status'] = 7;

                }
                $us_id = implode(',', $us_middleid);
//            插入数据库
//            用户需求表
                $where_us['id'] = array('in', $us_id);
                $fianl_Ussave = Db::name('UserNeed')->where($where_us)->update($userSave_status);
                if(!$fianl_Ussave){
                    Db::rollback();
                }
//            中介报价表
                $where_ag['id'] = array('in', $ag_id);
                $fianl_Agsave = Db::name('Agency')->where($where_ag)->update($agencySave_status);
                if(!$fianl_Agsave){
                    Db::rollback();
                }else{
                    Db::commit();
                }

            }

        }
    }

//    三天用户选择
        public function userChoose(){
            Db::startTrans();
//        查询用户发布需求信息
            $where_need['status'] = 2;
            $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time')->select()->toArray();



        }




//    三天中介确认
    public function intermediaryChoose(){

    }




}