<?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.BookProductDaoImpl">

	<!-- 书籍作品实体DTO -->
	<resultMap id="bookProductMap" type="bookProductDto">
		<id column="BOOK_PRODUCT_ID" property="bookProductId" jdbcType="BIGINT" />
		<result column="BOOK_ID" property="bookId" jdbcType="BIGINT" />
		<result column="PRODUCT_ID" property="productId" jdbcType="BIGINT" />
		<result column="IS_SUPER" property="isSuper" jdbcType="TINYINT" />
		<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="SOUND_ID" property="soundId" jdbcType="BIGINT" />
		<result column="PRODUCT_TYPE" property="productType" jdbcType="VARCHAR" />
		<result column="VERSION" property="version" jdbcType="BIGINT" />
	</resultMap>
	
	<!-- 新增书籍作品关联信息-->
	<insert id="insert" parameterType="bookProduct" useGeneratedKeys="true" keyProperty="bookProductId" flushCache="true">
		INSERT INTO BOOK_PRODUCT (
			BOOK_ID, PRODUCT_ID, CHANNEL_ID, IS_SUPER,CREATED_USER, CREATED_DATE,PRODUCT_TYPE
		)
		VALUES (
			#{bookId, jdbcType=BIGINT}, #{productId, jdbcType=BIGINT},  #{channelId, jdbcType=BIGINT}, #{isSuper, jdbcType=TINYINT},
			#{createdUser, jdbcType=BIGINT}, NOW(),#{productType, jdbcType=VARCHAR}
		)
	</insert>
	
	<!-- 删除书籍作品关联信息 -->
	<delete id="delete" parameterType="bookProduct" flushCache="true">
		DELETE FROM 
			BOOK_PRODUCT
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			PRODUCT_ID = #{productId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{createdUser, jdbcType=BIGINT}
	</delete>
	
	<!-- 删除书籍应用关联信息 -->
	<delete id="deleteByBook" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_PRODUCT
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
	</delete>
	
	<!-- 批量删除书籍作品关联信息 -->
	<delete id="deleteByBooks" parameterType="list" flushCache="true">
		DELETE FROM 
			BOOK_PRODUCT
		WHERE 
			BOOK_ID IN 
			<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
	</delete>
	
	<!-- 获取书籍关联的作品 -->
	<select id="getListById" resultMap="bookProductMap" parameterType="bookProduct">
		SELECT 
			BOOK_PRODUCT_ID, BOOK_ID, PRODUCT_ID, CHANNEL_ID,SOUND_ID,PRODUCT_TYPE,CREATED_USER
		FROM 
			BOOK_PRODUCT
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{createdUser, jdbcType=BIGINT}
	</select>
	
	<!-- 根据书籍和渠道删除书籍作品关联信息 -->
	<delete id="deleteByBookAndChannel" parameterType="map" flushCache="true">
		DELETE FROM 
			BOOK_PRODUCT
		WHERE 
			BOOK_ID = #{bookId}
		AND 
		    CHANNEL_ID = #{channelId}	
	</delete>
	
	<!-- 根据书籍和渠道删除书籍作品关联信息 -->
	<update id="update" parameterType="bookProduct" >
		UPDATE  
			BOOK_PRODUCT
		SET SOUND_ID = #{soundId}
		WHERE 
			BOOK_ID = #{bookId}
		AND 
		    CHANNEL_ID = #{channelId}	
		AND 
		    PRODUCT_ID = #{productId}
	</update>
	
	<!-- 获取书籍关联的作品 -->
	<select id="getMapById" resultMap="bookProductMap" parameterType="bookProduct">
		SELECT 
			BOOK_PRODUCT_ID, BOOK_ID, PRODUCT_ID, CHANNEL_ID,SOUND_ID,PRODUCT_TYPE,CREATED_USER
		FROM 
			BOOK_PRODUCT
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{createdUser, jdbcType=BIGINT}
		<if test="productType!=null">
		AND  
		    PRODUCT_TYPE = #{productType}
		</if>
	</select>
	
	
	<!-- 清除录音 -->
	<update id="cleanSound" parameterType="Long" >
		UPDATE  
			BOOK_PRODUCT
		SET SOUND_ID = null
		WHERE 
		    SOUND_ID = #{_parameter}
	</update>
	
	
	<!-- 获取与录音相关信息 -->
	<select id="listBySoundId" resultMap="bookProductMap" parameterType="Long">
		SELECT 
			BOOK_PRODUCT_ID, BOOK_ID, PRODUCT_ID, CHANNEL_ID,CREATED_USER
		FROM 
			BOOK_PRODUCT
		WHERE 
			SOUND_ID = #{_parameter}
	</select>
	
	<!-- 获取所有图书作品-->
	<select id="getAllBookProduct" resultMap="bookProductMap">
		SELECT 
			BOOK_PRODUCT_ID, BOOK_ID, PRODUCT_ID, CHANNEL_ID,CREATED_USER
		FROM 
			BOOK_PRODUCT
	</select>
	
	<!-- 	更新商品類型 -->
	<update id="updateProductType" parameterType="java.util.List" >  
		<foreach collection="list" index="index" item="item" >  
		      UPDATE 
		      	BOOK_PRODUCT 
		      SET
		     	 PRODUCT_TYPE = #{item.productType}
		      WHERE 
		      	BOOK_PRODUCT_ID = #{item.bookProductId};   
		  </foreach>
	</update>
	
	  <!-- 	 获取图书关联超级作者商品个数 -->
	<select id="getSuperProCount" resultType="Integer" parameterType="map">
		  SELECT COUNT(1) 
		  FROM  BOOK_PRODUCT 
		  WHERE BOOK_ID = #{bookId, jdbcType=BIGINT} 
		  AND  CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		  AND CREATED_USER = #{adviserId, jdbcType=BIGINT}
		  AND IS_SUPER = 1
	</select>
	
	  <!-- 	 获取图书关联普通作者商品个数 -->
	<select id="getProCount" resultType="Integer" parameterType="map">
		  SELECT COUNT(1) 
		  FROM  BOOK_PRODUCT 
		  WHERE BOOK_ID = #{bookId, jdbcType=BIGINT} 
		  AND  CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		  AND CREATED_USER = #{adviserId, jdbcType=BIGINT}
		  AND IS_SUPER = 0
	</select>
</mapper>