Xiajijian
2025-10-16 ac3bb8910dd285b9d49844f496ba1ff3dab55bab
1.批量新增无人机盒子方法优化。
5 files modified
237 ■■■■■ changed files
ard-admin/src/main/java/com/ard/web/controller/work/ArdDroneInfoController.java 20 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/service/IArdDroneInfoService.java 5 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java 144 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/util/UtilMethod.java 35 ●●●●● patch | view | raw | blame | history
ard-work/src/main/resources/mapper/ArdDroneInfoMapper.xml 33 ●●●●● patch | view | raw | blame | history
ard-admin/src/main/java/com/ard/web/controller/work/ArdDroneInfoController.java
@@ -106,4 +106,24 @@
        return toAjax(ardDroneInfoService.deleteArdDroneByIds(ids));
    }
    /**
     * 批量新增无人机盒子
     */
    @ApiOperation("批量新增无人机盒子")
    @PreAuthorize("@ss.hasPermi('work:drone:insertArdDroneLs')")
    @Log(title = "无人机管理", businessType = BusinessType.DELETE)
    @PostMapping("insertArdDroneLs/{boxSn}/{deptId}")
    public AjaxResult insertArdDroneLs(@PathVariable String[] boxSn, @PathVariable Long deptId) {
        try {
            int result = ardDroneInfoService.insertArdDroneLs(boxSn, deptId);
            if (result > 0) {
                return AjaxResult.success("成功新增 " + result + " 个无人机盒子");
            } else {
                return AjaxResult.success("没有需要新增的无人机盒子");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
ard-work/src/main/java/com/ard/work/service/IArdDroneInfoService.java
@@ -73,9 +73,10 @@
    /**
     * 批量新增无人机盒子
     *
     * @param ids 无人机盒子主键集合
     * @param boxSn 盒子编码
     * @param deptId 部门ID
     * @return 结果
     */
    public int insertArdDroneLs(String[] ids);
    public int insertArdDroneLs(String[] boxSn,Long deptId);
}
ard-work/src/main/java/com/ard/work/service/impl/ArdDroneInfoServiceImpl.java
@@ -1,6 +1,7 @@
package com.ard.work.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ard.common.exception.ServiceException;
import com.ard.common.utils.DateUtils;
import com.ard.common.utils.uuid.IdUtils;
import com.ard.system.authorize.service.AuthorizeService;
@@ -10,14 +11,12 @@
import com.ard.work.service.IArdDroneInfoService;
import com.ard.work.service.ITyService;
import com.ard.work.util.UtilMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -26,6 +25,7 @@
 * @author ruoyi
 * @date 2025-03-04
 */
@Slf4j
@Service
public class ArdDroneInfoServiceImpl implements IArdDroneInfoService {
@@ -34,6 +34,9 @@
    @Resource
    private ITyService tyService;
    @Resource
    private AuthorizeService authorizeService;
    UtilMethod utilMethod = new UtilMethod();
@@ -120,24 +123,129 @@
    /**
     * 批量新增无人机盒子
     *
     * @param ids 无人机盒子主键集合
     * @return 结果
     * @param boxSn 盒子编码数组
     * @param deptId 部门ID
     * @return 成功插入的记录数
     */
    @Override
    public int insertArdDroneLs(String[] ids) {
        // 获取无人机盒子信息
        List<ArdDroneInfo> drone = new ArrayList<>();
        for (String id : ids) {
            ArdDroneInfo ardDrone = ArdDroneInfoMapper.selectArdDroneById(id);
            drone.add(ardDrone);
    public int insertArdDroneLs(String[] boxSn, Long deptId) {
        // 参数校验
        if (boxSn == null || boxSn.length == 0) {
            log.warn("传入的盒子编码为空");
            return 1;
        }
        // 检验数量droneNum是否合规。
        utilMethod.authorizationVerification(drone.size());
        // 插入
        if (!drone.isEmpty()) {
            return ArdDroneInfoMapper.insertArdDroneLs(drone);
        if (deptId == null) {
            throw new IllegalArgumentException("部门ID不能为空");
        }
        return 0;
        try {
            //utilMethod.authorizationVerification(boxSn.length);
            // 1. 检查已存在的无人机盒子
            List<ArdDroneInfo> existingDrones = getExistingDrones(boxSn);
            // 2. 授权数量验证
            validateAuthorization(existingDrones.size(), boxSn.length);
            // 3. 获取并组装新的无人机信息
            List<ArdDroneInfo> newDrones = assembleNewDrones(boxSn, deptId);
            // 4. 批量插入
            return batchInsertDrones(newDrones);
        } catch (Exception e) {
            log.error("批量新增无人机盒子失败, boxSn: {}, deptId: {}", Arrays.toString(boxSn), deptId, e);
            throw new ServiceException("批量新增无人机盒子失败: " + e.getMessage());
        }
    }
    /**
     * 获取已存在的无人机信息
     */
    private List<ArdDroneInfo> getExistingDrones(String[] boxSn) {
        return Arrays.stream(boxSn)
                .map(ArdDroneInfoMapper::selectArdDroneByBoxSn)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
    /**
     * 验证授权数量
     */
    private void validateAuthorization(int existingCount, int newCount) {
        int totalCount = existingCount + newCount;
        JSONObject authInfo = authorizeService.getAuthorizationInfo();
        String authType = authInfo.getString("Type");
        switch (authType) {
            case "normal":
                int allowedDroneNum = authInfo.getIntValue("droneNum");
                if (totalCount > allowedDroneNum) {
                    throw new RuntimeException(String.format(
                            "授权数量%d,当前操作将导致总数达到%d,超过授权限制",
                            allowedDroneNum, totalCount));
                }
                break;
            case "expired":
                throw new RuntimeException("授权文件已过期");
            case "mismatch":
                throw new RuntimeException("授权文件不匹配");
            default:
                throw new RuntimeException("未知的授权类型: " + authType);
        }
    }
    /**
     * 组装新的无人机信息
     */
    private List<ArdDroneInfo> assembleNewDrones(String[] boxSn, Long deptId) {
        List<BoxInfoReceiver> allBoxes = tyService.getBoxList();
        Set<String> targetBoxSnSet = new HashSet<>(Arrays.asList(boxSn));
        return allBoxes.stream()
                .filter(box -> targetBoxSnSet.contains(box.getBoxSn()))
                .map(box -> createDroneInfo(box, deptId))
                .collect(Collectors.toList());
    }
    /**
     * 创建无人机信息对象
     */
    private ArdDroneInfo createDroneInfo(BoxInfoReceiver box, Long deptId) {
        ArdDroneInfo drone = new ArdDroneInfo();
        drone.setId(IdUtils.simpleUUID());
        drone.setBoxSn(box.getBoxSn());
        drone.setBoxName(box.getBoxName());
        drone.setStartTime(new Date());
        drone.setOnLine(box.getOnLine());
        drone.setVideoServer(box.getVideoServer());
        drone.setVideoPort(box.getVideoPort());
        drone.setLastTaskId(box.getLastTaskId());
        drone.setDeptId(deptId);
        // 设置创建信息和更新信息
        setCreateAndUpdateInfo(drone);
        return drone;
    }
    /**
     * 设置创建和更新信息
     */
    private void setCreateAndUpdateInfo(ArdDroneInfo drone) {
        Date currentTime = new Date();
        drone.setCreateTime(currentTime);
        drone.setUpdateTime(currentTime);
    }
    /**
     * 批量插入无人机信息
     */
    private int batchInsertDrones(List<ArdDroneInfo> drones) {
        if (drones.isEmpty()) {
            log.info("没有需要插入的无人机信息");
            return 0;
        }
        int result = ArdDroneInfoMapper.insertArdDroneLs(drones);
        log.info("成功插入 {} 条无人机记录", result);
        return result;
    }
    /**
ard-work/src/main/java/com/ard/work/util/UtilMethod.java
@@ -2,6 +2,9 @@
import com.alibaba.fastjson2.JSONObject;
import com.ard.system.authorize.service.AuthorizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -10,25 +13,35 @@
 * @date: 2025/3/4
 * @description:
 */
@Service
public class UtilMethod {
    @Resource
    @Autowired
    private AuthorizeService authorizeService;
    /**
     * 根据授权数量,验证授权信息是否合规
     */
    public void authorizationVerification(int ls){
        JSONObject data = authorizeService.getAuthorizationInfo();
        if ("normal".equals(data.get("Type"))){
            int droneNum = (int) data.get("droneNum");
            if (ls > droneNum){
                throw new RuntimeException("添加无人机数量超过当前授权数量!!!");
            }
        }else if ("expired".equals(data.get("Type"))){
            throw new RuntimeException("授权文件过期!!!");
        }else if ("mismatch".equals(data.get("Type"))){
            throw new RuntimeException("授权文件不匹配!!!");
        JSONObject authInfo = authorizeService.getAuthorizationInfo();
        String authType = authInfo.getString("Type");
        switch (authType) {
            case "normal":
                int allowedDroneNum = authInfo.getIntValue("droneNum");
                if (ls > allowedDroneNum) {
                    throw new RuntimeException(String.format(
                            "授权数量%d,当前操作将导致总数达到%d,超过授权限制",
                            allowedDroneNum, ls));
                }
                break;
            case "expired":
                throw new RuntimeException("授权文件已过期");
            case "mismatch":
                throw new RuntimeException("授权文件不匹配");
            default:
                throw new RuntimeException("未知的授权类型: " + authType);
        }
    }
ard-work/src/main/resources/mapper/ArdDroneInfoMapper.xml
@@ -84,6 +84,7 @@
            <if test="videoServer != null">video_server,</if>
            <if test="videoPort != null">video_port,</if>
            <if test="lastTaskId != null">last_task_id,</if>
            <if test="serverId != null">server_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="createBy != null">create_by,</if>
@@ -102,6 +103,7 @@
            <if test="videoServer != null">#{videoServer},</if>
            <if test="videoPort != null">#{videoPort},</if>
            <if test="lastTaskId != null">#{lastTaskId},</if>
            <if test="serverId != null">#{serverId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="createBy != null">#{createBy},</if>
@@ -123,6 +125,7 @@
            <if test="videoServer != null">video_server = #{videoServer},</if>
            <if test="videoPort != null">video_port = #{videoPort},</if>
            <if test="lastTaskId != null">last_task_id = #{lastTaskId},</if>
            <if test="serverId != null">server_id = #{serverId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
@@ -145,7 +148,7 @@
        </foreach>
    </delete>
    <insert id="insertArdDroneLs">
    <insert id="insertArdDroneLs" parameterType="java.util.List">
        insert into ard_drone_info(
            id,
            box_sn,
@@ -167,19 +170,21 @@
        ) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.id},
            #{item.boxSn}),
            #{item.boxName}),
            #{item.boxVersion}),
            #{item.startTime}),
            #{item.videoServer}),
            #{item.videoPort}),
            #{item.lastTaskId}),
            #{item.userId}),
            #{item.deptId}),
            #{item.createBy}),
            #{item.createTime}),
            #{item.updateBy}),
            #{item.updateTime}),
            #{item.boxSn},
            #{item.boxName},
            #{item.boxVersion},
            #{item.startTime},
            #{item.onLine},
            #{item.videoServer},
            #{item.videoPort},
            #{item.lastTaskId},
            #{item.serverId},
            #{item.userId},
            #{item.deptId},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.remark})
        </foreach>
    </insert>