<?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.ruoyi.web.modules.content.mapper.BizHomeBannerMapper">

    <resultMap id="BizHomeBannerResult" type="com.ruoyi.web.modules.content.domain.BizHomeBanner">
        <id property="bannerId" column="banner_id"/>
        <result property="bannerName" column="banner_name"/>
        <result property="bannerImage" column="banner_image"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="sortNo" column="sort_no"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select banner_id, banner_name, banner_image, enable_flag, sort_no,
               del_flag, create_by, create_time, update_by, update_time, remark
        from biz_home_banner
    </sql>

    <select id="selectById" resultMap="BizHomeBannerResult">
        <include refid="selectVo"/>
        where banner_id = #{bannerId} and del_flag = '0'
    </select>

    <select id="selectList" resultMap="BizHomeBannerResult">
        <include refid="selectVo"/>
        where del_flag = '0'
        <if test="bannerName != null and bannerName != ''">
            and banner_name like concat('%', #{bannerName}, '%')
        </if>
        <if test="enableFlag != null and enableFlag != ''">
            and enable_flag = #{enableFlag}
        </if>
        order by sort_no asc, create_time asc
    </select>

    <select id="selectEnabledForApp" resultMap="BizHomeBannerResult">
        <include refid="selectVo"/>
        where del_flag = '0' and enable_flag = '1'
        order by sort_no asc, create_time asc
        limit #{limit}
    </select>

    <select id="countByName" resultType="int">
        select count(1) from biz_home_banner
        where banner_name = #{bannerName} and del_flag = '0'
        <if test="excludeId != null">
            and banner_id != #{excludeId}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="bannerId">
        insert into biz_home_banner (
            banner_name, banner_image, enable_flag, sort_no,
            del_flag, create_by, create_time, remark
        ) values (
            #{bannerName}, #{bannerImage}, #{enableFlag}, #{sortNo},
            '0', #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="update">
        update biz_home_banner
        <set>
            <if test="bannerName != null">banner_name = #{bannerName},</if>
            <if test="bannerImage != null">banner_image = #{bannerImage},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where banner_id = #{bannerId} and del_flag = '0'
    </update>

    <update id="updateSortNo">
        update biz_home_banner
        set sort_no = #{sortNo}, update_by = #{updateBy}, update_time = sysdate()
        where banner_id = #{bannerId} and del_flag = '0'
    </update>

    <update id="logicDeleteByIds">
        update biz_home_banner
        set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0' and banner_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
