From 51a551a7f8eae7654f973d45cd2fa2952774673d Mon Sep 17 00:00:00 2001
From: linwei <872216696@qq.com>
Date: Fri, 07 Aug 2026 10:56:55 +0800
Subject: [PATCH] perf(task): 优化巡查任务审核性能并添加详细日志记录

---
 drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml |  209 ++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 139 insertions(+), 70 deletions(-)

diff --git a/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml b/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml
index e59b1c8..de3c689 100644
--- a/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml
+++ b/drone-service/drone-fw/src/main/java/org/sxkj/fw/device/mapper/FwDeviceMapper.xml
@@ -34,31 +34,36 @@
         <result column="update_user" property="updateUser"/>
         <result column="update_time" property="updateTime"/>
         <result column="status" property="status"/>
+        <result column="use_date" property="useDate"/>
+        <result column="service_life" property="serviceLife"/>
 <!--        <result column="is_deleted" property="isDeleted"/>-->
     </resultMap>
 
 
     <select id="selectFwDevicePage" resultMap="fwDeviceResultMap">
         select
-            *
+        *
         from
-            ja_fw_device
+        ja_fw_device
         <where>
-                is_deleted = 0
+            is_deleted = 0
+            <if test="param2.trackStatus != null ">
+                and track_status::text = #{param2.trackStatus}::text
+            </if>
             <if test="param2.id != null and param2.id != ''">
-                and id = #{param2.id}
+                and id::text = #{param2.id}::text
             </if>
             <if test="param2.deviceName != null and param2.deviceName != ''">
                 and device_name like concat('%',#{param2.deviceName},'%')
             </if>
             <if test="param2.deviceType != null and param2.deviceType != ''">
-                and device_type = #{param2.deviceType}
+                and device_type::text = #{param2.deviceType}::text
             </if>
             <if test="param2.deviceModel != null and param2.deviceModel != ''">
-                and device_model = #{param2.deviceModel}
+                and device_model::text = #{param2.deviceModel}::text
             </if>
             <if test="param2.deviceSpecification != null and param2.deviceSpecification != ''">
-                and device_specification = #{param2.deviceSpecification}
+                and device_specification::text = #{param2.deviceSpecification}::text
             </if>
             <if test="param2.effectiveRangeKmIsNotNull != null and param2.effectiveRangeKmIsNotNull == 1">
                 and effective_range_km is not null
@@ -68,39 +73,44 @@
                 and (effective_range_km is null or effective_range_km = 0)
             </if>
             <if test="param2.manufacturer != null and param2.manufacturer != ''">
-                and manufacturer = #{param2.manufacturer}
+                and manufacturer::text = #{param2.manufacturer}::text
             </if>
             <if test="param2.source != null and param2.source != ''">
-                and source = #{param2.source}
+                and source::text = #{param2.source}::text
             </if>
             <if test="param2.belongDept != null and param2.belongDept != ''">
-                and belong_dept = #{param2.belongDept}
+                and belong_dept::text = #{param2.belongDept}::text
+            </if>
+            <if test="param2.useDate != null">
+                and use_date = #{param2.useDate}
+            </if>
+            <if test="param2.serviceLife != null">
+                and service_life = #{param2.serviceLife}
             </if>
             <if test="param2.deviceStatusList != null and param2.deviceStatusList != ''">
-                and status in
+                and status::text in
                 <foreach item="item" collection="param2.deviceStatusList" separator="," open="(" close=")" index="">
-                    #{item}
+                    #{item}::text
                 </foreach>
             </if>
             <if test="param2.status != null">
-                and status = #{param2.status}
+                and status::text = #{param2.status}::text
             </if>
             <if test="param2.isTrack != null and param2.isTrack == 1">
-                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id = tmp.device_id::VARCHAR) > 0
+                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id::text = tmp.device_id::text) > 0
             </if>
             <if test="param2.isTrack != null and param2.isTrack == 2">
-                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id = tmp.device_id::VARCHAR) = 0
+                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id::text = tmp.device_id::text) = 0
             </if>
             <if test="param2.deptList != null and param2.deptList.size > 0">
-                and ( create_dept in
+                and ( create_dept::text in
                 <foreach collection="param2.deptList" item="deptId" open="(" separator="," close=")">
-                    #{deptId}
+                    #{deptId}::text
                 </foreach>
-                <!-- 或者设备是共享给当前部门的 -->
                 <if test="param2.currentDeptId != null and param2.currentDeptId != '' ">
-                    or id in (
-                    SELECT device_id FROM ja_fw_device_per_share
-                    WHERE loan_to_dept_id = #{param2.currentDeptId}
+                    or id::text in (
+                    SELECT device_id::text FROM ja_fw_device_per_share
+                    WHERE loan_to_dept_id = #{param2.currentDeptId}::bigint
                     AND is_deleted = 0
                     )
                 </if>
@@ -108,7 +118,7 @@
             </if>
         </where>
         order by
-            create_time desc
+        create_time desc
     </select>
 
     <select id="selectFwDeviceList" resultMap="fwDeviceResultMap">
@@ -188,48 +198,51 @@
             and track_status = 1
             <if test="query.isAreaSelect != null and query.isAreaSelect == 1">
                 and (
-                    <choose>
-                        <when test="query.areaId != null">
-                            exists (
-                                select 1
-                                from ja_fw_area_divide ad
-                                where ad.is_deleted = 0
-                                  and ad.id = #{query.areaId}
-                                  and ad.device_ids is not null
-                                  and ad.device_ids != ''
-                                  and ja_fw_device.id = ANY(string_to_array(ad.device_ids, ',')::bigint[])
-                            )
-                            or not exists (
-                                select 1
-                                from ja_fw_area_divide ad
-                                where ad.is_deleted = 0
-                                  and ad.device_ids is not null
-                                  and ad.device_ids != ''
-                                  and ja_fw_device.id = ANY(string_to_array(ad.device_ids, ',')::bigint[])
-                                  and ad.id != #{query.areaId}
-                            )
-                        </when>
-                        <otherwise>
-                            not exists (
-                                select 1
-                                from ja_fw_area_divide ad
-                                where ad.is_deleted = 0
-                                  and ad.device_ids is not null
-                                  and ad.device_ids != ''
-                                  and ja_fw_device.id = ANY(string_to_array(ad.device_ids, ',')::bigint[])
-                            )
-                        </otherwise>
-                    </choose>
+                <choose>
+                    <when test="query.areaId != null">
+                        exists (
+                        select 1
+                        from ja_fw_area_divide ad
+                        where ad.is_deleted = 0
+                        /* 修改点1:参数对齐 */
+                        and ad.id::text = #{query.areaId}::text
+                        and ad.device_ids is not null
+                        and ad.device_ids != ''
+                        /* 修改点2:使用 text 匹配文本数组,去掉不稳定的 ::bigint[] */
+                        and ja_fw_device.id::text = ANY(string_to_array(replace(ad.device_ids, ' ', ''), ','))
+                        )
+                        or not exists (
+                        select 1
+                        from ja_fw_area_divide ad
+                        where ad.is_deleted = 0
+                        and ad.device_ids is not null
+                        and ad.device_ids != ''
+                        and ja_fw_device.id::text = ANY(string_to_array(replace(ad.device_ids, ' ', ''), ','))
+                        and ad.id::text != #{query.areaId}::text
+                        )
+                    </when>
+                    <otherwise>
+                        not exists (
+                        select 1
+                        from ja_fw_area_divide ad
+                        where ad.is_deleted = 0
+                        and ad.device_ids is not null
+                        and ad.device_ids != ''
+                        and ja_fw_device.id::text = ANY(string_to_array(replace(ad.device_ids, ' ', ''), ','))
+                        )
+                    </otherwise>
+                </choose>
                 )
             </if>
             <if test="query.status != null">
                 and status = #{query.status}
             </if>
             <if test="query.isTrack != null and query.isTrack == 1">
-                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id = tmp.device_id::VARCHAR) > 0
+                /* 修改点3:解决 Position 575 报错,双向转 text */
+                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id::text = tmp.device_id::text) > 0
             </if>
             <if test="query.isTrack != null and query.isTrack == 2">
-                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id = tmp.device_id::VARCHAR) = 0
+                and (SELECT COUNT(*) FROM ja_fw_device_track tmp WHERE ja_fw_device.id::text = tmp.device_id::text) = 0
             </if>
             and longitude is not null
             and longitude != ''
@@ -237,12 +250,14 @@
             and latitude != ''
             <if test="query.polygons != null and query.polygons.size > 0">
                 and (
-                    <foreach collection="query.polygons" item="polygon" separator=" OR ">
-                        ST_Contains(
-                            ST_SRID(ST_GeomFromText(#{polygon}), 4326),
-                            ST_GeomFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326)
-                        )
-                    </foreach>
+                <foreach collection="query.polygons" item="polygon" separator=" OR ">
+                    public.st_contains(
+                    /* 直接在构造时指定 4326 */
+                    public.st_geomfromtext(#{polygon}::text, 4326),
+                    /* 将经纬度拼接为点,并指定坐标系 */
+                    public.st_geomfromtext(CONCAT('POINT(', longitude, ' ', latitude, ')'), 4326)
+                    )
+                </foreach>
                 )
             </if>
         </where>
@@ -279,7 +294,7 @@
         WHERE
         d.is_deleted = 0
         AND d.is_enabled = 1
-        /* 修改点2:修正 bpchar <> integer 报错,给 3 加引号 */
+        /* 修改点2:修正 bpchar  integer 报错,给 3 加引号 */
         AND d.status::VARCHAR != '3'
         <if test="regionCode != null and regionCode != ''">
             and (d.final_outbound_area_code like concat(#{regionCode}, '%')
@@ -324,7 +339,7 @@
         WHERE
         d.is_deleted = 0
         AND is_enabled = 1
-        /* 修改点5:修正 bpchar <> integer 报错 */
+        /* 修改点5:修正 bpchar  integer 报错 */
         AND d.status::VARCHAR != '3'
         AND device_type = '3'
         <if test="regionCode != null and regionCode != ''">
@@ -394,7 +409,7 @@
             ja_fw_device d
         WHERE
             d.is_deleted = 0
-            AND d.is_enabled = 1
+            AND d.track_status = 1
             AND d.final_outbound_area IS NOT NULL
         <if test="effectiveRangeKmIsNotNull != null and effectiveRangeKmIsNotNull == 2">
             and (d.effective_range_km is null or d.effective_range_km = 0)
@@ -633,7 +648,43 @@
 
     <select id="selectCockpitDevicePage" resultType="org.sxkj.fw.device.vo.CockpitFwDeviceVO">
         select
-        d.*,
+        d.id,
+        d.device_sn,
+        d.airport_id,
+        d.device_name,
+        d.device_type,
+        d.device_att,
+        d.device_model,
+        d.device_specification,
+        d.manufacturer,
+        d.source,
+        d.purpose,
+        d.belong_dept,
+        d.charger,
+        d.contact_phone,
+        d.remark,
+        d.battery_pct,
+        d.azimuth,
+        d.elevation,
+        d.detect_target_cnt,
+        d.effective_range_km,
+        d.work_mode,
+        d.area_code,
+        d.track_status,
+        d.final_outbound_area,
+        d.final_outbound_area_code,
+        d.is_enabled,
+        d.create_user,
+        d.create_dept,
+        d.create_time,
+        d.update_user,
+        d.update_time,
+        d.status,
+        d.is_deleted,
+        d.use_date,
+        d.service_life,
+        NULLIF(trim(d.latitude::text), '')::double precision AS latitude,
+        NULLIF(trim(d.longitude::text), '')::double precision AS longitude,
         min(ad.id) as area_divide_id,
         min(dsm.id) as defense_scene_manage_id
         from
@@ -746,11 +797,14 @@
             CASE
                 WHEN ps.id IS NOT NULL THEN TRUE
                 ELSE FALSE
-            END AS isShared
+                END AS isShared
         FROM
             ja_fw_device d
-            LEFT JOIN ja_fw_device_per_share ps ON ps.device_id = d.id::VARCHAR
-            AND ps.loan_to_dept_id = #{currentDeptId}
+                /* 修改点1:建议使用 ::text,且如果 id 本身是数字类型,能不转就不转以优化索引,
+                   这里保留转换逻辑但改为更通用的 text */
+                LEFT JOIN ja_fw_device_per_share ps ON ps.device_id::text = d.id::text
+        /* 修改点2:核心修复!显式转换参数为 bigint,匹配数据库字段类型 */
+        AND ps.loan_to_dept_id = #{currentDeptId}::bigint
             AND ps.is_deleted = 0
         WHERE
             d.is_deleted = 0
@@ -773,7 +827,11 @@
         AND ad.is_deleted = 0
         WHERE
         d.is_deleted = 0
-        AND d.is_enabled = 1
+        AND d.track_status = 1
+        <if test="isEnabled != null">
+            AND d.is_enabled = #{isEnabled}
+        </if>
+        AND d.track_status = 1
         AND d.final_outbound_area IS NOT NULL
         <if test="regionCode != null and regionCode != ''">
             and ( d.final_outbound_area_code::text LIKE concat(#{regionCode}, '%')
@@ -808,4 +866,15 @@
         d.final_outbound_area
     </select>
 
+    <!-- 批量更新设备启用状态 -->
+    <update id="batchUpdateIsEnabled">
+        UPDATE ja_fw_device
+        SET is_enabled = #{isEnabled}
+        WHERE id IN
+        <foreach collection="deviceIds" item="id" open="(" separator="," close=")">
+            #{id}::bigint
+        </foreach>
+        AND is_deleted = 0
+    </update>
+
 </mapper>

--
Gitblit v1.9.3