房间的营业时间

This commit is contained in:
2026-01-20 09:42:30 +08:00
parent 07672be58b
commit a75b3b173a
2 changed files with 169 additions and 2 deletions

View File

@@ -487,4 +487,18 @@ class Room extends BaseCom
$reslut = model('Room')->task_jump_room_recommend($task_id);
return V(1, '成功', $reslut);
}
//设置房间的营业时间
public function set_room_business_time()
{
$room_id = input('room_id', 0);
$start_time = input('start_time', '00:00:00');
$end_time = input('end_time', '00:00:00');
if(!$room_id){
return V(0, '参数错误');
}
$reslut = model('Room')->set_room_business_time($this->uid, $room_id, $start_time, $end_time);
return V($reslut['code'], $reslut['msg'], $reslut['data']);
}
}

View File

@@ -227,11 +227,43 @@ class Room extends Model
}
}
// $list = db::name('vs_room')
// ->field('id as room_id,room_number,user_id,room_name,room_cover,room_password,today_hot_value as hot_value,label_id,is_show_room')
// ->where($map)
// ->order('sort desc,hot_value desc,id asc')
// ->page($page, $page_limit)->select();
$currentTime = date('H:i:s');
$list = db::name('vs_room')
->field('id as room_id,room_number,user_id,room_name,room_cover,room_password,today_hot_value as hot_value,label_id,is_show_room')
->where($map)
// 营业时间判断(允许全天营业的房间)
->where(function($query) use ($currentTime) {
// 全天营业的房间start_time和end_time都为00:00:00
$query->where(function($q) {
$q->where('start_time', '00:00:00')
->where('end_time', '00:00:00');
})->whereOr(function($q) use ($currentTime) {
// 正常时间限制的房间
$q->where(function($q2) use ($currentTime) {
$q2->where('start_time', '<=', $currentTime)
->where('end_time', '>=', $currentTime)
->where('start_time', '<=', 'end_time')
->where('start_time', '<>', '00:00:00'); // 排除全天营业
})->whereOr(function($q2) use ($currentTime) {
// 跨天营业
$q2->where('start_time', '>', 'end_time')
->where(function($q3) use ($currentTime) {
$q3->where('start_time', '<=', $currentTime)
->whereOr('end_time', '>=', $currentTime);
});
});
});
})
->order('sort desc,hot_value desc,id asc')
->page($page, $page_limit)->select();
->page($page, $page_limit)
->select();
foreach ($list as &$v){
$v['user_list'] = model('RoomUser')->get_room_user_list($v['room_id']);
@@ -264,17 +296,65 @@ class Room extends Model
$map['a.is_show_room'] = 1; // 是否显示房间 1是2否
$map['a.apply_status'] = 2; // 1待审核 2审核通过 3审核失败
$map['a.is_recommend'] = 2; // 2推荐
$currentTime = date('H:i:s');
$roominfo = db::name('vs_room')->alias('a')
->join('vs_room_visitor b', 'a.id = b.room_id AND b.is_online = 1', 'INNER')
->field('a.id as room_id,a.user_id,a.room_name,a.room_cover,count(b.id) as count')
->where($map)
// 营业时间判断(允许全天营业的房间)
->where(function($query) use ($currentTime) {
// 全天营业的房间start_time和end_time都为00:00:00
$query->where(function($q) {
$q->where('start_time', '00:00:00')
->where('end_time', '00:00:00');
})->whereOr(function($q) use ($currentTime) {
// 正常时间限制的房间
$q->where(function($q2) use ($currentTime) {
$q2->where('start_time', '<=', $currentTime)
->where('end_time', '>=', $currentTime)
->where('start_time', '<=', 'end_time')
->where('start_time', '<>', '00:00:00'); // 排除全天营业
})->whereOr(function($q2) use ($currentTime) {
// 跨天营业
$q2->where('start_time', '>', 'end_time')
->where(function($q3) use ($currentTime) {
$q3->where('start_time', '<=', $currentTime)
->whereOr('end_time', '>=', $currentTime);
});
});
});
})
->group('a.id,a.user_id,a.room_name,a.room_cover')
->order('count asc')
->find();
if(!$roominfo){
//随机获取一个房间
$roominfo = db::name('vs_room')->field('id as room_id,user_id,room_name,room_cover')
->where(['apply_status'=>2,'room_status'=>1,'is_show_room'=>1,'delete_time'=>0,'is_recommend'=>2])->orderRaw('rand()')->find();
->where(['apply_status'=>2,'room_status'=>1,'is_show_room'=>1,'delete_time'=>0,'is_recommend'=>2])
// 营业时间判断(允许全天营业的房间)
->where(function($query) use ($currentTime) {
// 全天营业的房间start_time和end_time都为00:00:00
$query->where(function($q) {
$q->where('start_time', '00:00:00')
->where('end_time', '00:00:00');
})->whereOr(function($q) use ($currentTime) {
// 正常时间限制的房间
$q->where(function($q2) use ($currentTime) {
$q2->where('start_time', '<=', $currentTime)
->where('end_time', '>=', $currentTime)
->where('start_time', '<=', 'end_time')
->where('start_time', '<>', '00:00:00'); // 排除全天营业
})->whereOr(function($q2) use ($currentTime) {
// 跨天营业
$q2->where('start_time', '>', 'end_time')
->where(function($q3) use ($currentTime) {
$q3->where('start_time', '<=', $currentTime)
->whereOr('end_time', '>=', $currentTime);
});
});
});
})
->orderRaw('rand()')->find();
}
if(empty($roominfo)){
return ['code' => 0, 'msg' => '暂无数据', 'data' => null];
@@ -679,6 +759,31 @@ class Room extends Model
return ['code' => 1, 'msg' => '成功', 'data' => ['total_amount' => $total_amount, 'total_earning' => $total_earning,'list' => $list_data_array]];
}
// 方法2转换为时间戳进行比较更精确
public function isBetweenTime($start, $end, $current = null) {
if ($current === null) {
$current = date('H:i:s');
}
// 将时间转换为今天的秒数
list($h1, $m1, $s1) = explode(':', $start);
list($h2, $m2, $s2) = explode(':', $end);
list($hc, $mc, $sc) = explode(':', $current);
$startSec = $h1 * 3600 + $m1 * 60 + $s1;
$endSec = $h2 * 3600 + $m2 * 60 + $s2;
$currentSec = $hc * 3600 + $mc * 60 + $sc;
if ($startSec <= $endSec) {
// 同一天内
return $currentSec >= $startSec && $currentSec <= $endSec;
} else {
// 跨天
return $currentSec >= $startSec || $currentSec <= $endSec;
}
}
//进入房间前的判断
public function before_join_room_check($user_id, $room_id) {
$room = db::name('vs_room')->where(['id' => $room_id,'apply_status' => ['in',[1,2]]])->find();
@@ -713,6 +818,19 @@ class Room extends Model
}
}
//房间是否在营业时间
$startTime = $room['start_time'];
$endTime = $room['end_time'];
// 获取当前时间24小时制
$currentTime = date('H:i:s');
//查看现在时间是否在营业时间
$isBusinessHours = $this-> isBetweenTime($startTime, $endTime, $currentTime);
// 输出结果
if (!$isBusinessHours) {
return ['code' => 0, 'msg' => '当前时间不在营业时间内', 'data' => ''];
}
//用户是否在其他房间(重构)
//1其他房间游戏中不让退出 给前端返回房间ID
//2允许退出强行退出……
@@ -3610,4 +3728,39 @@ class Room extends Model
}
return ['code' => 1, 'msg' => '操作成功', 'data' => null];
}
//设置房间的营业时间
public function set_room_business_time($user_id,$room_id,$start_time,$end_time)
{
$user_type = 0;
//判断是否是房主
$room_owner = db::name('vs_room')->where(['id' => $room_id,'user_id' => $user_id])->find();
//判断是否是管理主持
$room_manager = db::name('vs_room_host')->where(['room_id' => $room_id,'user_id' => $user_id,'delete_time' => null])->find();
if($room_manager){
$user_type = 1;
}
if(!$room_owner && !$room_manager){
return ['code' => 0, 'msg' => '没有权限', 'data' => null];
}
$res = db::name('vs_room')->where('id',$room_id)->update(['start_time' => $start_time,'end_time' => $end_time]);
$data = [
'room_id' => $room_id,
'user_id' => $user_id,
'user_type' => $user_type,
'start_time' => $start_time,
'end_time' => $end_time,
'create_time' => time(),
];
$res1 = db::name('vs_room_business_time_log')->insert($data);
if($res && $res1){
return ['code' => 1, 'msg' => '操作成功', 'data' => null];
}else{
return ['code' => 0, 'msg' => '操作失败', 'data' => null];
}
}
}