<?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.BookFreezeDaoImpl">
	<resultMap id="BaseResultMap" type="bookFreezeDto">
		<id column="ID" property="id" jdbcType="BIGINT" />
		<result column="BOOK_ID" property="bookId" jdbcType="BIGINT" />
		<result column="FREEZETIME" property="freezetime" jdbcType="TIMESTAMP" />
		<result column="IS_FREEZE" property="freezeStatus" jdbcType="BIGINT" />
		<result column="THAW_TIME" property="thawTime" jdbcType="VARCHAR" />
		<result column="BOOK_FUND_ID" property="bookFundId" jdbcType="BIGINT" />
		<result column="CREATED_USER" property="createdUser" jdbcType="BIGINT" />
		<result column="CREATED_DATE" property="createdDate" jdbcType="TIMESTAMP" />
		<result column="LAST_MODIFIED_USER" property="lastModifiedUser" jdbcType="BIGINT" />
		<result column="LAST_MODIFIED_DATE" property="lastModifiedDate" jdbcType="TIMESTAMP" />
		<!--michael 新增于20180806--><result column="THAW_TIME" property="thawTime" jdbcType="VARCHAR" />
		<result column="PROXY_MODE" property="proxyMode" jdbcType="BIT" />
		<result column="TRANSFEROR" property="transferor" jdbcType="VARCHAR" />

	</resultMap>
	
	<!-- 新增冻结收益信息 -->
	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<insert id="insert" parameterType="bookFreeze" useGeneratedKeys="true" keyProperty="id" flushCache="true">
		insert into book_freeze (
			BOOK_ID, FREEZETIME, IS_FREEZE, BOOK_FUND_ID, CREATED_USER, CREATED_DATE, LAST_MODIFIED_USER, LAST_MODIFIED_DATE ,
			PROXY_MODE,TRANSFEROR
		)
		values (
			#{bookId,jdbcType=BIGINT}, #{freezetime,jdbcType=TIMESTAMP}, 1, #{bookFundId,jdbcType=BIGINT}, #{createdUser,jdbcType=BIGINT},
			now(),#{createdUser,jdbcType=BIGINT},now(),#{proxyMode,jdbcType=BIT},#{transferor,jdbcType=VARCHAR}
		)
	</insert>
	
	<!-- 获取冻结信息 -->
	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<select id="getBaseById" resultType="bookFreezeDto" parameterType="java.lang.Long">
		SELECT
			ID as id,
			BOOK_ID as bookId,
			FREEZETIME as freezetime,
			PROXY_MODE as proxyMode,
			TRANSFEROR as transferor
		FROM book_freeze
		WHERE BOOK_ID = #{bookId,jdbcType=BIGINT}
		AND IS_FREEZE = 1
		LIMIT 1
	</select>


	<select id="getInfoById" resultType="bookFreezeDto" parameterType="java.lang.Long">
		SELECT
			ID as id,
			BOOK_ID as bookId,
			FREEZETIME as freezetime,
			PROXY_MODE as proxyMode,
			TRANSFEROR as transferor
		FROM book_freeze
		WHERE BOOK_ID = #{bookId,jdbcType=BIGINT}
		ORDER BY freezetime DESC LIMIT 1
	</select>


	<!-- 解冻收益(没有被基金购买) -->
	<update id="thaw" parameterType="map" flushCache="true">
		UPDATE book_freeze
		SET
			IS_FREEZE = 2,
			THAW_TIME = now(),
			LAST_MODIFIED_USER = #{partyId, jdbcType=BIGINT},
			<if test="bookFundId != null">
				BOOK_FUND_ID = #{bookFundId, jdbcType=BIGINT},
			</if>
			LAST_MODIFIED_DATE = now()
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			IS_FREEZE = 1
	</update>


	<!-- 批量获取书籍信息 -->
	<select id="isFreeze" resultType="boolean" parameterType="Long">
		SELECT
			count(1)
		FROM
			`book_freeze`
		WHERE
			 BOOK_ID = #{bookId}
		AND
			IS_FREEZE = 1
		 <![CDATA[
		AND
		 	FREEZETIME <  NOW()
		  ]]> ;
	</select>

	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<select id="getIsFreeze" resultMap="BaseResultMap" parameterType="list">
		SELECT
			BOOK_ID,IS_FREEZE
		FROM
			`book_freeze`
		WHERE
		BOOK_ID IN
		<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
			${item}
		</foreach>
		AND
		  IS_FREEZE = 1
	</select>

	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<select id="getFreezeStatus" resultMap="BaseResultMap" parameterType="list">
		SELECT
		BOOK_ID,IS_FREEZE,PROXY_MODE,TRANSFEROR
		FROM
		`book_freeze`
		WHERE
		BOOK_ID IN
		<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
			${item}
		</foreach>
		AND
		IS_FREEZE = 1
		<![CDATA[
		AND
		 	FREEZETIME <  NOW()
		  ]]> ;
	</select>

	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<!-- 该书刊当前和以后基金冻结信息 -->
	<select id="getFreezeInfo" resultMap="BaseResultMap" parameterType="long">
		SELECT
			ID, BOOK_ID, IS_FREEZE, BOOK_FUND_ID,PROXY_MODE,TRANSFEROR
		FROM
			`book_freeze`
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			IS_FREEZE = 1
	</select>

	<!--michael 20180806 新增PROXY_MODE，TRANSFEROR-->
	<!-- 批量获取书刊当前和以后基金冻结信息 -->
	<select id="listFreezeInfo" resultMap="BaseResultMap" parameterType="list">
		SELECT
			ID, BOOK_ID, IS_FREEZE, BOOK_FUND_ID,PROXY_MODE,TRANSFEROR,THAW_TIME,FREEZETIME
		FROM
			`book_freeze`
		WHERE
			BOOK_ID IN
			<foreach collection="list" index="i" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
		AND
			IS_FREEZE = 1
	</select>

	<select id="filterFreezeBook" resultType="java.lang.Long">
		SELECT
		distinct BOOK_ID
		FROM
		`book_freeze`
		WHERE
		BOOK_ID IN
		<foreach collection="bookIds" index="i" item="item" open="(" separator="," close=")">
			${item}
		</foreach>
		AND
		IS_FREEZE = 1
	</select>

	<select id="filterBook4Erp" parameterType="map" resultType="long">
		SELECT BOOK_ID
		FROM book_freeze
		<where>
			<if test="bookFreeze != null">
				IS_FREEZE = #{bookFreeze}
		    </if>
			<if test="bookFreezeTimeBegin!=null and bookFreezeTimeBegin!= ''">
				AND FREEZETIME >= #{bookFreezeTimeBegin}
			</if>
			<if test="bookFreezeTimeEnd!=null and bookFreezeTimeEnd!=''">
				AND FREEZETIME <![CDATA[<=]]> #{bookFreezeTimeEnd}
			</if>
		</where>

	</select>

	<select id="listBookFreezeInfo" parameterType="map" resultType="com.pcloud.book.book.vo.BookFreezeInfoDto">
		SELECT
		BOOK_ID bookId,
		DATE_FORMAT( FREEZETIME, '%Y-%m-%d' ) freezeTime,
		IS_FREEZE freezeStatus,
		DATE_FORMAT( THAW_TIME, '%Y-%m-%d' ) thawTime
		FROM
		book_freeze
		where BOOK_ID IN
		<foreach collection="bookIds" open="(" item="it" separator="," close=")">
			#{it}
		</foreach>
	</select>
</mapper>