<?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.system.mapper.WaterLevelMapper">

    <resultMap type="com.ruoyi.system.domain.WaterLevel" id="WaterLevelMap">
        <result property="waterId" column="water_id" jdbcType="INTEGER"/>
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="waterlevel" column="waterlevel" jdbcType="NUMERIC"/>
        <result property="siteId" column="site_id" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap  type="com.ruoyi.system.dto.WaterLevelDTO" id="WaterLevelDTO">
        <result property="avgWaterlevel" column="avg_waterlevel" jdbcType="NUMERIC"/>
        <result property="time" column="time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="WaterLevelMap">
        select
            water_id, id, waterlevel, site_id, create_time
        from water_level
        where water_id = #{waterId}
    </select>


    <!--查询单个-->
    <select id="queryBySiteId" resultMap="WaterLevelMap">
        select
            water_id, id, waterlevel, site_id, create_time
        from water_level
        where site_id = #{siteId}
        order by create_time desc
        limit 1
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="WaterLevelMap">
        select
            water_id, id, waterlevel, site_id, create_time
        from water_level
        <where>
            <if test="waterId != null">
                and water_id = #{waterId}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="waterlevel != null">
                and waterlevel = #{waterlevel}
            </if>
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>


    <!--查询指定行数据-->
    <select id="queryAllByTime" resultMap="WaterLevelDTO">
        select
            <if test="waterLevelQuery.type == 1 or waterLevelQuery.type == null">
                FROM_UNIXTIME(
                UNIX_TIMESTAMP(create_time) - MOD(UNIX_TIMESTAMP(create_time), (5 * 60))
                ) AS time,
            </if>
            <if test="waterLevelQuery.type == 2">
                FROM_UNIXTIME(
                UNIX_TIMESTAMP(create_time) - MOD(UNIX_TIMESTAMP(create_time), (10 * 60))
                ) AS time,
            </if>
            <if test="waterLevelQuery.type == 3">
                FROM_UNIXTIME(
                UNIX_TIMESTAMP(create_time) - MOD(UNIX_TIMESTAMP(create_time), (30 * 60))
                ) AS time,
            </if>
            <if test="waterLevelQuery.type == 4">
                DATE_FORMAT(create_time, '%Y-%m-%d %H:00:00') AS time,
            </if>
            <if test="waterLevelQuery.type == 5">
                DATE_FORMAT(create_time, '%Y-%m-%d') AS time,
            </if>
            <if test="waterLevelQuery.type == 6">
                DATE_FORMAT(DATE_SUB(create_time, INTERVAL DAYOFWEEK(create_time) - 2 DAY), '%Y-%m-%d') AS time,
            </if>
            <if test="waterLevelQuery.type == 7">
                DATE_FORMAT(create_time, '%Y-%m-01') AS time,
            </if>
        ROUND(AVG(waterlevel),3) AS avg_waterlevel
        from water_level
        where site_id = #{waterLevelQuery.siteId} and create_time between #{waterLevelQuery.startTime} and #{waterLevelQuery.endTime}
        GROUP BY time
        ORDER BY time desc
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from water_level
        <where>
            <if test="waterId != null">
                and water_id = #{waterId}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="waterlevel != null">
                and waterlevel = #{waterlevel}
            </if>
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="waterId" useGeneratedKeys="true">
        insert into water_level(id, waterlevel, site_id, create_time)
        values (#{id}, #{waterlevel}, #{siteId}, #{createTime})
    </insert>

    <insert id="insertBatch" keyProperty="waterId" useGeneratedKeys="true">
        insert into water_level(id, waterlevel, site_id, create_time)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.id}, #{entity.waterlevel}, #{entity.siteId}, #{entity.createTime})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="waterId" useGeneratedKeys="true">
        insert into water_level(id, waterlevel, site_id, create_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id}, #{entity.waterlevel}, #{entity.siteId}, #{entity.createTime})
        </foreach>
        on duplicate key update
id = values(id),
waterlevel = values(waterlevel),
site_id = values(site_id),
create_time = values(create_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update water_level
        <set>
            <if test="id != null and id != ''">
                id = #{id},
            </if>
            <if test="waterlevel != null">
                waterlevel = #{waterlevel},
            </if>
            <if test="siteId != null">
                site_id = #{siteId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        where water_id = #{waterId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from water_level where water_id = #{waterId}
    </delete>

</mapper>

