package com.lanyuanxiaoyao.service.info.service;

import club.kingon.sql.builder.SqlBuilder;
import club.kingon.sql.builder.entry.Column;
import com.lanyuanxiaoyao.service.common.SQLConstants.HudiCollectBuild.TbAppCollectTableVersion;
import com.lanyuanxiaoyao.service.common.SQLConstants.HudiCollectBuild.TbAppHudiCompactionMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 清理数据库
 *
 * @author lanyuanxiaoyao
 * @date 2024-05-23
 */
@Service
public class CleanService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(CleanService.class);
    private static final Column before7Days = Column.as("DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
    private static final Column before14Days = Column.as("DATE_SUB(CURDATE(), INTERVAL 14 DAY)");

    private final JdbcTemplate mysqlJdbcTemplate;

    public CleanService(JdbcTemplate mysqlJdbcTemplate) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate;
    }

    public Integer cleanCompactionMetrics() {
        return mysqlJdbcTemplate.update(
                SqlBuilder.delete(TbAppHudiCompactionMetrics._origin_)
                        .whereLe(TbAppHudiCompactionMetrics.UPDATE_TIME_O, before7Days)
                        .build()
        );
    }

    public Integer cleanTableVersion() {
        return mysqlJdbcTemplate.update(
                SqlBuilder.delete(TbAppCollectTableVersion._origin_)
                        .whereLe(TbAppCollectTableVersion.UPDATE_TIME_O, before14Days)
                        .build()
        );
    }
}
