/*
 * Copyright 1998-2016 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

@Component
public class ScoreUpdater {
  private static final Logger logger = LoggerFactory.getLogger(ScoreUpdater.class);

  private JdbcTemplate jdbcTemplate;

  @Autowired
  public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
  }

  @Scheduled(cron="1 0 1 */2 * *")
  @Transactional
  public void updateScore() {
    logger.info("Updating score");

    jdbcTemplate.update("update users set score=score+1 " +
          "where id in " +
            "(select distinct comments.userid from comments, topics " +
            "where comments.postdate>CURRENT_TIMESTAMP-'2 days'::interval " +
            "and topics.id=comments.topic and " +
            "groupid!=8404 and groupid!=4068 and groupid!=19390 and groupid!=19405 and " +
            "not comments.deleted and not topics.deleted)");

    updateMaxScore();
  }

  @Scheduled(cron="1 15 * * * *")
  public void updateMaxScore() {
    jdbcTemplate.update("update users set max_score=score where score>max_score");
  }

  @Scheduled(cron="0 1 * * * *")
  public void block() {
    jdbcTemplate.update("update users set blocked='t' where id in (select id from users where score<-50 and nick!='anonymous' and max_score<150 and not blocked)");
    jdbcTemplate.update("update users set blocked='t' where id in (select id from users where score<-50 and nick!='anonymous' and max_score<150 and blocked is null)");
  }

  @Scheduled(cron="0 1 2 * * *")
  @Transactional
  public void deleteInactivated() {
    jdbcTemplate.update("delete from user_events where userid in (select id from users where not activated and not blocked and regdate<CURRENT_TIMESTAMP-'3 days'::interval)");
    jdbcTemplate.update("delete from topic_users_notified where userid in (select id from users where not activated and not blocked and regdate<CURRENT_TIMESTAMP-'3 days'::interval)");
    jdbcTemplate.update("delete from users where not activated and not blocked and regdate<CURRENT_TIMESTAMP-'3 days'::interval");

    jdbcTemplate.update("delete from ban_info where userid in (select id from users where not activated and regdate<CURRENT_TIMESTAMP-'90 days'::interval)");
    jdbcTemplate.update("delete from user_events where userid in (select id from users where not activated and regdate<CURRENT_TIMESTAMP-'90 days'::interval)");
    jdbcTemplate.update("delete from topic_users_notified where userid in (select id from users where not activated and regdate<CURRENT_TIMESTAMP-'90 days'::interval)");
    jdbcTemplate.update("delete from users where not activated and regdate<CURRENT_TIMESTAMP-'90 days'::interval");
  }
}

@@ -20,10 +20,10 @@
 8-20
+2
 1
-6
  Lin
@@ -2464,11 +2464,12 @@
 =%220 
-1 2
+30 *
  * *
@@ -2522,24 +2522,77 @@
 tivated() %7B%0A
+    logger.info(%22Deleting non-activated accounts%22);%0A%0A
     jdbcTemp
@@ -2935,32 +2935,46 @@
 interval)%22);%0A   
+ int deleted =
  jdbcTemplate.up
@@ -3570,32 +3570,53 @@
 interval)%22);%0A   
+ int deletedBlocked =
  jdbcTemplate.up
@@ -3711,15 +3711,107 @@
 erval%22);
+%0A%0A    logger.info(%22Deleted %7B%7D non-activated; %7B%7D blocked accounts%22, deleted, deletedBlocked);
 %0A  %7D%0A%7D%0A

add debug