<?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="AreaMapper">

	<sql id="areaColumns">
		a.id,
		a.parent_id AS "parentId",
		a.parent_ids as "parentIds",
		a.code,
		case #{lang} when 'en' then a.name_en  else a.name end as name,
		<!-- a.name, -->
		a.name_en as nameEn,
		a.sort,
		a.type,
		a.remarks,
		a.create_by AS "createById",
		a.create_date,
		a.update_by AS "updateById",
		a.update_date,
		a.del_flag
	</sql>
	<select id="getAllArea" resultType="com.wechat.model.dbEntity.Area">
		select  
			<include refid="areaColumns"/>
		FROM
			sys_area a
		WHERE
			a.parent_id = #{parentId}
		AND a.type = #{type}
		order by a.code asc
	</select>
	
	<select id="getAllAreaByParentIdList" resultType="com.wechat.model.dbEntity.Area">
		select  
			<include refid="areaColumns"/>
		FROM
			sys_area a
		WHERE
		   a.type = #{type}
		<if test="parentId != null">
			AND a.parent_id in 
			<foreach collection="parentId" item="item" open="(" close=")" separator=",">
				 #{item}
			</foreach>
		</if>
		order by a.code asc
	</select>
	
	<select id="getall" resultType="com.wechat.model.dbEntity.Area">
		select id ,name from sys_area order by code asc
	</select>
	
	<update id="updateAreaEnName"  parameterType="com.wechat.model.dbEntity.Area">
		update sys_area set name_en =#{nameEn} where id=#{id} 
	</update>
	<update id="updateAreaEnNameByCode"  parameterType="com.wechat.model.dbEntity.Area">
		update sys_area set name_en =#{nameEn} where code=#{code}
	</update>
	
	<select id="getNamesByCodes" resultType="java.lang.String">
		select 
			case #{lang} when 'en' then name_en  else name end as name
		from sys_area 
		where 
		code in  
		<if test="codeList != null">
			<foreach collection="codeList" item="item" open="(" close=")" separator=",">
				 #{item}
			</foreach>
		</if>
		order by type asc
	</select>
	
</mapper>