<?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="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" />
  </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="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" />
  </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>
  <sql id="Base_Column_List" >
    id,rights_number, introduce, detail, count, first_classify, second_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
  </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.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
  </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.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
  </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,
      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)
    values ( #{rightsNumber,jdbcType=VARCHAR}, #{introduce,jdbcType=VARCHAR}, #{detail,jdbcType=VARCHAR},
      #{count,jdbcType=INTEGER}, #{firstClassify,jdbcType=BIGINT}, #{secondClassify,jdbcType=BIGINT},
      #{gradeLabelId,jdbcType=BIGINT}, #{subjectLabelId,jdbcType=BIGINT}, now(),
      #{updateTime,jdbcType=TIMESTAMP}, #{enableGroupService}, #{rightsSettingType}, #{bookId},
      #{onlineCourseOpen},#{learningToolOpen},#{drawNowOpen},#{readTypeTitle}, #{volLabelId},
      #{guideIsOpen},#{guideTitle},#{guideType},#{guideId},#{guidePic}, #{verLabelId}, #{agentId})
  </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="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},
      grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
      subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      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}
    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="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.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
    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="adviserIds != null and adviserIds.size>0">
      AND a.ADVISER_ID in
      <foreach collection="adviserIds" item="item" index="index" separator="," open="(" close=")">
        ${item}
      </foreach>
    </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="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>

</mapper>
