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

	<!-- 书籍应用实体DTO -->
	<resultMap id="bookAppMap" type="bookAppDto">
		<id column="BOOK_APP_ID" property="bookAppId" jdbcType="BIGINT" />
		<result column="BOOK_ID" property="bookId" jdbcType="BIGINT" />
		<result column="APP_ID" property="appId" jdbcType="BIGINT" />
		<result column="CHANNEL_ID" property="channelId" jdbcType="BIGINT" />
		<result column="TYPE_CODE" property="typeCode" jdbcType="VARCHAR" />
		<result column="TYPE_NAME" property="typeName" jdbcType="VARCHAR" />
		<result column="TITLE" property="title" jdbcType="VARCHAR" />
		<result column="SQUARE_IMG" property="squareImg" jdbcType="VARCHAR" />
		<result column="REMARK" property="remark" jdbcType="VARCHAR" />
		<result column="TRANSVERSE_IMG" property="transverseImg" jdbcType="VARCHAR" />
		<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="bookApp" useGeneratedKeys="true" keyProperty="bookAppId" flushCache="true">
		INSERT INTO BOOK_APP (
			BOOK_ID, APP_ID, CHANNEL_ID, CREATED_USER, CREATED_DATE
		)
		VALUES (
			#{bookId, jdbcType=BIGINT}, #{appId, jdbcType=BIGINT}, #{channelId, jdbcType=BIGINT}, 
			#{createdUser, jdbcType=BIGINT}, NOW()
		)
	</insert>

    <!--   批量新增书籍应用关联信息 -->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">  
      INSERT INTO BOOK_APP (
			BOOK_ID, APP_ID, CHANNEL_ID, CREATED_USER, CREATED_DATE
		)
	    VALUES 
     <foreach collection="list" item="item" index="index" separator="," >  
       (#{item.bookId,jdbcType=BIGINT}, #{item.appId,jdbcType=BIGINT}, #{item.channelId,jdbcType=BIGINT}, 
        #{item.createdUser,jdbcType=BIGINT}, NOW()) 
     </foreach>  
    </insert>  
	
	<!-- 删除书籍应用关联信息 -->
	<delete id="deleteById" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_APP
		WHERE 
			BOOK_APP_ID = #{bookAppId, jdbcType=BIGINT}
	</delete>
	
	<!-- 删除书籍应用关联信息 -->
	<delete id="deleteByBook" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_APP
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
	</delete>
	
	<!-- 删除应用关联书籍信息 -->
	<delete id="deleteByAppId" parameterType="long" flushCache="true">
		DELETE FROM 
			BOOK_APP
		WHERE 
			APP_ID = #{appId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{adviserId, jdbcType=BIGINT}
		AND 
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
	</delete>
	
	<!-- 批量删除书籍应用关联信息 -->
	<delete id="deleteByBooks" parameterType="list" flushCache="true">
		DELETE FROM 
			BOOK_APP
		WHERE 
			BOOK_ID IN 
			<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
	</delete>
	
	<!-- 获取单个书籍和应用关联关系 -->
	<select id="listBaseById" resultMap="bookAppMap" parameterType="bookApp">
		SELECT 
			BOOK_APP_ID, BOOK_ID, APP_ID, CHANNEL_ID
		FROM 
			BOOK_APP
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{createdUser, jdbcType=BIGINT}
		AND 
			IS_DELETE = 0
	</select>
	
	<!-- 	获取关联信息 -->
	<select id="getByBookAppId" resultMap="bookAppMap" parameterType="Long">
		SELECT 
			BOOK_APP_ID, BOOK_ID, APP_ID, CHANNEL_ID, CREATED_USER
		FROM 
			BOOK_APP
		WHERE
			BOOK_APP_ID = #{_parameter}
	</select>
	
	<!-- 获取书籍关联的应用 -->
	<select id="getListById" resultMap="bookAppMap" parameterType="bookApp">
		SELECT 
			BOOK_APP_ID, BOOK_ID, b.APP_ID, b.CHANNEL_ID,a.TITLE,SQUARE_IMG,TRANSVERSE_IMG,T.TYPE_CODE,T.TYPE_NAME,a.REMARK
		FROM 
			BOOK_APP b
		LEFT JOIN 
			APP a ON b.APP_ID = A.APP_ID
		LEFT JOIN
		    APP_TYPE T ON  T.TYPE_CODE = a.TYPE_CODE
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			b.CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			b.CREATED_USER = #{createdUser, jdbcType=BIGINT}
		AND 
			b.IS_DELETE = 0
	</select>
	
	<!-- 根据书籍和渠道删除书籍应用关联信息 -->
	<delete id="deleteByBookAndChannel" parameterType="map" flushCache="true">
		DELETE FROM 
			BOOK_APP
		WHERE 
			BOOK_ID = #{bookId, jdbcType=BIGINT}
	    AND 
	        CHANNEL_ID = #{channelId, jdbcType=BIGINT}
	</delete>
	
	 <!-- 获取图书关联应用个数 -->
	<select id="getAppCount" resultType="Integer" parameterType="map">
		  SELECT COUNT(1) 
		  FROM  BOOK_APP 
		  WHERE BOOK_ID = #{bookId, jdbcType=BIGINT} 
		  AND  CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		  AND CREATED_USER = #{adviserId, jdbcType=BIGINT}
		  AND IS_DELETE = 0
	</select>
	
	<!-- 修改书籍和应用的关系 -->
	<update id="updateBookApp4Adviser" parameterType="map" flushCache="true">
		UPDATE BOOK_APP
		SET
			IS_DELETE = #{status, jdbcType=BIGINT}
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT} 
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			CREATED_USER = #{adviserId, jdbcType=BIGINT}
	</update>
	
</mapper>