Building.php
4.1 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
<?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');
}
}