<?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.BmOptionDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingBmOption" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="ad_id" property="adId" jdbcType="BIGINT" />
    <result column="seq" property="seq" jdbcType="BIGINT" />
    <result column="item_title" property="itemTitle" jdbcType="VARCHAR" />
    <result column="item_desc" property="itemDesc" jdbcType="VARCHAR" />
    <result column="item_input_type" property="itemInputType" jdbcType="VARCHAR" />
    <result column="item_data_type" property="itemDataType" jdbcType="VARCHAR" />
    <result column="required" property="required" jdbcType="BIT" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="cpa_id" property="cpaId" jdbcType="BIGINT" />
    <collection property="optionItem" column="id"
                select="com.pcloud.book.advertising.dao.impl.BmOptionItemDaoImpl.getByOptionId" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, ad_id, seq, item_title, item_desc, item_input_type, item_data_type, required,
    create_user, create_time, update_user, update_time, cpa_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingBmOption" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_bm_option (ad_id, seq, item_title,
      item_desc, item_input_type, item_data_type, 
      required, create_user, create_time, cpa_id
      )
    values (#{adId,jdbcType=BIGINT}, #{seq,jdbcType=BIGINT}, #{itemTitle,jdbcType=VARCHAR},
      #{itemDesc,jdbcType=VARCHAR}, #{itemInputType,jdbcType=VARCHAR}, #{itemDataType,jdbcType=VARCHAR}, 
      #{required,jdbcType=BIT}, #{createUser,jdbcType=BIGINT}, NOW(), #{cpaId}
      )
  </insert>

  <!-- 根据广告ID获取选项表单 -->
  <select id="getByCPAId" resultMap="BaseResultMap" parameterType="map" >
    select
    <include refid="Base_Column_List" />
    from advertising_bm_option
    where cpa_id = #{cpaId,jdbcType=BIGINT}
    order by seq asc, id asc
  </select>

  <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingBmOption" >
    update advertising_bm_option
    <set >
      <if test="seq != null" >
        seq = #{seq,jdbcType=BIGINT},
      </if>
      <if test="itemTitle != null" >
        item_title = #{itemTitle,jdbcType=VARCHAR},
      </if>
      <if test="itemDesc != null" >
        item_desc = #{itemDesc,jdbcType=VARCHAR},
      </if>
      <if test="itemInputType != null" >
        item_input_type = #{itemInputType,jdbcType=VARCHAR},
      </if>
      <if test="itemDataType != null" >
        item_data_type = #{itemDataType,jdbcType=VARCHAR},
      </if>
      <if test="required != null" >
        required = #{required,jdbcType=BIT},
      </if>
        update_user = #{updateUser,jdbcType=BIGINT},
        update_time = NOW(),
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <delete id="deleteByIds" parameterType="list">
    DELETE FROM advertising_bm_option
    WHERE 1=1
    AND id IN
    <foreach collection = "list" index="index" item = "item" open = "("  separator= "," close = ")">
      #{item}
    </foreach>
  </delete>

  <select id="getOptionIdsByCPAId" parameterType="Long" resultType="Long">
    SELECT id
    FROM advertising_bm_option
    WHERE cpa_id = #{cpaId,jdbcType=BIGINT}
  </select>

  <select id="getMobilePhoneOptionIdByCPA" parameterType="Long" resultType="Long">
    SELECT
        id
    FROM
        advertising_bm_option
    WHERE
        cpa_id = #{cpaId,jdbcType=BIGINT}
    AND item_title = '手机号'
    AND item_input_type = 'input'
    AND item_data_type = 'mobile'
    AND required = 1
    LIMIT 1
  </select>

  <update id="updateCPAId" parameterType="map">
    UPDATE advertising_bm_option
    SET cpa_id = #{cpaId,jdbcType=BIGINT}
    WHERE
        ad_id = #{adId,jdbcType=BIGINT}
  </update>

</mapper>