<?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="cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->


    <select id="lineDevicePage"
            resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineDeviceRespVO">

        select
        mo.id,
        iod.id as deviceId,
        mo.code as lineNode,
        mo.name as lineName,
        iod.device_code as deviceCode,
        IFNULL(mo.device_name, iod.device_name)  as deviceName,
        iod.status,
        CASE iod.status
            WHEN '1' THEN '在线'
            WHEN '2' THEN '离线'
            ELSE iod.status
        END as statusName
        from mes_organization mo
        left join iot_device iod on mo.machine_id = iod.id
        where mo.deleted = 0
        and mo.machine_id  is not  null
        and iod.id is not null
        and iod.deleted = 0
        and mo.deleted = 0
        <if test="pageReqVO.lineNode != null and pageReqVO.lineNode != ''">
            and mo.code like concat(concat('%', #{pageReqVO.lineNode}), '%')
        </if>
        <if test="pageReqVO.lineName != null and pageReqVO.lineName != ''">
            and mo.name like concat(concat('%', #{pageReqVO.lineName}), '%')
        </if>
        <!-- 新增ids过滤条件 -->
        <if test="pageReqVO.ids != null and pageReqVO.ids != ''">
            and FIND_IN_SET(mo.id, #{pageReqVO.ids})
        </if>
        <if test="pageReqVO.deviceCode != null and pageReqVO.deviceCode != ''">
            and iod.device_code like concat(concat('%', #{pageReqVO.deviceCode}), '%')
        </if>

        <if test="pageReqVO.deviceName != null and pageReqVO.deviceName != ''">
            and iod.device_name like concat(concat('%', #{pageReqVO.deviceName}), '%')
        </if>

        <if test="pageReqVO.status != null and pageReqVO.status != ''">
            and iod.status like concat(concat('%', #{pageReqVO.status}), '%')
        </if>

        order by mo.id desc
    </select>

    <select id="lineDeviceLedgerPage" resultType="java.lang.String">
        SELECT mo.workshop
        FROM mes_device_ledger mo
        WHERE mo.deleted = 0
        and mo.dv_id = #{id}
        LIMIT 1
    </select>

    <select id="deviceLedgerList" resultType="java.lang.Long">
        SELECT mo.dv_id
        FROM mes_device_ledger mo
        WHERE mo.deleted = 0
        AND mo.dv_id IS NOT NULL
        AND mo.dv_id != 0
    </select>


    <select id="getDeviceOperationalStatus"
            resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceOperationStatusRespVO">
        SELECT
        -- 设备总数
        COUNT(DISTINCT ide.id) AS totalDevices,

        -- 各状态设备数量
        SUM(CASE WHEN latest_status.rule = '1' THEN 1 ELSE 0 END) AS runningCount,
        SUM(CASE WHEN latest_status.rule = '2' THEN 1 ELSE 0 END) AS standbyCount,
        SUM(CASE WHEN latest_status.rule = '3' THEN 1 ELSE 0 END) AS faultCount,
        SUM(CASE WHEN latest_status.rule = '4' THEN 1 ELSE 0 END) AS warningCount

        FROM besure.iot_device ide
        LEFT JOIN (
        -- 获取每个设备的最新状态
        SELECT
        device_id,
        rule,
        ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY create_time DESC) AS rn
        FROM besure.iot_device_operation_record
        WHERE rule IN ('1', '2', '3', '4')
        AND deleted = 0
        ) latest_status ON ide.id = latest_status.device_id AND latest_status.rn = 1
        WHERE ide.deleted = 0

    </select>
    <select id="lineDeviceList"
            resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineDeviceRespVO">
        select
        mo.id,
        iod.id as deviceId,
        mo.code as lineNode,
        mo.name as lineName,
        iod.device_code as deviceCode,
        iod.device_name as deviceName,
        iod.status
        from mes_organization mo
        left join iot_device iod on mo.machine_id = iod.id
        where mo.deleted = 0
        and mo.machine_id  is not  null
        <if test="pageReqVO.lineNode != null and pageReqVO.lineNode != ''">
            and mo.code like concat(concat('%', #{pageReqVO.lineNode}), '%')
        </if>
        <if test="pageReqVO.lineName != null and pageReqVO.lineName != ''">
            and mo.name like concat(concat('%', #{pageReqVO.lineName}), '%')
        </if>
        <!-- 新增ids过滤条件 -->
        <if test="pageReqVO.ids != null and pageReqVO.ids != ''">
            and FIND_IN_SET(mo.id, #{pageReqVO.ids})
        </if>
        <if test="pageReqVO.deviceCode != null and pageReqVO.deviceCode != ''">
            and iod.device_code like concat(concat('%', #{pageReqVO.deviceCode}), '%')
        </if>

        <if test="pageReqVO.deviceName != null and pageReqVO.deviceName != ''">
            and iod.device_name like concat(concat('%', #{pageReqVO.deviceName}), '%')
        </if>

        <if test="pageReqVO.status != null and pageReqVO.status != ''">
            and iod.status like concat(concat('%', #{pageReqVO.status}), '%')
        </if>

        order by mo.id desc
    </select>
    <select id="selectIsReference" resultType="java.lang.Integer">
        select
        count(1)
        from mes_organization
        where machine_id = #{deviceId}
        and deleted = 0
    </select>

    <select id="selectWorkshopBatch" resultType="map">
        SELECT deviceId, workshop
        FROM (
        SELECT dv_id AS deviceId,
        workshop,
        ROW_NUMBER() OVER (PARTITION BY dv_id ORDER BY id DESC) AS rn
        FROM mes_device_ledger
        WHERE deleted = 0
        AND dv_id IN
        <foreach collection="deviceIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        ) t
        WHERE rn = 1
    </select>

</mapper>