BuildingEnterVerify.php 6.8 KB
<?php

namespace app\school\controller\project_appointment;

use app\common\controller\DoubleBase;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\school\model\project_appointment\BuildingEnter;
use app\school\model\project_appointment\BuildingEnterVerify as BuildingEnterVerifyM;
use app\school\model\project_appointment\RoomOrder;
use app\school\model\project_appointment\Room;
use think\Url;

/**
 * 续租退孵
 */
class BuildingEnterVerify extends DoubleBase
{
    protected $noNeedRight = ['*'];
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new BuildingEnterVerifyM();
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->field('v.*,v.createtime as v_createtime,en.*,s.name,c.college_name,u.username,u.mobile')
                ->alias('v')
                ->join('project_building_enter en','v.enter_id = en.id')
                ->join('user u','en.user_id = u.id','left')
                ->join('manystore_shop s','en.school_id = s.id','left')
                ->join('college c','en.college_id = c.id','left')
                ->where(['en.school_id' => SCHOOL_ID,'en.status' => 2])
                ->order('en.createtime', 'desc')
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    //退孵  续租审核同意
    public function agree()
    {
        $id = $this->request->param('id');
        if(empty($id)) {
            $this->error('参数错误');
        }
        $info = $this->model->where(['id' => $id])->find();
        if(!$info) {
            $this->error('该记录不存在');
        }
        $roomOrder = (new RoomOrder())->where(['enter_id'=>$info['enter_id']])->find();
        if(!$roomOrder) {
            $this->error('入驻场地不存在或已下线');
        }
        Db::startTrans();
        try {
            //退孵
            if($info['verify_type'] == BuildingEnterVerifyM::BUILD_EXIT) {
                $enterInfo = (new BuildingEnter())->where(['id' => $info['enter_id']])->find();
                if(!$enterInfo) {
                    $this->error('入驻信息不存在');
                }
                $room = (new Room())->where(['id' => $roomOrder['room_id']])->find();
                if(!$room) {
                    $this->error('房间不存在或已下线');
                }
                $room->is_use = 1;
                $room->save();
                $roomOrder->start_time = 0;
                $roomOrder->end_time = 0;
                $roomOrder->save();
                $roomOrder->delete();$roomOrder->delete();
            }else{
                //续租
                if($info['time_limit'] == 4) {
                    //自定义时间
                    if($info['renewal_time'] <= $roomOrder['end_time']) {
                        $this->error('续租日期不能小于当前预约结束日期');
                    }
                    $endTime = $info['renewal_time'];
                }else if ($info['time_limit'] == 1){
                    $endTime = strtotime("+6month",$roomOrder['end_time']);
                }else if ($info['time_limit'] == 2) {
                    $endTime = strtotime("+1year",$roomOrder['end_time']);
                }else {
                    $endTime =strtotime("+2year",$roomOrder['end_time']);
                }
                $roomOrder->end_time = $endTime;
                $roomOrder->save();
            }
            $info->verify_status = 2;
            $info->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('审核成功');
    }

    //审核驳回
    public function reject()
    {
        $id  = $this->request->param('id');
        $info = $this->model->where(['id' => $id])->find();
        $info->verify_status = 3;
        if(!$info->save()) {
            $this->error('驳回失败,稍后请重试');
        }
        $this->success('成功');
    }

    public function company_index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->field('en.*,u.username,u.mobile,s.name as school_name,c.college_name,p.start_time,p.end_time,b.building_name,f.floor_name,r.room_name')
                ->alias('en')
                ->join('user u','en.user_id = u.id','left')
                ->join('manystore_shop s','en.school_id = s.id','left')
                ->join('college c','en.college_id = c.id','left')
                ->join('project_room_order p','en.id = p.enter_id','inner')
                ->join('project_room r','p.room_id = r.id','left')
                ->join('project_building b','r.building_id = b.id','left')
                ->join('project_floor f','r.floor_id = f.id','left')
                ->where(['en.school_id' => SCHOOL_ID,'en.status' => 2])
                ->order('en.createtime', 'desc')
                ->paginate($limit);
            if($list) {
                foreach ($list as &$value) {
                    $value['expire_type'] = '未到期';
                    if(isset($value['end_time'])) {
                        if(time() > $value['end_time']) {
                            $value['expire_type'] = '已到期';
                        }
                    }else{
                        $value['expire_type'] = '已到期';
                    }
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    //强制到期
    public function expire()
    {
        $id  = $this->request->param('id');
        $roomOrder = (new RoomOrder())->where(['enter_id'=>$id])->find();
        if(!$roomOrder) {
            $this->error('该记录不存在');
        }
        $roomInfo = (new Room())->where(['id' => $roomOrder['room_id']])->find();
        Db::startTrans();
        try {
            $roomOrder->start_time = 0;
            $roomOrder->end_time = 0;
            $roomOrder->save();
            $roomInfo->is_use = 1;
            $roomInfo->save();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }
}