<?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.dao.impl.RightsSettingDAOImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.rightsSetting.entity.RightsSetting" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="rights_number" property="rightsNumber" jdbcType="VARCHAR" />
    <result column="introduce" property="introduce" jdbcType="VARCHAR" />
    <result column="detail" property="detail" jdbcType="VARCHAR" />
    <result column="count" property="count" jdbcType="INTEGER" />
    <result column="first_classify" property="firstClassify" jdbcType="BIGINT" />
    <result column="second_classify" property="secondClassify" jdbcType="BIGINT" />
    <result column="third_classify" property="thirdClassify" jdbcType="BIGINT" />
    <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT" />
    <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="enable_group_service" property="enableGroupService" jdbcType="INTEGER" />
    <result column="rights_setting_type" property="rightsSettingType" jdbcType="INTEGER" />
    <result column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="online_course_open" property="onlineCourseOpen" jdbcType="BOOLEAN" />
    <result column="learning_tool_open" property="learningToolOpen" jdbcType="BOOLEAN" />
    <result column="draw_open" property="drawNowOpen" jdbcType="BOOLEAN" />
    <result column="read_type_title" property="readTypeTitle" jdbcType="VARCHAR" />
    <result column="vol_label_id" property="volLabelId" jdbcType="BIGINT" />
    <result column="reading_guide" property="guideIsOpen" jdbcType="BOOLEAN" />
    <result column="guide_title" property="guideTitle" jdbcType="VARCHAR" />
    <result column="guide_type" property="guideType" jdbcType="VARCHAR" />
    <result column="guide_id" property="guideId" jdbcType="BIGINT" />
    <result column="guide_pic" property="guidePic" jdbcType="VARCHAR" />
    <result column="ver_label_id" property="verLabelId" jdbcType="BIGINT" />
    <result column="show_state" property="showState" jdbcType="BOOLEAN"/>
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
    <result column="guide_link_url" property="guideLinkUrl" jdbcType="VARCHAR" />
    <result column="create_user_id" property="createUserId" jdbcType="BIGINT" />
    <result column="update_user_id" property="updateUserId" jdbcType="BIGINT" />
    <result column="banner_type" property="bannerType" jdbcType="TINYINT" />
    <collection property="rightsSettingBanners" ofType="com.pcloud.book.rightsSetting.entity.RightsSettingBanner"
                column="id" select="com.pcloud.book.rightsSetting.dao.impl.RightsSettingBannerDaoImpl.getByRightsSettingId"/>
  </resultMap>
  
  <resultMap id="DtoResultMap" type="com.pcloud.book.rightsSetting.dto.RightsSettingDto">
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="rights_number" property="rightsNumber" jdbcType="VARCHAR" />
    <result column="introduce" property="introduce" jdbcType="VARCHAR" />
    <result column="detail" property="detail" jdbcType="VARCHAR" />
    <result column="count" property="count" jdbcType="INTEGER" />
    <result column="first_classify" property="firstClassify" jdbcType="BIGINT" />
    <result column="second_classify" property="secondClassify" jdbcType="BIGINT" />
    <result column="third_classify" property="thirdClassify" jdbcType="BIGINT" />
    <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT" />
    <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="enable_group_service" property="enableGroupService" jdbcType="INTEGER" />
    <result column="rights_setting_type" property="rightsSettingType" jdbcType="INTEGER" />
    <result column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="BOOK_NAME" property="bookName" jdbcType="VARCHAR" />
    <result column="online_course_open" property="onlineCourseOpen" jdbcType="BOOLEAN" />
    <result column="learning_tool_open" property="learningToolOpen" jdbcType="BOOLEAN" />
    <result column="draw_open" property="drawNowOpen" jdbcType="BOOLEAN" />
    <result column="read_type_title" property="readTypeTitle" jdbcType="VARCHAR" />
    <result column="vol_label_id" property="volLabelId" jdbcType="BIGINT" />
    <result column="ver_label_id" property="verLabelId" jdbcType="BIGINT" />
    <result column="show_state" property="showState" jdbcType="BOOLEAN" />
    <result column="agent_id" property="agentId" jdbcType="BIGINT" />
    <result column="create_user_id" property="createUserId" jdbcType="BIGINT" />
    <result column="update_user_id" property="updateUserId" jdbcType="BIGINT" />
    <result column="group_qrcode_url" property="groupQrcodeUrl" jdbcType="VARCHAR" />
    <result column="banner_type" property="bannerType" jdbcType="TINYINT" />
    <collection property="rightsSettingBanners" ofType="com.pcloud.book.rightsSetting.entity.RightsSettingBanner"
                column="id" select="com.pcloud.book.rightsSetting.dao.impl.RightsSettingBannerDaoImpl.getByRightsSettingId"/>
  </resultMap>

  <resultMap id="DtoResultMap4Book" type="com.pcloud.book.rightsSetting.dto.RightsSettingDto" extends="DtoResultMap">
    <collection property="rightsSettingBookRelations" ofType="com.pcloud.book.rightsSetting.entity.RightsSettingBookRelation"
      column="id" select="com.pcloud.book.rightsSetting.mapper.RightsSettingBookRelationMapper.selectBookByRithsSettingId">
    </collection>
  </resultMap>

  <resultMap id="BookNewsDTOMap" type="com.pcloud.book.applet.dto.HandleNewsDTO">
    <result column="book_adviser_id" property="adviserBookId" jdbcType="BIGINT"/>
    <result column="book_id" property="bookId" jdbcType="BIGINT"/>
    <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
    <collection property="appletNewsIds"  ofType="long">
      <result column="serve_id" jdbcType="BIGINT"/>
    </collection>
  </resultMap>

  <sql id="Base_Column_List" >
    id,rights_number, introduce, detail, count, first_classify, second_classify, third_classify,grade_label_id, subject_label_id,
    create_time, update_time, enable_group_service, rights_setting_type, book_id
    ,online_course_open,learning_tool_open,draw_open,read_type_title, vol_label_id,
    reading_guide,guide_title,guide_type,guide_id,guide_pic,ver_label_id, show_state, agent_id, guide_link_url,
    create_user_id, update_user_id, banner_type
  </sql>

  <sql id="Base_Column_List_own" >
    r.id,r.rights_number, r.introduce, r.detail, r.count, r.first_classify, r.second_classify, r.third_classify, r.grade_label_id, r.subject_label_id,
    r.create_time, r.update_time, r.enable_group_service, r.rights_setting_type, r.book_id
    ,r.online_course_open,r.learning_tool_open,r.draw_open,r.read_type_title,r.vol_label_id,r.ver_label_id, r.show_state,
    r.update_user_id, r.update_user_id
  </sql>

  <sql id="Base_Column_List_1" >
    r.id,r.rights_number, r.introduce, r.detail, r.count, r.first_classify, r.second_classify,r.third_classify, r.grade_label_id, r.subject_label_id,
    r.create_time, r.update_time, r.enable_group_service, r.rights_setting_type, b.book_id
    ,r.online_course_open,r.learning_tool_open,r.draw_open,r.read_type_title,r.vol_label_id,r.ver_label_id, r.update_user_id, r.update_user_id
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select
    <include refid="Base_Column_List" />
    from rights_setting
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from rights_setting
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.pcloud.book.rightsSetting.entity.RightsSetting" useGeneratedKeys="true" keyProperty="id">
    insert into rights_setting (rights_number,introduce, detail,
      count, first_classify, second_classify,third_classify,
      grade_label_id, subject_label_id, create_time,
      update_time, enable_group_service, rights_setting_type, book_id,
      online_course_open,learning_tool_open,draw_open,read_type_title, vol_label_id,
      reading_guide,guide_title,guide_type,guide_id,guide_pic,ver_label_id, agent_id, guide_link_url,create_user_id)
    values ( #{rightsNumber,jdbcType=VARCHAR}, #{introduce,jdbcType=VARCHAR}, #{detail,jdbcType=VARCHAR},
      #{count,jdbcType=INTEGER}, #{firstClassify,jdbcType=BIGINT}, #{secondClassify,jdbcType=BIGINT},#{thirdClassify},
      #{gradeLabelId,jdbcType=BIGINT}, #{subjectLabelId,jdbcType=BIGINT}, now(),
     now(), #{enableGroupService}, #{rightsSettingType}, #{bookId},
      #{onlineCourseOpen},#{learningToolOpen},#{drawNowOpen},#{readTypeTitle}, #{volLabelId},
      #{guideIsOpen},#{guideTitle},#{guideType},#{guideId},#{guidePic}, #{verLabelId}, #{agentId}, #{guideLinkUrl}, #{createUserId})
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.rightsSetting.entity.RightsSetting" >
    update rights_setting
    <set >
      <if test="rightsNumber != null" >
        rights_number = #{rightsNumber,jdbcType=VARCHAR},
      </if>
      <if test="introduce != null" >
        introduce = #{introduce,jdbcType=VARCHAR},
      </if>
      <if test="detail != null" >
        detail = #{detail,jdbcType=VARCHAR},
      </if>
      <if test="count != null" >
        count = #{count,jdbcType=INTEGER},
      </if>
      <if test="firstClassify != null" >
        first_classify = #{firstClassify,jdbcType=BIGINT},
      </if>
      <if test="secondClassify != null" >
        second_classify = #{secondClassify,jdbcType=BIGINT},
      </if>
      <if test="thirdClassify != null">
        third_classify = #{thirdClassify}
      </if>
      <if test="gradeLabelId != null" >
        grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
      </if>
      <if test="subjectLabelId != null" >
        subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="enableGroupService != null" >
        enable_group_service = #{enableGroupService},
      </if>
      <if test="rightsSettingType != null" >
        rights_setting_type = #{rightsSettingType},
      </if>
      <if test="bookId != null" >
        book_id = #{bookId},
      </if>
      <if test="onlineCourseOpen != null" >
        online_course_open = #{onlineCourseOpen},
      </if>
      <if test="learningToolOpen != null" >
        learning_tool_open = #{learningToolOpen},
      </if>
      <if test="drawNowOpen != null" >
        draw_open = #{drawNowOpen},
      </if>
      <if test="volLabelId != null" >
        vol_label_id = #{volLabelId,jdbcType=BIGINT},
      </if>
      <if test="guideIsOpen != null" >
        reading_guide = #{guideIsOpen},
      </if>
      <if test="guideTitle != null" >
        guide_title = #{guideTitle,jdbcType=VARCHAR},
      </if>
      <if test="guideType != null" >
        guide_type = #{guideType,jdbcType=VARCHAR},
      </if>
      <if test="guideId != null" >
        guide_id = #{guideId,jdbcType=BIGINT},
      </if>
      <if test="guidePic != null" >
        guide_pic = #{guidePic,jdbcType=VARCHAR},
      </if>
      <if test="verLabelId != null" >
        ver_label_id = #{verLabelId,jdbcType=BIGINT}
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="updateNumber" parameterType="map" >
    update rights_setting
    set  rights_number = #{rightsNumber,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>


  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.rightsSetting.entity.RightsSetting" >
    update rights_setting
    set introduce = #{introduce,jdbcType=VARCHAR},
      rights_number = #{rightsNumber,jdbcType=VARCHAR},
      detail = #{detail,jdbcType=VARCHAR},
      count = #{count,jdbcType=INTEGER},
      first_classify = #{firstClassify,jdbcType=BIGINT},
      second_classify = #{secondClassify,jdbcType=BIGINT},
      third_classify = #{thirdClassify},
      grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
      subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
      update_time = now(),
      enable_group_service = #{enableGroupService},
      book_id = #{bookId},
      online_course_open = #{onlineCourseOpen},
      learning_tool_open = #{learningToolOpen},
      draw_open = #{drawNowOpen},
      read_type_title = #{readTypeTitle},
      vol_label_id = #{volLabelId,jdbcType=BIGINT},
      reading_guide = #{guideIsOpen},
      guide_title = #{guideTitle,jdbcType=VARCHAR},
      guide_type = #{guideType,jdbcType=VARCHAR},
      guide_id = #{guideId,jdbcType=BIGINT},
      guide_pic = #{guidePic,jdbcType=VARCHAR},
      ver_label_id = #{verLabelId,jdbcType=BIGINT},
      guide_link_url = #{guideLinkUrl},
      update_user_id = #{updateUserId},
      banner_type = #{bannerType}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="listRightsSettingPage" parameterType="map" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List_own"/>
    from
    rights_setting   r
    left join rights_setting_book_relation b on r.id = b.rights_setting_id
    <where>
      <if test="firstClassify != null">
        first_classify = #{firstClassify}
      </if>
      <if test="secondClassify != null">
        and second_classify = #{secondClassify}
      </if>
      <if test="thirdClassify != null">
        and third_classify = #{thirdClassify}
      </if>
      <if test="gradeLabelId != null">
        and grade_label_id = #{gradeLabelId}
      </if>
      <if test="subjectLabelId != null">
        and subject_label_id = #{subjectLabelId}
      </if>
      <if test="rightsSettingType !=null">
        and rights_setting_type = #{rightsSettingType}
      </if>
      <if test="bookIds != null and bookIds.size() > 0">
        and b.book_id in
        <foreach collection="bookIds" item="item" index="index" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="exceptId!=null">
        and r.id != #{exceptId}
      </if>
      <if test="volLabelId != null" >
        and vol_label_id = #{volLabelId}
      </if>
      <if test="verLabelId != null" >
        and r.ver_label_id = #{verLabelId,jdbcType=BIGINT}
      </if>
    </where>
    order by r.create_time desc
  </select>

  <select id="listBookRightsSettingPage" parameterType="map" resultMap="DtoResultMap4Book">
    select
    r.id,r.rights_number, r.introduce, r.detail, r.count, r.first_classify, r.second_classify, r.third_classify, r.grade_label_id, r.subject_label_id,
    r.create_time, r.update_time, r.enable_group_service, r.rights_setting_type, r.book_id, b.BOOK_NAME, r.vol_label_id,
    r.ver_label_id, r.show_state,
    b.isbn,
    ifnull(s.adviser_id,a.adviser_id) adviser_id,
    r.create_user_id,
    r.update_user_id
    from
    rights_setting r
    left join rights_setting_book_relation s on r.id = s.rights_setting_id
    LEFT JOIN book b ON s.book_id=b.BOOK_ID
    INNER JOIN book_adviser a ON b.BOOK_ID=a.BOOK_ID AND a.IS_DELETE=0
    WHERE
    r.rights_setting_type = 2
    <if test="bookQuery !=null">
      AND (b.BOOK_NAME LIKE CONCAT("%",#{bookQuery},"%") OR b.ISBN LIKE CONCAT("%",#{bookQuery},"%"))
    </if>
    <if test="bookQueryList != null">
      AND
      <foreach collection="bookQueryList" item="item" index="index" separator="and" open="(" close=")">
        (b.BOOK_NAME LIKE CONCAT("%",'${item}',"%") OR b.ISBN LIKE CONCAT("%",'${item}',"%"))
      </foreach>
    </if>
    <if test="adviserIds != null and adviserIds.size>0">
      AND a.ADVISER_ID in
      <foreach collection="adviserIds" item="item" index="index" separator="," open="(" close=")">
        ${item}
      </foreach>
    </if>
    <if test="firstClassify != null">
      and a.TEMPLET_ID = #{firstClassify}
    </if>
    <if test="secondClassify != null">
      and a.SECOND_TEMPLET_ID = #{secondClassify}
    </if>
    <if test="thirdClassify != null">
      and a.third_templet_id = #{thirdClassify}
    </if>
    <if test="gradeLabelId != null">
      and a.GRA_LABEL_ID = #{gradeLabelId}
    </if>
    <if test="subjectLabelId != null">
      and a.SUB_LABEL_ID = #{subjectLabelId}
    </if>
    <if test="volLabelId != null">
      and a.vol_label_id = #{volLabelId}
    </if>
    <if test="verLabelId != null">
      and a.VER_LABEL_ID = #{verLabelId}
    </if>
    group by r.id
    order by r.create_time desc
  </select>

  <select id="getByLabel" parameterType="map" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    rights_setting
    where
      rights_setting_type = 1
      <if test="firstClassify != null">
        and first_classify = #{firstClassify}
      </if>
      <if test="secondClassify != null">
        and second_classify = #{secondClassify}
      </if>
    <if test="thirdClassify != null">
      and third_classify = #{thirdClassify}
    </if>
      <if test="gradeLabelId != null">
        and grade_label_id = #{gradeLabelId}
      </if>
      <if test="subjectLabelId != null">
        and subject_label_id = #{subjectLabelId}
      </if>
      <if test="volLabelId != null" >
        and vol_label_id = #{volLabelId}
      </if>
      <if test="verLabelId != null" >
        and ver_label_id = #{verLabelId,jdbcType=BIGINT}
      </if>
    limit 1
  </select>

  <select id="getByBookId" parameterType="map" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List_own"/>
    from
    rights_setting r
    left join rights_setting_book_relation b on r.id = b.rights_setting_id
    WHERE b.book_id = #{bookId}
    and rights_setting_type = 2
    limit 1
  </select>

  <select id="selctByNumber" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    rights_setting r
    WHERE
    r.rights_number = #{rightsNumber,jdbcType=VARCHAR}
    and rights_setting_type = #{rightsSettingType}
    limit 1
  </select>

  <select id="listByBookId" parameterType="map" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List_own"/>
    from
    rights_setting r
    left join rights_setting_book_relation b on r.id = b.rights_setting_id
    WHERE b.book_id = #{bookId}
    and rights_setting_type = 2
  </select>

  <select id="listByBookIds" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List_1"/>
    from
    rights_setting r
    left join rights_setting_book_relation b on r.id = b.rights_setting_id
    WHERE b.book_id IN
    <foreach collection="list" item="item" separator="," open="(" close=")">
        ${item}
    </foreach>
    and rights_setting_type = 2
  </select>

  <update id="batchUpdateShowState" parameterType="com.pcloud.book.rightsSetting.dto.RightsSettingShowStateDTO">
  UPDATE
  rights_setting
  SET
  show_state = #{showState},
  update_time=NOW()
  WHERE id IN
  <foreach collection="rightsSettingIds" item="item" separator="," open="(" close=")" >
    #{item}
  </foreach>
  </update>

  <update id="updateUser" parameterType="com.pcloud.book.rightsSetting.entity.RightsSetting">
    update
    rights_setting
    set
      update_time = now(),
      update_user_id = #{updateUserId}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getDTOById" parameterType="long" resultMap="DtoResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    rights_setting
    where
    id = #{id,jdbcType=BIGINT}
  </select>

  <select id="rightsCountByBookIds" resultType="com.pcloud.book.rightsSetting.dto.BookRightsCountsDto">
SELECT
	a.book_id bookId,
	ifnull(count(1),0) count
FROM
	rights_setting_book_relation a
INNER JOIN rights_now_item b ON a.rights_setting_id = b.rights_setting_id
WHERE
	a.book_id IN
    <foreach collection="bookIds" item="item" separator="," open="(" close=")" >
      #{item}
    </foreach>
    GROUP BY a.book_id
  </select>

  <select id="getAllNews" resultMap="com.pcloud.book.applet.dao.impl.AppletNewsDaoImpl.BaseResultMap">
    select
     news.id, news.news_name, news.source, news.news_classify_id, news.pro_label_id, news.dep_label_id, news.pur_label_id, news.type, news.digest,
     pic1, pic2, pic3, news.content, news.create_time, news.update_time, news.first_classify, news.second_classify, news.grade_label_id, news.subject_label_id, news.rights_classify_id,
     news.jump_type, news.jump_url, news.url_number, news.custom_tag_id
    from
    rights_setting a
    inner join
    rights_setting_book_relation  c on a.id = c.rights_setting_id
    inner join
    rights_now_item b on a.id = b.rights_setting_id
    inner join
    applet_news news on b.serve_id = news.id
    where
    a.rights_setting_type = 2
    and
    b.serve_type = 'NEWS'
    and
    content is not null
    group  by news.id
  </select>

  <select id="getAllBookNews" resultMap="BookNewsDTOMap">
    SELECT
	c.book_adviser_id,
	c.book_id,
	c.adviser_id,
	c.channel_id,
	b.serve_id
    FROM
	rights_setting a
    INNER JOIN rights_setting_book_relation c ON a.id = c.rights_setting_id
    INNER JOIN rights_now_item b ON a.id = b.rights_setting_id
    INNER JOIN applet_news news ON b.serve_id = news.id
    INNER JOIN book_group d ON c.book_id = d.book_id
    INNER JOIN resource_page e ON e.book_group_id = d.id
    WHERE
	a.rights_setting_type = 2
    AND b.serve_type = 'NEWS'
    AND content IS NOT NULL
  </select>

</mapper>
