<?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.appeal.dao.impl.BookAppealDaoImpl">

	<!-- 书籍申诉实体DTO -->
	<resultMap id="bookAppealMap" type="bookAppealDto">
		<id column="BOOK_APPEAL_ID" property="bookAppealId" jdbcType="BIGINT" />
		<result column="BOOK_ID" property="bookId" jdbcType="BIGINT" />
		<result column="CHANNEL_ID" property="channelId" jdbcType="BIGINT" />
		<result column="TEMPLET_ID" property="templetId" jdbcType="BIGINT" />
		<result column="SECOND_TEMPLET_ID" property="secondTempletId" jdbcType="BIGINT" />
		<result column="third_TEMPLET_ID" property="thirdTempletId" jdbcType="BIGINT" />
		<result column="REASON" property="reason" jdbcType="VARCHAR" />
		<result column="DECLARANT" property="declarant" jdbcType="BIGINT" />
		<result column="PHONE" property="phone" jdbcType="VARCHAR" />
		<result column="APPEAL_SUBMIT_DATE" property="appealSubmitDate" jdbcType="TIMESTAMP" />
		<result column="APPEAL_STATE" property="appealState" jdbcType="BIGINT" />
		<result column="AUDIT_REASON" property="auditReason" jdbcType="VARCHAR" />
		<result column="AUDIT_USER" property="auditUser" 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" />
		<result column="VERSION" property="version" jdbcType="BIGINT" />
		<result column="BOOK_NAME" property="bookName" jdbcType="VARCHAR" />
		<result column="ISBN" property="isbn" jdbcType="VARCHAR" />
		<result column="SERIAL_NUMBER" property="serialNumber" jdbcType="VARCHAR" />
		<result column="IS_BOOK_GROUP" property="isBookGroup" jdbcType="BIT" />
		<result column="ADD_TYPE" property="addType" jdbcType="BIT" />
		<collection property="defendants" column="BOOK_APPEAL_ID" ofType="bookDefendant"
			select="com.pcloud.book.appeal.dao.impl.BookDefendantDaoImpl.list" />
	</resultMap>
	
	<!-- 新增书籍信息-->
	<insert id="insert" parameterType="bookAppeal" useGeneratedKeys="true" keyProperty="bookAppealId" flushCache="true">
		INSERT INTO `BOOK_APPEAL` (
			BOOK_ID, CHANNEL_ID, TEMPLET_ID,SECOND_TEMPLET_ID,REASON,DECLARANT, PHONE, APPEAL_SUBMIT_DATE, APPEAL_STATE, AUDIT_REASON, AUDIT_USER,add_type,
			CREATED_USER,CREATED_DATE,LAST_MODIFIED_USER, LAST_MODIFIED_DATE, IS_BOOK_GROUP, third_TEMPLET_ID
		)
		VALUES (
			#{bookId, jdbcType=BIGINT}, #{channelId, jdbcType=BIGINT}, #{templetId, jdbcType=BIGINT}, #{secondTempletId, jdbcType=BIGINT},#{reason, jdbcType=VARCHAR}, #{declarant, jdbcType=BIGINT},
			#{phone, jdbcType=VARCHAR}, #{appealSubmitDate, jdbcType=TIMESTAMP}, #{appealState, jdbcType=BIGINT}, #{auditReason, jdbcType=VARCHAR}, 
			#{auditUser, jdbcType=BIGINT},#{addType, jdbcType=BIT}, #{createdUser, jdbcType=BIGINT},  NOW(),
			#{lastModifiedUser, jdbcType=BIGINT}, NOW(), #{isBookGroup, jdbcType=BIT}, #{thirdTempletId}
		)
	</insert>
	
	<!-- 修改书刊申诉信息-->
	<update id="update" parameterType="bookAppeal" flushCache="true">
		UPDATE BOOK_APPEAL
		SET
			APPEAL_STATE = #{appealState, jdbcType=BIGINT}, 
			AUDIT_REASON = #{auditReason, jdbcType=VARCHAR}, 
			AUDIT_USER = #{auditUser, jdbcType=BIGINT},
			AUDIT_DATE = now(),
			LAST_MODIFIED_USER = #{lastModifiedUser, jdbcType=BIGINT}, 
			LAST_MODIFIED_DATE =  NOW(),
			VERSION = VERSION + 1
		WHERE
			BOOK_APPEAL_ID = #{bookAppealId, jdbcType=BIGINT}
		AND
			VERSION = #{version, jdbcType=BIGINT}
	</update>
	
	<!-- 获取申诉列表 -->
	<select id="list" resultMap="bookAppealMap" parameterType="map">
		SELECT 
			BOOK_APPEAL_ID, PHONE, DECLARANT,B.ISBN, B.BOOK_NAME,B.SERIAL_NUMBER,A.APPEAL_STATE,A.TEMPLET_ID,A.SECOND_TEMPLET_ID
		FROM 
			BOOK_APPEAL A
		LEFT JOIN
			BOOK B
		ON
			B.BOOK_ID = A.BOOK_ID
		WHERE 1=1
		<if test="bookName != null">
			AND B.BOOK_NAME like '%${bookName}%'
		</if>
		<if test="appealState != null">
			AND A.APPEAL_STATE = #{appealState}
		</if>
		ORDER BY
			A.CREATED_DATE DESC
	</select>
	
	<!-- 获取申诉详情 -->
	<select id="getById" resultMap="bookAppealMap" parameterType="long">
		SELECT 
			BOOK_APPEAL_ID, PHONE,CHANNEL_ID, DECLARANT,REASON,BOOK_ID,VERSION,APPEAL_STATE,AUDIT_REASON,TEMPLET_ID,CREATED_USER,IS_BOOK_GROUP,ADD_TYPE,SECOND_TEMPLET_ID, third_TEMPLET_ID
		FROM 
			BOOK_APPEAL A
		WHERE 
		    BOOK_APPEAL_ID = #{_parameter} LIMIT 1
	</select>
	
	
</mapper>