<?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.inspect.metadata.area.mapper.BaseDataAreaMapper">
	<resultMap type="com.inspect.metadata.area.domain.BaseDataArea" id="BaseDataAreaResult">
		<id     property="areaId"     column="area_id"     />
		<result property="parentId"   column="parent_id"   />
		<result property="relationParentId"   column="relation_parent_id"   />
		<result property="relationAncestors"   column="relation_ancestors"   />
		<result property="ancestors"  column="ancestors"   />
		<result property="areaName"   column="area_name"   />
		<result property="areaType"   column="area_type"   />
		<result property="areaAddr"   column="area_addr"   />
		<result property="remark"   column="remark"   />
		<result property="areaCode"   column="area_code"   />
		<result property="orderNum"   column="order_num"   />
		<result property="deviceTypeCode"   column="device_type_code"   />
		<result property="status"     column="status"      />
		<result property="delFlag"    column="del_flag"    />
		<result property="parentName" column="parent_name" />
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
		<result property="oldAreaId" column="old_area_id" />
		<result property="oldParentId" column="old_parent_id" />
	</resultMap>
	
	<sql id="selectAreaVo">
        select d.area_id, d.parent_id, d.ancestors, d.area_name, d.area_type, d.order_num, d.status, d.del_flag, d.create_by, d.create_time,d.device_type_code,d.area_addr,d.remark,d.area_code
			,d.old_area_id, d.old_parent_id
		from basedata_area d
    </sql>
    
	<select id="selectAreaList" parameterType="com.inspect.metadata.area.domain.BaseDataArea" resultMap="BaseDataAreaResult">
        <include refid="selectAreaVo"/>
        where d.del_flag = '0' and d.parent_id is not null
		<if test="areaId != null and areaId != 0">
			AND area_id = #{areaId}
		</if>
        <if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>
		<if test="areaType != null and areaType != ''">
			AND area_type = #{areaType}
		</if>
		<if test="areaName != null and areaName != ''">
			AND area_name like concat('%', #{areaName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="areaCode != null and areaCode != ''">
			AND area_code = #{areaCode}
		</if>
		<if test="oldAreaId != null and oldAreaId != ''">
			AND old_area_id = #{oldAreaId}
		</if>
		<if test="oldParentId != null and oldParentId != ''">
			AND old_parent_id = #{oldParentId}
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		<!-- 区域数组查询 -->
		<if test="params != null">
			<if test="params.areaIdList != null">
				and area_id in
				<foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
		</if>
		order by d.parent_id, d.order_num
    </select>


	<select id="selectAreaRelationList" parameterType="com.inspect.metadata.area.domain.BaseDataArea" resultMap="BaseDataAreaResult">
		select d.area_id, d.parent_id, d.relation_parent_id, d.ancestors, d.relation_ancestors, d.area_name, d.area_type, d.order_num, d.status, d.del_flag, d.create_by, d.create_time,d.device_type_code,d.area_addr,d.remark,d.area_code
		,d.old_area_id, d.old_parent_id
		from basedata_area d
		where d.del_flag = '0' and d.relation_parent_id is not null
		<if test="areaId != null and areaId != 0">
			AND area_id = #{areaId}
		</if>
		<if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>
		<if test="areaType != null and areaType != ''">
			AND area_type = #{areaType}
		</if>
		<if test="areaName != null and areaName != ''">
			AND area_name like concat('%', #{areaName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="areaCode != null and areaCode != ''">
			AND area_code = #{areaCode}
		</if>
		<if test="oldAreaId != null and oldAreaId != ''">
			AND old_area_id = #{oldAreaId}
		</if>
		<if test="oldParentId != null and oldParentId != ''">
			AND old_parent_id = #{oldParentId}
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		<!-- 区域数组查询 -->
		<if test="params != null">
			<if test="params.areaIdList != null">
				and area_id in
				<foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
					#{item}
				</foreach>
			</if>
		</if>
		order by d.relation_parent_id, d.order_num
	</select>
    
    <select id="selectAreaListByRoleId" resultType="java.lang.Long">
		select d.area_id
		from basedata_area d
            left join sys_role_dept rd on d.area_id = rd.area_id
        where rd.role_id = #{roleId}
            <if test="areaCheckStrictly">
              and d.area_id not in (select d.parent_id from basedata_area d inner join sys_role_dept rd on d.area_id = rd.area_id and rd.role_id = #{roleId})
            </if>
		order by d.parent_id, d.order_num
	</select>
    
    <select id="selectAreaById" parameterType="Long" resultMap="BaseDataAreaResult">
		<include refid="selectAreaVo"/>
		where area_id = #{areaId}
	</select>

	<select id="selectAreaRelationById" parameterType="Long" resultMap="BaseDataAreaResult">
		select d.area_id, d.parent_id, d.relation_parent_id, d.ancestors, d.relation_ancestors, d.area_name, d.area_type, d.order_num, d.status, d.del_flag, d.create_by, d.create_time,d.device_type_code,d.area_addr,d.remark,d.area_code
			 ,d.old_area_id, d.old_parent_id
		from basedata_area d
		where area_id = #{areaId}
	</select>

	<update id="updateAreaRelationById">
		update
			basedata_area
		set
			relation_parent_id = #{toAreaId} , relation_ancestors = concat(#{ancestors}, ',', #{toAreaId})
		where area_id = #{areaId}
	</update>

    <select id="checkAreaExistBook" parameterType="Long" resultType="int">
		select count(1) from basedata_eqpbook where area_id = #{areaId}
	</select>
	
	<select id="hasChildByAreaId" parameterType="Long" resultType="int">
		select count(1) from basedata_area
		where del_flag = '0' and parent_id = #{areaId} limit 1
	</select>
	
	<select id="selectChildrenAreaById" parameterType="Long" resultMap="BaseDataAreaResult">
		select * from basedata_area where find_in_set(#{areaId}, ancestors)
	</select>
	
	<select id="selectNormalChildrenAreaById" parameterType="Long" resultType="int">
		select count(*) from basedata_area where status = 0 and del_flag = '0' and find_in_set(#{areaId}, ancestors)
	</select>
	
	<select id="checkAreaNameUnique" resultMap="BaseDataAreaResult">
	    <include refid="selectAreaVo"/>
		where area_name=#{areaName} and parent_id = #{parentId} limit 1
	</select>
    
    <insert id="insertArea" parameterType="com.inspect.metadata.area.domain.BaseDataArea" useGeneratedKeys="true" keyProperty="areaId">
 		insert into basedata_area(
 			<if test="areaId != null and areaId != 0">area_id,</if>
 			<if test="parentId != null and parentId != 0">parent_id,</if>
		    <if test="parentId != null and parentId != 0">relation_parent_id,</if>
 			<if test="areaName != null and areaName != ''">area_name,</if>
		    <if test="areaType != null and areaType != ''">area_type,</if>
 			<if test="ancestors != null and ancestors != ''">ancestors,</if>
		    <if test="ancestors != null and ancestors != ''">relation_ancestors,</if>
 			<if test="orderNum != null and orderNum != ''">order_num,</if>
 			<if test="status != null">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
			<if test="areaCode != null and areaCode != ''">area_code,</if>
			<if test="areaAddr != null and areaAddr != ''">area_addr,</if>
			<if test="remark != null and remark != ''">remark,</if>
		<if test="oldAreaId != null and oldAreaId != ''">old_area_id,</if>
		<if test="oldParentId != null and oldParentId != ''">old_parent_id,</if>
			<if test="deviceTypeCode != null and deviceTypeCode != ''">device_type_code,</if>
 			create_time
 		)values(
 			<if test="areaId != null and areaId != 0">#{areaId},</if>
 			<if test="parentId != null and parentId != 0">#{parentId},</if>
		    <if test="parentId != null and parentId != 0">#{parentId},</if>
 			<if test="areaName != null and areaName != ''">#{areaName},</if>
		    <if test="areaType != null and areaName != ''">#{areaType},</if>
 			<if test="ancestors != null and ancestors != ''">#{ancestors},</if>
		    <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
 			<if test="orderNum != null and orderNum != ''">#{orderNum},</if>
 			<if test="status != null">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
			<if test="areaCode != null and areaCode != ''">#{areaCode},</if>
			<if test="areaAddr != null and areaAddr != ''">#{areaAddr},</if>
			<if test="remark != null and remark != ''">#{remark},</if>
		<if test="oldAreaId != null and oldAreaId != ''">#{oldAreaId},</if>
		<if test="oldParentId != null and oldParentId != ''">#{oldParentId},</if>
			<if test="deviceTypeCode != null and deviceTypeCode != ''">#{deviceTypeCode},</if>
 			sysdate()
 		)
	</insert>
	
	<update id="updateArea" parameterType="com.inspect.metadata.area.domain.BaseDataArea">
 		update basedata_area
 		<set>
 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
			<if test="relationParentId != null and relationParentId != 0">relation_parent_id = #{relationParentId},</if>
 			<if test="areaName != null and areaName != ''">area_name = #{areaName},</if>
			<if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
 			<if test="areaType != null and areaType != ''">area_type = #{areaType},</if>
			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
			<if test="relationAncestors != null and relationAncestors != ''">relation_ancestors = #{relationAncestors},</if>
 			<if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			update_time = sysdate()
 		</set>
 		where area_id = #{areaId}
	</update>

	<update id="updateAreaChildren" parameterType="java.util.List">
		update basedata_area set ancestors =
		<foreach collection="areas" item="item" index="index"
				 separator=" " open="case area_id" close="end">
			when #{item.areaId} then #{item.ancestors}
		</foreach>
		where area_id in
		<foreach collection="areas" item="item" index="index"
				 separator="," open="(" close=")">
			#{item.areaId}
		</foreach>
	</update>

	<update id="updateAreaStatusNormal" parameterType="Long">
 	    update basedata_area set status = '0' where area_id in 
 	    <foreach collection="array" item="areaId" open="(" separator="," close=")">
        	#{areaId}
        </foreach>
	</update>
	
	<delete id="deleteAreaById" parameterType="Long">
		update basedata_area set del_flag = '2' where area_id = #{areaId}
	</delete>

    <delete id="deleteAreaBySendCode" parameterType="String">
		delete from basedata_area where area_code = #{sendCode} or old_area_id like concat(#{sendCode}, '%')
	</delete>

    <select id="selectBasedataAreaByPatrolPointId" parameterType="String" resultMap="BaseDataAreaResult">
		SELECT
			c.*
		FROM
			basedata_patrolpoint a,
			basedata_device b,
			basedata_area c
		WHERE
			1=1
		  AND a.main_device_id = b.device_id
		  AND b.area_id = c.area_id
		  AND a.patrol_point_id = #{patrolpointId}
	</select>

	<select id="selectAreaIdByName" parameterType="String" resultType="String">
		SELECT area_id FROM basedata_area WHERE area_name = #{areaName}
		  AND parent_id = #{parentId}
	</select>
</mapper> 