<?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.mapper.AppletUserLabelMapper">
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletUserLabel">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="label_name" jdbcType="VARCHAR" property="labelName" />
    <result column="applet_book_classify_id" jdbcType="BIGINT" property="appletBookClassifyId" />
    <result column="creat_time" jdbcType="TIMESTAMP" property="creatTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
  </resultMap>

  <resultMap id="DTOResultMap" type="com.pcloud.book.applet.dto.AppletUserLabelDTO">
    <id column="aid" jdbcType="BIGINT" property="id" />
    <result column="label_name" jdbcType="VARCHAR" property="labelName" />
    <result column="applet_book_classify_id" jdbcType="BIGINT" property="appletBookClassifyId" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
    <collection property="appletUserLabelRelations" ofType="com.pcloud.book.applet.entity.AppletUserLabelRelation">
      <id column="arid" property="id" jdbcType="BIGINT"/>
      <id column="aid" property="appletUserLabelId" jdbcType="BIGINT"/>
      <result column="book_label_id" property="bookLabelId" jdbcType="BIGINT"/>
      <result column="label_type" property="labelType" jdbcType="TINYINT"/>
    </collection>
  </resultMap>

  <sql id="Base_Column_List">
    id, label_name, applet_book_classify_id, creat_time, update_time, agent_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from applet_user_label
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="listUserLabel" resultMap="DTOResultMap" parameterType="map">
    select
    a.id aid, label_name, applet_book_classify_id,
    ar.id arid, ar.book_label_id, ar.label_type, a.agent_id
    from
    applet_user_label a
    left join applet_user_label_relation ar on a.id = ar.applet_user_label_id
    where
    a.applet_book_classify_id = #{appletBookClassifyId}
    <choose>
      <when test="agentId != null">
        and a.agent_id = #{agentId}
      </when>
      <otherwise>
        and a.agent_id = 0
      </otherwise>
    </choose>
    order by a.creat_time desc
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from applet_user_label
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletUserLabel" useGeneratedKeys="true" keyProperty="id">
    insert into applet_user_label (id, label_name, applet_book_classify_id, 
      creat_time, update_time, agent_id)
    values (#{id,jdbcType=BIGINT}, #{labelName,jdbcType=VARCHAR}, #{appletBookClassifyId,jdbcType=BIGINT}, 
      now(), now(), #{agentId})
  </insert>
  <insert id="insertSelective" parameterType="com.pcloud.book.applet.entity.AppletUserLabel">
    insert into applet_user_label
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="labelName != null">
        label_name,
      </if>
      <if test="appletBookClassifyId != null">
        applet_book_classify_id,
      </if>
      <if test="creatTime != null">
        creat_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="labelName != null">
        #{labelName,jdbcType=VARCHAR},
      </if>
      <if test="appletBookClassifyId != null">
        #{appletBookClassifyId,jdbcType=BIGINT},
      </if>
      <if test="creatTime != null">
        #{creatTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.applet.entity.AppletUserLabel">
    update applet_user_label
    <set>
      <if test="labelName != null">
        label_name = #{labelName,jdbcType=VARCHAR},
      </if>
      <if test="appletBookClassifyId != null">
        applet_book_classify_id = #{appletBookClassifyId,jdbcType=BIGINT},
      </if>
      <if test="creatTime != null">
        creat_time = #{creatTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.applet.entity.AppletUserLabel">
    update applet_user_label
    set label_name = #{labelName,jdbcType=VARCHAR},
      applet_book_classify_id = #{appletBookClassifyId,jdbcType=BIGINT},
      update_time = now()
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getBookClassifyAndLabel" resultType="com.pcloud.book.applet.dto.AppletLabelDTO">
    SELECT
	l.id appletUserLabelId,
	l.label_name labelName,
	c.id appletBookClassifyId
    FROM
        applet_user_label l
    LEFT JOIN applet_book_classify c ON l.applet_book_classify_id = c.id
    WHERE 1=1
    <choose>
      <when test="agentId != null">
        and l.agent_id = #{agentId}
      </when>
      <otherwise>
        and l.agent_id = 0
      </otherwise>
    </choose>
    ORDER BY l.creat_time DESC
  </select>

</mapper>