<?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.warmer.web.dao.CustomDictDao">
    <resultMap id="CustomDictMap" type="com.warmer.web.model.CustomDictEntry">
        <id property="id" column="id"/>
        <result property="word" column="word"/>
        <result property="nature" column="nature"/>
        <result property="freq" column="freq"/>
        <result property="domain" column="domain"/>
        <result property="enabled" column="enabled"/>
        <result property="source" column="source"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
    </resultMap>

    <select id="list" resultMap="CustomDictMap">
        SELECT * FROM kg_custom_dict
        WHERE 1=1
        <if test="domain != null and domain != ''">
            AND domain = #{domain}
        </if>
        <if test="enabled != null">
            AND enabled = #{enabled}
        </if>
        ORDER BY updated_at DESC
    </select>

    <select id="getByWord" resultMap="CustomDictMap">
        SELECT * FROM kg_custom_dict
        WHERE word = #{word}
        <if test="domain != null and domain != ''">
            AND domain = #{domain}
        </if>
        LIMIT 1
    </select>

    <insert id="insert" parameterType="com.warmer.web.model.CustomDictEntry" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO kg_custom_dict (word, nature, freq, domain, enabled, source)
        VALUES (#{word}, #{nature}, #{freq}, #{domain}, #{enabled}, #{source})
    </insert>

    <update id="disable">
        UPDATE kg_custom_dict SET enabled = 0 WHERE id = #{id}
    </update>
</mapper>