<?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.AdvertisingBrandDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingBrand" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="brand_name" property="brandName" jdbcType="VARCHAR" />
    <result column="login_name" property="loginName" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="is_delete" property="isDelete" jdbcType="BIT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, brand_name, login_name, password, is_delete, create_time, create_user, update_time,
    update_user
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingBrand" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_brand (brand_name, login_name,
      password, is_delete, create_time, 
      create_user
      )
    values (#{brandName,jdbcType=VARCHAR}, #{loginName,jdbcType=VARCHAR},
      #{password,jdbcType=VARCHAR}, #{isDelete,jdbcType=BIT}, NOW(),
      #{createUser,jdbcType=BIGINT}
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingBrand" >
    update advertising_brand
    <set >
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
        update_time = NOW(),
      <if test="updateUser != null" >
        update_user = #{updateUser,jdbcType=BIGINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getByGroupQrcodeId" parameterType="long" resultType="com.pcloud.book.advertising.dto.AdvertisingBrandDTO">
    SELECT
        b.id id,
        b.brand_name brandName
    FROM
        advertising_group_tag t
    LEFT JOIN advertising_brand b ON t.brand_id = b.id
    WHERE
        t.group_qrcode_id = #{groupQrcodeId}
        AND b.is_delete = 0
    ORDER BY b.id ASC
  </select>

  <select id="getAllBrand" resultType="com.pcloud.book.advertising.dto.AdvertisingBrandDTO">
    SELECT
        id id,
        brand_name brandName
    FROM
        advertising_brand
    WHERE
        is_delete = 0
    ORDER BY id ASC
  </select>

  <update id="deleteBrand" parameterType="long">
    update advertising_brand
    set is_delete = 1
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getBrandById" parameterType="long" resultType="com.pcloud.book.advertising.dto.AdvertisingBrandDTO">
     SELECT
        brand_name brandName,
        login_name loginName,
        password password
    FROM
        advertising_brand
    WHERE
        is_delete = 0
        AND id = #{id,jdbcType=BIGINT}
  </select>

  <select id="checkBrandNameRepeat" parameterType="string" resultType="boolean">
    SELECT
	EXISTS (
		SELECT
			brand_name
		FROM
			advertising_brand
		WHERE
			brand_name = #{brandName}
		AND is_delete = 0
	)
  </select>

  <select id="checkLoginNameRepeat" resultType="boolean" parameterType="string">
    SELECT
	EXISTS (
		SELECT
			brand_name
		FROM
			advertising_brand
		WHERE
			login_name = #{loginName}
		AND is_delete = 0
	)
  </select>

  <select id="listPageBrand" parameterType="map" resultType="com.pcloud.book.advertising.dto.AdvertisingBrandDTO">
     SELECT
        id id,
        brand_name brandName,
        login_name loginName,
        create_time createTime
    FROM
        advertising_brand
    WHERE
        is_delete = 0
    <if test="name != null">
      AND login_name LIKE concat('%', #{name}, '%')
    </if>
    ORDER BY
        create_time DESC
  </select>

  <select id="getLoginInfo" parameterType="map" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM
    advertising_brand
    WHERE
    login_name = #{loginName}
    AND is_delete = 0
    LIMIT 1
  </select>

    <!--根据id集合获取列表-->
    <select id="getMapByIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from
        advertising_brand
        where
        is_delete = 0
        and id in
        <foreach collection="brandIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>
</mapper>