<?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.OpEnergyStrategyMapper">

    <resultMap type="com.ruoyi.ems.domain.OpEnergyStrategy" id="StrategyResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="strategyCode" column="strategy_code"/>
        <result property="strategyName" column="strategy_name"/>
        <result property="strategyDesc" column="strategy_desc"/>
        <result property="sceneType" column="scene_type"/>
        <result property="strategyCategory" column="strategy_category"/>
        <result property="triggerType" column="trigger_type"/>
        <result property="triggerConfig" column="trigger_config"/>
        <result property="strategyState" column="strategy_state"/>
        <result property="priority" column="priority"/>
        <result property="execMode" column="exec_mode"/>
        <result property="timeout" column="timeout"/>
        <result property="retryTimes" column="retry_times"/>
        <result property="lastExecTime" column="last_exec_time"/>
        <result property="lastExecResult" column="last_exec_result"/>
        <result property="execCount" column="exec_count"/>
        <result property="successCount" column="success_count"/>
        <result property="failCount" column="fail_count"/>
        <result property="version" column="version"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectStrategyVo">
        select id, area_code, strategy_code, strategy_name, strategy_desc, scene_type,
               strategy_category, trigger_type, trigger_config, strategy_state, priority,
               exec_mode, timeout, retry_times, last_exec_time, last_exec_result,
               exec_count, success_count, fail_count, version,
               create_by, create_time, update_by, update_time
        from adm_op_energy_strategy
    </sql>

    <select id="selectStrategyList" parameterType="com.ruoyi.ems.domain.OpEnergyStrategy" resultMap="StrategyResult">
        <include refid="selectStrategyVo"/>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and area_code = #{areaCode}
            </if>
            <if test="strategyCode != null and strategyCode != ''">
                and strategy_code = #{strategyCode}
            </if>
            <if test="sceneType != null">
                and scene_type = #{sceneType}
            </if>
            <if test="strategyCategory != null and strategyCategory != ''">
                and strategy_category = #{strategyCategory}
            </if>
            <if test="triggerType != null">
                and trigger_type = #{triggerType}
            </if>
            <if test="execMode != null">
                and exec_mode = #{execMode}
            </if>
            <if test="strategyState != null">
                and strategy_state = #{strategyState}
            </if>
            <if test="strategyName != null and strategyName != ''">
                and strategy_name like concat('%', #{strategyName}, '%')
            </if>
        </where>
        order by priority desc, create_time desc
    </select>

    <select id="selectStrategyById" parameterType="Long" resultMap="StrategyResult">
        <include refid="selectStrategyVo"/>
        where id = #{id}
    </select>

    <select id="selectStrategyByCode" parameterType="String" resultMap="StrategyResult">
        <include refid="selectStrategyVo"/>
        where strategy_code = #{strategyCode}
    </select>

    <select id="selectScheduledStrategies" resultMap="StrategyResult">
        <include refid="selectStrategyVo"/>
        where strategy_state = 1
        and trigger_type = 2
        order by priority desc
    </select>

    <insert id="insertStrategy" parameterType="com.ruoyi.ems.domain.OpEnergyStrategy" useGeneratedKeys="true" keyProperty="id">
        insert into adm_op_energy_strategy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="strategyCode != null and strategyCode != ''">strategy_code,</if>
            <if test="strategyName != null and strategyName != ''">strategy_name,</if>
            <if test="strategyDesc != null">strategy_desc,</if>
            <if test="sceneType != null">scene_type,</if>
            <if test="strategyCategory != null">strategy_category,</if>
            <if test="triggerType != null">trigger_type,</if>
            <if test="triggerConfig != null">trigger_config,</if>
            <if test="strategyState != null">strategy_state,</if>
            <if test="priority != null">priority,</if>
            <if test="execMode != null">exec_mode,</if>
            <if test="timeout != null">timeout,</if>
            <if test="retryTimes != null">retry_times,</if>
            <if test="version != null">version,</if>
            <if test="createBy != null">create_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="strategyCode != null and strategyCode != ''">#{strategyCode},</if>
            <if test="strategyName != null and strategyName != ''">#{strategyName},</if>
            <if test="strategyDesc != null">#{strategyDesc},</if>
            <if test="sceneType != null">#{sceneType},</if>
            <if test="strategyCategory != null">#{strategyCategory},</if>
            <if test="triggerType != null">#{triggerType},</if>
            <if test="triggerConfig != null">#{triggerConfig},</if>
            <if test="strategyState != null">#{strategyState},</if>
            <if test="priority != null">#{priority},</if>
            <if test="execMode != null">#{execMode},</if>
            <if test="timeout != null">#{timeout},</if>
            <if test="retryTimes != null">#{retryTimes},</if>
            <if test="version != null">#{version},</if>
            <if test="createBy != null">#{createBy},</if>
        </trim>
    </insert>

    <update id="updateStrategy" parameterType="com.ruoyi.ems.domain.OpEnergyStrategy">
        update adm_op_energy_strategy
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="strategyName != null and strategyName != ''">strategy_name = #{strategyName},</if>
            <if test="strategyDesc != null">strategy_desc = #{strategyDesc},</if>
            <if test="sceneType != null"> scene_type = #{sceneType},</if>
            <if test="strategyCategory != null">strategy_category = #{strategyCategory},</if>
            <if test="triggerType != null">trigger_type = #{triggerType},</if>
            <if test="triggerConfig != null">trigger_config = #{triggerConfig},</if>
            <if test="strategyState != null">strategy_state = #{strategyState},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="execMode != null">exec_mode = #{execMode},</if>
            <if test="timeout != null">timeout = #{timeout},</if>
            <if test="retryTimes != null">retry_times = #{retryTimes},</if>
            <if test="lastExecTime != null">last_exec_time = #{lastExecTime},</if>
            <if test="lastExecResult != null">last_exec_result = #{lastExecResult},</if>
            <if test="execCount != null">exec_count = #{execCount},</if>
            <if test="successCount != null">success_count = #{successCount},</if>
            <if test="failCount != null">fail_count = #{failCount},</if>
            <if test="version != null">version = #{version},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStrategyById" parameterType="Long">
        delete from adm_op_energy_strategy where id = #{id}
    </delete>

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

    <!-- 按场景类型统计数量（带区块过滤） -->
    <select id="selectSceneTypeCountByArea" resultType="java.util.Map">
        SELECT
        IFNULL(scene_type, 'UNKNOWN') AS sceneType,
        COUNT(*) AS count
        FROM adm_op_energy_strategy
        <where>
            <if test="areaCode != null and areaCode != ''">
                AND area_code = #{areaCode}
            </if>
        </where>
        GROUP BY scene_type
    </select>
</mapper>
