<?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.equipment.dao.TSafeEquipmentTypeDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.ddd.safetyControl.infrastructure.repository.equipment.entity.TSafeEquipmentTypeDO">
        <id column="id" property="id"/>
        <id column="org_id" property="orgId"/>
        <id column="parent_id" property="parentId"/>
        <id column="name" property="name"/>
        <id column="code" property="code"/>
    </resultMap>

    <sql id="select_base_column">
        se.id, se.org_id orgId, se.parent_id parentId, se.name, se.code,
        se.status, se.remarks, se.create_by createBy, se.create_date createDate,
        se.update_by as updateBy, se.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and se.id=#{query.id}
        </if>
<!--        <if test="query.orgId!=null and query.orgId!=''">-->
<!--            and se.org_id=#{query.orgId}-->
<!--        </if>-->
        <if test="query.parentId!='' and query.parentId!=null">
            and se.parent_id=#{query.parentId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and se.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.code!='' and query.code!=null">
            and se.code=#{query.code}
        </if>
    </sql>

    <select id="listPage" resultType="com.testor.ddd.safetyControl.interfaces.model.vo.equipment.TSafeEquipmentTypeVO"
            parameterType="com.testor.ddd.safetyControl.interfaces.model.dto.equipment.TSafeEquipmentTypeDTOParam">
        select   pse.name as parentName, pse.code as parentCode,
        <include refid="select_base_column" />
        from t_safe_equipment_type se
        left join t_safe_equipment_type pse on pse.id = se.parent_id
        <where>
            se.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>
        <if test="query.noParentId!='' and query.noParentId!=null">
            and se.parent_id != #{query.noParentId}
        </if>
        <if test="query.remarks!='' and query.remarks!=null">
            and se.id in(
            select equipment_type_id from t_safe_equipment ff where ff.org_id = #{query.orgId}
            )
        </if>
    </select>
</mapper>