<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caliverse.admin.domain.dao.admin.HistoryMapper">
    <resultMap id="LogResultMap" type="com.caliverse.admin.domain.entity.Log">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="adminId" column="admin_id"/>
        <result property="content" column="content"/>
        <result property="historyType" column="type"/>
        <result property="createDt" column="create_dt"/>
        <result property="name" column="name"/>
        <result property="mail" column="mail"/>
    </resultMap>
    <!--사용 이력 리스트 조회-->
    <select id="getHistoryList" parameterType="map" resultMap="LogResultMap">
        SELECT * FROM
        (
            SELECT
                    (@row_number:=@row_number + 1) AS row_num
                    , a.*
                FROM admin_log a
                   , (SELECT @row_number:=0) AS t
            WHERE 1 = 1
            <if test="history_type != null and history_type != ''">
                <choose>
                    <when test="history_type == 'ALL' ">
                    </when>
                    <otherwise>
                        AND a.type = #{history_type}
                    </otherwise>
                </choose>
            </if>
            <choose>
                <when test="search_type == 'NAME' || search_type == 'name'  ">
                    <if test="search_key != null and search_key != ''">
                        AND a.name LIKE CONCAT('%',#{search_key},'%')
                    </if>
                </when>
                <otherwise>
                    <if test="search_key != null and search_key != ''">
                        AND a.mail LIKE CONCAT('%',#{search_key},'%')
                    </if>
                </otherwise>
            </choose>
            <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
                AND a.create_dt BETWEEN #{start_dt, jdbcType=TIMESTAMP} and #{end_dt, jdbcType=TIMESTAMP}
            </if>
            ORDER BY a.create_dt
        )c
        <if test="orderby != null and orderby != ''">
            ORDER BY c.row_num ${orderby}
        </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>
    </select>
    <select id="getTotal" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM admin_log
    </select>
    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM admin_log a
                        WHERE 1 = 1
        <if test="history_type != null and history_type != ''">
            <choose>
                <when test="history_type == 'ALL' ">
                </when>
                <otherwise>
                    AND a.type = #{history_type}
                </otherwise>
            </choose>
        </if>
        <choose>
            <when test="search_type == 'NAME' || search_type == 'name'  ">
                <if test="search_key != null and search_key != ''">
                    AND a.name LIKE CONCAT('%',#{search_key},'%')
                </if>
            </when>
            <otherwise>
                <if test="search_key != null and search_key != ''">
                    AND a.mail LIKE CONCAT('%',#{search_key},'%')
                </if>
            </otherwise>
        </choose>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.create_dt BETWEEN #{start_dt, jdbcType=TIMESTAMP} and #{end_dt, jdbcType=TIMESTAMP}
        </if>
    </select>
    <select id="getLogJson" resultType="String" parameterType="java.lang.String">
        SELECT content FROM admin_log WHERE id = #{id}
    </select>
    <!--로그 기록-->
    <insert id="saveLog" parameterType="map">
        INSERT INTO admin_log (admin_id,name,mail,type,content) VALUES (#{adminId},#{name},#{mail}, #{type}, #{content});
    </insert>
    <update id="truncateMetaTable">
        TRUNCATE TABLE caliverse_meta_data
    </update>

    <insert id="insertMetaData" parameterType="map">
        INSERT INTO caliverse_meta_data (file_name, data_id, json_data) VALUES (#{fileName}, #{dataId}, #{jsonData})
    </insert>

</mapper>
