<?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.tmerclub.cloud.delivery.mapper.AreaMapper">
  <resultMap id="areaMap" type="com.tmerclub.cloud.delivery.model.Area">
    <id column="area_id" property="areaId" />
    <result column="area_name" property="areaName"/>
    <result column="parent_id" property="parentId"/>
    <result column="level" property="level"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `area_id`,`area_name`,`parent_id`,`level`,`create_time`,`update_time`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    SELECT
      `area_id`,
      `area_name`,
      `parent_id`,
      `level`
    FROM
      area
    <where>
      <if test="areaDTO.level != null">
        level = #{areaDTO.level}
      </if>
      <if test="areaDTO.parentId != null">
        AND parent_id = #{areaDTO.parentId}
      </if>
    </where>
    ORDER BY
      area_id ASC
  </select>
  <select id="getByAreaId" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    select `area_id`,`area_name`,`parent_id`,`level` from area where area_id = #{areaId}
  </select>
  <insert id="save">
    insert into area (`area_name`,`parent_id`,`level`)
    values (#{area.areaName},#{area.parentId},#{area.level});
  </insert>
  <update id="update">
    update area
    <set>
      <if test="area.areaName != null">
        `area_name` = #{area.areaName},
      </if>
      <if test="area.parentId != null">
        `parent_id` = #{area.parentId},
      </if>
      <if test="area.level != null">
        `level` = #{area.level},
      </if>
    </set>
    where area_id = #{area.areaId}
  </update>
  <delete id="deleteById">
    delete from area where area_id = #{areaId}
  </delete>

  <select id="countByAreaId" resultType="int">
    SELECT COUNT(*) FROM `area` WHERE parent_id = #{areaId}
  </select>

  <select id="listByPid" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    SELECT `area_id`,`area_name`,`parent_id`,`level` FROM `area` WHERE parent_id = #{pid}
  </select>

  <select id="getAreaListInfo" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    SELECT * FROM`area`
  </select>
  <select id="listAreaOfEnable" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    SELECT `area_id`,`area_name`,`parent_id`,`level` FROM`area` where level > 0
  </select>

  <select id="countByNameAndParentId" resultType="int">
    SELECT ifnull(count(*),0) FROM `area`
    where area_name = #{areaName} and `level` = #{level}
    <if test="parentId != null">
      and parent_id = #{parentId}
    </if>
    <if test="areaId != null">
      and area_id != #{areaId}
    </if>
  </select>

  <select id="listByAreaIdList" resultType="com.tmerclub.cloud.common.order.vo.AreaVO">
    SELECT * FROM `area` where area_id IN
    <foreach collection="areaIdList" item="areaId" open="(" close=")" separator=",">
      #{areaId}
    </foreach>
  </select>
</mapper>
