<?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.goafanti.common.dao.AdminLocationMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.AdminLocation" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="admin_id" property="adminId" jdbcType="VARCHAR" />
    <result column="province" property="province" jdbcType="INTEGER" />
    <result column="city" property="city" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, admin_id, province, city
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from admin_location
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from admin_location
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.AdminLocation" >
    insert into admin_location (id, admin_id, province, 
      city)
    values (#{id,jdbcType=VARCHAR}, #{adminId,jdbcType=VARCHAR}, #{province,jdbcType=INTEGER}, 
      #{city,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.AdminLocation" >
    insert into admin_location
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="adminId != null" >
        admin_id,
      </if>
      <if test="province != null" >
        province,
      </if>
      <if test="city != null" >
        city,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="adminId != null" >
        #{adminId,jdbcType=VARCHAR},
      </if>
      <if test="province != null" >
        #{province,jdbcType=INTEGER},
      </if>
      <if test="city != null" >
        #{city,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.AdminLocation" >
    update admin_location
    <set >
      <if test="adminId != null" >
        admin_id = #{adminId,jdbcType=VARCHAR},
      </if>
      <if test="province != null" >
        province = #{province,jdbcType=INTEGER},
      </if>
      <if test="city != null" >
        city = #{city,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.AdminLocation" >
    update admin_location
    set admin_id = #{adminId,jdbcType=VARCHAR},
      province = #{province,jdbcType=INTEGER},
      city = #{city,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <insert id="insertBatch" parameterType="com.goafanti.common.model.AdminLocation">
    insert into admin_location 
    	(id, admin_id, 
    	province, city)
    values 
    <foreach item="item" index="index" collection="list" separator=",">
      (
      	#{item.id,jdbcType=VARCHAR}, #{item.adminId,jdbcType=VARCHAR}, 
      	#{item.province,jdbcType=INTEGER}, #{item.city,jdbcType=INTEGER}
      )
    </foreach>
  </insert>
  
  <select id="selectProvinceWhereCityIsNullByAdminId" parameterType="java.lang.String" resultType = "java.lang.Integer">
  	select 
  		province
  	from admin_location 
  	where admin_id = #{adminId, jdbcType=VARCHAR}
  	and city is null
  </select>
  
   <select id="selectCityByAdminId" parameterType="java.lang.String" resultType = "java.lang.Integer">
  	select 
  		city
  	from admin_location 
  	where admin_id = #{adminId, jdbcType=VARCHAR}
  </select>
  
  <select id="selectByAdminId" parameterType="java.lang.String" resultType="com.goafanti.admin.bo.AdminLocationBo">
  	SELECT
		province,
		GROUP_CONCAT(city) as city
	FROM
	admin_location
	WHERE
		admin_id = #{adminId,jdbcType=VARCHAR} 
	GROUP BY province
  </select>
  
  <delete id="deleteByAdminId" parameterType="java.lang.String">
  	delete from admin_location
  	where admin_id = #{adminId,jdbcType=VARCHAR}
  </delete>
</mapper>