# -*- coding: utf-8 -*-
"""
@author: yq
@time: 2024/11/8
@desc: 处理占位符的函数
"""
import re

from .utils import f_get_date


def _fill_date_placeholder(sql: str):
    date_placeholder = re.findall(r"{{date.*?}}", sql)
    if date_placeholder:
        date_placeholder = date_placeholder[0]
        offset = date_placeholder.replace("{{date", "").replace("}}", "")
        if len(offset) > 0:
            symbol = offset[0:1]
            offset = int(offset[1:])
            if "-" == symbol:
                offset = -offset
        else:
            offset = 0
        date_new = f_get_date(offset)
        sql = sql.replace(date_placeholder, repr(date_new))
    return sql


def f_fill_placeholder(sql: str):
    sql = _fill_date_placeholder(sql)
    return sql
