package com.lanyuanxiaoyao.service.api.service;

import club.kingon.sql.builder.SqlBuilder;
import java.time.Instant;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import static com.lanyuanxiaoyao.service.common.SQLConstants.HudiCollectBuild.TbAppCollectTableVersion;

/**
 * 跨天版本更新
 *
 * @author ZhangJiacheng
 * @date 2022-11-17
 */
@Service
public class VersionUpdateService {
    private static final Logger logger = LoggerFactory.getLogger(VersionUpdateService.class);

    private final JdbcTemplate jdbcTemplate;

    public VersionUpdateService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private Date now() {
        return Date.from(Instant.now());
    }

    public void saveUpdateVersion(Long flinkJobId, String alias, String version, String opTs) {
        jdbcTemplate.update(
                SqlBuilder
                        .insertInto(
                                TbAppCollectTableVersion._origin_,
                                TbAppCollectTableVersion.FLINK_JOB_ID_O,
                                TbAppCollectTableVersion.ALIAS_O,
                                TbAppCollectTableVersion.VERSION_O,
                                TbAppCollectTableVersion.OP_TS_O,
                                TbAppCollectTableVersion.CREATE_TIME_O
                        )
                        .values()
                        .addValue(null, null, null, null, null)
                        .precompileSql(),
                flinkJobId,
                alias,
                version,
                opTs,
                now()
        );
    }
}
