<?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.rightsSetting.mapper.RightsSettingBookRelationMapper" >

  <resultMap id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation">
    <id column="id" property="id" jdbcType="BIGINT"/>
    <result column="rights_setting_id" property="rightsSettingId" jdbcType="BIGINT"/>
    <result column="book_adviser_id" property="bookAdviserId" jdbcType="BIGINT"/>
    <result column="book_id" property="bookId" jdbcType="BIGINT"/>
    <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
    <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
    <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    <result column="book_name" property="bookName" jdbcType="VARCHAR"/>
    <result column="TEMPLET_ID" property="templetId" jdbcType="BIGINT"/>
    <result column="SECOND_TEMPLET_ID" property="secondTempletId" jdbcType="BIGINT"/>
    <result column="third_TEMPLET_ID" property="thirdTempletId" jdbcType="BIGINT"/>
    <result column="GRA_LABEL_ID" property="graLabelId" jdbcType="BIGINT"/>
    <result column="SUB_LABEL_ID" property="subLabelId" jdbcType="BIGINT"/>
    <result column="VER_LABEL_ID" property="verLabelId" jdbcType="BIGINT"/>
    <result column="AREA_LABEL_ID" property="areaLabelId" jdbcType="BIGINT"/>
    <result column="vol_label_id" property="volLabelId" jdbcType="INTEGER"/>
    <result column="group_qrcode_url" property="groupQrcodeUrl" jdbcType="VARCHAR"/>
    <result column="COVER_IMG" property="coverImg" jdbcType="VARCHAR"/>
  </resultMap>

  <sql id="Base_Column_List">
        id, rights_setting_id, book_adviser_id, book_id, adviser_id, channel_id, agent_id, create_time, update_time
    </sql>


  <sql id="Base_Column_List_owm" >
        r.id,
        r.rights_setting_id,
        r.book_adviser_id,
        r.book_id,
        r.channel_id,
        r.agent_id,
        r.create_time,
        r.update_time
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from rights_setting_book_relation
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="selectByRithsSettingId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List_owm" />,b.book_name, b.isbn, ifnull(r.adviser_id,a.adviser_id) adviser_id,
    c.group_qrcode_url,
    a.TEMPLET_ID,
    a.SECOND_TEMPLET_ID,
    a.third_TEMPLET_ID,
    a.GRA_LABEL_ID,
    a.SUB_LABEL_ID,
    a.VER_LABEL_ID,
    a.AREA_LABEL_ID,
    a.vol_label_id,
    c.group_qrcode_url,
    b.isbn,
    b.COVER_IMG
    from rights_setting_book_relation r
    left join book b on r.book_id = b.book_Id
    INNER JOIN book_adviser a ON b.BOOK_ID=a.BOOK_ID AND a.IS_DELETE=0
    left join book_group c on a.book_id = c.book_id and a.channel_id = c.channel_id and c.is_delete = 0
    where rights_setting_id = #{rightsSettingId}
    group by r.book_id
  </select>


  <select id="getAllBookIds" resultType="java.lang.Long">
    select book_id
    from
    rights_setting_book_relation
  </select>


  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from rights_setting_book_relation
    where id = #{id,jdbcType=BIGINT}
  </delete>


  <delete id="deleteByRightsSettingId" parameterType="long">
    delete from rights_setting_book_relation
    where rights_setting_id = #{rightsSettingId,jdbcType=BIGINT}
  </delete>


  <insert id="insert" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation" >
    insert into rights_setting_book_relation (id, rights_setting_id, book_id, 
      create_time, update_time)
    values (#{id,jdbcType=BIGINT}, #{rightsSettingId,jdbcType=BIGINT}, #{bookId,jdbcType=BIGINT}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
  </insert>


  <insert id="insertSelective" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation" >
    insert into rights_setting_book_relation
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="rightsSettingId != null" >
        rights_setting_id,
      </if>
      <if test="bookId != null" >
        book_id,
      </if>
      <if test="createTime != null" >
        create_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="rightsSettingId != null" >
        #{rightsSettingId,jdbcType=BIGINT},
      </if>
      <if test="bookId != null" >
        #{bookId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

  <insert id="batchInsert" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation">
    insert into rights_setting_book_relation (
    `rights_setting_id`,
    `book_adviser_id`,
    `book_id`,
    `adviser_id`,
    `channel_id`,
    `agent_id`,
    `create_time`,
    `update_time`
    )
    values
    <foreach collection="list" index="index" item="item" separator=",">
    (#{item.rightsSettingId,jdbcType=BIGINT},
    #{item.bookAdviserId,jdbcType=BIGINT},
    #{item.bookId,jdbcType=BIGINT},
    #{item.adviserId,jdbcType=BIGINT},
    #{item.channelId,jdbcType=BIGINT},
    #{item.agentId,jdbcType=BIGINT},
    now(),
    now()
    )
    </foreach>
  </insert>


  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation" >
    update rights_setting_book_relation
    <set >
      <if test="rightsSettingId != null" >
        rights_setting_id = #{rightsSettingId,jdbcType=BIGINT},
      </if>
      <if test="bookId != null" >
        book_id = #{bookId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,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.rightsSetting.entity.RightsSettingBookRelation" >
    update rights_setting_book_relation
    set rights_setting_id = #{rightsSettingId,jdbcType=BIGINT},
      book_id = #{bookId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectBookByRithsSettingId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List_owm" />,b.book_name, b.isbn, ifnull(r.adviser_id,a.adviser_id) adviser_id,
     c.group_qrcode_url,
    a.TEMPLET_ID,
    a.SECOND_TEMPLET_ID,
    a.third_templet_id,
    a.GRA_LABEL_ID,
    a.SUB_LABEL_ID,
    a.VER_LABEL_ID,
    a.AREA_LABEL_ID,
    a.vol_label_id,
    c.group_qrcode_url,
    b.isbn,
    b.COVER_IMG
    from rights_setting_book_relation r
    left join book b on r.book_id = b.book_Id
    INNER JOIN book_adviser a ON b.BOOK_ID=a.BOOK_ID AND a.IS_DELETE=0
    left join book_group c on a.book_id = c.book_id and a.channel_id = c.channel_id and c.is_delete = 0
    where rights_setting_id = #{_parameter}
    group by r.book_id
  </select>

  <select id="listReousrceNum4Book" resultType="com.pcloud.book.book.vo.BookResourceNumDTO">
    select count(a.id) resourceNum, b.adviser_id adviserId, b.book_id bookId, b.channel_id channelId
    from rights_setting_item a
           left join rights_setting_book_relation b on a.rights_setting_id = b.rights_setting_id
    where book_id in
      <foreach collection="bookIds" open="(" close=")" item="item" separator=",">
        ${item}
      </foreach>
      and adviser_id in
      <foreach collection="adviserIds" open="(" close=")" item="item" separator=",">
        ${item}
      </foreach>
      and channel_id in
      <foreach collection="channelIds" open="(" close=")" item="item" separator=",">
        ${item}
      </foreach>
    group by b.adviser_id, b.book_id, b.channel_id
  </select>
</mapper>