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

	<!-- 书籍类型DTO -->
	<resultMap id="bookTypeMap" type="bookTypeDto">
		<id column="BOOK_TYPE_ID" property="bookTypeId" jdbcType="BIGINT" />
		<result column="TYPE_CODE" property="typeCode" jdbcType="VARCHAR" />
		<result column="TYPE_NAME" property="typeName" 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" />
		<result column="COUNT" property="count" jdbcType="BIGINT" />
		<result column="PERCENT" property="percent" jdbcType="DOUBLE" />
	</resultMap>

	<!-- 获取类型列表 -->
	<select id="getList" resultMap="bookTypeMap">
		SELECT 
			TYPE_CODE, TYPE_NAME
		FROM 
			BOOK_TYPE
		ORDER BY 
			BOOK_TYPE_ID ASC
	</select>
	
	<!-- 图书和期刊占比 -->
	<select id="getPercent4Type" resultMap="bookTypeMap" parameterType="long">
		SELECT 
			T.TYPE_CODE, T.TYPE_NAME, COUNT(DISTINCT A.BOOK_ID) `COUNT`
		FROM 
			BOOK_ADVISER A 
		INNER JOIN 
			BOOK B 
		ON 
			A.BOOK_ID = B.BOOK_ID AND A.ADVISER_ID = #{adviserId}
		INNER JOIN 
			BOOK_TYPE T 
		ON 
			B.TYPE_CODE = T.TYPE_CODE 
		GROUP BY 
			T.TYPE_CODE
	</select>
	
	<!-- 获取图书类型Map关联 -->
	<select id="getTypeCodeMap" resultMap="bookTypeMap">
		SELECT 
			TYPE_CODE, TYPE_NAME
		FROM 
			BOOK_TYPE
		ORDER BY 
			BOOK_TYPE_ID ASC
	</select>
	
	
	
</mapper>