Building.php 4.1 KB
<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Auth;
use app\school\model\project_appointment\Building as BuildingModel;
use app\school\model\project_appointment\Room;
use app\school\model\project_appointment\Room as RoomModel;
use app\school\model\project_appointment\Floor as FloorModel;
use app\school\model\project_appointment\RoomOrder ;
use think\Cookie;
use think\Exception;
use think\Db;

/**
 * Class Building
 * 预约申请
 */
class Building extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';
    protected $building = null;

    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 根据楼宇获取对应楼层列表
     */
    public function getFloorList()
    {
        $buildingId = $this->request->param('building_id','');
        if(!$buildingId) {
            return false;
        }
        $model = new FloorModel();
        $floorList = $model->getListByBuildingId($buildingId);

        $this->success('成功',null,collection($floorList)->toArray());
    }

    /**
     * 根据查询条件 返回房间列表
     */
    public function getRoomList()
    {
        $params = $this->request->param();
        $where = [];
        if(isset($params['room_type']) && !empty($params['room_type'])) {
            $where['room_type'] = $params['room_type'];
        }
        if(isset($params['building_id']) && !empty($params['building_id'])) {
            $where['building_id'] = $params['building_id'];
        }

        if(isset($params['floor_id']) && !empty($params['floor_id'])) {
            $where['floor_id'] = $params['floor_id'];
        }
        $model = new RoomModel();
        $list = $model->where($where)->where(['is_use' => 1])->select();
        $this->success('成功',null,collection($list)->toArray());
    }


    public function roomApply()
    {
        $userInfo = $this->auth->getUser();
        if(!$userInfo) {
//            $this->redirect('/');
            $this->success('请登入','/index/student_login/index');
        }
        $params = $this->request->post();
        if(!isset($params['room_id']) || empty($params['room_id'])){
            $this->error('请选择房间');
        }
        if(strtotime($params['start_time']) <= time()) {
            $this->error('请选择未来时间');
        }
        $startTime = strtotime($params['apply_date'].$params['start_time']);
        $endTime = strtotime($params['apply_date'].$params['end_time']);

        if($startTime == $endTime) {
            $this->error('预约开始时间不能等于结束时间');
        }

        if($endTime <= $startTime) {
            $this->error('结束时间不能小于开始时间');
        }

        $start = $startTime - 1;

        $roomInfo = (new RoomModel())
            ->field('r.id')
            ->alias('r')
            ->join('project_floor f','r.floor_id = f.id','inner')
            ->join('project_building b','r.building_id = b.id','inner')
            ->where('f.deletetime',NULL)
            ->where('b.deletetime',NULL)
            ->where(['r.id' => $params['room_id']])
            ->find();
        if(!$roomInfo) {
            $this->error('该房间不存在或已经被下架');
        }
        $model = new RoomOrder();
        $res = Db::name('project_room_order')
            ->whereOr("start_time >= {$start} AND start_time <= {$endTime}")
            ->whereOr("start_time <= {$start} AND end_time >= {$endTime}")
            ->whereOr("end_time >= {$start} AND end_time <= {$endTime}")
            ->having("room_id = {$params['room_id']}")
            ->select();
        if($res) {
            $this->error('该时间存在已经被预约,请重新选择');
        }

//
        $params['start_time'] = $startTime;
        $params['end_time'] = $endTime;
        $params['user_id'] = $userInfo['id'];
        $result = $model->allowField(true)->save($params);
        if(!$result) {
            $this->error('预约失败,请重新尝试');
        }

        $this->success('预约成功','/index/sx_basement/index');


    }

}