最近网站实行用户积分过期制度,只保留用户近三月积分,三月前未使用积分进行扣除
*平台用户量比较大,感觉这样做会出问题,有没有好的解决方法?(windows iis服务器)
代码如下:
ignore_user_abort();
set_time_limit(0);
do{
$this->del_score();
sleep(24*3600);
}while (true);
private function del_score()
{
$date = to_date(to_timespan(to_date(TIME_UTC)." -3 month"),'Y-m-d');
// 近三月使用积分
$user_scores = $GLOBALS['db']->getAll("SELECT user_id,sum(score) as score FROM ".DB_PREFIX."user_score_log WHERE score<0 AND create_time_ymd > '".$date."' GROUP BY user_id");
// 扣除用户过期积分
$update_sql = "UPDATE ".DB_PREFIX."user SET score = score - CASE id ";
// 添加过期积分扣除日志
$insert_sql = "INSERT INTO ".DB_PREFIX."user (`user_id`, `score`, `account_score`, `mome`, `type`, `create_time`, `create_time_ymd`, `create_time_ym`, `create_time_y`) VALUES ";
foreach ($user_scores as $k => $v) {
// 总过期积分
$old_account_score = $GLOBALS['db']->getOne("SELECT account_score FROM ".DB_PREFIX."user_score_log WHERE user_id=".$v['user_id']." AND create_time_ymd<=".$date." ORDER BY id DESC LIMIT 1");
if($old_account_score > abs($v['score'])) // 过期积分未消费完
{
$arr[$v['user_id']] = $v['score'];
// 应扣除积分
$score = intval($account_score - abs($v['score']));
$update_sql .= sprintf("WHEN %d THEN %d ", $v['user_id'], $score);
// 账户现有积分
$account_score = $GLOBALS['db']->getOne("SELECT score - {$score} FROM ".DB_PREFIX."user WHERE id=".$v['user_id']);
$create_time = TIME_UTC;
$create_time_ymd = to_date(TIME_UTC,"Y-m-d");
$create_time_ym = to_date(TIME_UTC,"Ym");
$create_time_y = to_date(TIME_UTC,"Y");
$insert_sql .= "(".$v['user_id'].",-".$score.",".$account_score.",'积分到期未使用部分扣除',26,".$create_time.",'".$create_time_ymd."','".$create_time_ym."','".$create_time_y."'),";
}
continue;
}
$ids = implode(',', array_keys($arr));
$update_sql .= " END WHERE id IN ($ids)";
$insert_sql = substr($insert_sql, 0, -1);
$GLOBALS['db']->query($update_sql);
$GLOBALS['db']->query($insert_sql);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。