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

    <resultMap type="com.ruoyi.ems.domain.ElecExpendForecast" id="ElecExpendForecastResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="objCode"    column="obj_code"    />
        <result property="objType"    column="obj_type"    />
        <result property="date"    column="date"    />
        <result property="elecUseQuantity"    column="elec_use_quantity"    />
    </resultMap>

    <sql id="selectElecExpendForecastVo">
        select id, area_code, obj_code, obj_type, date, elec_use_quantity from adm_ems_elec_expend_forecast
    </sql>

    <select id="selectElecExpendForecastList" parameterType="com.ruoyi.ems.domain.ElecExpendForecast" resultMap="ElecExpendForecastResult">
        select
        f.id,
        f.area_code,
        f.obj_code,
        f.obj_type,
        date,
        elec_use_quantity,
        COALESCE(a.area_name, fc.facs_name, d.device_name) AS obj_name
        FROM
        adm_ems_elec_expend_forecast f
        LEFT JOIN
        adm_area a ON
        f.obj_code = a.area_code
        AND f.obj_type = 1
        LEFT JOIN
        adm_ems_facs fc ON
        f.obj_code = fc.facs_code
        AND f.obj_type = 2
        LEFT JOIN
        adm_ems_device d ON
        f.obj_code = d.device_code
        AND f.obj_type = 3
        <where>
            <if test="areaCode != null  and areaCode != ''">and f.area_code = #{areaCode}</if>
            <if test="objCode != null  and objCode != ''">and f.obj_code = #{objCode}</if>
            <if test="objType != null ">and f.obj_type = #{objType}</if>
            <if test="date != null ">and f.date = #{date}</if>
            <if test="elecUseQuantity != null ">and elec_use_quantity = #{elecUseQuantity}</if>
        </where>
    </select>

    <select id="selectElecExpendForecastById" parameterType="Long" resultMap="ElecExpendForecastResult">
        <include refid="selectElecExpendForecastVo"/>
        where id = #{id}
    </select>

    <insert id="insertElecExpendForecast" parameterType="com.ruoyi.ems.domain.ElecExpendForecast" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_expend_forecast
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="objCode != null and objCode != ''">obj_code,</if>
            <if test="objType != null">obj_type,</if>
            <if test="date != null">date,</if>
            <if test="elecUseQuantity != null">elec_use_quantity,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="objCode != null and objCode != ''">#{objCode},</if>
            <if test="objType != null">#{objType},</if>
            <if test="date != null">#{date},</if>
            <if test="elecUseQuantity != null">#{elecUseQuantity},</if>
         </trim>
    </insert>

    <update id="updateElecExpendForecast" parameterType="com.ruoyi.ems.domain.ElecExpendForecast">
        update adm_ems_elec_expend_forecast
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="objCode != null and objCode != ''">obj_code = #{objCode},</if>
            <if test="objType != null">obj_type = #{objType},</if>
            <if test="date != null">date = #{date},</if>
            <if test="elecUseQuantity != null">elec_use_quantity = #{elecUseQuantity},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElecExpendForecastById" parameterType="Long">
        delete from adm_ems_elec_expend_forecast where id = #{id}
    </delete>

    <delete id="deleteElecExpendForecastByIds" parameterType="String">
        delete from adm_ems_elec_expend_forecast where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="calcElecExpendForecastDateRange" parameterType="ElecExpendForecast" resultMap="ElecExpendForecastResult">
        select sum(
        COALESCE(CAST(elec_use_quantity AS DECIMAL(10, 2)), 0)) elec_use_quantity,
        `date`
        from adm_ems_elec_expend_forecast d
        <where>
            obj_type = 1
            <if test="startRecTime != null and startRecTime != ''">and DATE >=#{startRecTime}</if>
            <if test="endRecTime != null and endRecTime != ''">and DATE <![CDATA[ <= ]]>#{endRecTime}</if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and obj_code = #{areaCode}
            </if>
        </where>
        group by `date`
    </select>
</mapper>
