SxBasement.php 6.2 KB
<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\school\model\project_appointment\Building as BuildingModel;
use app\school\model\project_appointment\AppointmentRule as AppointmentRuleModel;
use app\school\model\project_appointment\Room as RoomModel;
use app\index\model\BuildingEnter;
use app\school\model\station\Station;
use think\Db;

class SxBasement extends Frontend
{

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

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $roomTypeList = RoomModel::$room_type;
        $buildList = (new BuildingModel)->where("school_id",$this->school_id)->select();
        $roomList = (new RoomModel)->getCollegeRoom(["school_id"=>$this->school_id]);
        $enterRules = (new AppointmentRuleModel)->where(['school_id'=>$this->school_id])->find();
        if($enterRules) {
            $enterRules['data_url'] = json_decode($enterRules['data_url'],true);
        }
        //工位
        $stationList = Station::alias('a')
            ->field('a.id,a.station_name,count(sa.id) as appoint_count')
            ->join('fa_station_appointment sa','a.id = sa.station_id','LEFT')
            ->where(['a.school_id'=>$this->auth->school_id])
            ->group('a.id')
            ->select();
        //入驻公司
        $buildingEnter = BuildingEnter::where(['enter_type' => 2,'status' => 2,'school_id'=>$this->auth->school_id,'user_id' => $this->auth->id])->column('id,enter_project_name');
        $this->assign('roomTypeList',$roomTypeList);
        $this->assign('stationList',$stationList);
        $this->assign('enterRules',$enterRules);
        $this->assign('buildList',$buildList);
        $this->assign('roomList',$roomList);
        $this->assign('buildingEnter',$buildingEnter);
    }

    public function index()
    {
        $this->assign('index','index');
        return $this->view->fetch();
    }

    public function downFiles($file_url)
    {
        $file_url = $this->request->param('file_url');
        $file_name = $this->request->param('file_name');
        downloadFile($file_url,$file_name);
    }

    /**
     * 工位分配提交
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function stationApply()
    {
        $userInfo = $this->auth->getUser();
        if(!$userInfo) {
            $this->success('请登入','/index/student_login/index');
        }
        $params = $this->request->post();
        if(!isset($params['station_id']) || empty($params['station_id'])){
            $this->error('请选择工位');
        }
        if(!isset($params['enter_id']) || empty($params['enter_id'])){
            $this->error('请选择公司');
        }

        $startTime = strtotime($params['station_date'].$params['station_start_time']);
        $endTime = strtotime($params['station_date'].$params['station_end_time']);

        if($startTime <= time()) {
            $this->error('开始时间必须大于当前时间');
        }

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

        if($endTime <= $startTime) {
            $this->error('结束时间不能小于开始时间');
        }
        $stationInfo = Station::alias('a')
            ->field('a.id,a.station_name,a.capacity,count(sa.id) as appoint_count')
            ->join('fa_station_appointment sa','a.id = sa.station_id','LEFT')
            ->where('a.deletetime',NULL)
            ->where(['a.id' => $params['station_id']])
            ->find();
        if(!$stationInfo) {
            $this->error('该工位不存在或已经被下架');
        }
        if ($stationInfo['appoint_count']){
            $this->error('该工位已被预约');
        }
        if ($stationInfo['capacity'] < $params['station_user_count']){
            $this->error("该工位可容纳{$stationInfo['capacity']}人");
        }
        $res = Db::table('fa_station_appointment')
            ->whereOr("station_start_time >= {$startTime} AND station_start_time <= {$endTime}")
            ->whereOr("station_start_time <= {$startTime} AND station_end_time >= {$endTime}")
            ->whereOr("station_end_time >= {$startTime} AND station_end_time <= {$endTime}")
            ->having("station_id = {$params['station_id']}")
            ->select();
        if($res) {
            $this->error('该时间存在已经被预约,请重新选择');
        }
        $params['station_start_time'] = $startTime;
        $params['station_end_time'] = $endTime;
        $params['user_id'] = $userInfo['id'];
        $params['school_id'] = $userInfo['school_id'];
        $params['createtime'] = time();
        $result = Db::table('fa_station_appointment')->insert($params);
        if(!$result) {
            $this->error('预约失败,请重新尝试');
        }
        $this->success('预约成功','/index/sx_basement/index');
    }

    /**
     * 工位列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function station(){
        $params = $this->request->post();
        $startTime = strtotime($params['station_date'].$params['station_start_time']);
        $endTime = strtotime($params['station_date'].$params['station_end_time']);
        //工位
        $stationList = Station::alias('a')
            ->field('a.id,a.station_name,count(sa.id) as appoint_count')
            ->join('fa_station_appointment sa','a.id = sa.station_id','LEFT')
            ->whereOr("station_start_time >= {$startTime} AND station_start_time <= {$endTime}")
            ->whereOr("station_start_time <= {$startTime} AND station_end_time >= {$endTime}")
            ->whereOr("station_end_time >= {$startTime} AND station_end_time <= {$endTime}")
            ->where('a.school_id',$this->auth->school_id)
            ->group('a.id')
            ->select();
        $this->success('查询成功',$stationList);
    }
}