<?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.AdvertisingGroupTagDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingGroupTag" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="tag_book_id" property="tagBookId" jdbcType="BIGINT" />
    <result column="group_qrcode_id" property="groupQrcodeId" jdbcType="BIGINT" />
    <result column="brand_id" property="brandId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, tag_book_id, group_qrcode_id, brand_id, create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingGroupTag" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_group_tag (tag_book_id, group_qrcode_id,
      brand_id, create_time)
    values (#{tagBookId,jdbcType=BIGINT}, #{groupQrcodeId,jdbcType=BIGINT},
      #{brandId,jdbcType=BIGINT},NOW())
  </insert>

  <delete id="deleteByBrandId" parameterType="long">
    DELETE
    FROM
        advertising_group_tag
    WHERE
        brand_id = #{brandId}
  </delete>

  <insert id="batchInsert" parameterType="com.pcloud.book.advertising.entity.AdvertisingGroupTag" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_group_tag (
    tag_book_id,
    group_qrcode_id,
    brand_id,
    create_time
    ) values
    <foreach collection="list" item="item" index="index"
             separator=",">
      (#{item.tagBookId,jdbcType=BIGINT},
      #{item.groupQrcodeId,jdbcType=BIGINT},
      #{item.brandId,jdbcType=BIGINT},
      NOW())
    </foreach>
  </insert>

  <select id="getBookIdsByBrandId" parameterType="long" resultType="long">
    SELECT DISTINCT
	tag_book_id
    FROM
        advertising_group_tag
    WHERE
        brand_id = #{brandId}
    AND tag_book_id IS NOT NULL
    AND group_qrcode_id = 0
  </select>

  <delete id="deleteByGroupQrcodeId" parameterType="long">
    DELETE
    FROM
        advertising_group_tag
    WHERE group_qrcode_id = #{groupQrcodeId}
  </delete>

  <select id="getGroupQrcodeIdsByBrandId" parameterType="long" resultType="long">
    SELECT DISTINCT
	group_qrcode_id
    FROM
        advertising_group_tag
    WHERE
        brand_id = #{brandId}
    AND group_qrcode_id != 0
  </select>

  <delete id="deleteByBook" parameterType="map" >
    DELETE
    FROM
    advertising_group_tag
    WHERE
    brand_id = #{brandId}
    AND tag_book_id in
      <foreach collection="bookIds" index="index" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
  </delete>

  <select id="getTagWxGroupIdsByBrandId" parameterType="long" resultType="string">
    SELECT DISTINCT
	q.weixin_group_id
    FROM
        book_group_qrcode q
    LEFT JOIN advertising_group_tag t ON q.id = t.group_qrcode_id
    WHERE
        t.brand_id = #{brandId}
    AND q.weixin_group_id IS NOT NULL
  </select>

  <select id="getBrandIdsByBookId" parameterType="long" resultType="long">
    SELECT DISTINCT
    brand_id
    FROM
    advertising_group_tag
    WHERE
    tag_book_id = #{bookId}
    AND group_qrcode_id = 0
  </select>

  <select id="getBrandIdsByWxGroupId" parameterType="string" resultType="long">
    SELECT DISTINCT
	t.brand_id
    FROM
        advertising_group_tag t
    LEFT JOIN book_group_qrcode q ON t.group_qrcode_id = q.id
    WHERE
        q.weixin_group_id = #{wxGroupId}
  </select>

  <update id="updateId" parameterType="map">
    UPDATE advertising_group_tag
    SET brand_id = #{brandId}
    WHERE
        brand_id = #{masterId}
  </update>

</mapper>