Files
yusheng-php/application/cron/controller/DaySeconds.php
2025-12-18 18:21:06 +08:00

141 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\cron\controller;
use think\Db;
/*
* 定时任务,每秒执行的方法
*/
class DaySeconds
{
/*
* 运行函数
*/
function index()
{
echo "清除房间热度值:\n";
$this->clear_room_today_hot_value();//0点以后房间热度值清零
echo "\n";
echo "清除房间离线超24小时的用户\n";
$this->clear_room_offline_user();//0点以后清除房间离线超24小时用户
echo "\n";
echo "系统降身价:\n";
$this->system_downgrade();
echo "\n";
}
/*
* 0点以后清除房间离线超24小时用户
* 配置:定时脚本每天 0点 执行
* 配置:
*/
public function clear_room_offline_user() {
$room = db::name('vs_room_visitor')->where(['is_online' => 2])->select();
if($room){
foreach ($room as $value) {
$time = time() - $value['updatetime'];
if($time > 86400){
model('api/Room')->quit_room($value['user_id'], $value['room_id'],$value['user_id']);
}
}
}
}
/*
* 0点以后房间热度值清零
* 配置:定时脚本每天 0点 执行
* 配置:
*/
public function clear_room_today_hot_value() {
$where = [];
$where['delete_time'] = 0;
$where['is_show_room'] = 1;
$room = db::name('vs_room')->where($where)->select();
echo date('Y-m-d H:i:s').' 开始清零:'.count($room)."\n";
foreach ($room as $key => $value) {
$data = [
'today_hot_value' => 0,
];
db::name('vs_room')->where(['id' => $value['id']])->update($data);
}
echo date('Y-m-d H:i:s').' 完成'."\n";
}
//系统降身价
public function system_downgrade(){
//获取系统信息
$down_market_value = explode(',',get_system_config_value('down_market_value'));
if($down_market_value){
$day = $down_market_value[0];
$market_value = $down_market_value[1];
}else{
$day = 0;
$market_value = 0;
}
$num = 0;
if($day && $market_value){
//查询每个用户的身价
$user_market_value = db::name('user')->field('id,market_value')->select();
if($user_market_value){
foreach ($user_market_value as $value){
if($value['market_value'] > 1){
//获取他的降级时间
$down_time = db::name('vs_user_market_value_log')->where(['user_id' => $value['id'],'type' =>1])
->order('id desc')->value('createtime');
//如果没有降级记录,则使用当前时间作为起始时间
if(empty($down_time)){
$down_time = 0;
}
//检查是否到了降级时间
if(time() - $down_time >= $day*86400){
//开始降身价
db::startTrans();
try {
$market_values = max($value['market_value'] - $market_value, 1);
$update_result = db::name('user')->where('id', $value['id'])->update(['market_value' => $market_values]);
if($update_result) {
$data = [
'user_id' => $value['id'],
'before' => $value['market_value'],
'change_value' => $market_value,
'afterwards' => $market_values,
'type' => 1,
'createtime' => time()
];
$res = db::name('vs_user_market_value_log')->insert($data);
if($res){
$num += 1;
db::commit();
}else{
db::rollback();
\think\Log::error("系统降身价失败: 插入日志失败 user_id={$value['id']}");
}
} else {
db::rollback();
\think\Log::error("系统降身价失败: 更新用户身价失败 user_id={$value['id']}");
}
} catch (\Exception $e) {
db::rollback();
\think\Log::error("系统降身价异常: " . $e->getMessage() . " user_id={$value['id']}");
}
}
}
}
}
}
echo "系统降身价完成:".$num."\n";
}
}