Files
yusheng-php/application/cron/controller/DaySeconds.php

158 lines
5.7 KiB
PHP
Raw Permalink 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 "系统降身价:\n";
$this->system_downgrade();
echo "\n";
echo "过期验证码删除:\n";
$this->deleteExpireCode();
echo "\n";
echo "过期的公会申请记录处理:\n";
$this->deleteExpireGuildApply();
echo "\n";
}
/*
* 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";
}
//删除过期的短信验证码
public function deleteExpireCode(){
$time = time() - 60 * 60 * 24 * 7;
$code = db::name('sms')->where(['createtime' => ['<',$time]])->select();
if ($code) {
foreach ($code as $key => $value) {
db::name('sms')->where(['id' => $value['id']])->delete();
}
}
}
//删除过期的公会申请记录
public function deleteExpireGuildApply(){
echo "开始自动审核过期公会申请记录:".date('Y-m-d H:i:s')."\n";
$time = time() - 60 * 60 * 24; //一天前
$apply_guild_user = db::name('vs_guild_user')->where(['createtime' => ['<',$time],'status' => 2])->select();
if ($apply_guild_user) {
foreach ($apply_guild_user as $key => $value) {
db::name('vs_guild_user')->where(['id' => $value['id']])->update([
'status' => 3,
'remarks' => '申请已过期',
'updatetime' => time(),
]);
}
}
echo "完成自动审核过期公会申请记录:{".count($apply_guild_user)."}条)".date('Y-m-d H:i:s')."\n";
}
}