|
|
<?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 iot_device ide
|
|
|
LEFT JOIN (
|
|
|
-- 获取每个设备的最新状态
|
|
|
SELECT
|
|
|
device_id,
|
|
|
rule,
|
|
|
ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY create_time DESC) AS rn
|
|
|
FROM 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>
|
|
|
|
|
|
<select id="selectLineBatch"
|
|
|
resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.LineCodeAndNameRespVO">
|
|
|
|
|
|
SELECT
|
|
|
iotd.id AS deviceId,
|
|
|
mo.code AS lineCode,
|
|
|
mo.name AS lineName
|
|
|
FROM mes_organization mo
|
|
|
LEFT JOIN iot_device iotd ON mo.dv_id = iotd.id
|
|
|
WHERE iotd.id IN
|
|
|
<foreach collection="deviceIds" item="id" open="(" separator="," close=")">
|
|
|
#{id}
|
|
|
</foreach>
|
|
|
AND mo.deleted = 0
|
|
|
</select>
|
|
|
<select id="selectDeviceIdsByLine" resultType="java.lang.Long">
|
|
|
SELECT iotd.id
|
|
|
FROM mes_organization mo
|
|
|
LEFT JOIN iot_device iotd ON mo.dv_id = iotd.id
|
|
|
WHERE mo.deleted = 0
|
|
|
and mo.dv_id is not null
|
|
|
<if test="lineNode != null and lineNode != ''">
|
|
|
AND mo.code LIKE CONCAT('%', #{lineNode}, '%')
|
|
|
</if>
|
|
|
|
|
|
<if test="lineName != null and lineName != ''">
|
|
|
AND mo.name LIKE CONCAT('%', #{lineName}, '%')
|
|
|
</if>
|
|
|
</select>
|
|
|
<select id="getTotalDeviceCount" resultType="java.lang.Integer">
|
|
|
SELECT COUNT(DISTINCT id)
|
|
|
FROM iot_device
|
|
|
WHERE deleted = 0
|
|
|
</select>
|
|
|
|
|
|
<select id="getAllDeviceIds" resultType="java.lang.Long">
|
|
|
SELECT id
|
|
|
FROM iot_device
|
|
|
WHERE deleted = 0
|
|
|
ORDER BY id
|
|
|
</select>
|
|
|
|
|
|
<select id="selectDeviceSelectList"
|
|
|
resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSelectRespVO">
|
|
|
SELECT
|
|
|
d.id,
|
|
|
d.device_code AS deviceCode,
|
|
|
d.device_name AS deviceName,
|
|
|
d.device_name AS displayName,
|
|
|
CASE
|
|
|
WHEN EXISTS (
|
|
|
SELECT 1
|
|
|
FROM mes_organization mo
|
|
|
WHERE mo.deleted = 0
|
|
|
AND mo.dv_id = d.id
|
|
|
) THEN TRUE
|
|
|
ELSE FALSE
|
|
|
END AS selected
|
|
|
FROM iot_device d
|
|
|
WHERE d.deleted = 0
|
|
|
ORDER BY d.id DESC
|
|
|
</select>
|
|
|
<select id="selectByIdWithDeleted"
|
|
|
resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO">
|
|
|
SELECT *
|
|
|
FROM iot_device
|
|
|
WHERE id = #{id}
|
|
|
LIMIT 1
|
|
|
</select>
|
|
|
|
|
|
<select id="selectListIncludeDeletedByIds"
|
|
|
resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO">
|
|
|
SELECT
|
|
|
id,
|
|
|
device_name,
|
|
|
deleted
|
|
|
FROM iot_device
|
|
|
WHERE id IN
|
|
|
<foreach collection="ids" item="id" open="(" separator="," close=")">
|
|
|
#{id}
|
|
|
</foreach>
|
|
|
</select>
|
|
|
|
|
|
<select id="selectScheduledDvIds" resultType="java.lang.Long">
|
|
|
SELECT DISTINCT mo.dv_id
|
|
|
FROM mes_organization mo
|
|
|
INNER JOIN mes_device_ledger mdl ON mdl.id = mo.machine_id
|
|
|
WHERE mo.deleted = 0
|
|
|
AND mo.machine_id IS NOT NULL
|
|
|
AND mo.dv_id IS NOT NULL
|
|
|
AND mdl.deleted = 0
|
|
|
AND mdl.is_scheduled = 1
|
|
|
<if test="requestIds != null and requestIds.size() > 0">
|
|
|
AND mo.dv_id IN
|
|
|
<foreach collection="requestIds" item="id" open="(" separator="," close=")">
|
|
|
#{id}
|
|
|
</foreach>
|
|
|
</if>
|
|
|
</select>
|
|
|
|
|
|
<select id="selectHolidayDays" resultType="java.util.Date">
|
|
|
SELECT the_day
|
|
|
FROM mes_cal_holiday
|
|
|
WHERE deleted = 0
|
|
|
AND holiday_type = 'HOLIDAY'
|
|
|
AND the_day >= #{startDay}
|
|
|
AND the_day < #{endDay}
|
|
|
</select>
|
|
|
<select id="getAvailableDevicePage"
|
|
|
resultType="cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSelectRespVO">
|
|
|
SELECT
|
|
|
d.id,
|
|
|
d.device_code AS deviceCode,
|
|
|
d.device_name AS deviceName,
|
|
|
d.device_name AS displayName,
|
|
|
CASE
|
|
|
WHEN EXISTS (
|
|
|
SELECT 1
|
|
|
FROM mes_organization mo
|
|
|
WHERE mo.deleted = 0
|
|
|
AND mo.dv_id = d.id
|
|
|
) THEN TRUE
|
|
|
ELSE FALSE
|
|
|
END AS selected
|
|
|
FROM iot_device d
|
|
|
WHERE d.deleted = 0
|
|
|
<if test="param.deviceCode != null and param.deviceCode != ''">
|
|
|
AND d.device_code LIKE CONCAT('%', #{param.deviceCode}, '%')
|
|
|
</if>
|
|
|
|
|
|
<if test="param.deviceName != null and param.deviceName != ''">
|
|
|
AND d.device_name LIKE CONCAT('%', #{param.deviceName}, '%')
|
|
|
</if>
|
|
|
ORDER BY d.id DESC
|
|
|
|
|
|
</select>
|
|
|
<select id="selectHolidayDaysInRange" resultType="java.util.Date">
|
|
|
SELECT DISTINCT the_day
|
|
|
FROM mes_cal_holiday
|
|
|
WHERE deleted = b'0'
|
|
|
AND the_day >= #{startDay}
|
|
|
AND the_day < #{endDayExclusive}
|
|
|
AND holiday_type = 'HOLIDAY'
|
|
|
</select>
|
|
|
|
|
|
</mapper>
|