<?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.AppletBooklistClassifyDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBooklistClassify" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="classify_name" property="classifyName" 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="pic" property="pic" jdbcType="VARCHAR" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, classify_name, seq, show_state, create_time,update_time,pic, agent_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletBooklistClassify" useGeneratedKeys="true" keyProperty="id">
    insert into applet_booklist_classify (
        classify_name, seq, show_state, create_time, update_time, pic, agent_id
      )
    values (
      #{classifyName}, #{seq}, #{showState}, NOW(), NOW(), #{pic}, #{agentId}
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.applet.entity.AppletBooklistClassify">
    update applet_booklist_classify
    <set>
      <if test="classifyName != null">
        classify_name = #{classifyName,jdbcType=BIGINT},
      </if>
      <if test="seq != null">
        seq = #{seq},
      </if>
      <if test="showState != null">
        show_state = #{showState},
      </if>
      <if test="pic != null">
        pic=#{pic},
      </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_booklist_classify
    where id=#{id}
  </select>

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

  <select id="listBooklistClassify" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBooklistClassifyDTO">
        SELECT
        b.id id,
        b.classify_name classifyName,
        b.seq seq,
        COUNT(c.id) booklistCount,
        b.show_state showState,
        b.pic
        FROM applet_booklist_classify b
        LEFT JOIN applet_booklist c ON b.id=c.classify_id
        WHERE 1=1
        <if test="name != null">
          AND b.classify_name LIKE CONCAT("%",#{name},"%")
        </if>
        <choose>
            <when test="agentId != null">
                and b.agent_id = #{agentId}
            </when>
            <otherwise>
                and b.agent_id = 0
            </otherwise>
        </choose>
        GROUP BY b.id
        order by b.seq asc
  </select>


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

    <select id="getAllBooklistClassify" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBooklistClassifyDTO">
    SELECT
    id, classify_name classifyName, seq, show_state showState, pic
    FROM applet_booklist_classify
    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, id asc
    </select>

    <insert id="batchInsertUserBooklistClassify" parameterType="list" keyProperty="id" useGeneratedKeys="true">
        insert into applet_booklist_classify_user (
        classify_id, wechat_user_id, create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.classifyId}, #{item.wechatUserId}, NOW() )
        </foreach>
    </insert>

    <delete id="deleteClassifyByWechatUesrId" parameterType="long">
        DELETE FROM applet_booklist_classify_user
        WHERE wechat_user_id = #{wechatUserId}
    </delete>

    <select id="getClassifyIdsByUserId" parameterType="long" resultType="long">
        SELECT classify_id
        FROM applet_booklist_classify_user
        WHERE wechat_user_id = #{wechatUserId}
    </select>

    <select id="getClassifysByUserId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletBooklistClassifyDTO">
        SELECT
        c.id, c.classify_name classifyName, c.seq, c.show_state showState, c.pic
        FROM
        applet_booklist_classify_user u
        LEFT JOIN applet_booklist_classify c ON u.classify_id = c.id
        WHERE
        u.wechat_user_id = #{wechatUserId}
        AND c.show_state = 1
        ORDER BY
        c.seq ASC, c.id asc
    </select>

</mapper>