<?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.predict.mapper.WtgStopPlantMapper">

    <resultMap type="WtgStopPlant" id="WtgStopPlantResult">
        <result property="windplantNo" column="windplant_no"/>
        <result property="wturNo" column="wtur_no"/>
        <result property="dataTime" column="data_time"/>
        <result property="setTime" column="set_time"/>
        <result property="ratePower" column="rate_power"/>
        <result property="ifstop" column="ifstop"/>
        <result property="outageType" column="outage_type"/>
        <result property="reasonDowntime" column="reason_downtime"/>
        <result property="overhaulResults" column="overhaul_results"/>
    </resultMap>

    <sql id="selectWtgStopPlantVo">
        select windplant_no, wtur_no, data_time, set_time, rate_power, ifstop, outage_type, reason_downtime, overhaul_results from wtg_stop_plant
    </sql>

    <select id="selectWtgStopPlantList" parameterType="WtgStopPlant" resultMap="WtgStopPlantResult">
        select
        wsp.windplant_no,
        wsp.wtur_no,
        wsp.data_time,
        wsp.set_time,
        wsp.rate_power,
        wsp.ifstop,
        wsp.outage_type,
        wsp.reason_downtime,
        wsp.overhaul_results,
        w.name as "wturName"
        from
        wtg_stop_plant wsp
        left join wtur w on w.wtur_no = wsp.wtur_no
        <where>
            <if test="windplantNo != null ">and wsp.windplant_no = #{windplantNo}</if>
            <if test="wturNo != null ">and wsp.wtur_no = #{wturNo}</if>
            <if test="dataTime != null ">and wsp.data_time = #{dataTime}</if>
            <if test="setTime != null ">and wsp.set_time = #{setTime}</if>
            <if test="ratePower != null ">and wsp.rate_power = #{ratePower}</if>
            <if test="ifstop != null ">and wsp.ifstop = #{ifstop}</if>
            <if test="outageType != null ">and wsp.outage_type = #{outageType}</if>
            <if test="reasonDowntime != null  and reasonDowntime != ''">and wsp.reason_downtime = #{reasonDowntime}</if>
            <if test="overhaulResults != null  and overhaulResults != ''">and wsp.overhaul_results = #{overhaulResults}</if>
            <if test="params.beginDataTime != null and params.beginDataTime != '' and params.endDataTime != null and params.endDataTime != ''">
                and wsp.data_time between to_date(#{params.beginDataTime},'yyyy-MM-dd HH24:MI:SS') and
                to_date(#{params.endDataTime},'yyyy-MM-dd HH24:MI:SS')
            </if>
        </where>
        order by wsp.set_time desc
    </select>

    <select id="selectWtgStopPlantById" parameterType="WtgStopPlant" resultMap="WtgStopPlantResult">
        <include refid="selectWtgStopPlantVo"/>
        where windplant_no = #{windplantNo} and wtur_no = #{wturNo} and data_time = #{dataTime}
    </select>

    <select id="selectWtgStopPlantByWindplantNo" parameterType="WtgStopPlant" resultMap="WtgStopPlantResult">
        <include refid="selectWtgStopPlantVo"/>
        where windplant_no = #{windplantNo} and ifstop = 1 and data_time >= to_date(to_char(#{dataTime},'yyyy-MM-dd HH24:MI:SS'),'yyyy-MM-dd HH24:MI:SS')
    </select>

    <select id="selectWtgStopPlantByWturNo" parameterType="WtgStopPlant" resultMap="WtgStopPlantResult">
        <include refid="selectWtgStopPlantVo"/>
        where windplant_no = #{windplantNo} and wtur_no = #{wturNo} and ifstop = 1 and data_time >= to_date(to_char(#{dataTime},'yyyy-MM-dd HH24:MI:SS'),'yyyy-MM-dd HH24:MI:SS')
    </select>

    <insert id="insertWtgStopPlant" parameterType="WtgStopPlant">
        insert into wtg_stop_plant
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="windplantNo != null">windplant_no,</if>
            <if test="wturNo != null">wtur_no,</if>
            <if test="dataTime != null">data_time,</if>
            <if test="setTime != null">set_time,</if>
            <if test="ratePower != null">rate_power,</if>
            <if test="ifstop != null">ifstop,</if>
            <if test="outageType != null">outage_type,</if>
            <if test="reasonDowntime != null">reason_downtime,</if>
            <if test="overhaulResults != null">overhaul_results,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="windplantNo != null">#{windplantNo},</if>
            <if test="wturNo != null">#{wturNo},</if>
            <if test="dataTime != null">#{dataTime},</if>
            <if test="setTime != null">#{setTime},</if>
            <if test="ratePower != null">#{ratePower},</if>
            <if test="ifstop != null">#{ifstop},</if>
            <if test="outageType != null">#{outageType},</if>
            <if test="reasonDowntime != null">#{reasonDowntime},</if>
            <if test="overhaulResults != null">#{overhaulResults},</if>
        </trim>
    </insert>

    <insert id="insertWtgStopPlantList">
        insert into wtg_stop_plant(windplant_no, wtur_no, data_time, set_time, rate_power, ifstop, outage_type, reason_downtime)
        <foreach collection="list" item="item" separator="UNION ALL">
            SELECT
            #{item.windplantNo},
            #{item.wturNo},
            #{item.dataTime},
            #{item.setTime},
            #{item.ratePower},
            #{item.ifstop},
            #{item.outageType},
            #{item.reasonDowntime}
            FROM DUAL
        </foreach>
    </insert>

    <update id="updateWtgStopPlant" parameterType="WtgStopPlant">
        update wtg_stop_plant
        <trim prefix="SET" suffixOverrides=",">
            <if test="wturNo != null">wtur_no = #{wturNo},</if>
            <if test="dataTime != null">data_time = #{dataTime},</if>
            <if test="setTime != null">set_time = #{setTime},</if>
            <if test="ratePower != null">rate_power = #{ratePower},</if>
            <if test="ifstop != null">ifstop = #{ifstop},</if>
            <if test="outageType != null">outage_type = #{outageType},</if>
            <if test="reasonDowntime != null">reason_downtime = #{reasonDowntime},</if>
            <if test="overhaulResults != null">overhaul_results = #{overhaulResults},</if>
        </trim>
        where windplant_no = #{windplantNo}
    </update>

    <delete id="deleteWtgStopPlantById" parameterType="WtgStopPlant">
        delete from wtg_stop_plant where windplant_no = #{windplantNo} and wtur_no = #{wturNo} and data_time = #{dataTime} and ifstop = #{ifstop}
    </delete>

</mapper>