<?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.module.safeCheck.dao.TSafeHazardCheckObjectDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.safeCheck.model.domain.TSafeHazardCheckObject">
        <id column="id" property="id"/>
        <id column="plan_id" property="planId"/>
        <id column="obj_type_id" property="objTypeId"/>
        <id column="obj_id" property="objId"/>
        <id column="standard_id" property="standardId"/>
        <id column="type" property="type"/>
        <id column="obj_parent_ids" property="objParentIds"/>
        <id column="standard_parent_ids" property="standardParentIds"/>
        <id column="obj_type_parent_ids" property="objTypeParentIds"/>
        <id column="group_index" property="groupIndex"/>
    </resultMap>

    <sql id="select_base_column">
        rs.id, rs.plan_id planId, rs.obj_type_id objTypeId, rs.obj_id objId, rs.standard_id standardId,rs.type,
        rs.obj_parent_ids objParentIds, rs.standard_parent_ids standardParentIds,rs.obj_type_parent_ids objTypeParentIds,
        rs.status, rs.remarks, rs.create_by createBy, rs.create_date createDate,rs.group_index groupIndex,
        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.planId!=null and query.planId!=''">
            and rs.plan_id=#{query.planId}
        </if>
        <if test="query.objTypeId!='' and query.objTypeId!=null">
            and rs.obj_type_id=#{query.objTypeId}
        </if>
        <if test="query.objId!='' and query.objId!=null">
            and rs.obj_id=#{query.objId}
        </if>
        <if test="query.standardId!='' and query.standardId!=null">
            and rs.standard_id=#{query.standardId}
        </if>
    </sql>

<!--    <select id="listByGroup" resultType="com.testor.module.safeCheck.model.vo.TSafeHazardCheckPlanVO"-->
<!--            parameterType="com.testor.module.safeCheck.model.domain.TSafeHazardCheckObject">-->
<!--        select rs.plan_id planId, rs.obj_type_id objTypeId, rs.standard_id standardId,-->
<!--               rs.GROUP_CONCAT(obj_id) as objIds-->
<!--        from t_safe_hazard_check_object rs-->
<!--        <where>-->
<!--            rs.status = '0'-->
<!--            <include refid="common_where_if"></include>-->
<!--        </where>-->
<!--        group by rs.plan_id, rs.obj_type_id, rs.standard_id-->
<!--        <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="findObjectNamesByPlan" resultType="java.lang.String">
        select rs.plan_id planId, GROUP_CONCAT(se.name, ',', ss.name, ',', st.name) as checkObjectNames
        from t_safe_hazard_check_object rs
        left join t_safe_equipment se on rs.obj_id = se.id
        left join t_safe_space ss on rs.obj_id = ss.id
        left join t_safe_hazard_standard_type st on rs.obj_id = ss.id
        <where>
            rs.status = '0'
            and rs.plan_id=#{planId}
        </where>
    </select>

    <select id="listVOByPlan" resultType="com.testor.module.safeCheck.model.vo.SafeHazardCheckObjectVO" databaseId="mysql" >
        select CONCAT_WS('',se.name, ss.name) as objName,
        hs.standard_name standardName,
              ( case when rs.type =1 then et.name
                    when rs.type =2 then st.name
                    when rs.type =3 then hst.category_item
        end ) objTypeName,
        <include refid="select_base_column" />
        from t_safe_hazard_check_object rs
        left join t_safe_equipment se on rs.obj_id = se.id and rs.type =1
        left join t_safe_space ss on rs.obj_id = ss.id and rs.type =2
        left join t_safe_hazard_standard hs on rs.standard_id = hs.id
        left join t_safe_equipment_type et on rs.obj_type_id = et.id and rs.type =1
        left join t_safe_space_type st on rs.obj_type_id = st.id and rs.type =2
        left join t_safe_hazard_standard_type hst on rs.obj_type_id = hst.id and rs.type =3
        <where>
            rs.status = '0'
            and rs.plan_id=#{planId}
        </where>
    </select>

    <select id="listVOByPlan" resultType="com.testor.module.safeCheck.model.vo.SafeHazardCheckObjectVO" databaseId="kingbase">
        select CONCAT_WS(' ',se.name, ss.name) as objName,
         hs.standard_name standardName,
        ( case when rs.type =1 then et.name
        when rs.type =2 then st.name
        when rs.type =3 then hst.category_item
        end ) objTypeName,
        <include refid="select_base_column" />
        from t_safe_hazard_check_object rs
        left join t_safe_equipment se on rs.obj_id = se.id and rs.type =1
        left join t_safe_space ss on rs.obj_id = ss.id and rs.type =2
        left join t_safe_hazard_standard hs on rs.standard_id = hs.id
        left join t_safe_equipment_type et on rs.obj_type_id = et.id and rs.type =1
        left join t_safe_space_type st on rs.obj_type_id = st.id and rs.type =2
        left join t_safe_hazard_standard_type hst on rs.obj_type_id = hst.id and rs.type =3
        <where>
            rs.status = '0'
            and rs.plan_id=#{planId}
        </where>
    </select>

</mapper>