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

    <resultMap type="com.ruoyi.ems.domain.InspectionPlan" id="InspectionPlanResult">
        <id property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="planCode" column="plan_code"/>
        <result property="planName" column="plan_name"/>
        <result property="planType" column="plan_type"/>
        <result property="planStatus" column="plan_status"/>
        <result property="targetType" column="target_type"/>
        <result property="targetCodes" column="target_codes"/>
        <result property="targetNames" column="target_names"/>
        <result property="cronExpression" column="cron_expression"/>
        <!-- 新增调度相关字段映射 -->
        <result property="scheduleEnabled" column="schedule_enabled"/>
        <result property="lastExecTime" column="last_exec_time"/>
        <result property="nextExecTime" column="next_exec_time"/>
        <result property="execCount" column="exec_count"/>
        <!-- 结束新增 -->
        <result property="planTime" column="plan_time"/>
        <result property="executor" column="executor"/>
        <result property="description" column="description"/>
        <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="selectVo">
        SELECT p.id, p.area_code, a.area_name, p.plan_code, p.plan_name, p.plan_type,
               p.plan_status, p.target_type, p.target_codes, p.target_names,
               p.cron_expression,
               p.schedule_enabled, p.last_exec_time, p.next_exec_time, p.exec_count,
               p.plan_time, p.executor, p.description,
               p.create_by, p.create_time, p.update_by, p.update_time
        FROM adm_op_inspection_plan p
                 LEFT JOIN adm_area a ON p.area_code = a.area_code
    </sql>

    <select id="selectById" parameterType="Long" resultMap="InspectionPlanResult">
        <include refid="selectVo"/>
        WHERE p.id = #{id}
    </select>

    <select id="selectByPlanCode" parameterType="String" resultMap="InspectionPlanResult">
        <include refid="selectVo"/>
        WHERE p.plan_code = #{planCode}
    </select>

    <select id="selectList" parameterType="com.ruoyi.ems.domain.InspectionPlan" resultMap="InspectionPlanResult">
        <include refid="selectVo"/>
        <where>
            <if test="areaCode != null and areaCode != ''">AND p.area_code = #{areaCode}</if>
            <if test="planCode != null and planCode != ''">AND p.plan_code = #{planCode}</if>
            <if test="planName != null and planName != ''">AND p.plan_name LIKE CONCAT('%', #{planName}, '%')</if>
            <if test="planType != null">AND p.plan_type = #{planType}</if>
            <if test="planStatus != null">AND p.plan_status = #{planStatus}</if>
            <if test="targetType != null">AND p.target_type = #{targetType}</if>
            <if test="executor != null and executor != ''">AND p.executor LIKE CONCAT('%', #{executor}, '%')</if>
            <if test="scheduleEnabled != null">AND p.schedule_enabled = #{scheduleEnabled}</if>
        </where>
        ORDER BY p.create_time DESC
    </select>

    <!-- 查询已启用调度的自动巡检计划（供调度器启动时加载） -->
    <select id="selectEnabledSchedulePlans" resultMap="InspectionPlanResult">
        <include refid="selectVo"/>
        WHERE p.plan_type = 2
        AND p.schedule_enabled = 1
        AND p.cron_expression IS NOT NULL
        AND p.cron_expression != ''
    </select>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.InspectionPlan" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO adm_op_inspection_plan (
            area_code, plan_code, plan_name, plan_type, plan_status, target_type,
            target_codes, target_names, cron_expression, schedule_enabled,
            plan_time, executor, description, create_by, create_time
        ) VALUES (
                     #{areaCode}, #{planCode}, #{planName}, #{planType}, #{planStatus}, #{targetType},
                     #{targetCodes}, #{targetNames}, #{cronExpression}, #{scheduleEnabled},
                     #{planTime}, #{executor}, #{description}, #{createBy}, NOW()
                 )
    </insert>

    <update id="update" parameterType="com.ruoyi.ems.domain.InspectionPlan">
        UPDATE adm_op_inspection_plan
        <set>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="planName != null and planName != ''">plan_name = #{planName},</if>
            <if test="planType != null">plan_type = #{planType},</if>
            <if test="planStatus != null">plan_status = #{planStatus},</if>
            <if test="targetType != null">target_type = #{targetType},</if>
            <if test="targetCodes != null">target_codes = #{targetCodes},</if>
            <if test="targetNames != null">target_names = #{targetNames},</if>
            <if test="cronExpression != null">cron_expression = #{cronExpression},</if>
            <if test="scheduleEnabled != null">schedule_enabled = #{scheduleEnabled},</if>
            <if test="lastExecTime != null">last_exec_time = #{lastExecTime},</if>
            <if test="nextExecTime != null">next_exec_time = #{nextExecTime},</if>
            <if test="execCount != null">exec_count = #{execCount},</if>
            <if test="planTime != null">plan_time = #{planTime},</if>
            <if test="executor != null">executor = #{executor},</if>
            <if test="description != null">description = #{description},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateStatus">
        UPDATE adm_op_inspection_plan SET plan_status = #{status}, update_time = NOW()
        WHERE plan_code = #{planCode}
    </update>

    <!-- 更新调度执行信息 -->
    <update id="updateExecInfo">
        UPDATE adm_op_inspection_plan
        SET last_exec_time = #{lastExecTime},
            next_exec_time = #{nextExecTime},
            exec_count = IFNULL(exec_count, 0) + 1,
            update_time = NOW()
        WHERE plan_code = #{planCode}
    </update>

    <!-- 更新调度启用状态 -->
    <update id="updateScheduleEnabled">
        UPDATE adm_op_inspection_plan
        SET schedule_enabled = #{scheduleEnabled},
            update_time = NOW()
        WHERE plan_code = #{planCode}
    </update>

    <delete id="deleteById" parameterType="Long">
        DELETE FROM adm_op_inspection_plan WHERE id = #{id}
    </delete>

    <delete id="deleteByIds" parameterType="Long">
        DELETE FROM adm_op_inspection_plan WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>