<?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)
	    VALUES
       (#{bookId,jdbcType=BIGINT}, #{channelId,jdbcType=BIGINT}, #{adviserId,jdbcType=BIGINT},
		 #{wechatUserId}, #{months}, #{province}, #{city}, #{isAuthCode},NOW(), #{adviserId}, NOW())
    </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}
	</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}
	</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>
		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>
		GROUP BY months
	</select>

</mapper>