<?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.riskStatistics.dao.RiskStatisticsDao">


    <!--*******以下危险源统计****************** -->
    <select id="statisticsSourceNum" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">
        select so.org_id coordinateId,so.tree_level treeLevel, so.nature nature, so.org_name coordinateName, rt.dict_data_id riskLevelId, rt.dict_key riskLevelKey, rt.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_org so
                 left join (select s.id,s.Inherent_level_id,s.real_level_id,s.STATUS,concat(o.parent_ids,',',s.org_id) as org_all_path from t_safe_risk_source s,t_sys_org o where s.org_id=o.org_id) rs
                     ON rs.org_all_path LIKE CONCAT( '%', so.org_id, '%' )  and rs.status=0
                 left join t_sys_dict_data rt
                     on
                <if test='query.riskType==1'>
                    rt.dict_data_id = rs.Inherent_level_id
                </if>
                <if test='query.riskType==2'>
                    rt.dict_data_id = rs.real_level_id
                </if>
        where so.status = 0 and so.is_dept != 1
            and (so.parent_id = #{query.orgId} or ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}))
        group by so.org_id, rt.dict_data_id
        order by  num desc,so.org_name asc, so.tree_level asc,so.tree_sort ASC
    </select>
<!--    having num>0;-->

    <select id="statisticsSourceNumBySpaceType" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">
        select st.id coordinateId, st.name coordinateName, rt.dict_data_id riskLevelId, rt.dict_key riskLevelKey, rt.dict_value riskLevelValue, count(rs.id) as num
        from t_safe_space_type st
        left join t_safe_space s on s.space_type_id = st.id
        left join t_safe_risk_source rs on rs.relation_space_parent_id = s.id
        and  rs.status = 0 and rs.org_id = #{query.orgId}
        left join t_sys_dict_data rt
        on
        <if test='query.riskType==1'>
            rt.dict_data_id = rs.Inherent_level_id
        </if>
        <if test='query.riskType==2'>
            rt.dict_data_id = rs.real_level_id
        </if>
        where st.status=0
        group by st.id,rt.dict_data_id
    </select>

    <select id="statisticsSourceLevel" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">

        select dd.dict_data_id riskLevelId, dd.dict_key riskLevelKey, dd.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_dict_type dt, t_sys_dict_data dd
         left join t_safe_risk_source rs on
                <if test='query.riskType==1'>
                    dd.dict_data_id = rs.Inherent_level_id
                </if>
                <if test='query.riskType==2'>
                    dd.dict_data_id = rs.real_level_id
                </if>
        and rs.org_id in (select org_id from t_sys_org so where ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}) or so.parent_ids like CONCAT('%,', #{query.orgId} ,'%'))
            and rs.status=0
        where dd.dict_id=dt.dict_id and dd.status=0
          and dt.dict_type=#{query.dictType}
            group by dd.dict_data_id,dd.dict_key
    </select>

    <select id="statisticsSourceType" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">

        select dd.dict_data_id riskLevelId, dd.dict_key riskLevelKey, dd.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_dict_type dt, t_sys_dict_data dd
         left join t_safe_risk_source rs on rs.type_id = dd.dict_data_id
            and rs.org_id in (select org_id from t_sys_org so where ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}) or so.parent_ids like CONCAT('%,', #{query.orgId} ,'%'))
            and rs.status='0'
        where dd.dict_id=dt.dict_id and dd.status=0
          and dt.dict_type='risk_source_type'
        group by dd.dict_data_id,dd.dict_key
    </select>


    <!--*******以下风险点统计****************** -->
    <select id="statisticsPointNum" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">
        select so.org_id coordinateId,so.tree_level treeLevel, so.nature nature, so.org_name coordinateName, rt.dict_data_id riskLevelId, rt.dict_key riskLevelKey, rt.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_org so
        LEFT JOIN (select s.id,s.Inherent_level_id,s.real_level_id,s.STATUS,concat(o.parent_ids,',',s.org_id) as org_all_path from t_safe_risk_point s,t_sys_org o where s.org_id=o.org_id) rs
            ON rs.org_all_path LIKE CONCAT( '%', so.org_id, '%' )  and rs.status=0
        left join t_sys_dict_data rt
        on
        <if test='query.riskType==1'>
            rt.dict_data_id = rs.Inherent_level_id
        </if>
        <if test='query.riskType==2'>
            rt.dict_data_id = rs.real_level_id
        </if>
        where so.status = '0' and so.is_dept != 1
        and (so.parent_id = #{query.orgId} or ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}))
        group by so.org_id, rt.dict_data_id
        order by so.org_name asc, so.tree_level asc,so.tree_sort ASC
    </select>
<!--    having num>0;-->

    <select id="statisticsPointNumBySpaceType" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">
        select st.id coordinateId, st.name coordinateName, rt.dict_data_id riskLevelId, rt.dict_key riskLevelKey, rt.dict_value riskLevelValue, count(rs.id) as num
        from t_safe_space_type st
        left join  t_safe_space s on s.space_type_id = st.id
        left join t_safe_risk_point rs on rs.relation_space_parent_id = s.id
        and rs.status = 0 and rs.org_id = #{query.orgId}
        left join t_sys_dict_data rt
        on
        <if test='query.riskType==1'>
            rt.dict_data_id = rs.Inherent_level_id
        </if>
        <if test='query.riskType==2'>
            rt.dict_data_id = rs.real_level_id
        </if>
        where st.status = '0'
        group by st.id,rt.dict_data_id
    </select>

    <select id="statisticsPointLevel" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">

        select dd.dict_data_id riskLevelId, dd.dict_key riskLevelKey, dd.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_dict_type dt, t_sys_dict_data dd
        left join t_safe_risk_point rs on
        <if test='query.riskType==1'>
            dd.dict_data_id = rs.Inherent_level_id
        </if>
        <if test='query.riskType==2'>
            dd.dict_data_id = rs.real_level_id
        </if>
        and rs.org_id in (select org_id from t_sys_org so where ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}) or so.parent_ids like CONCAT('%,', #{query.orgId} ,'%'))
        and rs.status=0
        where dd.dict_id=dt.dict_id and dd.status=0
        and dt.dict_type=#{query.dictType}
        group by dd.dict_data_id,dd.dict_key
    </select>

    <select id="statisticsPointType" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.riskStatistics.RiskStatisticsNumVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.riskStatistics.RiskStatisticsDTO">

        select dd.dict_data_id riskLevelId, dd.dict_key riskLevelKey, dd.dict_value riskLevelValue, count(rs.id) as num
        from t_sys_dict_type dt, t_sys_dict_data dd
                                     left join t_safe_risk_point rs on rs.type_id = dd.dict_data_id
            and rs.org_id in (select org_id from t_sys_org so where ((so.tree_level=4 or so.nature=4) and so.org_id=#{query.orgId}) or so.parent_ids like CONCAT('%,', #{query.orgId} ,'%'))
            and rs.status=0
        where dd.dict_id=dt.dict_id and dd.status=0
          and dt.dict_type='risk_type'
        group by dd.dict_data_id,dd.dict_key
    </select>

    <!-- 危险源 导出数据 -->
    <sql id="select_base_column">
        rs.id, rs.org_all_path orgAllPath, rs.org_id orgId, 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.control_basis controlBasis, rs.control_measures controlMeasures, rs.improvement_plan improvementPlan,
        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>
    </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,dpo.org_name as departmentName,
        ss.name as riskPoint, se.name as relationEquipmentName,se.code relationSpaceCode,
        <include refid="select_base_column" />
        from t_safe_risk_source rs
        left join t_safe_risk_point ss on rs.relation_space_id = ss.relation_space_id
            and ss.status=0
        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 dpo on dpo.org_id = rs.department_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>


    </select>

</mapper>