<?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.amms.mapper.RelicMapper">

    <resultMap type="com.amms.domain.Relic" id="RelicResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="categoryId"    column="category_id"    />
        <result property="coverImageUrl"    column="cover_image_url"    />
        <result property="age"    column="age"    />
        <result property="material"    column="material"    />
        <result property="excavationInfo"    column="excavation_info"    />
        <result property="story"    column="story"    />
        <result property="modelUrl"    column="model_url"    />
        <result property="isHot"    column="is_hot"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
        <association property="categoryInfo" column="category_id" select="com.amms.mapper.ItemCategoryMapper.selectItemCategoryById"/>
    </resultMap>

    <sql id="selectRelicVo">
        select id, name, category_id, cover_image_url, age, material, excavation_info, story, model_url, is_hot, status, create_time, creator, update_time, updater from relic
    </sql>

    <select id="selectRelicList" parameterType="com.amms.domain.Relic" resultMap="RelicResult">
        <include refid="selectRelicVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="coverImageUrl != null  and coverImageUrl != ''"> and cover_image_url = #{coverImageUrl}</if>
            <if test="age != null  and age != ''"> and age = #{age}</if>
            <if test="material != null  and material != ''"> and material = #{material}</if>
            <if test="excavationInfo != null  and excavationInfo != ''"> and excavation_info = #{excavationInfo}</if>
            <if test="story != null  and story != ''"> and story = #{story}</if>
            <if test="modelUrl != null  and modelUrl != ''"> and model_url = #{modelUrl}</if>
            <if test="isHot != null "> and is_hot = #{isHot}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="creator != null "> and creator = #{creator}</if>
            <if test="updater != null "> and updater = #{updater}</if>
        </where>
    </select>

    <select id="selectRelicById" parameterType="Long" resultMap="RelicResult">
        <include refid="selectRelicVo"/>
        where id = #{id}
    </select>

    <insert id="insertRelic" parameterType="com.amms.domain.Relic" useGeneratedKeys="true" keyProperty="id">
        insert into relic
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="coverImageUrl != null and coverImageUrl != ''">cover_image_url,</if>
            <if test="age != null">age,</if>
            <if test="material != null">material,</if>
            <if test="excavationInfo != null">excavation_info,</if>
            <if test="story != null">story,</if>
            <if test="modelUrl != null">model_url,</if>
            <if test="isHot != null">is_hot,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="coverImageUrl != null and coverImageUrl != ''">#{coverImageUrl},</if>
            <if test="age != null">#{age},</if>
            <if test="material != null">#{material},</if>
            <if test="excavationInfo != null">#{excavationInfo},</if>
            <if test="story != null">#{story},</if>
            <if test="modelUrl != null">#{modelUrl},</if>
            <if test="isHot != null">#{isHot},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>

    <update id="updateRelic" parameterType="com.amms.domain.Relic">
        update relic
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="coverImageUrl != null and coverImageUrl != ''">cover_image_url = #{coverImageUrl},</if>
            <if test="age != null">age = #{age},</if>
            <if test="material != null">material = #{material},</if>
            <if test="excavationInfo != null">excavation_info = #{excavationInfo},</if>
            <if test="story != null">story = #{story},</if>
            <if test="modelUrl != null">model_url = #{modelUrl},</if>
            <if test="isHot != null">is_hot = #{isHot},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRelicById" parameterType="Long">
        delete from relic where id = #{id}
    </delete>
</mapper>
