<?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.ruoyi.project.business.mapper.TpParaRelatedCaseMapper">
    <resultMap type="TpParaRelatedCase" id="TpParaRelatedCaseResult">
        <result property="id" column="id"/>
        <result property="caseName" column="case_name"/>
        <result property="relationship" column="relationship"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectTpParaRelatedCaseVo">
        select id,
               case_name,
               relationship,
               status,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tp_para_related_case
    </sql>
    <delete id="clearAll">
        delete from tp_para_related_case where 1 = 1
    </delete>

    <resultMap id="RelatedLogicCache" type="com.ruoyi.project.business.dto.TpParaRelatedLogicCache">
        <id column="rl_id" property="id"/>
        <result column="rl_para_code" property="paraCode"/>
        <result column="operation" property="operation"/>
        <result column="value_type" property="valueType"/>
        <result column="rl_value" property="value"/>
    </resultMap>
    <resultMap id="RelatedActionCache" type="com.ruoyi.project.business.dto.TpParaRelatedActionCache">
        <id column="ra_id" property="id"/>
        <result column="ra_para_code" property="paraCode"/>
        <result column="computed_flag" property="computedFlag"/>
        <result column="ra_value" property="value"/>
        <result column="value_formula_txt" property="valueFormulaTxt"/>
        <result column="select_item" property="selectItem"/>
        <result column="editable" property="editable"/>
        <result column="display_flag" property="displayFlag"/>
        <result column="empty_flag" property="emptyFlag"/>
    </resultMap>
    <resultMap id="RelatedCaseCache" type="com.ruoyi.project.business.dto.TpParaRelatedCaseCache">
        <id column="id" property="id"/>
        <result column="case_name" property="caseName"/>
        <result column="relationship" property="relationship"/>
        <result column="run_type" property="runType"/>
        <result column="run_index" property="runIndex"/>
        <result column="elevator_ids" property="elevatorIds"/>
        <collection property="logics" resultMap="RelatedLogicCache"/>
        <collection property="actions" resultMap="RelatedActionCache"/>
    </resultMap>
    <select id="getRelatedCaseCacheList" resultMap="RelatedCaseCache">
        select rc.id, rc.case_name, rc.relationship, rc.run_type, rc.run_index,
               rl.id as rl_id, rl.para_code as rl_para_code, rl.operation, rl.value_type, rl.value as rl_value,
               ra.id as ra_id, ra.para_code as ra_para_code, ra.computed_flag, ra.value as ra_value, ra.value_formula_txt, ra.select_item, ra.editable,
               ra.display_flag, ra.empty_flag
        from tp_para_related_case rc
                 left join tp_para_related_logic rl on rc.id = rl.case_id and rl.del_flag = 0
                 left join tp_para_related_action ra on rc.id = ra.case_id and ra.del_flag = 0
        where rc.del_flag = 0 and rc.status = 0
        order by  rc.run_index asc
    </select>
    <select id="getRelatedElevators" resultMap="RelatedCaseCache">
        select rc.id, GROUP_CONCAT(ce.elevator_model_id) as elevator_ids
        from tp_para_related_case rc
                 left join tp_case_elevator ce on rc.id = ce.related_case_id and ce.del_flag = 0
        where rc.del_flag = 0 and rc.status = 0 and ce.elevator_model_id is not null
        group by rc.id
    </select>

</mapper>