ThirdsendController.php 20.8 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
<?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\UserNeedModel;
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分钟后发布(sure)(分钟执行)
    public function thirdSend(){
        Db::startTrans();
        $where_need['status'] = 2;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,pay_time')->select()->toArray();
        $time = time();
        $change_arr = array();
        foreach ($userNeedList as $userk=>$userv){
            if($time >= $userv['pay_time']+1800){
                $change_arr[] = $userv['id'];
            }
        }
        if($change_arr){
            $where_save['id'] = array('in',$change_arr);
            $save['status'] = 3;
            $save['update_time'] = time();
            $fianl_save = Db::name('UserNeed')->where($where_save)->update($save);
            if($fianl_save){
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }

//    规定时间结束报价(sure)(分钟执行)
    public function over()
    {
        Db::startTrans();
//        查询用户发布需求信息
        $where_need['status'] = 3;
        $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,quotation_time,create_time,pay_time,user_id')->select()->toArray();
        $time = time();
//        循环需求,查询相应报价
        foreach ($userNeedList as $uNeedk => $uNeedv) {
            $final_time = $uNeedv['pay_time'] + $uNeedv['quotation_time'] * 86400;
            if ($time >= $final_time) {
                $us_middleid = $uNeedv['id'];
                $where_agency['a.status'] = 2;
                $where_agency['a.pay_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) {
                            $else_changeList[] = $mok;
                            if ($k < 3) {
                                $fin_list[$k]['id'] = $mok;
                                $fin_list[$k]['money'] = $mov;
                                $fin_arr[] = $mok;
                            }
                            $k += 1;
                        }
                        if($agen_arr){
                            if(!in_array($agen_arr[0]['id'],$fin_arr)){
//                            品牌中介不在取出三个最低价中
                                $middle_agen[0] = $agen_arr[0]['id'];
                                $get_list = array_merge($fin_arr,$middle_agen);
                            }else{
                                $get_list = $fin_arr;
                            }
                        }
                        foreach ($else_changeList as $getk=>$getv){
                            if(in_array($getv,$get_list)){
                                $change_final['id'] = $getv;
                                $change_final['status'] = 4;
                            }else{
                                $change_final['id'] = $getv;
                                $change_final['status'] = 3;
                            }
                            $agencyModel = new  AgencyModel();
                            $fianl_AgsaveFirst = $agencyModel->isUpdate(true)->save($change_final);
                            if(!$fianl_AgsaveFirst){
                                Db::rollback();
                            }
                        }
//                        报价成功,更改状态
                        $userSave_statusFirst['id'] = $uNeedv['id'];
                        $userSave_statusFirst['status'] = 5;
                        $userSave_statusFirst['choose_time'] = time();
                        $userNeedModel = new UserNeedModel();
                        $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_statusFirst);
                        if(!$fianl_Ussave){
                            Db::rollback();
                        }
                    } else {
//                报价小于3人,更改订单状态,报价失败
                        foreach ($agency_list as $changeka=>$changeva){
                            $change_final['id'] = $changeva['id'];
                            $change_final['status'] = 7;
                            $agencyModel = new  AgencyModel();
                            $fianl_AgsaveNext = $agencyModel->isUpdate(true)->save($change_final);
                            if(!$fianl_AgsaveNext){
                                Db::rollback();
                            }
//                            退回中介保证金
                            $add_a['user_id'] = $changeva['agency_id'];
                            $add_a['title'] = '报价人数不足';
                            $add_a['type'] = 1;
                            $add_a['money'] = $changeva['deposit'];
                            $add_a['create_time'] = time();
                            $add_a['update_time'] = time();
                            $amoney_id = Db::name('AmoenyDetail')->insertGetId($add_a);
                            if (!$amoney_id) {
                                Db::rollback();
                            }
//                            用户表增加金额
                            $where_member['type'] = 2;
                            $where_member['id'] = $changeva['agency_id'];
                            $add_money = Db::name('Member')->where($where_member)->setInc('money',$changeva['deposit']);
                            if (!$add_money) {
                                Db::rollback();
                            }

                        }
                        $userSave_status['id'] = $uNeedv['id'];
                        $userSave_status['status'] = 4;
                        $userNeedModel = new UserNeedModel();
                        $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                        if(!$fianl_Ussave){
                            Db::rollback();
                        }

//                        退回保证金
//                        用户
//                           加用户金额明细表
                        $add_del['need_id'] = $uNeedv['id'];
                        $add_del['user_id'] = $uNeedv['user_id'];
                        $add_del['title'] = '报价人数不足';
                        $add_del['type'] = 1;
                        $add_del['money'] = $uNeedv['pay_money'];
                        $add_del['create_time'] = time();
                        $add_del['update_time'] = time();
                        $umoney_id = Db::name('UmoenyDetail')->insertGetId($add_del);
                        if (!$umoney_id) {
                            Db::rollback();
                        }
                        $where_memberNext['type'] = 1;
                        $where_memberNext['id'] = $uNeedv['user_id'];
                        $add_moneyNext = Db::name('Member')->where($where_memberNext)->setInc('money',$uNeedv['pay_money']);
                        if (!$add_moneyNext) {
                            Db::rollback();
                        }

                    }
                } else {
//                没有报价
                    $userSave_status['id'] = $uNeedv['id'];
                    $userSave_status['status'] = 4;
                    $userNeedModel = new UserNeedModel();
                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($userSave_status);
                    if(!$fianl_Ussave){
                        Db::rollback();
                    }

//                        退回保证金
//                        用户
//                           加用户金额明细表
                    $add_del['need_id'] = $uNeedv['id'];
                    $add_del['user_id'] = $uNeedv['user_id'];
                    $add_del['title'] = '报价人数不足';
                    $add_del['type'] = 1;
                    $add_del['money'] = $uNeedv['pay_money'];
                    $add_del['create_time'] = time();
                    $add_del['update_time'] = time();
                    $umoney_id = Db::name('UmoenyDetail')->insertGetId($add_del);
                    if (!$umoney_id) {
                        Db::rollback();
                    }
                    $where_memberNext['type'] = 1;
                    $where_memberNext['id'] = $uNeedv['user_id'];
                    $add_moneyNext = Db::name('Member')->where($where_memberNext)->setInc('money',$uNeedv['pay_money']);
                    if (!$add_moneyNext) {
                        Db::rollback();
                    }

                }
//            插入数据库
//            用户需求表
//                $userNeedModel = new UserNeedModel();
//                foreach ($userSave_status as $fck=>$fcv){
//                    $fianl_Ussave = $userNeedModel->isUpdate(true)->save($fcv);
//                    if(!$fianl_Ussave){
//                        Db::rollback();
//                    }
//                }
//            中介报价表
                Db::commit();
            }

        }
    }

//    三天用户选择(sure)(天执行)
        public function userChoose(){
            Db::startTrans();
//        查询用户发布需求信息
            $where_need['status'] = 5;
            $userNeedList = Db::name('UserNeed')->where($where_need)->field('id,choose_time,user_id,pay_money,choose_time')->select()->toArray();
            if($userNeedList){
               foreach ($userNeedList as $needk=>$needv) {
                   if (time() >= ($needv['choose_time'] + 259200)) {
                       $where_agency['need_id'] = $needv['id'];
                       $where_agency['status'] = 4;
                       $agency_list = Db::name('Agency')->where($where_agency)->select()->toArray();
                       if ($agency_list) {
                           $where_agencyUpdate['need_id'] = $needv['id'];
                           $where_agencyUpdate['status'] = 4;
                           $agency_save['status'] = 8;
                           $agency_save['update_time'] = time();
                           $agency_result = Db::name('Agency')->where($where_agencyUpdate)->update($agency_save);
                           if (!$agency_result) {
                               Db::rollback();
                           }
                       }
                       $where_needUpdate['id'] = $needv['id'];
                       $needUpdate['status'] = 6;
                       $need_result = Db::name('UserNeed')->where($where_needUpdate)->update($needUpdate);
                       if (!$need_result) {
                           Db::rollback();
                       } else {
//                           加用户金额明细表
                           $add_del['need_id'] = $needv['id'];
                           $add_del['user_id'] = $needv['user_id'];
                           $add_del['title'] = '未选择中介';
                           $add_del['type'] = 2;
                           $add_del['money'] = $needv['pay_money'];
                           $add_del['create_time'] = time();
                           $add_del['update_time'] = time();
                           $umoney_id = Db::name('UmoenyDetail')->insertGetId($add_del);
                           if (!$umoney_id) {
                               Db::rollback();
                           }
//                           中介金额明细表
                           foreach ($agency_list as $agenk=>$agenv){
                               $add_a['ag_id'] = $agenv['id'];
                               $add_a['user_id'] = $agenv['agency_id'];
                               $add_a['title'] = '用户未选择中介';
                               $add_a['type'] = 1;
                               $add_a['money'] = $agenv['deposit'];
                               $add_a['create_time'] = time();
                               $add_a['update_time'] = time();
                               $amoney_id = Db::name('AmoenyDetail')->insertGetId($add_a);
                               if (!$amoney_id) {
                                   Db::rollback();
                               }
                               $where_member['type'] = 2;
                               $where_member['id'] = $agenv['agency_id'];
                               $add_money = Db::name('Member')->where($where_member)->setInc('money',$agenv['deposit']);
                               if (!$add_money) {
                                   Db::rollback();
                               }
                           }

                       }
                   }
               }
            }
            Db::commit();


        }


//    三天中介确认
        public function intermediaryChoose(){
            Db::startTrans();
            $where_need['status'] = 7;
            $need_list = Db::name('UserNeed')->where($where_need)->field('id,choose_time')->select()->toArray();
            if($need_list){
                foreach ($need_list as $needk=>$needv){
                    if(time() >= ($needv['choose_time']+259200)){
                        $where_agency['status'] = 5;
                        $where_agency['need_id'] = $needv['id'];
                        $agency = Db::name('Agency')->where($where_agency)->find();
//                    修改中介报价状态
                        if($agency){
                            $where_changeAgency['id'] = $agency['id'];
                            $changeAgency['status'] = 10;
                            $changeAgency['update_time'] = time();
                            $update_a = Db::name('Agency')->where($where_changeAgency)->update($changeAgency);
                            if(!$update_a){
                                Db::rollback();
                            }
                        }
//                    增加中介保证金变化
                        $add_a['ag_id'] = $agency['id'];
                        $add_a['user_id'] = $agency['agency_id'];
                        $add_a['title'] = '中介未确认';
                        $add_a['type'] = 2;
                        $add_a['money'] = $agency['deposit'];
                        $add_a['create_time'] = time();
                        $add_a['update_time'] = time();
                        $amoney_id = Db::name('AmoenyDetail')->insertGetId($add_a);
                        if (!$amoney_id) {
                            Db::rollback();
                        }
//                    修改用户需求状态
                        $where_needChange['id'] = $needv['id'];
                        $update_need['status'] = 8;
                        $update_need['update_time'] = time();
                        $change_needFinal = Db::name('UserNeed')->where($where_needChange)->update($update_need);
                        if (!$change_needFinal) {
                            Db::rollback();
                        }
//                    增加用户保证金,余额
                        $add_del['need_id'] = $needv['id'];
                        $add_del['user_id'] = $needv['user_id'];
                        $add_del['title'] = '中介未确认';
                        $add_del['type'] = 1;
                        $add_del['money'] = $needv['pay_money'];
                        $add_del['create_time'] = time();
                        $add_del['update_time'] = time();
                        $umoney_id = Db::name('UmoenyDetail')->insertGetId($add_del);
                        if (!$umoney_id) {
                            Db::rollback();
                        }
                        $where_member['type'] = 1;
                        $where_member['id'] = $needv['user_id'];
                        $add_money = Db::name('Member')->where($where_member)->setInc('money',$needv['pay_money']);
                        if (!$add_money) {
                            Db::rollback();
                        }
                    }
                }
                Db::commit();
            }
        }




}