<?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.backendsys.mapper.Cms.CmsBannerMapper">

    <sql id="includeBanner">
        id,
        id banner_id,
        title,
        type,
        link,
        image,
        image_thumb,
        sort,
        status,
        create_time,
        update_time
    </sql>

    <resultMap id="resultMapBanner" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="banner_id" column="banner_id" javaType="java.lang.Long"/>
        <result property="title" column="title" />
        <result property="type" column="type" javaType="java.lang.Integer"/>
        <result property="link" column="link" />
        <result property="image" column="image" />
        <result property="image_thumb" column="image_thumb" />
        <result property="sort" column="sort" />
        <result property="status" column="status" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryBannerList" resultMap="resultMapBanner">
        SELECT
           <include refid="includeBanner" />
        FROM cms_banner
        <where>
            <if test="title != null and title != ''">
                AND title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="create_time != null and create_time != ''">
                AND create_time >= #{create_time}
            </if>
        </where>
        ORDER BY update_time DESC
    </select>

    <select id="queryBannerDetail" resultMap="resultMapBanner">
        SELECT <include refid="includeBanner" />
        FROM cms_banner
        WHERE id = #{banner_id}
    </select>

    <insert id="insertBanner" parameterType="com.backendsys.entity.Cms.CmsBannerDTO"
        useGeneratedKeys="true" keyProperty="banner_id">
        INSERT INTO cms_banner (
            title, image, image_thumb
            <if test="type != null and type != ''">, type</if>
            <if test="link != null and link != ''">, link</if>
            <if test="sort != null and sort != ''">, sort</if>
            <if test="status != null and status != ''">, status</if>
        )
        VALUES (
            #{title}, #{image}, #{image_thumb}
            <if test="type != null and type != ''">, #{type}</if>
            <if test="link != null and link != ''">, #{link}</if>
            <if test="sort != null and sort != ''">, #{sort}</if>
            <if test="status != null and status != ''">, #{status}</if>
        );
    </insert>

    <update id="updateBanner" parameterType="com.backendsys.entity.Cms.CmsBannerDTO"
        useGeneratedKeys="true" keyProperty="banner_id">
        UPDATE cms_banner
        SET
            title = #{title},
            image = #{image},
            image_thumb = #{image_thumb}
            <if test="type != null and type != ''">, type = #{type}</if>
            <if test="link != null and link != ''">, link = #{link}</if>
            <if test="sort != null and sort != ''">, sort = #{sort}</if>
            <if test="status != null and status != ''">, status = #{status}</if>
        WHERE id = #{banner_id}
    </update>

    <delete id="deleteBanner" >
        DELETE FROM cms_banner WHERE id = #{banner_id}
    </delete>

</mapper>