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

    <resultMap type="Projectplan" id="ProjectplanResult">
        <result property="id"    column="id"    />
        <result property="projectid"    column="projectid"    />
        <result property="generatingcapacity"    column="generatingcapacity"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
        <result property="windpower"    column="windpower"    />
    </resultMap>

    <sql id="selectProjectplanVo">
        select id, projectid, generatingcapacity, year, month, windpower from projectplan
    </sql>

    <select id="selectProjectplanList" parameterType="Projectplan" resultMap="ProjectplanResult">
        select
        p.*,
        w.name as "windplantName",
        pps.name as "pvStationName"
        from
        projectplan p
        left join windplant w on w.windplant_no = p.windpower
        left join pv_power_station pps on pps.pv_station_no = p.windpower
        <where>
            <if test="id != null  and id != ''"> and p.id = #{id}</if>
            <if test="projectid != null  and projectid != ''"> and p.projectid = #{projectid}</if>
            <if test="generatingcapacity != null  and generatingcapacity != ''"> and p.generatingcapacity = #{generatingcapacity}</if>
            <if test="year != null  and year != ''"> and p.year = #{year}</if>
            <if test="month != null  and month != ''"> and p.month = #{month}</if>
            <if test="windpower != null  and windpower != ''"> and p.windpower = #{windpower}</if>
        </where>
        order by w.windplant_no asc
    </select>

    <select id="selectProjectplanById" parameterType="String" resultMap="ProjectplanResult">
        <include refid="selectProjectplanVo"/>
        where id = #{id}
    </select>

    <select id="selectNewData" resultType="Long">
        SELECT * FROM ( SELECT id FROM projectplan ORDER BY id DESC ) WHERE ROWNUM = 1
    </select>

    <select id="checkProjectplanUnique" resultType="int">
        select count(1) from projectplan where windpower = #{windpower} and projectid = #{projectid} and year = #{year} and month = #{month} and rownum <![CDATA[ <= ]]> 1
    </select>

    <select id="selectProjectplan" parameterType="Projectplan" resultMap="ProjectplanResult">
        <include refid="selectProjectplanVo"/>
        where windpower = #{windplantNo} and year = #{year} and month = #{month}
    </select>

    <select id="selectProjectplanByYear" parameterType="Projectplan" resultMap="ProjectplanResult">
        <include refid="selectProjectplanVo"/>
        where windpower = #{windplantNo} and year = #{year}
    </select>

    <select id="selectProjectplanByProjectNo" parameterType="Projectplan" resultMap="ProjectplanResult">
        <include refid="selectProjectplanVo"/>
        where windpower = #{windplantNo} and projectid = #{projectid} and year = #{year} and month = #{month}
    </select>

    <insert id="insertProjectplan" parameterType="Projectplan">
        insert into projectplan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectid != null">projectid,</if>
            <if test="generatingcapacity != null">generatingcapacity,</if>
            <if test="year != null">year,</if>
            <if test="month != null">month,</if>
            <if test="windpower != null">windpower,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectid != null">#{projectid},</if>
            <if test="generatingcapacity != null">#{generatingcapacity},</if>
            <if test="year != null">#{year},</if>
            <if test="month != null">#{month},</if>
            <if test="windpower != null">#{windpower},</if>
         </trim>
    </insert>

    <update id="updateProjectplan" parameterType="Projectplan">
        update projectplan
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectid != null">projectid = #{projectid},</if>
            <if test="generatingcapacity != null">generatingcapacity = #{generatingcapacity},</if>
            <if test="year != null">year = #{year},</if>
            <if test="month != null">month = #{month},</if>
            <if test="windpower != null">windpower = #{windpower},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProjectplanById" parameterType="String">
        delete from projectplan where id = #{id}
    </delete>

    <delete id="deleteProjectplanByIds" parameterType="String">
        delete from projectplan where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>