zhubaomin
2025-02-17 2ce6aa8eee18b7436cfe144614074494cb11b551
巡检时计算巡检距离
8个文件已修改
1个文件已添加
199 ■■■■ 已修改文件
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoOp/OpeInspectMapper.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoOp/OpeTrackMapper.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/pojoOp/OpeInspect.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/voOp/VoTrackPoint.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/mapper/OpeInspectMapper.xml 44 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/mapper/OpeTrackMapper.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java 59 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectSv.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoOp/OpeInspectMapper.java
@@ -4,6 +4,7 @@
import com.dy.pipIrrGlobal.pojoOp.OpeInspect;
import com.dy.pipIrrGlobal.voOp.VoInspect;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
@@ -11,7 +12,7 @@
/**
 * @author ZhuBaoMin
 * @date 2024-09-24 10:07
 * @LastEditTime 2024-09-24 10:07
 * @LastEditTime 2025-02-17 15:11
 * @Description
 */
@@ -42,4 +43,12 @@
     * @return
     */
    List<VoInspect> getInspects(Map<String, Object> params);
    /**
     * 更新指定巡检记录的巡检距离
     * @param inspectId
     * @param distance
     * @return
     */
    int updateInspectDistance(@Param("inspectId") Long inspectId, @Param("distance")Double distance );
}
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoOp/OpeTrackMapper.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dy.pipIrrGlobal.pojoOp.OpeTrack;
import com.dy.pipIrrGlobal.voOp.VoTrackPoint;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@@ -40,4 +41,11 @@
     * @return
     */
    List<OpeTrack> selectByInspectId(Long inspectId);
    /**
     * 获取指定巡检ID下的全部轨迹点
     * @param inspectId
     * @return
     */
    List<VoTrackPoint> getTrackPointsById(Long inspectId);
}
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/pojoOp/OpeInspect.java
@@ -15,8 +15,8 @@
/**
 * @author ZhuBaoMin
 * @date 2024-09-24 10:07
 * @LastEditTime 2024-09-24 10:07
 * @Description
 * @LastEditTime 2025-02-17 15:14
 * @Description 添加巡检距离字段
 */
/**
 * 巡检表
@@ -32,28 +32,33 @@
    public static final long serialVersionUID = 202409241013001L;
    /**
    * 主键
    */
     * 主键
     */
    @JSONField(serializeUsing= ObjectWriterImplToString.class)
    @TableId(type = IdType.INPUT)
    private Long id;
    /**
    * 巡检员ID
    */
     * 巡检员ID
     */
    @JSONField(serializeUsing= ObjectWriterImplToString.class)
    @NotNull(message = "巡检员ID不能为空")
    private Long inspectorId;
    /**
    * 开始巡检时间
    */
     * 开始巡检时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;
    /**
    * 结束巡检时间
    */
     * 结束巡检时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date stopTime;
    /**
     * 巡检距离
     */
    private Double inspectDistance;
}
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/voOp/VoTrackPoint.java
New file
@@ -0,0 +1,25 @@
package com.dy.pipIrrGlobal.voOp;
import lombok.Data;
import java.math.BigDecimal;
/**
 * @author ZhuBaoMin
 * @date 2025-02-17 15:23
 * @LastEditTime 2025-02-17 15:23
 * @Description 巡检点对象,计算巡检距离使用
 */
@Data
public class VoTrackPoint {
    /**
     * 经度
     */
    private BigDecimal lng;
    /**
     * 纬度
     */
    private BigDecimal lat;
}
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -78,7 +78,7 @@
pipIrr:
    global:
        dev: false  #是否开发阶段,true或false
        dev: true  #是否开发阶段,true或false
        dsName: ym  #开发阶段,设置临时的数据库名称
    nginx:
        root: E:/apps/pipIrr/nginx-1.27.0
pipIrr-platform/pipIrr-global/src/main/resources/mapper/OpeInspectMapper.xml
@@ -8,10 +8,11 @@
    <result column="inspector_id" jdbcType="BIGINT" property="inspectorId" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="stop_time" jdbcType="TIMESTAMP" property="stopTime" />
    <result column="inspect_distance" jdbcType="FLOAT" property="inspectDistance" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, inspector_id, start_time, stop_time
    id, inspector_id, start_time, stop_time, inspect_distance
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--@mbg.generated-->
@@ -28,9 +29,9 @@
  <insert id="insert" parameterType="com.dy.pipIrrGlobal.pojoOp.OpeInspect">
    <!--@mbg.generated-->
    insert into ope_inspect (id, inspector_id, start_time, 
      stop_time)
      stop_time, inspect_distance)
    values (#{id,jdbcType=BIGINT}, #{inspectorId,jdbcType=BIGINT}, #{startTime,jdbcType=TIMESTAMP}, 
      #{stopTime,jdbcType=TIMESTAMP})
      #{stopTime,jdbcType=TIMESTAMP}, #{inspectDistance,jdbcType=FLOAT})
  </insert>
  <insert id="insertSelective" parameterType="com.dy.pipIrrGlobal.pojoOp.OpeInspect">
    <!--@mbg.generated-->
@@ -48,6 +49,9 @@
      <if test="stopTime != null">
        stop_time,
      </if>
      <if test="inspectDistance != null">
        inspect_distance,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
@@ -61,6 +65,9 @@
      </if>
      <if test="stopTime != null">
        #{stopTime,jdbcType=TIMESTAMP},
      </if>
      <if test="inspectDistance != null">
        #{inspectDistance,jdbcType=FLOAT},
      </if>
    </trim>
  </insert>
@@ -77,6 +84,9 @@
      <if test="stopTime != null">
        stop_time = #{stopTime,jdbcType=TIMESTAMP},
      </if>
      <if test="inspectDistance != null">
        inspect_distance = #{inspectDistance,jdbcType=FLOAT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
@@ -85,10 +95,12 @@
    update ope_inspect
    set inspector_id = #{inspectorId,jdbcType=BIGINT},
      start_time = #{startTime,jdbcType=TIMESTAMP},
      stop_time = #{stopTime,jdbcType=TIMESTAMP}
      stop_time = #{stopTime,jdbcType=TIMESTAMP},
      inspect_distance = #{inspectDistance,jdbcType=FLOAT}
    where id = #{id,jdbcType=BIGINT}
  </update>
<!-- 查询总数 -->
  <!-- 查询总数 -->
  <select id="getInspectsCount" resultType="java.lang.Long">
    SELECT
    count(*)
@@ -109,15 +121,16 @@
        AND oi.start_time BETWEEN #{timeStart} AND #{timeStop}
      </if>
    </where>
    </select>
<!-- 查询列表 -->
  </select>
  <!-- 查询列表 -->
  <select id="getInspects" resultType="com.dy.pipIrrGlobal.voOp.VoInspect">
    SELECT
      oi.inspector_id AS inspectorId,
      ba_user.`name` AS inspectorName,
      oi.id AS inspectId,
      oi.start_time AS startTime,
      oi.stop_time AS stopTime
    oi.inspector_id AS inspectorId,
    ba_user.`name` AS inspectorName,
    oi.id AS inspectId,
    oi.start_time AS startTime,
    oi.stop_time AS stopTime
    FROM    `ope_inspect` oi
    INNER JOIN `ba_user` ON oi.inspector_id = ba_user.id
    <where>
@@ -141,4 +154,11 @@
      </if>
    </trim>
  </select>
  <!--更新指定巡检记录的巡检距离-->
  <update id="updateInspectDistance">
    UPDATE ope_inspect
    SET inspect_distance = #{distance}
    WHERE id = #{inspectId}
  </update>
</mapper>
pipIrr-platform/pipIrr-global/src/main/resources/mapper/OpeTrackMapper.xml
@@ -119,4 +119,9 @@
      locate_time AS locateTime
    FROM ope_track WHERE inspect_id = #{inspectId}
  </select>
  <!--获取指定巡检ID下的全部轨迹点-->
  <select id="getTrackPointsById" resultType="com.dy.pipIrrGlobal.voOp.VoTrackPoint">
    SELECT lng, lat FROM ope_track WHERE inspect_id = #{inspectId}
  </select>
</mapper>
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
@@ -12,6 +12,7 @@
import com.dy.pipIrrGlobal.pojoOp.OpeInspect;
import com.dy.pipIrrGlobal.pojoOp.OpeTrack;
import com.dy.pipIrrGlobal.voOp.VoInspect;
import com.dy.pipIrrGlobal.voOp.VoTrackPoint;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +24,7 @@
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
 * @author ZhuBaoMin
@@ -39,6 +41,9 @@
    private InspectSv inspectSv;
    private final IDLongGenerator idLongGenerator;
    // 地球半径,单位:千米
    private static final double EARTH_RADIUS = 6371.0;
    public InspectCtrl(IDLongGenerator idLongGenerator) {
        this.idLongGenerator = idLongGenerator;
@@ -104,6 +109,11 @@
                    if(rec == null || rec == 0) {
                        return BaseResponseUtils.buildErrorMsg("巡检轨迹添加失败");
                    }
                    Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0);
                    if(distance > 0) {
                        inspectSv.updateInspectDistance(inspectId, distance);
                    }
                }else {
                    // 该巡检已上传过记录,续传
                    if(stopTime != null) {
@@ -122,6 +132,10 @@
                        if(rec == null || rec == 0) {
                            return BaseResponseUtils.buildErrorMsg("巡检轨迹添加失败");
                        }
                    }
                    Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0);
                    if(distance > 0) {
                        inspectSv.updateInspectDistance(inspectId, distance);
                    }
                }
            }
@@ -150,4 +164,49 @@
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
    /**
     * 根据巡检ID获取轨迹总距离
     * @param inspectId
     * @return
     */
    public double getInspectDistance(Long inspectId) {
        List<VoTrackPoint> points = inspectSv.getTrackPointsById(inspectId);
        if(points == null || points.size() == 0) {
            return 0.0;
        }
        double totalDistance = calculateTotalDistance(points);
        return totalDistance;
    }
    // 计算轨迹的总距离
    public static double calculateTotalDistance(List<VoTrackPoint> points) {
        double totalDistance = 0.0;
        for (int i = 0; i < points.size() - 1; i++) {
            VoTrackPoint currentPoint = points.get(i);
            VoTrackPoint nextPoint = points.get(i + 1);
            double distance = haversine( currentPoint.getLat().doubleValue(), currentPoint.getLng().doubleValue(), nextPoint.getLat().doubleValue(), nextPoint.getLng().doubleValue());
            totalDistance += distance;
        }
        return totalDistance;
    }
    // Haversine公式计算两个经纬度点之间的距离
    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return EARTH_RADIUS * c;
    }
}
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectSv.java
@@ -7,6 +7,7 @@
import com.dy.pipIrrGlobal.pojoOp.OpeInspect;
import com.dy.pipIrrGlobal.pojoOp.OpeTrack;
import com.dy.pipIrrGlobal.voOp.VoInspect;
import com.dy.pipIrrGlobal.voOp.VoTrackPoint;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -99,4 +100,23 @@
        rsVo.obj = inspects;
        return rsVo;
    }
    /**
     * 获取指定巡检ID下的全部轨迹点
     * @param inspectId
     * @return
     */
    public List<VoTrackPoint> getTrackPointsById(Long inspectId) {
        return opeTrackMapper.getTrackPointsById(inspectId);
    }
    /**
     * 更新指定巡检记录的巡检距离
     * @param inspectId
     * @param distance
     * @return
     */
    public int updateInspectDistance(Long inspectId, double distance ) {
        return opeInspectMapper.updateInspectDistance(inspectId, distance);
    }
}