<?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.EmsEcoDMapper">
    
    <resultMap type="EmsEcoD" id="AdmEmsEcoDResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="date"    column="date"    />
        <result property="elecEcoQuantity"    column="elec_eco_quantity"    />
        <result property="elecEcoCost"    column="elec_eco_cost"    />
        <result property="waterEcoQuantity"    column="water_eco_quantity"    />
        <result property="waterEcoCost"    column="water_eco_cost"    />
    </resultMap>

    <sql id="selectEmsEcoDVo">
        select id, area_code, date, elec_eco_quantity, elec_eco_cost, water_eco_quantity, water_eco_cost from adm_ems_eco_d
    </sql>

    <select id="selectEmsEcoDList" parameterType="EmsEcoD" resultMap="AdmEmsEcoDResult">
        select d.id, area.area_code, area.area_name, d.`date`, d.elec_eco_quantity, d.elec_eco_cost, d.water_eco_quantity,
        water_eco_cost from adm_ems_eco_d d
        inner join adm_area area on area.area_code = d.area_code and area.parent_code = '0'
        <where>
            <if test="areaCode != null  and areaCode != '' and areaCode != '-1'">and d.area_code = #{areaCode}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and d.`date` &gt;= #{startRecTime} and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        order by date desc
    </select>
    
    <select id="selectEmsEcoDById" parameterType="Long" resultMap="AdmEmsEcoDResult">
        <include refid="selectEmsEcoDVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertEmsEcoD" parameterType="EmsEcoD" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_eco_d
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="date != null">date,</if>
            <if test="elecEcoQuantity != null">elec_eco_quantity,</if>
            <if test="elecEcoCost != null">elec_eco_cost,</if>
            <if test="waterEcoQuantity != null">water_eco_quantity,</if>
            <if test="waterEcoCost != null">water_eco_cost,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="date != null">#{date},</if>
            <if test="elecEcoQuantity != null">#{elecEcoQuantity},</if>
            <if test="elecEcoCost != null">#{elecEcoCost},</if>
            <if test="waterEcoQuantity != null">#{waterEcoQuantity},</if>
            <if test="waterEcoCost != null">#{waterEcoCost},</if>
         </trim>
    </insert>

    <update id="updateEmsEcoD" parameterType="EmsEcoD">
        update adm_ems_eco_d
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="date != null">date = #{date},</if>
            <if test="elecEcoQuantity != null">elec_eco_quantity = #{elecEcoQuantity},</if>
            <if test="elecEcoCost != null">elec_eco_cost = #{elecEcoCost},</if>
            <if test="waterEcoQuantity != null">water_eco_quantity = #{waterEcoQuantity},</if>
            <if test="waterEcoCost != null">water_eco_cost = #{waterEcoCost},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsEcoDById" parameterType="Long">
        delete from adm_ems_eco_d where id = #{id}
    </delete>

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

    <delete id="deleteEmsEcoD" parameterType="String">
        delete from adm_ems_eco_d where `date` = #{date} and area_code = #{areaCode}
    </delete>

    <select id="calcEmsEcoDateRange" parameterType="EmsEcoD" resultMap="AdmEmsEcoDResult">
        select sum(
        COALESCE(CAST(elec_eco_quantity AS DECIMAL(10, 2)), 0)) elec_eco_quantity,
        sum(
        COALESCE(CAST(elec_eco_cost AS DECIMAL(10, 2)), 0)) elec_eco_cost,
        sum(
        COALESCE(CAST(water_eco_quantity AS DECIMAL(10, 2)), 0)) water_eco_quantity,
        sum(
        COALESCE(CAST(water_eco_cost AS DECIMAL(10, 2)), 0)) water_eco_cost
        from adm_ems_eco_d d
        <where>
            <if test="date != null and date != ''">DATE like concat(#{date}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and area_code = #{areaCode}
            </if>
        </where>
    </select>
</mapper>