<?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.riskPoint.dao.TSafeRiskPointDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.ddd.safetyControl.infrastructure.repository.riskPoint.entity.TSafeRiskPointDO">
        <id column="id" property="id"/>
        <id column="org_all_path" property="orgAllPath"/>
        <id column="org_id" property="orgId"/>
        <id column="region_org_id" property="regionOrgId"/>
        <id column="department_id" property="departmentId"/>
        <id column="name" property="name"/>
        <id column="code" property="code"/>
        <id column="type_id" property="typeId"/>
        <id column="relation_type" property="relationType"/>
        <id column="relation_equipment_id" property="relationEquipmentId"/>
        <id column="relation_space_parent_id" property="relationSpaceParentId"/>
        <id column="relation_space_id" property="relationSpaceId"/>
        <id column="space_location" property="spaceLocation"/>
        <id column="Inherent_level_id" property="inherentLevelId"/>
        <id column="real_level_id" property="realLevelId"/>
        <id column="Control_level_id" property="controlLevelId"/>
        <id column="relation_space_level_ids" property="relationSpaceLevelIds"/>
        <id column="relation_equipment_level_ids" property="relationEquipmentLevelIds"/>
        <id column="basic_org_id" property="basicOrgId"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.org_all_path orgAllPath, rs.org_id orgId, rs.region_org_id regionOrgId, rs.basic_org_id basicOrgId,
        rs.department_id departmentId, rs.name, rs.code,
       rs.type_id typeId, rs.relation_type relationType, rs.relation_equipment_id relationEquipmentId, rs.relation_space_id relationSpaceId,
       rs.inherent_level_id inherentLevelId, rs.real_level_id realLevelId,
        rs.control_level_id controlLevelId,rs.relation_space_parent_id relationSpaceParentId,
        rs.relation_space_level_ids relationSpaceLevelIds, rs.relation_equipment_level_ids relationEquipmentLevelIds,
        rs.status, rs.remarks, rs.create_by createBy, rs.create_date createDate,
        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.orgId!=null and query.orgId!=''">-->
<!--            and rs.org_id=#{query.orgId}-->
<!--        </if>-->
        <if test="query.departmentId!='' and query.departmentId!=null">
            and rs.department_id=#{query.departmentId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and rs.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.code!='' and query.code!=null">
            and rs.code=#{query.code}
        </if>
        <if test="query.typeId!='' and query.typeId!=null">
            and rs.type_id=#{query.typeId}
        </if>
        <if test="query.inherentLevelId!='' and query.inherentLevelId!=null">
            and rs.inherent_level_id=#{query.inherentLevelId}
        </if>
        <if test="query.realLevelId!='' and query.realLevelId!=null">
            and rs.real_level_id=#{query.realLevelId}
        </if>
        <if test="query.controlLevelId!='' and query.controlLevelId!=null">
            and rs.control_level_id=#{query.controlLevelId}
        </if>
        <if test="query.relationEquipmentId!='' and query.relationEquipmentId!=null">
            and rs.relation_equipment_id=#{query.relationEquipmentId}
        </if>
        <if test="query.relationSpaceParentId!='' and query.relationSpaceParentId!=null">
            and rs.relation_space_parent_id=#{query.relationSpaceParentId}
        </if>
        <if test="query.relationSpaceId!='' and query.relationSpaceId!=null">
            and rs.relation_space_id=#{query.relationSpaceId}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskPoint.TSafeRiskPointVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskPoint.TSafeRiskPointDTOParam" databaseId="mysql">
        select so.org_name as orgName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,dpo.org_name as departmentName,
        CONCAT_WS('',ss.name, ssp.name, sst.name) as spaceLocation, se.name as relationEquipmentName,se.code relationSpaceCode,
        <include refid="select_base_column" />
        from t_safe_risk_point rs
        left join t_safe_equipment se on rs.relation_equipment_id = se.id
        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 dpo on dpo.org_id = rs.department_id
        left join t_sys_org bso on bso.org_id = rs.basic_org_id
        left join t_safe_space ss on rs.relation_space_id = ss.id
        left join t_safe_space ssp on rs.relation_space_parent_id = ssp.id
        left join t_safe_space_type sst on ssp.space_type_id = sst.id
        <where>
            rs.status = '0'
            <if test="query.orgId!=null and query.orgId!=''">
                and  (rs.org_id = #{query.orgId} or rs.org_all_path like concat('%',#{query.orgId},'%') or so.parent_ids like concat('%,',#{query.orgId},'%'))
            </if>
            <include refid="common_where_if"></include>
            <if test="query.spaceLocation!='' and query.spaceLocation!=null">
                and (ss.name like concat('%',#{query.spaceLocation},'%')
                or ssp.name like concat('%',#{query.spaceLocation},'%')
                or sst.name like concat('%',#{query.spaceLocation},'%'))
            </if>
        </where>
        <if test="query.orderBy!='' and query.orderBy!=null and query.orderType!='' and query.orderType!=null" >
            order by ${query.orderBy} ${query.orderType}
        </if>

    </select>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskPoint.TSafeRiskPointVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskPoint.TSafeRiskPointDTOParam" databaseId="kingbase">
        select so.org_name as orgName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,dpo.org_name as departmentName,
        CONCAT_WS(' ',ss.name, ssp.name, sst.name) as spaceLocation,


               se.name as relationEquipmentName,se.code relationSpaceCode,
        <include refid="select_base_column" />
        from t_safe_risk_point rs
        left join t_safe_equipment se on rs.relation_equipment_id = se.id
        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 dpo on dpo.org_id = rs.department_id
        left join t_sys_org bso on bso.org_id = rs.basic_org_id
        left join t_safe_space ss on rs.relation_space_id = ss.id
        left join t_safe_space ssp on rs.relation_space_parent_id = ssp.id
        left join t_safe_space_type sst on ssp.space_type_id = sst.id
        <where>
            rs.status = '0'
            <if test="query.orgId!=null and query.orgId!=''">
                and  (rs.org_id = #{query.orgId} or rs.org_all_path like concat('%',#{query.orgId},'%') or so.parent_ids like concat('%,',#{query.orgId},'%'))
            </if>
            <include refid="common_where_if"></include>
            <if test="query.spaceLocation!='' and query.spaceLocation!=null">
                and (ss.name like concat('%',#{query.spaceLocation},'%')
                or ssp.name like concat('%',#{query.spaceLocation},'%')
                or sst.name like concat('%',#{query.spaceLocation},'%'))
            </if>
        </where>
        <if test="query.orderBy!='' and query.orderBy!=null and query.orderType!='' and query.orderType!=null" >
            order by ${query.orderBy} ${query.orderType}
        </if>

    </select>


    <select id="findBySource" resultType="com.testor.ddd.safetyControl.interfaces.model.dto.riskPoint.TSafeRiskPointDTO">
        select  <include refid="select_base_column" />
        from t_safe_risk_point_source ps ,t_safe_risk_point rs
        <where>
            rs.id = ps.point_id
            and ps.status = '0' and rs.status='0'
            and ps.source_id = #{sourceId}
            order by ps.create_date desc
        </where>
    </select>
</mapper>