<?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.book.dao.impl.BookResourceDaoImpl">

	<!-- 书籍应用实体DTO -->
	<resultMap id="bookResourceMap" type="bookResourceDto">
		<id column="BOOK_RESOURCE_ID" property="bookResourceId" jdbcType="BIGINT" />
		<result column="BOOK_ID" property="bookId" jdbcType="BIGINT" />
		<result column="RESOURCE_ID" property="resourceId" jdbcType="BIGINT" />
		<result column="RESOURCE_TYPE" property="resourceType" jdbcType="VARCHAR" />
		<result column="CHANNEL_ID" property="channelId" jdbcType="BIGINT" />
		<result column="CREATED_USER" property="createdUser" jdbcType="BIGINT" />
		<result column="CREATED_DATE" property="createdDate" jdbcType="TIMESTAMP" />
		<result column="VERSION" property="version" jdbcType="BIGINT" />
	</resultMap>
	
	<!-- 新增书籍资源关联信息-->
	<insert id="insert" parameterType="bookResource" useGeneratedKeys="true" keyProperty="bookResourceId" flushCache="true">
		INSERT INTO BOOK_RESOURCE (
			BOOK_ID, RESOURCE_ID,RESOURCE_TYPE, CHANNEL_ID, CREATED_USER, CREATED_DATE
		)
		VALUES (
			#{bookId, jdbcType=BIGINT}, #{resourceId, jdbcType=BIGINT}, #{resourceType, jdbcType=VARCHAR}, #{channelId, jdbcType=BIGINT}, 
			#{createdUser, jdbcType=BIGINT}, NOW()
		)
	</insert>
	
	<!-- 删除书籍资源关联信息 -->
	<delete id="deleteById" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_RESOURCE
		WHERE 
			BOOK_RESOURCE_ID = #{bookResourceId, jdbcType=BIGINT}
	</delete>
	
	<!-- 删除书籍资源关联信息 -->
	<delete id="deleteByBook" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_RESOURCE
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
	</delete>
	
	<!-- 批量删除书籍资源关联信息 -->
	<delete id="deleteByBooks" parameterType="list" flushCache="true">
		DELETE FROM 
			BOOK_RESOURCE
		WHERE 
			BOOK_ID IN 
			<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
	</delete>
	
	<!-- 获取书籍关联的资源 -->
	<select id="getListById" resultMap="bookResourceMap" parameterType="bookResource">
		SELECT 
			BOOK_RESOURCE_ID, BOOK_ID, RESOURCE_ID, CHANNEL_ID,RESOURCE_TYPE
		FROM 
			BOOK_RESOURCE
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{createdUser, jdbcType=BIGINT}
	</select>
	
	<!-- 根据书籍和渠道删除书籍资源关联信息 -->
	<delete id="deleteByBookAndChannel" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_RESOURCE
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND 
	        CHANNEL_ID = #{channelId, jdbcType=BIGINT}
	</delete>

		<!-- 获取单个书籍和资源关联关系 -->
	<select id="getByBookResourceId" resultMap="bookResourceMap" parameterType="Long">
		SELECT 
			BOOK_RESOURCE_ID, BOOK_ID, RESOURCE_ID, CHANNEL_ID, CREATED_USER,RESOURCE_TYPE
		FROM 
			BOOK_RESOURCE
		WHERE
			BOOK_RESOURCE_ID = #{_parameter, jdbcType=BIGINT}
	</select>
	
	
	<!--根据资源标识获取图书资源关联关系 -->
	<select id="listBookByResource" resultMap="bookResourceMap" parameterType="Long">	
	    SELECT
			BOOK_RESOURCE_ID, BOOK_ID, RESOURCE_ID,RESOURCE_TYPE, CHANNEL_ID, CREATED_USER
		FROM
			 BOOK_RESOURCE
		 WHERE
			 RESOURCE_ID = #{_parameter, jdbcType=BIGINT}
	</select>
	  
	  <!-- 	 获取图书关联资源个数 -->
	<select id="getResourceCount" resultType="Integer" parameterType="map">
		  SELECT COUNT(1) 
		  FROM  BOOK_RESOURCE 
		  WHERE BOOK_ID = #{bookId, jdbcType=BIGINT} 
		  AND  CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		  AND CREATED_USER = #{adviserId, jdbcType=BIGINT}
	</select>
	
	<!-- 获取资源关联的图书 -->
	<select id="listByResourceId" resultType="Long" parameterType="Long">
		SELECT 
			DISTINCT BOOK_ID
		FROM 
			BOOK_RESOURCE
		WHERE
			RESOURCE_ID = #{_parameter, jdbcType=BIGINT}
	</select>
</mapper>