<?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.yfd.platform.modules.basedata.mapper.SubstationDeviceMapper">

    <select id="getDeviceByCode" resultType="java.lang.String">
        SELECT device_id FROM iis_substation_device WHERE patroldevice_json &lt;&gt; '' AND JSON_UNQUOTE(JSON_EXTRACT(patroldevice_json, '$.device_code')) = #{patrolDeviceCode}
    </select>
    <select id="getDeviceByJson" resultType="java.util.Map">
        SELECT *,REPLACE(json_extract(patroldevice_json, "$[0].patroldevice_pos"), '"', '') patroldevice_pos FROM iis_substation_device WHERE patroldevice_json !='' AND json_extract(patroldevice_json, "$[0].patroldevice_code") = #{internationalId} AND recognition_type_list ='4'
    </select>
    <select id="selectByIds" resultType="java.util.Map">
        select * from iis_substation_device where device_id in
        <foreach collection="ids.split(',')" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY FIELD(device_id,
        <foreach collection="ids.split(',')" item="id"  separator=",">
            #{id}
        </foreach>
        )
    </select>
    <select id="selectByIdList" resultType="com.yfd.platform.modules.basedata.domain.SubstationDevice">
        select * from iis_substation_device where device_id in
        <foreach collection="ids.split(',')" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        ORDER BY FIELD(device_id,
        <foreach collection="ids.split(',')" item="id"  separator=",">
            #{id}
        </foreach>
        )
    </select>

    <select id="selectNextUavDeviceCode" resultType="java.lang.String">
        SELECT (MAX(CAST(SUBSTRING(device_code, 4) AS UNSIGNED))+1) device_code FROM `iis_substation_device` WHERE device_code LIKE 'DJ_%'
    </select>
    <select id="getRobotCodeByDeviceId" resultType="java.util.Map">
        SELECT
        device_code,
        GROUP_CONCAT(device_id SEPARATOR ',') AS device_ids  -- 将device_id用逗号合并
        FROM (
        SELECT COALESCE
        (
        NULLIF( JSON_UNQUOTE( JSON_EXTRACT( video_pos, '$[0].device_code' )), '' ),
        NULLIF( JSON_UNQUOTE( JSON_EXTRACT( video_pos, '$[0].robot_code' )), '' ),
        NULLIF( JSON_UNQUOTE( JSON_EXTRACT( video_pos, '$[0].uav_code' )), '' )
        ) AS device_code,device_id
        FROM
        iis_substation_device
        WHERE
        device_id IN
        <foreach collection ='deviceIds' item='id' open ='(' separator=',' close=')' >
            #{id}
        </foreach>
        AND ( JSON_EXTRACT( video_pos, '$[0].device_code' ) &lt;&gt; '' OR JSON_EXTRACT( video_pos, '$[0].robot_code' ) &lt;&gt; '' OR JSON_EXTRACT( video_pos, '$[0].uav_code' ) &lt;&gt; '' )
        ) AS subquery  -- 将原查询作为子查询
        GROUP BY device_code  -- 按device_code分组
        ORDER BY device_code DESC;  -- 按device_code降序排列
    </select>
</mapper>
