<?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.copyright.dao.impl.BookAuthServeDaoImpl">

    <!--   批量新增书籍应用关联信息 -->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">  
      INSERT INTO BOOK_AUTH_SERVE (
			BOOK_ID, CHANNEL_ID,ADVISER_ID,SERVE_ID,SERVE_TYPE, CREATED_USER, CREATED_DATE
		)
	    VALUES 
     <foreach collection="list" item="item" index="index" separator="," >  
       (#{item.bookId,jdbcType=BIGINT}, #{item.channelId,jdbcType=BIGINT}, #{item.adviserId,jdbcType=BIGINT},
		 #{item.serveId,jdbcType=BIGINT}, #{item.serveType,jdbcType=VARCHAR},
        #{item.adviserId,jdbcType=BIGINT}, NOW())
     </foreach>  
    </insert>  
	

	<select id="isSetServeAuth" resultType="com.pcloud.book.copyright.dto.ServeDTO" parameterType="map">
		SELECT 
			SERVE_ID serveId, SERVE_TYPE serveType
		FROM 
			BOOK_AUTH_SERVE
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND 
			SERVE_ID in
		<foreach collection="serveIds" item="item" open="(" separator="," close=")">
			${item}
		</foreach>
	   AND IS_DELETE = 0
	</select>

	<update id="deleteServes"  parameterType="List">
		UPDATE
		BOOK_AUTH_SERVE
		SET IS_DELETE = 1
		WHERE
		ID in
		<foreach collection="list" item="item" open="(" separator="," close=")">
		${item}
	     </foreach>
	</update>

	<select id="listServesByBook" resultType="serveVO" parameterType="map">
		SELECT
		SERVE_ID serveId, SERVE_TYPE serveType
		FROM
		BOOK_AUTH_SERVE
		WHERE
		BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
		CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
		ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
		IS_DELETE = 0
	</select>


	<select id="checkIsNeedAuth" resultType="Boolean" parameterType="map">
		SELECT
		  COUNT(1)
		FROM
		BOOK_AUTH_SERVE
		WHERE
		BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
		CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
		ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND SERVE_ID = #{serveId}
		AND SERVE_TYPE = #{serveType}
		AND IS_DELETE = 0
	</select>

	<select id="selectNeedDelete" resultType="long" parameterType="map">
		SELECT
		ID
		FROM
		BOOK_AUTH_SERVE
		WHERE
		BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
		CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
		ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
		IS_DELETE = 0
	</select>

	<select id="getBookAuthServeList" resultType="com.pcloud.book.copyright.dto.BookAuthServerDTO">
		SELECT DISTINCT b.serve_id serveId, b.serve_type serveType
		FROM `book_auth_info` a
		INNER JOIN book_auth_serve b ON b.book_id = a.book_id AND b. channel_id = a.channel_id AND b.adviser_id = a.adviser_id
		WHERE a.book_status = 1
		AND b.is_delete = 0
		AND a.channel_id = #{channelId}
	</select>

</mapper>