<?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.BmRegisterItemDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingBmRegisterItem" >
    <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="register_id" property="registerId" jdbcType="BIGINT" />
    <result column="option_id" property="optionId" jdbcType="BIGINT" />
    <result column="option_item_id" property="optionItemId" jdbcType="BIGINT" />
    <result column="option_value" property="optionValue" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="cpa_id" property="cpaId" jdbcType="BIGINT" />
  </resultMap>

  <resultMap id="registerItemDtoMap" type="BmRegisterItemDTO" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="register_id" property="registerId" jdbcType="BIGINT" />
    <result column="option_id" property="optionId" jdbcType="BIGINT" />
    <result column="option_item_id" property="optionItemId" jdbcType="BIGINT" />
    <result column="option_value" property="optionValue" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, user_id, ad_id, register_id, option_id, option_item_id, option_value, create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingBmRegisterItem" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_bm_register_item (user_id, ad_id,
      register_id, option_id, option_item_id, 
      option_value, create_time,cpa_id)
    values (#{userId,jdbcType=BIGINT}, #{adId,jdbcType=BIGINT},
      #{registerId,jdbcType=BIGINT}, #{optionId,jdbcType=BIGINT}, #{optionItemId,jdbcType=BIGINT}, 
      #{optionValue,jdbcType=VARCHAR}, NOW(),#{cpaId})
  </insert>

  <select id="getRegisterItemByUser" resultMap="registerItemDtoMap" parameterType="java.lang.Long" >
    SELECT
    register_id,
    option_id,
    option_item_id,
    option_value
    FROM
    advertising_bm_register_item a
    LEFT JOIN advertising_bm_option b ON a.option_id = b.ID
    WHERE
    a.register_id = #{id}
    ORDER BY
    b.seq ASC,
    b.id ASC
  </select>
  
  <select id="checkMobilePhone" resultType="Integer" parameterType="map">
    SELECT
	COUNT(1)
  FROM
      advertising_bm_register_item i
  LEFT JOIN advertising_bm_register r ON i.register_id = r.id
  LEFT JOIN advertising_bm_option o ON i.option_id = o.id
  WHERE
      i.ad_id = #{adId}
  AND r.from_type = #{fromType}
  AND r.from_id = #{fromId}
  AND o.item_title = '手机号'
  AND i.option_value = #{optionValue}
  </select>

  <select id="checkMobilePhone4Cpa" resultType="Integer" parameterType="map">
    SELECT
	COUNT(1)
  FROM
      advertising_bm_register_item i
  LEFT JOIN advertising_bm_register r ON i.register_id = r.id
  LEFT JOIN advertising_bm_option o ON i.option_id = o.id
  WHERE
      i.cpa_id = #{cpaId}
  AND r.from_type = #{fromType}
  AND r.from_id = #{fromId}
  AND o.item_title = '手机号'
  AND i.option_value = #{optionValue}
  </select>
  
  <select id="getRegisterOptionValue" resultType="string" parameterType="map">
    SELECT option_value FROM `advertising_bm_register_item`
    WHERE user_id = #{userId}
      AND register_id = #{registerId}
      AND option_id = #{optionId}
      LIMIT 1
  </select>

</mapper>