<?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.testor.ddd.safetyControl.infrastructure.repository.riskMap.dao.TSafeRiskMapDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="com.testor.ddd.safetyControl.infrastructure.repository.riskMap.entity.TSafeRiskMapDO">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="name" property="name"/>
        <id column="level" property="level"/>
        <id column="space_id" property="spaceId"/>
        <id column="image_id" property="imageId"/>
        <id column="region_org_id" property="regionOrgId"/>
        <id column="basic_org_id" property="basicOrgId"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.org_id orgId, rs.name, rs.level, rs.space_id spaceId,rs.image_id imageId,
        rs.status, rs.remarks, rs.create_by createBy, rs.create_date createDate,
        rs.region_org_id regionOrgId, rs.basic_org_id basicOrgId,
        rs.update_by as updateBy, rs.update_date updateDate
    </sql>

    <sql id="common_where_if">

        <if test="query.id!=null and query.id!=''">
            and rs.id=#{query.id}
        </if>
        <if test="query.name!='' and query.name!=null">
            and rs.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.level!='' and query.level!=null">
            and rs.level=#{query.level}
        </if>
        <if test="query.spaceId!='' and query.spaceId!=null">
            and rs.space_id=#{query.spaceId}
        </if>
        <if test="query.imageId!='' and query.imageId!=null">
            and rs.image_id=#{query.imageId}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskMap.TSafeRiskMapVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskMap.TSafeRiskMapDTOParam">
        select * from ( select so.org_name as orgName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,
        <include refid="select_base_column" />
        from t_safe_risk_map rs
        left join t_sys_org so on so.org_id = rs.org_id
        left join t_sys_org rso on rso.org_id = rs.region_org_id
        left join t_sys_org bso on bso.org_id = rs.basic_org_id
        <where>
            rs.status = '0'
            <include refid="common_where_if"></include>
        </where>
        <if test="query.orderBy!='' and query.orderBy!=null and query.orderType!='' and query.orderType!=null" >
            order by ${query.orderBy} ${query.orderType}
        </if>) mm where mm.orgId in  (select org.org_id from t_sys_org org where org.parent_ids like '%,${query.orgId}%' or org.org_id = '${query.orgId}')



    </select>
</mapper>