<?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.advertising.dao.impl.BmRegisterDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingBmRegister" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="user_id" property="userId" jdbcType="BIGINT" />
    <result column="ad_id" property="adId" jdbcType="BIGINT" />
    <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT" />
    <result column="scene_id" property="sceneId" jdbcType="BIGINT"/>
    <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
    <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
    <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
    <result column="official_account_id" property="officialAccountId" jdbcType="BIGINT"/>
    <result column="from_type" property="fromType" jdbcType="VARCHAR"/>
    <result column="from_id" property="fromId" jdbcType="BIGINT"/>
    <result column="book_id" property="bookId" jdbcType="BIGINT"/>
    <result column="create_day" property="createDay" jdbcType="DATE"/>
    <result column="create_month" property="createMonth" jdbcType="VARCHAR"/>
  </resultMap>

  <!-- 微信端用户详细信息 -->
  <resultMap id="wechatDetailMap" type="BmRegisterDTO">
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="user_id" property="userId" jdbcType="BIGINT" />
    <result column="ad_id" property="adId" jdbcType="BIGINT" />
    <result column="nick_name" property="nickName" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT" />
    <collection property="registerItemDTOS" column="id"
                ofType="registerItemDtoMap"
                select="com.pcloud.book.advertising.dao.impl.BmRegisterItemDaoImpl.getRegisterItemByUser" />
  </resultMap>


  <sql id="Base_Column_List" >
    id, user_id, ad_id, nick_name, create_time, qrcode_id, scene_id, adviser_id, agent_id, channel_id, official_account_id, from_type,
        from_id,book_id,create_day, create_month
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingBmRegister" useGeneratedKeys="true" keyProperty="id">
      INSERT INTO advertising_bm_register (
      user_id,
      ad_id,
      nick_name,
      create_time,
      qrcode_id,
      scene_id,
      adviser_id,
      agent_id,
      channel_id,
      official_account_id,
      from_type,
      from_id,
      book_id,
      create_day,
      create_month
      )
      VALUES
          (
          #{userId,jdbcType=BIGINT},
          #{adId,jdbcType=BIGINT},
          #{nickName,jdbcType=VARCHAR},
           NOW(),
          #{qrcodeId},
          #{sceneId,jdbcType=BIGINT},
          #{adviserId,jdbcType=BIGINT},
          #{agentId,jdbcType=BIGINT},
          #{channelId,jdbcType=BIGINT},
          #{officialAccountId,jdbcType=BIGINT},
          #{fromType,jdbcType=VARCHAR},
          #{fromId,jdbcType=BIGINT},
          #{bookId,jdbcType=BIGINT},
          NOW(),
          DATE_FORMAT(NOW(), '%Y-%m')
      )
  </insert>

    <!-- 微信端获取个人报名信息 -->
    <select id="getDetailByUser" resultMap="wechatDetailMap" parameterType="map" >
      select
      <include refid="Base_Column_List" />
      from advertising_bm_register
      where ad_id = #{adId,jdbcType=BIGINT}
      and user_id = #{wechatUserId}
      <if test="qrcodeId != null">
        AND qrcode_id = #{qrcodeId}
      </if>
      ORDER BY create_time DESC
    </select>
  
  <select id="getRegisterCountByAdId" parameterType="map" resultType="Long">
        SELECT
            COUNT(1)
        FROM
            advertising_bm_register
        WHERE
            ad_id = #{adId,jdbcType=BIGINT}
          <if test="bookId != null">
              AND book_id = #{bookId}
              AND adviser_id = #{adviserId}
              AND channel_id = #{channelId}
          </if>
          <if test="statisMonth != null">
              and create_month = #{statisMonth}
          </if>
  </select>

  <select id="getRegisterCountByAdIds" parameterType="list" resultType="Long">
     SELECT
        COUNT(1)
    FROM
        advertising_bm_register
    WHERE ad_id in
      <foreach collection="list" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
  </select>

  <select id="getRegisterCountByAdIdQrcodeId" parameterType="map" resultType="Long">
    SELECT
      COUNT(1)
    FROM
    advertising_bm_register
    WHERE
      from_id = #{qrcodeId}
      AND from_type = 'WECHAT_GROUP'
    <if test="adId != null">
      AND ad_id = #{adId,jdbcType=BIGINT}
    </if>
    <if test="statisMonth != null">
      AND create_month = #{statisMonth}
    </if>
  </select>

    <select id="getRegisterUserNumByAdIdQrcodeId" resultType="Long" parameterType="map">
        SELECT
        COUNT(DISTINCT user_id)
        FROM
        advertising_bm_register
        WHERE
        from_id = #{qrcodeId}
        AND from_type = 'WECHAT_GROUP'
        <if test="adId != null">
            AND ad_id = #{adId,jdbcType=BIGINT}
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
    </select>

    <select id="getRegisterInfoListByAdIdQrcodeId" parameterType="map" resultMap="wechatDetailMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM advertising_bm_register
        WHERE
            ad_id = #{adId}
        <if test="qrcodeId != null">
            AND from_id = #{qrcodeId}
            AND from_type = 'WECHAT_GROUP'
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getRegisterInfoListByAdId" parameterType="map" resultMap="wechatDetailMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM advertising_bm_register
        WHERE
            ad_id = #{adId}
        <if test="bookId != null">
            AND book_id = #{bookId}
            AND adviser_id = #{adviserId}
            AND channel_id = #{channelId}
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getRegisterCountByBookId" parameterType="map" resultType="Long">
        SELECT
        count(1)
        FROM
        advertising_bm_register
        WHERE
        book_id = #{bookId}
        AND adviser_id = #{adviserId}
        AND channel_id = #{channelId}
        AND from_type != 'WECHAT_GROUP'
        <if test="adId != null">
            AND ad_id = #{adId,jdbcType=BIGINT}
        </if>
        <if test="statisMonth != null">
            AND create_month = #{statisMonth}
        </if>
    </select>

</mapper>