吉安感知网项目-后端
linwei
2026-04-02 3dcef55348087d16bb6d2c5f604f3cfa52425fb8
opt: sql改造其他模块改造9
2 files modified
142 ■■■■ changed files
drone-service/drone-fw/src/main/java/org/sxkj/fw/area/mapper/FwAreaDivideMapper.xml 111 ●●●● patch | view | raw | blame | history
drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml 31 ●●●●● patch | view | raw | blame | history
drone-service/drone-fw/src/main/java/org/sxkj/fw/area/mapper/FwAreaDivideMapper.xml
@@ -457,73 +457,76 @@
        from ja_fw_area_divide ad
        left join ja_fw_police_station ps on ps.id::VARCHAR = ad.police_station_id::VARCHAR and ps.is_deleted = 0
        inner join (
                SELECT
                    d.id
                FROM
                    ja_fw_device d
                WHERE
                    d.is_deleted = 0
                    AND d.is_enabled = 1
                    AND d.final_outbound_area IS NOT NULL
                <if test="regionCode != null and regionCode != ''">
                    and ( d.final_outbound_area_code like concat(#{regionCode},'%')
                    <if test="currentDeptId != null and currentDeptId != '' ">
                        or d.id in (
                        SELECT device_id FROM ja_fw_device_per_share
                        WHERE loan_to_dept_id = #{currentDeptId}
                        AND is_deleted = 0
                        )
                    </if>
                    )
                </if>
                GROUP BY
                    d.id
        ) dd on dd.id = ANY(string_to_array(replace(ad.device_ids, ' ', ''), ',')::bigint[])
        SELECT
        d.id
        FROM
        ja_fw_device d
        WHERE
        d.is_deleted = 0
        AND d.is_enabled = 1
        AND d.final_outbound_area IS NOT NULL
        <if test="regionCode != null and regionCode != ''">
            and ( d.final_outbound_area_code like concat(#{regionCode},'%')
            <if test="currentDeptId != null and currentDeptId != '' ">
                or d.id in (
                SELECT device_id FROM ja_fw_device_per_share
                /* 修改点1:参数强制转为 bigint 匹配数据库字段 */
                WHERE loan_to_dept_id = #{currentDeptId}::bigint
                AND is_deleted = 0
                )
            </if>
            )
        </if>
        GROUP BY
        d.id
        /* 修改点2:dd.id 是 bigint,转 VARCHAR 匹配文本数组,去掉不稳定的 ::bigint[] */
        ) dd on dd.id::VARCHAR = ANY(string_to_array(replace(ad.device_ids, ' ', ''), ','))
        <where>
            ad.is_deleted = 0
            <if test="isSetSceneManage != null and isSetSceneManage == 1">
                and exists (
                select 1
                from ja_fw_defense_scene ds
                join ja_fw_defense_scene_manage dsm on dsm.defense_scene_id::VARCHAR = ds.id::VARCHAR and dsm.is_deleted = 0
                where ds.is_deleted = 0
                and ds.area_divide_ids is not null
                and ds.area_divide_ids != ''
                and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
                from ja_fw_defense_scene ds_sub
                join ja_fw_defense_scene_manage dsm_sub on dsm_sub.defense_scene_id::VARCHAR = ds_sub.id::VARCHAR and dsm_sub.is_deleted = 0
                where ds_sub.is_deleted = 0
                and ds_sub.area_divide_ids is not null
                and ds_sub.area_divide_ids != ''
                /* 修改点3:ad.id 转 VARCHAR 匹配 ANY */
                and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub.area_divide_ids, ' ', ''), ','))
                )
            </if>
            <if test="isSetSceneManage != null and isSetSceneManage == 1 and flyTime != null">
                and exists (
                select 1
                from ja_fw_defense_scene ds
                join ja_fw_defense_scene_manage dsm on dsm.defense_scene_id::VARCHAR = ds.id::VARCHAR and dsm.is_deleted = 0
                where ds.is_deleted = 0
                and ds.area_divide_ids is not null
                and ds.area_divide_ids != ''
                and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
                and (dsm.effective_date_start is null or dsm.effective_date_start &lt;= #{flyTime})
                and (dsm.effective_date_end is null or dsm.effective_date_end &gt;= #{flyTime})
                from ja_fw_defense_scene ds_sub2
                join ja_fw_defense_scene_manage dsm_sub2 on dsm_sub2.defense_scene_id::VARCHAR = ds_sub2.id::VARCHAR and dsm_sub2.is_deleted = 0
                where ds_sub2.is_deleted = 0
                and ds_sub2.area_divide_ids is not null
                and ds_sub2.area_divide_ids != ''
                and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub2.area_divide_ids, ' ', ''), ','))
                and (dsm_sub2.effective_date_start is null or dsm_sub2.effective_date_start &lt;= #{flyTime})
                and (dsm_sub2.effective_date_end is null or dsm_sub2.effective_date_end &gt;= #{flyTime})
                )
            </if>
            <if test="filterSelected != null and filterSelected == 1">
                and (
                not exists (
                select 1
                from ja_fw_defense_scene ds
                where ds.is_deleted = 0
                and ds.area_divide_ids is not null
                and ds.area_divide_ids != ''
                and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
                from ja_fw_defense_scene ds_sub3
                where ds_sub3.is_deleted = 0
                and ds_sub3.area_divide_ids is not null
                and ds_sub3.area_divide_ids != ''
                and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub3.area_divide_ids, ' ', ''), ','))
                )
                <if test="sceneId != null">
                    or exists (
                    select 1
                    from ja_fw_defense_scene ds
                    where ds.is_deleted = 0
                    and ds.id = #{sceneId}
                    and ds.area_divide_ids is not null
                    and ds.area_divide_ids != ''
                    and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
                    from ja_fw_defense_scene ds_sub4
                    where ds_sub4.is_deleted = 0
                    and ds_sub4.id = #{sceneId}::bigint
                    and ds_sub4.area_divide_ids is not null
                    and ds_sub4.area_divide_ids != ''
                    and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub4.area_divide_ids, ' ', ''), ','))
                    )
                </if>
                )
@@ -531,18 +534,18 @@
            <if test="sceneId != null and (filterSelected == null or filterSelected != 1)">
                and exists (
                select 1
                from ja_fw_defense_scene ds
                where ds.is_deleted = 0
                and ds.id = #{sceneId}
                and ds.area_divide_ids is not null
                and ds.area_divide_ids != ''
                and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
                from ja_fw_defense_scene ds_sub5
                where ds_sub5.is_deleted = 0
                and ds_sub5.id = #{sceneId}::bigint
                and ds_sub5.area_divide_ids is not null
                and ds_sub5.area_divide_ids != ''
                and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub5.area_divide_ids, ' ', ''), ','))
                )
            </if>
            <if test="areaTypeKeyList != null and areaTypeKeyList.size > 0">
                and (
                <foreach collection="areaTypeKeyList" item="key" separator=" or ">
                    ARRAY_POSITION(string_to_array(ad.area_type_keys, ','), #{key}) IS NOT NULL
                    ARRAY_POSITION(string_to_array(ad.area_type_keys, ','), #{key}::VARCHAR) IS NOT NULL
                </foreach>
                )
            </if>
drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml
@@ -763,15 +763,16 @@
    <select id="getDeviceStatistics" resultType="org.sxkj.fw.cockpit.vo.DeviceStatisticsVO">
        SELECT
            d.final_outbound_area AS type,
            COUNT(*) AS count
        d.final_outbound_area AS type,
        COUNT(*) AS count
        FROM
            ja_fw_device d
        ja_fw_device d
        LEFT JOIN ja_fw_area_divide ad
        /* 修改点1:d.id 是数字,匹配文本数组需转 VARCHAR */
        on d.id::VARCHAR = ANY(string_to_array(ad.device_ids, ','))
        AND ad.is_deleted = 0
        WHERE
            d.is_deleted = 0
        d.is_deleted = 0
        AND d.is_enabled = 1
        AND d.final_outbound_area IS NOT NULL
        <if test="regionCode != null and regionCode != ''">
@@ -780,7 +781,9 @@
                or d.id IN (
                SELECT device_id::bigint
                FROM ja_fw_device_per_share
                WHERE loan_to_dept_id = #{currentDeptId}
                WHERE
                /* 修改点2:强制显式转换参数,防止 bigint = character varying */
                loan_to_dept_id = #{currentDeptId}::bigint
                AND is_deleted = 0
                )
            </if>
@@ -789,14 +792,16 @@
        <if test="flyTime != null">
            and exists (
            select 1
            from ja_fw_defense_scene ds
            join ja_fw_defense_scene_manage dsm on dsm.defense_scene_id::VARCHAR = ds.id::VARCHAR and dsm.is_deleted = 0
            where ds.is_deleted = 0
            and ds.area_divide_ids is not null
            and ds.area_divide_ids != ''
            and ad.id = ANY(string_to_array(replace(ds.area_divide_ids, ' ', ''), ',')::bigint[])
            and (dsm.effective_date_start is null or dsm.effective_date_start &lt;= #{flyTime}::timestamp)
            and (dsm.effective_date_end is null or dsm.effective_date_end &gt;= #{flyTime}::timestamp)
            from ja_fw_defense_scene ds_sub
            join ja_fw_defense_scene_manage dsm_sub on dsm_sub.defense_scene_id::VARCHAR = ds_sub.id::VARCHAR and dsm_sub.is_deleted = 0
            where ds_sub.is_deleted = 0
            and ds_sub.area_divide_ids is not null
            and ds_sub.area_divide_ids != ''
            /* 修改点3:ad.id 转 VARCHAR 匹配,去掉不稳定的 ::bigint[] */
            and ad.id::VARCHAR = ANY(string_to_array(replace(ds_sub.area_divide_ids, ' ', ''), ','))
            /* 修改点4:日期比对确保类型对等 */
            and (dsm_sub.effective_date_start is null or dsm_sub.effective_date_start &lt;= #{flyTime})
            and (dsm_sub.effective_date_end is null or dsm_sub.effective_date_end &gt;= #{flyTime})
            )
        </if>
        GROUP BY