<?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.safe.dao.TSafeHazardStandardTypeDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.safe.model.domain.TSafeHazardStandardType">
        <id column="id" property="id"/>
        <id column="ref_id" property="refId"/>
        <id column="parent_id" property="parentId"/>
        <id column="tenant_id" property="tenantId"/>
        <id column="process_id" property="processId"/>
        <id column="process_status" property="processStatus"/>
        <id column="name" property="name"/>
        <id column="category_item" property="categoryItem"/>
        <id column="built_in" property="builtIn"/>
        <id column="image" property="image"/>
        <id column="sort" property="sort"/>
        <id column="level" property="level"/>
        <id column="precautions" property="precautions"/>
    </resultMap>

    <select id="getTSafeHazardStandardTypePage" resultMap="BaseResultMap">
        SELECT t.name,
        s.category_item,
        CASE WHEN s.image IS NULL THEN t.image ELSE s.image END                   AS image,
        CASE WHEN s.precautions IS NULL THEN t.precautions ELSE s.precautions END AS precautions,
        t.sort
        FROM t_safe_hazard_standard_type AS t
        LEFT JOIN t_safe_hazard_standard_type AS s ON t.id = s.parent_id
        WHERE t.level = '1' AND t.status=0
        ORDER BY t.sort
        <if test="limit!=null and limit!=''">
            LIMIT #{limit} OFFSET #{offset}
        </if>
    </select>

    <select id="maxSort" resultType="integer">
        SELECT MAX(sort) AS sort
        FROM t_safe_hazard_standard_type
    </select>

    <select id="count" resultType="int">
        SELECT count(*) as count
        FROM t_safe_hazard_standard_type AS t
                 LEFT JOIN t_safe_hazard_standard_type AS s ON t.id = s.parent_id
        WHERE t.level = '1' AND t.status=0
        ORDER BY t.sort
    </select>
</mapper>