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

    <resultMap type="com.ruoyi.ems.domain.OpEnergyStrategyExecLog" id="ExecLogResult">
        <result property="id" column="id"/>
        <result property="strategyCode" column="strategy_code"/>
        <result property="strategyName" column="strategy_name"/>
        <result property="execId" column="exec_id"/>
        <result property="triggerType" column="trigger_type"/>
        <result property="triggerSource" column="trigger_source"/>
        <result property="execStatus" column="exec_status"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="duration" column="duration"/>
        <result property="contextData" column="context_data"/>
        <result property="resultData" column="result_data"/>
        <result property="errorMessage" column="error_message"/>
        <result property="execBy" column="exec_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectExecLogVo">
        select
            l.id, l.strategy_code, l.exec_id, l.trigger_type, l.trigger_source,
            l.exec_status, l.start_time, l.end_time, l.duration, l.context_data,
            l.result_data, l.error_message, l.exec_by, l.create_time,
            s.strategy_name
        from adm_op_energy_strategy_exec_log l
                 left join adm_op_energy_strategy s on l.strategy_code = s.strategy_code
    </sql>

    <select id="selectExecLogList" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyExecLog" resultMap="ExecLogResult">
        <include refid="selectExecLogVo"/>
        <where>
            <if test="strategyCode != null and strategyCode != ''">
                and l.strategy_code = #{strategyCode}
            </if>
            <if test="execStatus != null">
                and l.exec_status = #{execStatus}
            </if>
            <if test="triggerType != null and triggerType != ''">
                and l.trigger_type = #{triggerType}
            </if>
            <if test="triggerSource != null and triggerSource != ''">
                and l.trigger_source like concat('%', #{triggerSource}, '%')
            </if>
            <if test="execBy != null and execBy != ''">
                and l.exec_by = #{execBy}
            </if>
            <if test="startTimeBegin != null and startTimeBegin != ''">
                and l.start_time &gt;= #{startTimeBegin}
            </if>
            <if test="startTimeEnd != null and startTimeEnd != ''">
                and l.start_time &lt;= #{startTimeEnd}
            </if>
            <if test="startTime != null">
                and l.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and l.end_time &lt;= #{endTime}
            </if>
        </where>
        order by l.start_time desc
    </select>

    <select id="selectByExecId" parameterType="String" resultMap="ExecLogResult">
        <include refid="selectExecLogVo"/>
        where l.exec_id = #{execId}
    </select>

    <select id="selectByStrategyCode" parameterType="String" resultMap="ExecLogResult">
        <include refid="selectExecLogVo"/>
        where l.strategy_code = #{strategyCode}
        order by l.start_time desc
        limit 100
    </select>

    <insert id="insertExecLog" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyExecLog" useGeneratedKeys="true" keyProperty="id">
        insert into adm_op_energy_strategy_exec_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">strategy_code,</if>
            <if test="execId != null and execId != ''">exec_id,</if>
            <if test="triggerType != null">trigger_type,</if>
            <if test="triggerSource != null">trigger_source,</if>
            <if test="execStatus != null">exec_status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="duration != null">duration,</if>
            <if test="contextData != null">context_data,</if>
            <if test="resultData != null">result_data,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="execBy != null">exec_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">#{strategyCode},</if>
            <if test="execId != null and execId != ''">#{execId},</if>
            <if test="triggerType != null">#{triggerType},</if>
            <if test="triggerSource != null">#{triggerSource},</if>
            <if test="execStatus != null">#{execStatus},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="duration != null">#{duration},</if>
            <if test="contextData != null">#{contextData},</if>
            <if test="resultData != null">#{resultData},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="execBy != null">#{execBy},</if>
        </trim>
    </insert>

    <update id="updateExecLog" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyExecLog">
        update adm_op_energy_strategy_exec_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="execStatus != null">exec_status = #{execStatus},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="resultData != null">result_data = #{resultData},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteExecLog" parameterType="Long">
        delete from adm_op_energy_strategy_exec_log where id = #{id}
    </delete>
</mapper>