<?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.TSafeHazardCheckResultDetailDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.safeCheck.model.domain.TSafeHazardCheckResultDetail">
        <id column="id" property="id"/>
        <id column="result_id" property="resultId"/>
        <id column="result_type_id" property="resultTypeId"/>
        <id column="relation_id" property="relationId"/>
        <id column="option_id" property="optionId"/>
        <id column="option_parent_id" property="optionParentId"/>
    </resultMap>

    <sql id="select_base_column">
        crd.id, crd.result_id resultId, crd.result_type_id resultTypeId, crd.relation_id relationId,
        crd.option_id optionId, crd.option_parent_id optionParentId,
        crd.status, crd.remarks, crd.create_by createBy, crd.create_date createDate,
        crd.update_by as updateBy, crd.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and crd.id=#{query.id}
        </if>
        <if test="query.resultId!=null and query.resultId!=''">
            and crd.result_id=#{query.resultId}
        </if>
        <if test="query.resultTypeId!='' and query.resultTypeId!=null">
            and crd.result_type_id=#{query.resultTypeId}
        </if>
        <if test="query.relationId!='' and query.relationId!=null">
            and crd.relation_id=#{query.relationId}
        </if>
        <if test="query.optionId!='' and query.optionId!=null">
            and crd.option_id=#{query.optionId}
        </if>
        <if test="query.optionParentId!='' and query.optionParentId!=null">
            and crd.option_parent_id=#{query.optionParentId}
        </if>
    </sql>

    <select id="listByResult" resultType="com.testor.module.safeCheck.model.vo.SafeHazardCheckResultDetailVO">
        select ho.check_content checkContent, ho.check_method checkMethod, hop.check_name checkName,
        crt.name resultTypeName,crt.code resultTypeCode,
               sho.name hazardOrgName,
               <include refid="select_base_column" />
        from t_safe_hazard_check_result_detail crd
        left join t_safe_hazard_option ho on crd.option_id = ho.id
        left join t_safe_hazard_option hop on crd.option_parent_id = hop.id
        left join t_safe_hazard_check_result_type crt on crt.id = crd.result_type_id
        left join t_safe_hazard_org sho on sho.id = crd.relation_id
        <where>
            crd.status = '0'
            and crd.result_id=#{resultId}
        </where>
    </select>

    <select id="countByResult" resultType="com.testor.module.safeCheck.model.vo.SafeHazardCheckResultDetailCountVO">
        select count(crd.id) as detailCount,
               crt.id resultTypeId, crt.name resultTypeName,crt.code resultTypeCode
        from t_safe_hazard_check_result_type crt
        left join t_safe_hazard_check_result_detail crd
            on crt.id = crd.result_type_id and crd.status = '0'
                   and crd.result_id=#{resultId}
        group by crd.result_id,crt.id, crt.name ,crt.code
    </select>
</mapper>