<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tasteby.mapper.StatsMapper">

    <update id="recordVisit">
        MERGE INTO site_visits sv
        USING (SELECT TRUNC(SYSDATE) AS d FROM dual) src
        ON (sv.visit_date = src.d)
        WHEN MATCHED THEN UPDATE SET sv.visit_count = sv.visit_count + 1
        WHEN NOT MATCHED THEN INSERT (visit_date, visit_count) VALUES (src.d, 1)
    </update>

    <select id="getTodayVisits" resultType="int">
        SELECT NVL(visit_count, 0)
        FROM site_visits
        WHERE visit_date = TRUNC(SYSDATE)
    </select>

    <select id="getTotalVisits" resultType="int">
        SELECT NVL(SUM(visit_count), 0)
        FROM site_visits
    </select>

</mapper>
