<?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.copyright.dao.impl.BookAuthUserDaoImpl">

	<insert id="insert" useGeneratedKeys="true" parameterType="bookAuthUser">
      INSERT INTO BOOK_AUTH_USER(
			BOOK_ID, CHANNEL_ID,ADVISER_ID,WECHAT_USER_ID,MONTHS,province,city,IS_AUTH_CODE, CREATED_DATE,CREATED_USER, CREATED_TIME, is_paper_book, is_group_book, book_auth_code_id)
	    VALUES
       (#{bookId,jdbcType=BIGINT}, #{channelId,jdbcType=BIGINT}, #{adviserId,jdbcType=BIGINT},
		 #{wechatUserId}, #{months}, #{province}, #{city}, #{isAuthCode},NOW(), #{adviserId}, NOW(), #{isPaperBook}, #{isGroupBook}, #{bookAuthCodeId})
    </insert>

	<select id="getAuthUserCount" resultType="bookAuthCodeUserVO" parameterType="map">
		SELECT
			COUNT(1) totalCount,
			IFNULL(SUM(is_auth_code),0) payUserCount
		FROM
			BOOK_AUTH_USER
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		<if test="isPaperBook != null">
			AND
			is_paper_book = #{isPaperBook}
		</if>
		<if test="isGroupBook!=null">
			AND
			is_group_book = #{isGroupBook}
		</if>
	</select>

	<select id="getAuthUserCountByMonth" resultType="bookAuthCodeUserVO" parameterType="map">
		SELECT
			COUNT(1) totalCount,
			IFNULL(SUM(is_auth_code),0) payUserCount
		FROM
			BOOK_AUTH_USER
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		<if test="monthDate!=null">
		AND MONTHS = #{monthDate}
		</if>
	</select>


	<select id="getIsHaveAuth" resultType="Boolean" parameterType="map">
		SELECT
			COUNT(1)
		FROM
			BOOK_AUTH_USER
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
			WECHAT_USER_ID = #{wechatUserId}
		<if test="isPaperBook != null">
			AND
			is_paper_book = #{isPaperBook}
		</if>
		<if test="isGroupBook!=null">
			AND
			is_group_book = #{isGroupBook}
		</if>
	</select>


	<select id="listThirtyDay" resultType="thirtyDayCountVO" parameterType="map">
		SELECT
			COUNT(1) totalCount,
			IFNULL(SUM(is_auth_code),0) payUserCount,
			created_date  as date
		FROM
			BOOK_AUTH_USER
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND created_date  <![CDATA[ > ]]>  #{startDate} AND created_date  <![CDATA[ < ]]> #{endDate}
		<if test="province!=null">
			AND PROVINCE = #{province}
		</if>
		<if test="isPaperBook != null">
			AND
			is_paper_book = #{isPaperBook}
		</if>
		<if test="isGroupBook!=null">
			AND
			is_group_book = #{isGroupBook}
		</if>
		GROUP BY created_date
	</select>


	<select id="listTop10ByBook" resultType="provinceTop10VO" parameterType="map">
		SELECT
			COUNT(1) totalCount,
			IFNULL(SUM(is_auth_code),0) payUserCount,
            province
		FROM
			BOOK_AUTH_USER
		WHERE
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		<if test="months!=null">
		AND months in
			<foreach collection="months" item="item" open="(" separator="," close=")">
			#{item}
			</foreach>
		</if>
		<if test="startDate!=null">
		AND created_date  <![CDATA[ > ]]>  #{startDate} AND created_date  <![CDATA[ < ]]> #{endDate}
		</if>
		GROUP BY province order by totalCount desc limit 10
	</select>

	<select id="getAuthUserTotalCount" resultType="bookAuthCodeUserVO" parameterType="map" >
        SELECT
            COUNT(1) totalCount,
            IFNULL(SUM(is_auth_code),0) payUserCount
        FROM
            BOOK_AUTH_USER WHERE 1 = 1
        <if test="adviserIds != null">
			 AND  ADVISER_ID in
			<foreach collection="list" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
		</if>
		<if test="removeAdviserIds != null">
			AND  ADVISER_ID not in
			<foreach collection="removeAdviserIds" item="item" open="(" separator="," close=")">
				${item}
			</foreach>
		</if>
    </select>


	<select id="listSixMonth" resultType="sixMonthCountVO" parameterType="map">
		SELECT
		COUNT(1) totalCount,
		IFNULL(SUM(is_auth_code),0) payUserCount,
		months as date
		FROM
		BOOK_AUTH_USER
		WHERE
		BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
		CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
		ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND months in
		<foreach collection="months" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		<if test="province!=null">
			AND PROVINCE = #{province}
		</if>
		<if test="isPaperBook != null">
			AND
			is_paper_book = #{isPaperBook}
		</if>
		<if test="isGroupBook!=null">
			AND
			is_group_book = #{isGroupBook}
		</if>
		GROUP BY months
	</select>

	<select id="checkUserIsHaveAuth" resultType="Boolean" parameterType="map">
		SELECT
			COUNT(1)
		FROM
			BOOK_AUTH_USER
		WHERE
			is_group_book = 1
		AND
			BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND
			CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND
			ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND
			WECHAT_USER_ID IN
		<foreach collection="wechatUserIds" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="haveUsedAuthCode" parameterType="list" resultType="com.pcloud.book.copyright.dto.HaveUsedAuthCode">
		SELECT book_id bookId, count(book_id) haveUsedAuthCode from book_auth_user
		WHERE book_id in
		<foreach collection="list" open="(" item="item" close=")" separator=",">
			${item}
		</foreach>
		GROUP BY book_id
	</select>

	<select id="getAuthUser" resultType="com.pcloud.book.copyright.entity.BookAuthUser" parameterType="map">
		SELECT
		id, is_auth_code isAuthCode
		FROM
		BOOK_AUTH_USER
		WHERE BOOK_ID = #{bookId, jdbcType=BIGINT}
		AND CHANNEL_ID = #{channelId, jdbcType=BIGINT}
		AND ADVISER_ID = #{adviserId, jdbcType=BIGINT}
		AND WECHAT_USER_ID = #{wechatUserId}
		<if test="isPaperBook != null">
			AND
			is_paper_book = #{isPaperBook}
		</if>
		<if test="isGroupBook!=null">
			AND
			is_group_book = #{isGroupBook}
		</if>
		LIMIT 1
	</select>

	<select id="getByAuthCodeIds" resultType="com.pcloud.book.copyright.dto.BookAuthUserDTO">
		SELECT
		id, is_auth_code isAuthCode, created_time createdTime, wechat_user_id wechatUserId, book_auth_code_id bookAuthCodeId
		FROM
		BOOK_AUTH_USER
		WHERE is_auth_code = 0
		AND book_auth_code_id IN
		<foreach collection="list" item="item" open="(" close=")" separator=",">
			#{item}
		</foreach>
	</select>

</mapper>
