178 lines
6.5 KiB
XML
178 lines
6.5 KiB
XML
<?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.klp.ems.mapper.EmsEnergyConsumptionMapper">
|
||
|
||
<resultMap type="com.klp.ems.domain.EmsEnergyConsumption" id="EmsEnergyConsumptionResult">
|
||
<result property="energyConsumptionId" column="energy_consumption_id"/>
|
||
<result property="meterId" column="meter_id"/>
|
||
<result property="startReading" column="start_reading"/>
|
||
<result property="endReading" column="end_reading"/>
|
||
<result property="consumption" column="consumption"/>
|
||
<result property="startTime" column="start_time"/>
|
||
<result property="endTime" column="end_time"/>
|
||
<result property="recordedBy" column="recorded_by"/>
|
||
<result property="createBy" column="create_by"/>
|
||
<result property="updateBy" column="update_by"/>
|
||
<result property="createTime" column="create_time"/>
|
||
<result property="updateTime" column="update_time"/>
|
||
<result property="delFlag" column="del_flag"/>
|
||
<result property="remark" column="remark"/>
|
||
</resultMap>
|
||
|
||
<select id="selectDailySummary" resultType="com.klp.ems.domain.vo.SummaryDailyVo">
|
||
SELECT
|
||
DATE(end_time) AS day,
|
||
SUM(consumption) AS totalConsumption
|
||
FROM
|
||
ems_energy_consumption
|
||
WHERE
|
||
DATE(end_time) BETWEEN #{startDate} AND #{endDate}
|
||
GROUP BY
|
||
DATE(end_time)
|
||
ORDER BY
|
||
day
|
||
</select>
|
||
<select id="selectMonthlySummary" resultType="com.klp.ems.domain.vo.SummaryMonthlyVo">
|
||
SELECT
|
||
DATE_FORMAT(end_time, '%Y-%m') AS month,
|
||
SUM(consumption) AS totalConsumption
|
||
FROM
|
||
ems_energy_consumption
|
||
WHERE
|
||
DATE_FORMAT(end_time, '%Y') = #{year}
|
||
GROUP BY
|
||
DATE_FORMAT(end_time, '%Y-%m')
|
||
ORDER BY
|
||
month
|
||
</select>
|
||
|
||
<!-- 过滤版:按设备集合(energyTypeId/locationId会先解析出设备集合) -->
|
||
<select id="selectDailySummaryWithMeters" parameterType="com.klp.ems.domain.bo.TimeRangeWithMetersBo" resultType="com.klp.ems.domain.vo.SummaryDailyVo">
|
||
SELECT
|
||
DATE(end_time) AS day,
|
||
SUM(consumption) AS totalConsumption
|
||
FROM ems_energy_consumption
|
||
WHERE DATE(end_time) BETWEEN #{start} AND #{end}
|
||
<if test="meterIds != null and meterIds.size() > 0">
|
||
AND meter_id IN
|
||
<foreach collection="meterIds" item="id" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
</if>
|
||
GROUP BY DATE(end_time)
|
||
ORDER BY day
|
||
</select>
|
||
|
||
<select id="selectMonthlySummaryWithMeters" parameterType="com.klp.ems.domain.bo.TimeRangeWithMetersBo" resultType="com.klp.ems.domain.vo.SummaryMonthlyVo">
|
||
SELECT
|
||
DATE_FORMAT(end_time, '%Y-%m') AS month,
|
||
SUM(consumption) AS totalConsumption
|
||
FROM ems_energy_consumption
|
||
WHERE DATE_FORMAT(end_time, '%Y') = #{start}
|
||
<if test="meterIds != null and meterIds.size() > 0">
|
||
AND meter_id IN
|
||
<foreach collection="meterIds" item="id" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
</if>
|
||
GROUP BY DATE_FORMAT(end_time, '%Y-%m')
|
||
ORDER BY month
|
||
</select>
|
||
|
||
<select id="sumConsumptionBetween" resultType="java.lang.Double">
|
||
SELECT IFNULL(SUM(consumption), 0)
|
||
FROM ems_energy_consumption
|
||
WHERE end_time >= #{startTime}
|
||
AND end_time <= #{endTime}
|
||
</select>
|
||
|
||
<select id="sumConsumptionBetweenWithMeters" parameterType="com.klp.ems.domain.bo.TimeRangeWithMetersBo" resultType="java.lang.Double">
|
||
SELECT IFNULL(SUM(consumption), 0)
|
||
FROM ems_energy_consumption
|
||
WHERE end_time >= #{start}
|
||
AND end_time <= #{end}
|
||
<if test="meterIds != null and meterIds.size() > 0">
|
||
AND meter_id IN
|
||
<foreach collection="meterIds" item="id" open="(" separator="," close=")">
|
||
#{id}
|
||
</foreach>
|
||
</if>
|
||
</select>
|
||
|
||
<select id="selectLatestBefore" resultMap="EmsEnergyConsumptionResult">
|
||
SELECT *
|
||
FROM ems_energy_consumption
|
||
WHERE meter_id = #{meterId}
|
||
AND end_time <= #{endTime}
|
||
ORDER BY end_time DESC
|
||
LIMIT 1
|
||
</select>
|
||
|
||
<select id="selectOverlapRange" resultMap="EmsEnergyConsumptionResult">
|
||
SELECT *
|
||
FROM ems_energy_consumption
|
||
WHERE meter_id = #{meterId}
|
||
AND end_time >= #{startTime}
|
||
AND start_time <= #{endTime}
|
||
ORDER BY start_time ASC
|
||
</select>
|
||
|
||
<select id="selectCoveringRange" resultMap="EmsEnergyConsumptionResult">
|
||
SELECT *
|
||
FROM ems_energy_consumption
|
||
WHERE meter_id = #{meterId}
|
||
AND start_time <= #{startTime}
|
||
AND end_time >= #{endTime}
|
||
ORDER BY start_time ASC
|
||
LIMIT 1
|
||
</select>
|
||
|
||
<select id="selectLatestTwoReadings" resultMap="EmsEnergyConsumptionResult">
|
||
SELECT *
|
||
FROM ems_energy_consumption
|
||
WHERE meter_id = #{meterId}
|
||
ORDER BY end_time DESC
|
||
LIMIT 2
|
||
</select>
|
||
|
||
<!-- 统计查询 -->
|
||
<select id="getStatistics" parameterType="com.klp.ems.domain.bo.EmsEnergyConsumptionBo" resultType="java.util.Map">
|
||
SELECT
|
||
COUNT(*) AS totalCount,
|
||
IFNULL(SUM(consumption), 0) AS totalConsumption,
|
||
IFNULL(AVG(consumption), 0) AS avgConsumption,
|
||
IFNULL(MAX(consumption), 0) AS maxConsumption,
|
||
IFNULL(MIN(consumption), 0) AS minConsumption
|
||
FROM ems_energy_consumption
|
||
WHERE 1=1
|
||
<if test="meterId != null">
|
||
AND meter_id = #{meterId}
|
||
</if>
|
||
<if test="energyTypeId != null">
|
||
AND meter_id IN (
|
||
SELECT meter_id FROM ems_meter WHERE energy_type_id = #{energyTypeId}
|
||
)
|
||
</if>
|
||
<if test="startTime != null">
|
||
AND start_time >= #{startTime}
|
||
</if>
|
||
<if test="endTime != null">
|
||
AND end_time <= #{endTime}
|
||
</if>
|
||
</select>
|
||
|
||
<!-- 查询最近一次抄表的时间范围 -->
|
||
<select id="selectLatestMeterReadTime" resultType="com.klp.ems.domain.vo.LatestMeterReadTimeVo">
|
||
SELECT
|
||
start_time AS startTime,
|
||
end_time AS endTime
|
||
FROM ems_energy_consumption
|
||
WHERE end_time IS NOT NULL
|
||
ORDER BY end_time DESC
|
||
LIMIT 1
|
||
</select>
|
||
|
||
</mapper>
|