<?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" property="id" jdbcType="BIGINT" />
    <result column="banner_pic" property="bannerPic" jdbcType="VARCHAR" />
    <result column="jump_type" property="jumpType" jdbcType="INTEGER" />
    <result column="jump_url" property="jumpUrl" jdbcType="VARCHAR" />
    <result column="seq" property="seq" jdbcType="INTEGER" />
    <result column="show_state" property="showState" jdbcType="BOOLEAN" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, banner_pic, jump_type, jump_url, seq, show_state, create_time, update_time, agent_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletBanner" useGeneratedKeys="true" keyProperty="id">
    insert into applet_banner(
     banner_pic, jump_type, jump_url, seq, show_state, create_time, update_time, agent_id
      )
    values (
      #{bannerPic}, #{jumpType}, #{jumpUrl}, #{seq}, #{showState}, NOW(), NOW(), #{agentId}
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.applet.entity.AppletBanner">
    update applet_banner
    <set>
      <if test="bannerPic != null">
        banner_pic = #{bannerPic},
      </if>
      <if test="jumpType != null">
        jump_type = #{jumpType,jdbcType=INTEGER},
      </if>
      <if test="jumpUrl != null">
        jump_url = #{jumpUrl},
      </if>
      <if test="seq != null">
        seq = #{seq,jdbcType=INTEGER},
      </if>
      <if test="showState != null">
        show_state = #{showState,jdbcType=BOOLEAN},
      </if>
      update_time=NOW()
    </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
    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>

</mapper>