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

    <resultMap type="com.ruoyi.ems.domain.CaMeterD" id="CaMeterDResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="date"    column="date"    />
        <result property="caEmissionQuantity"    column="ca_emission_quantity"    />
        <result property="caSinkQuantity"    column="ca_sink_quantity"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
    </resultMap>

    <sql id="selectCaMeterDVo">
        select id, area_code, date, ca_emission_quantity, ca_sink_quantity from adm_ems_ca_meter_d
    </sql>

    <!-- 按日查询（原有逻辑） -->
    <select id="selectCaMeterDList" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select d.id, d.area_code, area.area_name, d.date, d.ca_emission_quantity, d.ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null  and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null  and areaCode != '' and areaCode !='-1'"> and d.area_code = #{areaCode}</if>
            <if test="date != null "> and d.date = #{date}</if>
            <if test="startRecTime != null and startRecTime != '' ">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime !=''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        order by d.date desc, d.area_code
    </select>

    <!-- ===================== 按月聚合查询（分页用） ===================== -->
    <select id="selectCaMeterDListByMonth" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        MIN(d.id) as id,
        d.area_code,
        area.area_name,
        DATE_FORMAT(d.date, '%Y-%m-01') as date,
        YEAR(d.date) as year,
        MONTH(d.date) as month,
        SUM(d.ca_emission_quantity) as ca_emission_quantity,
        SUM(d.ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and d.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != ''">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        group by d.area_code, area.area_name, YEAR(d.date), MONTH(d.date)
        order by YEAR(d.date) desc, MONTH(d.date) desc, d.area_code
    </select>

    <!-- ===================== 按年聚合查询（分页用） ===================== -->
    <select id="selectCaMeterDListByYear" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        MIN(d.id) as id,
        d.area_code,
        area.area_name,
        DATE_FORMAT(d.date, '%Y-01-01') as date,
        YEAR(d.date) as year,
        SUM(d.ca_emission_quantity) as ca_emission_quantity,
        SUM(d.ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and d.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != ''">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        group by d.area_code, area.area_name, YEAR(d.date)
        order by YEAR(d.date) desc, d.area_code
    </select>

    <!-- ===================== 统计汇总查询（不分页） ===================== -->

    <!-- 按日汇总 - 修复ca_sink_quantity别名问题 -->
    <select id="selectSumCaMeterDList" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        MIN(d.id) as id,
        d.area_code,
        area.area_name,
        d.date,
        SUM(d.ca_emission_quantity) as ca_emission_quantity,
        SUM(d.ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and d.area_code = #{areaCode}</if>
            <if test="date != null">and d.date = #{date}</if>
            <if test="startRecTime != null and startRecTime != ''">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        group by d.date, d.area_code, area.area_name
        order by d.date desc
    </select>

    <!-- 按月汇总（统计卡片用） -->
    <select id="selectSumCaMeterDListByMonth" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        MIN(d.id) as id,
        d.area_code,
        area.area_name,
        DATE_FORMAT(d.date, '%Y-%m-01') as date,
        YEAR(d.date) as year,
        MONTH(d.date) as month,
        SUM(d.ca_emission_quantity) as ca_emission_quantity,
        SUM(d.ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and d.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != ''">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        group by d.area_code, area.area_name, YEAR(d.date), MONTH(d.date)
        order by YEAR(d.date) desc, MONTH(d.date) desc
    </select>

    <!-- 按年汇总（统计卡片用） -->
    <select id="selectSumCaMeterDListByYear" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        MIN(d.id) as id,
        d.area_code,
        area.area_name,
        DATE_FORMAT(d.date, '%Y-01-01') as date,
        YEAR(d.date) as year,
        SUM(d.ca_emission_quantity) as ca_emission_quantity,
        SUM(d.ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d d
        inner join adm_area area on area.area_code = d.area_code
        <where>
            <if test="areaName != null and areaName != ''">and area.area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and d.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != ''">
                and d.`date` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                and d.`date` &lt;= #{endRecTime}
            </if>
        </where>
        group by d.area_code, area.area_name, YEAR(d.date)
        order by YEAR(d.date) desc
    </select>

    <!-- 平均值查询 -->
    <select id="selectAvgCaMeterDList" parameterType="com.ruoyi.ems.domain.CaMeterD" resultMap="CaMeterDResult">
        select
        area_code,
        AVG(ca_emission_quantity) as ca_emission_quantity,
        AVG(ca_sink_quantity) as ca_sink_quantity
        from adm_ems_ca_meter_d
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and area_code = #{areaCode}</if>
        </where>
        group by area_code
    </select>

    <select id="selectCaMeterDById" parameterType="Long" resultMap="CaMeterDResult">
        <include refid="selectCaMeterDVo"/>
        where id = #{id}
    </select>

    <insert id="insertCaMeterD" parameterType="com.ruoyi.ems.domain.CaMeterD" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_ca_meter_d
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="date != null">date,</if>
            <if test="caEmissionQuantity != null">ca_emission_quantity,</if>
            <if test="caSinkQuantity != null">ca_sink_quantity,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="date != null">#{date},</if>
            <if test="caEmissionQuantity != null">#{caEmissionQuantity},</if>
            <if test="caSinkQuantity != null">#{caSinkQuantity},</if>
        </trim>
    </insert>

    <update id="updateCaMeterD" parameterType="com.ruoyi.ems.domain.CaMeterD">
        update adm_ems_ca_meter_d
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="date != null">date = #{date},</if>
            <if test="caEmissionQuantity != null">ca_emission_quantity = #{caEmissionQuantity},</if>
            <if test="caSinkQuantity != null">ca_sink_quantity = #{caSinkQuantity},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCaMeterDById" parameterType="Long">
        delete from adm_ems_ca_meter_d where id = #{id}
    </delete>

    <delete id="deleteCaMeterDByArea">
        delete from adm_ems_ca_meter_d where area_code = #{areaCode} and `date` = #{date}
    </delete>

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