<?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.riskSource.dao.TSafeRiskSourceDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.ddd.safetyControl.infrastructure.repository.riskSource.entity.TSafeRiskSourceDO">
        <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="processes" property="processes"/>
        <id column="consequence_id" property="consequenceId"/>
        <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="is_major" property="isMajor"/>
        <id column="risk_level_id" property="riskLevelId"/>
        <id column="inherent_level_id" property="inherentLevelId"/>
        <id column="real_level_id" property="realLevelId"/>
        <id column="identification_method" property="identificationMethod"/>
        <id column="lec_l" property="lecL"/>
        <id column="lec_e" property="lecE"/>
        <id column="lec_c" property="lecC"/>
        <id column="ls_l" property="lsL"/>
        <id column="ls_s" property="lsS"/>
        <id column="control_level_id" property="controlLevelId"/>
        <id column="control_status_id" property="controlStatusId"/>
        <id column="check_frequency_id" property="checkFrequencyId"/>
        <id column="control_basis" property="controlBasis"/>
        <id column="control_measures" property="controlMeasures"/>
        <id column="further_control_measures" property="furtherControlMeasures"/>
        <id column="improvement_plan" property="improvementPlan"/>
        <id column="relation_space_level_ids" property="relationSpaceLevelIds"/>
        <id column="relation_equipment_level_ids" property="relationEquipmentLevelIds"/>
        <id column="updated" property="updated"/>
        <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.processes,rs.consequence_id consequenceId,
       rs.type_id typeId, rs.relation_type relationType, rs.relation_equipment_id relationEquipmentId, rs.relation_space_id relationSpaceId,rs.relation_space_parent_id relationSpaceParentId,
        rs.space_location spaceLocation, rs.is_major isMajor, rs.risk_level_id riskLevelId, rs.inherent_level_id inherentLevelId, rs.real_level_id realLevelId,
        rs.identification_method identificationMethod, rs.lec_l lecL, rs.lec_e lecE, rs.lec_c lecC, rs.ls_l lsL, rs.ls_s lsS,
        rs.control_level_id controlLevelId, rs.control_status_id controlStatusId, rs.check_frequency_id checkFrequencyId,rs.updated,
        rs.control_basis controlBasis, rs.control_measures controlMeasures, rs.improvement_plan improvementPlan,rs.further_control_measures furtherControlMeasures,
        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.controlStatusId!='' and query.controlStatusId!=null">
            and rs.control_status_id=#{query.controlStatusId}
        </if>
        <if test="query.controlLevelId!='' and query.controlLevelId!=null">
            and rs.control_level_id=#{query.controlLevelId}
        </if>
        <if test="query.identificationMethod!='' and query.identificationMethod!=null">
            and rs.identificationMethod=#{query.identificationMethod}
        </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>
        <if test="query.updated!='' and query.updated!=null">
            and rs.updated=#{query.updated}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskSource.TSafeRiskSourceVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskSource.TSafeRiskSourceDTOParam">
        select so.org_name as orgName,rso.org_name as regionOrgName,bso.org_name as basicOrgName,dpo.org_name as departmentName,
               se.name as relationEquipmentName,
        <include refid="select_base_column" />
        from t_safe_risk_source 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
        <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>
            <if test="query.isMajor != null and query.isMajor != ''">
                and  rs.is_major = #{query.isMajor}
            </if>
            <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>

    </select>
    <select id="listExpData"
            resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskSource.TSafeRiskSourceVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskSource.TSafeRiskSourceDTOParam">
        SELECT
        so.org_name AS orgName, rso.org_name AS regionOrgName, rso2.org_name  AS  basicOrgName, rso3.org_name AS departmentName,
        se.NAME AS relationEquipmentName, rs.id, CONCAT(so.parent_ids, ',', rs.org_id) AS orgAllPath, rs.org_id AS orgId,
        rs.region_org_id AS regionOrgId, rs.basic_org_id AS basicOrgId, rs.department_id AS departmentId, rs.NAME, rs.CODE,
        rs.processes, rs.consequence_id AS consequenceId, ddt.dict_value AS typeName, rs.relation_type AS relationType,
        rs.relation_equipment_id AS relationEquipmentId, rs.relation_space_id AS relationSpaceId, rs.relation_space_parent_id AS relationSpaceParentId,
        rs.space_location AS spaceLocation, rs.is_major AS isMajor, ddr.dict_value AS riskLevelName,
        ddi.dict_value AS inherentLevelName, ddi.dict_key AS inherentLevelKey, ddr2.dict_value AS realLevelName,
        ddr2.dict_key AS realLevelKey, rs.identification_method AS identificationMethod, rs.lec_l AS lecL,
        rs.lec_e AS lecE, rs.lec_c AS lecC, rs.ls_l AS lsL, rs.ls_s AS lsS, ddcl.dict_value AS controlLevelName,
        rs.control_status_id AS controlStatusId, ddcs.dict_value as controlStatusName, ddcs.dict_key AS controlLevelKey,
        dcf.dict_value AS checkFrequencyName, rs.updated, rs.control_basis AS controlBasis, rs.control_measures AS controlMeasures,
        rs.improvement_plan AS improvementPlan, rs.further_control_measures AS furtherControlMeasures,
        rs.relation_space_level_ids AS relationSpaceLevelIds, rs.relation_equipment_level_ids AS relationEquipmentLevelIds,
        rs.STATUS, rs.remarks, rs.create_by AS createBy, rs.create_date AS createDate, rs.update_by AS updateBy,
        rs.update_date AS updateDate
        FROM
        t_safe_risk_source rs
        inner JOIN t_sys_org so ON so.org_id = rs.org_id
        left JOIN t_safe_equipment se ON rs.relation_equipment_id = se.id
        LEFT JOIN t_sys_org rso ON rso.org_id = rs.region_org_id
        LEFT JOIN t_sys_org rso2 ON rso2.org_id = rs.basic_org_id
        LEFT JOIN t_sys_org rso3 ON rso3.org_id = rs.department_id
        LEFT JOIN t_sys_dict_data ddt ON ddt.dict_data_id = rs.type_id
        LEFT JOIN t_sys_dict_data ddr ON ddr.dict_data_id = rs.risk_level_id
        LEFT JOIN t_sys_dict_data ddi ON ddi.dict_data_id = rs.inherent_level_id
        LEFT JOIN t_sys_dict_data ddr2 ON ddr2.dict_data_id = rs.real_level_id
        LEFT JOIN t_sys_dict_data ddcl ON ddcl.dict_data_id = rs.control_level_id
        LEFT JOIN t_sys_dict_data ddcs ON ddcs.dict_data_id = rs.control_status_id
        LEFT JOIN t_sys_dict_data dcf ON dcf.dict_data_id = rs.check_frequency_id
        <where>
            rs.status = '0'
            <if test="query.orgId!=null and query.orgId!=''">
                and (rs.org_id = #{query.orgId} or so.parent_ids LIKE concat( '%,', #{query.orgId}, '%' ))
            </if>
            <if test="query.isMajor != null and query.isMajor != ''">
                and  rs.is_major = #{query.isMajor}
            </if>
            <include refid="common_where_if"></include>
        </where>
    </select>
</mapper>