<?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.ems.mapper.OpEnergyStrategyStepMapper">

    <resultMap type="com.ruoyi.ems.domain.OpEnergyStrategyStep" id="StepResult">
        <result property="id" column="id"/>
        <result property="strategyCode" column="strategy_code"/>
        <result property="stepCode" column="step_code"/>
        <result property="stepName" column="step_name"/>
        <result property="stepType" column="step_type"/>
        <result property="stepIndex" column="step_index"/>
        <result property="parentStepCode" column="parent_step_code"/>
        <result property="conditionExpr" column="condition_expr"/>
        <result property="targetObjType" column="target_obj_type"/>
        <result property="targetObjCode" column="target_obj_code"/>
        <result property="targetModelCode" column="target_model_code"/>
        <result property="abilityKey" column="ability_key"/>
        <result property="abilityParam" column="ability_param"/>
        <result property="paramSource" column="param_source"/>
        <result property="paramMapping" column="param_mapping"/>
        <result property="delaySeconds" column="delay_seconds"/>
        <result property="retryOnFail" column="retry_on_fail"/>
        <result property="retryTimes" column="retry_times"/>
        <result property="retryInterval" column="retry_interval"/>
        <result property="continueOnFail" column="continue_on_fail"/>
        <result property="timeout" column="timeout"/>
        <result property="enable" column="enable"/>
        <result property="remark" column="remark"/>
        <result property="loopMaxCount" column="loop_max_count"/>
        <result property="loopInterval" column="loop_interval"/>
        <result property="loopCondition" column="loop_condition"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectStepVo">
        select id, strategy_code, step_code, step_name, step_type, step_index, parent_step_code,
               condition_expr, target_obj_type, target_obj_code, target_model_code, ability_key,
               ability_param, param_source, param_mapping, delay_seconds, retry_on_fail, retry_times,
               retry_interval, continue_on_fail, timeout, enable, remark, loop_max_count, loop_interval, loop_condition,
               create_time, update_time
        from adm_op_energy_strategy_step
    </sql>

    <select id="selectStepByStrategyCode" parameterType="String" resultMap="StepResult">
        <include refid="selectStepVo"/>
        where strategy_code = #{strategyCode}
        order by step_index
    </select>

    <select id="selectStepById" parameterType="Long" resultMap="StepResult">
        <include refid="selectStepVo"/>
        where id = #{id}
    </select>

    <insert id="insertStep" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyStep" useGeneratedKeys="true" keyProperty="id">
        insert into adm_op_energy_strategy_step
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">strategy_code,</if>
            <if test="stepCode != null and stepCode != ''">step_code,</if>
            <if test="stepName != null and stepName != ''">step_name,</if>
            <if test="stepType != null and stepType != ''">step_type,</if>
            <if test="stepIndex != null">step_index,</if>
            <if test="parentStepCode != null">parent_step_code,</if>
            <if test="conditionExpr != null">condition_expr,</if>
            <if test="targetObjType != null">target_obj_type,</if>
            <if test="targetObjCode != null">target_obj_code,</if>
            <if test="targetModelCode != null">target_model_code,</if>
            <if test="abilityKey != null">ability_key,</if>
            <if test="abilityParam != null">ability_param,</if>
            <if test="paramSource != null">param_source,</if>
            <if test="paramMapping != null">param_mapping,</if>
            <if test="delaySeconds != null">delay_seconds,</if>
            <if test="retryOnFail != null">retry_on_fail,</if>
            <if test="retryTimes != null">retry_times,</if>
            <if test="retryInterval != null">retry_interval,</if>
            <if test="continueOnFail != null">continue_on_fail,</if>
            <if test="timeout != null">timeout,</if>
            <if test="enable != null">`enable`,</if>
            <if test="remark != null">remark,</if>
            <if test="loopMaxCount != null">loop_max_count,</if>
            <if test="loopInterval != null">loop_interval,</if>
            <if test="loopCondition != null">loop_condition,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">#{strategyCode},</if>
            <if test="stepCode != null and stepCode != ''">#{stepCode},</if>
            <if test="stepName != null and stepName != ''">#{stepName},</if>
            <if test="stepType != null and stepType != ''">#{stepType},</if>
            <if test="stepIndex != null">#{stepIndex},</if>
            <if test="parentStepCode != null">#{parentStepCode},</if>
            <if test="conditionExpr != null">#{conditionExpr},</if>
            <if test="targetObjType != null">#{targetObjType},</if>
            <if test="targetObjCode != null">#{targetObjCode},</if>
            <if test="targetModelCode != null">#{targetModelCode},</if>
            <if test="abilityKey != null">#{abilityKey},</if>
            <if test="abilityParam != null">#{abilityParam},</if>
            <if test="paramSource != null">#{paramSource},</if>
            <if test="paramMapping != null">#{paramMapping},</if>
            <if test="delaySeconds != null">#{delaySeconds},</if>
            <if test="retryOnFail != null">#{retryOnFail},</if>
            <if test="retryTimes != null">#{retryTimes},</if>
            <if test="retryInterval != null">#{retryInterval},</if>
            <if test="continueOnFail != null">#{continueOnFail},</if>
            <if test="timeout != null">#{timeout},</if>
            <if test="enable != null">#{enable},</if>
            <if test="remark != null">#{remark},</if>
            <if test="loopMaxCount != null">#{loopMaxCount},</if>
            <if test="loopInterval != null">#{loopInterval},</if>
            <if test="loopCondition != null">#{loopCondition},</if>
        </trim>
    </insert>

    <update id="updateStep" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyStep">
        update adm_op_energy_strategy_step
        <trim prefix="SET" suffixOverrides=",">
            <if test="stepName != null and stepName != ''">step_name = #{stepName},</if>
            <if test="stepType != null and stepType != ''">step_type = #{stepType},</if>
            <if test="stepIndex != null">step_index = #{stepIndex},</if>
            <if test="parentStepCode != null">parent_step_code = #{parentStepCode},</if>
            <if test="conditionExpr != null">condition_expr = #{conditionExpr},</if>
            <if test="targetObjType != null">target_obj_type = #{targetObjType},</if>
            <if test="targetObjCode != null">target_obj_code = #{targetObjCode},</if>
            <if test="targetModelCode != null">target_model_code = #{targetModelCode},</if>
            <if test="abilityKey != null">ability_key = #{abilityKey},</if>
            <if test="abilityParam != null">ability_param = #{abilityParam},</if>
            <if test="paramSource != null">param_source = #{paramSource},</if>
            <if test="paramMapping != null">param_mapping = #{paramMapping},</if>
            <if test="delaySeconds != null">delay_seconds = #{delaySeconds},</if>
            <if test="retryOnFail != null">retry_on_fail = #{retryOnFail},</if>
            <if test="retryTimes != null">retry_times = #{retryTimes},</if>
            <if test="retryInterval != null">retry_interval = #{retryInterval},</if>
            <if test="continueOnFail != null">continue_on_fail = #{continueOnFail},</if>
            <if test="timeout != null">timeout = #{timeout},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="loopMaxCount != null">loop_max_count = #{loopMaxCount},</if>
            <if test="loopInterval != null">loop_interval = #{loopInterval},</if>
            <if test="loopCondition != null">loop_condition = #{loopCondition},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStepById" parameterType="Long">
        delete from adm_op_energy_strategy_step where id = #{id}
    </delete>

    <delete id="deleteStepByIds">
        delete from adm_op_energy_strategy_step where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteStepByStrategyCode" parameterType="String">
        delete from adm_op_energy_strategy_step where strategy_code = #{strategyCode}
    </delete>

    <insert id="insertStepBatch">
        insert into adm_op_energy_strategy_step
        (strategy_code, step_code, step_name, step_type, step_index, parent_step_code,
        condition_expr, target_obj_type, target_obj_code, target_model_code, ability_key,
        ability_param, param_source, param_mapping, delay_seconds, retry_on_fail, retry_times,
        retry_interval, continue_on_fail, timeout, enable, remark, loop_max_count, loop_interval, loop_condition)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.strategyCode}, #{item.stepCode}, #{item.stepName}, #{item.stepType},
            #{item.stepIndex}, #{item.parentStepCode}, #{item.conditionExpr}, #{item.targetObjType},
            #{item.targetObjCode}, #{item.targetModelCode}, #{item.abilityKey}, #{item.abilityParam},
            #{item.paramSource}, #{item.paramMapping}, #{item.delaySeconds}, #{item.retryOnFail},
            #{item.retryTimes}, #{item.retryInterval}, #{item.continueOnFail}, #{item.timeout},
            #{item.enable}, #{item.remark}, #{item.loopMaxCount}, #{item.loopInterval}, #{item.loopCondition})
        </foreach>
    </insert>

    <select id="selectStepsByParentCode" resultMap="StepResult">
        <include refid="selectStepVo"/>
        WHERE strategy_code = #{strategyCode}
        AND parent_step_code = #{parentStepCode}
        ORDER BY step_index ASC
    </select>
</mapper>