package com.uas.platform.b2b.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 系统日志任务
 *
 *
 * @author hejq
 * @date 2018-06-22
 */
@Component
public class ErrorLogTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 每天凌晨0点到1点开始执行
     */
    @Scheduled(cron = "0 0/5 0,1 1 * ? ")
    public void deleteLogs() {
         // 删除日志明细
        jdbcTemplate.execute("delete from log$error$detail where log_logid in (select log_id from log$error " +
                "where date_format(log_date, '%Y-%m-%d') <= date_format(date_add(now(), interval -1 week), '%Y-%m-%d')) limit 1000");
        // 删除日志主表信息
        jdbcTemplate.execute("delete from log$error " +
                "where date_format(log_date, '%Y-%m-%d') <= date_format(date_add(now(), interval -1 week), '%Y-%m-%d') limit 1000");
        // 删除单据记录
        jdbcTemplate.execute("delete from erp$orders where date_format(or_date, '%Y-%m-%d') <= date_format(date_add(now(), interval -1 week), '%Y-%m-%d') limit 1000");
        // 删除物料记录
        jdbcTemplate.execute("delete from erp$products where date_format(pr_date, '%Y-%m-%d') <= date_format(date_add(now(), interval -1 week), '%Y-%m-%d') limit 1000");
    }
}
