<?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.usky.rule.mapper.RuleEngineMapper">
    <resultMap id="Base" type="com.usky.rule.domain.RuleEngine">
        <id column="id" property="id"/>
        <result column="project_id" property="projectId"/>
        <result column="name" property="name"/>
        <result column="space_id" property="spaceId"/>
        <result column="status" property="status"/>
        <result column="descr" property="descr"/>
        <result column="detail" property="detail"/>
        <result column="updated_by" property="updatedBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="created_by" property="createdBy"/>
        <result column="create_time" property="createTime"/>
        <result column="tenant_id" property="tenantId"/>
    </resultMap>

    <delete id="deleteById">DELETE FROM rule_engine WHERE id=#{id}</delete>
    <select id="selectById" resultMap="Base">SELECT * FROM rule_engine WHERE id=#{id}
        <if test="tenantId != null">AND tenant_id = #{tenantId}</if>
    </select>
    <update id="updateStatus">UPDATE rule_engine SET status=#{status}, update_time=NOW() WHERE id=#{id}</update>
    <select id="selectEnabledByProjectId" resultMap="Base">
        SELECT * FROM rule_engine WHERE project_id=#{projectId} AND status=1
        <if test="tenantId != null">AND tenant_id = #{tenantId}</if>
    </select>

    <!-- 查询直接子节点 id（兼容 MySQL 5.7，递归在 Service 层实现）-->
    <select id="selectDirectChildrenSpaceIds" resultType="java.lang.Long">
        SELECT s.id FROM base_space s WHERE s.parent_id = #{parentId}
    </select>
</mapper>
