<?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.pcloud.book.applet.dao.impl.AppletBannerDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBanner">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="banner_pic" jdbcType="VARCHAR" property="bannerPic"/>
        <result column="jump_type" jdbcType="INTEGER" property="jumpType"/>
        <result column="jump_url" jdbcType="VARCHAR" property="jumpUrl"/>
        <result column="seq" jdbcType="INTEGER" property="seq"/>
        <result column="show_state" jdbcType="BOOLEAN" property="showState"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="agent_id" jdbcType="BIGINT" property="agentId"/>
        <result column="serve_id" jdbcType="BIGINT" property="serveId"/>
        <result column="serve_type" jdbcType="TINYINT" property="serveType"/>
        <result column="fill_type" jdbcType="TINYINT" property="fillType"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, banner_pic, jump_type, jump_url, seq, show_state, create_time, update_time, agent_id,
    serve_id, serve_type, fill_type, start_time, end_time
  </sql>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.applet.entity.AppletBanner"
            useGeneratedKeys="true">
        insert into applet_banner
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bannerPic != null">
                banner_pic,
            </if>
            <if test="jumpType != null">
                jump_type,
            </if>
            <if test="jumpUrl != null">
                jump_url,
            </if>
            <if test="seq != null">
                seq,
            </if>
            <if test="showState != null">
                show_state,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="agentId != null">
                agent_id,
            </if>
            <if test="serveId != null">
                serve_id,
            </if>
            <if test="serveType != null">
                serve_type,
            </if>
            <if test="fillType != null">
                fill_type,
            </if>
            <if test="startTime != null">
                start_time,
            </if>
            <if test="endTime != null">
                end_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bannerPic != null">
                #{bannerPic,jdbcType=VARCHAR},
            </if>
            <if test="jumpType != null">
                #{jumpType,jdbcType=INTEGER},
            </if>
            <if test="jumpUrl != null">
                #{jumpUrl,jdbcType=VARCHAR},
            </if>
            <if test="seq != null">
                #{seq,jdbcType=INTEGER},
            </if>
            <if test="showState != null">
                #{showState,jdbcType=BOOLEAN},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="agentId != null">
                #{agentId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null">
                #{serveType,jdbcType=TINYINT},
            </if>
            <if test="fillType != null">
                #{fillType,jdbcType=BOOLEAN},
            </if>
            <if test="startTime != null">
                #{startTime},
            </if>
            <if test="endTime != null">
                #{endTime},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.applet.entity.AppletBanner">
        update applet_banner
        <set>
            <if test="bannerPic != null">
                banner_pic = #{bannerPic,jdbcType=VARCHAR},
            </if>
            <if test="jumpType != null">
                jump_type = #{jumpType,jdbcType=INTEGER},
            </if>
            <if test="jumpUrl != null">
                jump_url = #{jumpUrl,jdbcType=VARCHAR},
            </if>
            <if test="seq != null">
                seq = #{seq,jdbcType=INTEGER},
            </if>
            <if test="showState != null">
                show_state = #{showState,jdbcType=BOOLEAN},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            update_time = NOW(),
            <if test="agentId != null">
                agent_id = #{agentId,jdbcType=BIGINT},
            </if>
            <if test="serveId != null">
                serve_id = #{serveId,jdbcType=BIGINT},
            </if>
            <if test="serveType != null">
                serve_type = #{serveType,jdbcType=TINYINT},
            </if>
            <if test="fillType != null">
                fill_type = #{fillType,jdbcType=BOOLEAN},
            </if>
                start_time = #{startTime},
                end_time = #{endTime},
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getById" parameterType="long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from applet_banner
        where id=#{id}
    </select>

    <delete id="deleteById" parameterType="long">
    delete from applet_banner
    where id=#{id}
  </delete>

    <select id="listBanner" resultType="com.pcloud.book.applet.dto.AppletBannerDTO" parameterType="map">
        SELECT
        id id,
        banner_pic bannerPic,
        seq seq,
        show_state showState,
        jump_type jumpType,
        jump_url jumpUrl,
        fill_type fillType,
        serve_id serveId,
        serve_type serveType,
        start_time startTime,
        end_time endTime
        FROM applet_banner
        where 1=1
        <if test="showState != null">
            and show_state = #{showState}
        </if>
        <choose>
            <when test="agentId != null">
                and agent_id = #{agentId}
            </when>
            <otherwise>
                and agent_id = 0
            </otherwise>
        </choose>
        ORDER BY seq ASC
    </select>

    <select id="getMaxSeq" resultType="Integer" parameterType="map">
     SELECT IFNULL(MAX(seq),0) FROM applet_banner
     WHERE agent_id = #{agentId}
  </select>


    <select id="listBannerByGrade" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBannerDTO">
        SELECT
        c.id,
        c.banner_pic bannerPic,
        c.jump_type jumpType,
        c.jump_url jumpUrl,
        c.fill_type fillType,
        c.serve_id serveId,
        c.serve_type serveType
        FROM
        applet_resource_apply a
        LEFT JOIN applet_banner c ON a.target_id = c.id
        WHERE
        a.crowd_type = #{crowdType}
        AND a.target_type = "BANNER"
        AND a.target_id > 0
        AND c.show_state = 1
        GROUP BY
        a.target_id
        ORDER BY
        c.seq ASC
    </select>

    <update id="updateShowState" parameterType="com.pcloud.book.applet.entity.AppletBanner">
        update
                applet_banner
        set show_state = #{showState}
        where id = #{id}
    </update>

</mapper>