<?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.malk.tonglibo.Mapper.MachineDataMapper">

    <!-- 查询每个 paramId 最新一条数据 -->
    <select id="selectLatestRuntimeByParamId" resultType="com.malk.tonglibo.entity.MachineData">
        SELECT
            paramId,
            machineNo,
            jqyxsh,
            zcn,
            timeStr,
            updateTime
        FROM (
                 SELECT
                     paramId,
                     machineNo,
                     zcn,
                     timeStr,
                     jqyxsh,
                     time AS updateTime,
                     ROW_NUMBER() OVER (PARTITION BY machineNo ORDER BY time DESC) AS rn
                 FROM T_TLB_MACHINEDATA
             ) t
        WHERE rn = 1
        ORDER BY updateTime DESC
    </select>

    <!-- 支持条件过滤的版本 -->
    <select id="selectLatestRuntimeByParamIdWithFilter" resultType="com.malk.tonglibo.entity.MachineData">
        SELECT
        t1.paramId,
        t1.machineNo,
        t1.jqyxsh,
        t1.time AS updateTime
        FROM
        T_TLB_MACHINEDATA t1
        WHERE
        t1.time = (
        SELECT MAX(t2.time)
        FROM T_TLB_MACHINEDATA t2
        WHERE t2.paramId = t1.paramId
        )
        <!-- 动态条件 -->
        <if test="paramId != null and paramId != ''">
            AND t1.paramId = #{paramId}
        </if>
        <if test="machineNo != null and machineNo != ''">
            AND t1.machineNo = #{machineNo}
        </if>
        ORDER BY t1.time DESC
    </select>

    <select id="selectDailyZcnAtEight" resultType="com.malk.tonglibo.entity.MachineData">
        SELECT
            date,
            machineNo,
            zcn,
            timeStr
        FROM (
            SELECT
            DATE(time) AS date,
            machineNo AS machineNo,
            zcn,
            timeStr,
            ROW_NUMBER() OVER (PARTITION BY DATE(time), machineNo ORDER BY time ASC) AS rn
            FROM T_TLB_MACHINEDATA
            WHERE HOUR(time) = 8
            AND zcn IS NOT NULL
            AND TRIM(zcn) != ''
            ) t
        WHERE rn = 1
        ORDER BY date, machineNo
    </select>

    <select id="selectDailyShiftOutput" resultType="map">
        SELECT
        CASE
        WHEN HOUR(time) < 8 THEN DATE_SUB(DATE(time), INTERVAL 1 DAY)
        ELSE DATE(time)
        END AS date,

        CASE
        WHEN HOUR(time) >= 8 AND HOUR(time) < 16 THEN '早班'
        WHEN HOUR(time) >= 16 THEN '中班'
        ELSE '晚班'
        END AS shift,

        SUM(CAST(zcn AS DECIMAL(10,2))) AS output

        FROM T_TLB_MACHINEDATA
        WHERE zcn REGEXP '^[0-9]+(\\.[0-9]+)?$'
        AND time >= #{startDate}
        AND time &lt; #{endDate}

        GROUP BY
        CASE WHEN HOUR(time) < 8 THEN DATE_SUB(DATE(time), INTERVAL 1 DAY) ELSE DATE(time) END,
        CASE
        WHEN HOUR(time) >= 8 AND HOUR(time) < 16 THEN '早班'
        WHEN HOUR(time) >= 16 THEN '中班'
        ELSE '晚班'
        END

        ORDER BY date, FIELD(shift, '早班', '中班', '晚班')
    </select>

</mapper>