Procházet zdrojové kódy

提交地磅管理

chenxiaofei před 2 měsíci
rodič
revize
de9d933528

+ 643 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/service/KwsWeighbridgeManageService.java

@@ -0,0 +1,643 @@
+package com.sckw.system.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson2.JSON;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.sckw.core.exception.SystemException;
+import com.sckw.core.model.page.PageResult;
+import com.sckw.core.web.constant.HttpStatus;
+import com.sckw.core.web.context.LoginUserHolder;
+import com.sckw.system.model.KwsEnterprise;
+import com.sckw.system.model.KwsPrinter;
+import com.sckw.system.model.KwsWeighbridge;
+import com.sckw.system.model.KwsWeighbridgeDiffConfig;
+import com.sckw.system.model.vo.req.WeighbridgeDiffConfigReqVo;
+import com.sckw.system.model.vo.req.WeighbridgePageReqVo;
+import com.sckw.system.model.vo.req.WeighbridgeRestartReqVo;
+import com.sckw.system.model.vo.req.WeighbridgeSaveReqVo;
+import com.sckw.system.model.vo.req.WeighbridgeStatusReqVo;
+import com.sckw.system.model.vo.res.PlatformEnterpriseResVo;
+import com.sckw.system.model.vo.res.WeighbridgeDetailResVo;
+import com.sckw.system.model.vo.res.WeighbridgeDiffConfigResVo;
+import com.sckw.system.model.vo.res.WeighbridgePageResVo;
+import com.sckw.system.repository.KwsEnterpriseRepository;
+import com.sckw.system.repository.KwsPrinterRepository;
+import com.sckw.system.repository.KwsWeighbridgeDiffConfigRepository;
+import com.sckw.system.repository.KwsWeighbridgeRepository;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * 地磅管理服务类
+ * 提供地磅的增删改查、状态管理、误差配置及关联信息查询等功能
+ */
+@Service
+@RequiredArgsConstructor
+@Slf4j
+public class KwsWeighbridgeManageService {
+
+    /**
+     * 地磅数据访问层
+     */
+    private final KwsWeighbridgeRepository kwsWeighbridgeRepository;
+    /**
+     * 企业数据访问层
+     */
+    private final KwsEnterpriseRepository kwsEnterpriseRepository;
+    /**
+     * 打印机数据访问层
+     */
+    private final KwsPrinterRepository kwsPrinterRepository;
+    /**
+     * 地磅误差配置数据访问层
+     */
+    private final KwsWeighbridgeDiffConfigRepository kwsWeighbridgeDiffConfigRepository;
+
+    /**
+     * 分页查询地磅列表
+     * <p>
+     * 根据请求参数中的企业名称解析出对应的企业ID集合,结合地磅名称进行分页查询。
+     * 若解析出的企业ID集合为空(例如普通用户无权限访问任何企业),则直接返回空结果。
+     *
+     * @param reqVo 分页查询请求参数,包含页码、每页大小、地磅名称、企业名称等
+     * @return 分页结果,包含地磅列表及总数
+     */
+    public PageResult page(WeighbridgePageReqVo reqVo) {
+        log.info("分页查询地磅列表, reqVo: {}", JSON.toJSONString(reqVo));
+        // 根据企业名称解析有权限访问的企业ID集合
+        Set<Long> entIds = resolveQueryEntIds(reqVo.getEnterpriseName());
+        if (entIds.isEmpty()) {
+            // 若无有效企业ID,直接返回空分页结果
+            return PageResult.build(reqVo.getPage(), reqVo.getPageSize(), 0L, Collections.emptyList());
+        }
+        // 执行数据库分页查询
+        IPage<KwsWeighbridge> page = kwsWeighbridgeRepository.pageQuery(reqVo.getPage(), reqVo.getPageSize(),
+                reqVo.getWeighbridgeName(), entIds);
+        // 构建并返回响应结果
+        return PageResult.of(page, buildPageRes(page.getRecords()));
+    }
+
+    /**
+     * 获取地磅详情
+     * <p>
+     * 根据地磅ID查询地磅信息,并填充企业名称、打印机名称等关联信息。
+     *
+     * @param id 地磅ID
+     * @return 地磅详情响应对象
+     */
+    public WeighbridgeDetailResVo detail(Long id) {
+        log.info("获取地磅详情, id: {}", id);
+        // 获取并校验地磅实体(包含权限校验)
+        KwsWeighbridge weighbridge = getAndCheck(id);
+        WeighbridgeDetailResVo resVo = new WeighbridgeDetailResVo();
+        // 填充基础信息及关联名称
+        fillBaseRes(weighbridge,
+                enterpriseNameMap(Collections.singleton(weighbridge.getEntId())),
+                printerNameMap(weighbridge.getPrinterId() == null ? Collections.emptySet() : Collections.singleton(weighbridge.getPrinterId())),
+                resVo);
+        // 设置误差配置JSON字符串
+        resVo.setDiffConfig(weighbridge.getDiffConfig());
+        return resVo;
+    }
+
+
+    /**
+     * 新增地磅信息
+     * <p>
+     * 校验请求参数合法性及唯一性后,创建新的地磅记录。
+     *
+     * @param reqVo 地磅保存请求参数
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void add(WeighbridgeSaveReqVo reqVo) {
+        log.info("新增地磅信息, reqVo: {}", JSON.toJSONString(reqVo));
+        // 校验请求参数(新增时excludeId为null)
+        validateSaveReq(reqVo, null);
+        Date now = new Date();
+        // 构建地磅实体对象
+        KwsWeighbridge weighbridge = new KwsWeighbridge()
+                .setEntId(reqVo.getEntId())
+                .setWeighbridgeName(reqVo.getWeighbridgeName())
+                .setUniqueCode(reqVo.getUniqueCode())
+                .setOnlineStatus(defaultOnlineStatus(reqVo.getOnlineStatus()))
+                .setPrinterId(reqVo.getPrinterId())
+                .setDescription(reqVo.getDescription())
+                .setDiffConfig(reqVo.getDiffConfig())
+                .setStatus(0) // 默认启用
+                .setDelFlag(0) // 默认未删除
+                .setCreateBy(LoginUserHolder.getUserId())
+                .setCreateTime(now)
+                .setUpdateBy(LoginUserHolder.getUserId())
+                .setUpdateTime(now);
+        // 保存至数据库
+        if (!kwsWeighbridgeRepository.save(weighbridge)) {
+            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
+        }
+    }
+
+    /**
+     * 更新地磅信息
+     * <p>
+     * 校验请求参数及权限后,更新指定地磅的信息。
+     *
+     * @param reqVo 地磅保存请求参数,必须包含地磅ID
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void update(WeighbridgeSaveReqVo reqVo) {
+        log.info("更新地磅信息, reqVo: {}", JSON.toJSONString(reqVo));
+        if (reqVo.getId() == null) {
+            throw new SystemException("地磅ID不能为空");
+        }
+        // 获取并校验原地磅信息
+        KwsWeighbridge weighbridge = getAndCheck(reqVo.getId());
+        // 校验请求参数(更新时需排除当前ID以检查唯一编码冲突)
+        validateSaveReq(reqVo, weighbridge.getId());
+        // 更新字段
+        weighbridge.setEntId(reqVo.getEntId());
+        weighbridge.setWeighbridgeName(reqVo.getWeighbridgeName());
+        weighbridge.setUniqueCode(reqVo.getUniqueCode());
+        weighbridge.setOnlineStatus(defaultOnlineStatus(reqVo.getOnlineStatus()));
+        weighbridge.setPrinterId(reqVo.getPrinterId());
+        weighbridge.setDescription(reqVo.getDescription());
+        weighbridge.setDiffConfig(reqVo.getDiffConfig());
+        weighbridge.setUpdateBy(LoginUserHolder.getUserId());
+        weighbridge.setUpdateTime(new Date());
+        // 执行更新
+        if (!kwsWeighbridgeRepository.updateById(weighbridge)) {
+            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
+        }
+    }
+
+    /**
+     * 更新地磅状态
+     * <p>
+     * 仅允许将状态更新为0(启用)或1(停用)。
+     *
+     * @param reqVo 状态更新请求参数,包含地磅ID和目标状态
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void updateStatus(WeighbridgeStatusReqVo reqVo) {
+        log.info("更新地磅状态, reqVo: {}", JSON.toJSONString(reqVo));
+        if (reqVo.getId() == null) {
+            throw new SystemException("地磅ID不能为空");
+        }
+        // 校验状态值合法性
+        if (!Objects.equals(reqVo.getStatus(), 0) && !Objects.equals(reqVo.getStatus(), 1)) {
+            throw new SystemException("无效的地磅状态");
+        }
+        // 获取并校验地磅
+        KwsWeighbridge weighbridge = getAndCheck(reqVo.getId());
+        weighbridge.setStatus(reqVo.getStatus());
+        weighbridge.setUpdateBy(LoginUserHolder.getUserId());
+        weighbridge.setUpdateTime(new Date());
+        if (!kwsWeighbridgeRepository.updateById(weighbridge)) {
+            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
+        }
+    }
+
+    /**
+     * 更新地磅误差配置
+     * <p>
+     * 更新指定企业的误差配置表,并同步更新地磅主表中的误差配置JSON字段。
+     * 若配置不存在则新增,存在则更新。
+     *
+     * @param reqVo 误差配置请求参数,包含企业ID及各项误差值
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void updateDiffConfig(WeighbridgeDiffConfigReqVo reqVo) {
+        log.info("更新地磅误差配置, reqVo: {}", JSON.toJSONString(reqVo));
+        // 校验请求参数非空
+        validateDiffConfigReq(reqVo);
+        // 校验企业权限及状态
+        checkEnterprise(reqVo.getEntId());
+        Date now = new Date();
+        // 查询现有配置
+        KwsWeighbridgeDiffConfig config = kwsWeighbridgeDiffConfigRepository.findByEntId(reqVo.getEntId());
+        if (config == null) {
+            // 新增配置
+            config = new KwsWeighbridgeDiffConfig()
+                    .setEntId(reqVo.getEntId())
+                    .setTareErrorValue(reqVo.getTareErrorValue())
+                    .setLoadErrorValue(reqVo.getLoadErrorValue())
+                    .setEmptyLoadValue(reqVo.getEmptyLoadValue())
+                    .setCreateBy(LoginUserHolder.getUserId())
+                    .setCreateTime(now)
+                    .setUpdateBy(LoginUserHolder.getUserId())
+                    .setUpdateTime(now)
+                    .setDelFlag(0);
+            if (!kwsWeighbridgeDiffConfigRepository.save(config)) {
+                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
+            }
+        } else {
+            // 更新配置
+            config.setTareErrorValue(reqVo.getTareErrorValue());
+            config.setLoadErrorValue(reqVo.getLoadErrorValue());
+            config.setEmptyLoadValue(reqVo.getEmptyLoadValue());
+            config.setUpdateBy(LoginUserHolder.getUserId());
+            config.setUpdateTime(now);
+            if (!kwsWeighbridgeDiffConfigRepository.updateById(config)) {
+                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
+            }
+        }
+        // 同步更新地磅表中的误差配置JSON字段,确保数据一致性
+        kwsWeighbridgeRepository.updateDiffConfigByEntId(reqVo.getEntId(), buildDiffConfigJson(reqVo), LoginUserHolder.getUserId());
+    }
+
+    /**
+     * 获取地磅误差配置详情
+     * <p>
+     * 查询指定企业的误差配置信息。
+     *
+     * @param entId 企业ID
+     * @return 误差配置详情响应对象
+     */
+    public WeighbridgeDiffConfigResVo diffConfigDetail(Long entId) {
+        log.info("获取地磅误差配置详情, entId: {}", entId);
+        if (entId == null) {
+            throw new SystemException("企业ID不能为空");
+        }
+        // 校验企业权限
+        checkEnterprise(entId);
+        WeighbridgeDiffConfigResVo resVo = new WeighbridgeDiffConfigResVo();
+        resVo.setEntId(entId);
+        // 填充企业名称
+        resVo.setEnterpriseName(enterpriseNameMap(Collections.singleton(entId)).getOrDefault(entId, ""));
+        // 查询配置详情
+        KwsWeighbridgeDiffConfig config = kwsWeighbridgeDiffConfigRepository.findByEntId(entId);
+        if (config != null) {
+            resVo.setId(config.getId());
+            resVo.setTareErrorValue(config.getTareErrorValue());
+            resVo.setLoadErrorValue(config.getLoadErrorValue());
+            resVo.setEmptyLoadValue(config.getEmptyLoadValue());
+        }
+        return resVo;
+    }
+
+
+    /**
+     * 重启地磅(更新最后重启时间)
+     * <p>
+     * 记录地磅最后一次重启的时间戳。
+     *
+     * @param reqVo 重启请求参数,包含地磅ID
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void restart(WeighbridgeRestartReqVo reqVo) {
+        log.info("重启地磅, reqVo: {}", JSON.toJSONString(reqVo));
+        if (reqVo.getId() == null) {
+            throw new SystemException("地磅ID不能为空");
+        }
+        // 获取并校验地磅
+        KwsWeighbridge weighbridge = getAndCheck(reqVo.getId());
+        Date now = new Date();
+        weighbridge.setLastRestartTime(now);
+        weighbridge.setUpdateBy(LoginUserHolder.getUserId());
+        weighbridge.setUpdateTime(now);
+        if (!kwsWeighbridgeRepository.updateById(weighbridge)) {
+            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.UPDATE_FAIL);
+        }
+    }
+
+    /**
+     * 获取企业选项列表
+     * <p>
+     * 根据当前用户权限获取可选的企业列表,支持关键词搜索。
+     * 管理员可查看所有企业,普通用户仅可查看授权范围内的企业。
+     *
+     * @param keyword 搜索关键词
+     * @return 企业选项列表
+     */
+    public List<PlatformEnterpriseResVo> enterpriseOptions(String keyword) {
+        log.info("获取企业选项列表, keyword: {}", keyword);
+        // 获取当前用户授权的企业ID集合
+        Set<Long> authEntIds = resolveAuthorizedEntIds();
+        List<KwsEnterprise> enterpriseList;
+        if (LoginUserHolder.isManager()) {
+            // 管理员查询所有匹配的企业
+            enterpriseList = kwsEnterpriseRepository.queryByEntIdAndName(null, keyword);
+        } else if (authEntIds.isEmpty()) {
+            // 普通用户无授权企业,返回空列表
+            return Collections.emptyList();
+        } else {
+            // 普通用户查询授权范围内的匹配企业
+            enterpriseList = kwsEnterpriseRepository.queryByEntIds(authEntIds, keyword);
+        }
+        // 转换为响应VO
+        return enterpriseList.stream().map(item -> {
+            PlatformEnterpriseResVo resVo = new PlatformEnterpriseResVo();
+            resVo.setId(item.getId());
+            resVo.setFirmName(item.getFirmName());
+            return resVo;
+        }).toList();
+    }
+
+    /**
+     * 检查唯一编码是否可用
+     * <p>
+     * 用于前端实时校验或提交前校验,确保唯一编码未被其他地磅占用。
+     *
+     * @param uniqueCode 唯一编码
+     * @param id         地磅ID(更新时传入当前ID以排除自身)
+     * @return 是否可用
+     */
+    public boolean checkUniqueCodeAvailable(String uniqueCode, Long id) {
+        log.info("检查唯一编码是否可用, uniqueCode: {}, id: {}", uniqueCode, id);
+        if (StringUtils.isBlank(uniqueCode)) {
+            return false;
+        }
+        // 查询是否存在该编码的地磅
+        KwsWeighbridge exists = kwsWeighbridgeRepository.findByUniqueCode(uniqueCode.trim());
+        // 若不存在,或存在的记录即为当前记录,则视为可用
+        return exists == null || Objects.equals(exists.getId(), id);
+    }
+
+    /**
+     * 构建分页响应结果
+     * <p>
+     * 批量查询关联的企业名称和打印机名称,避免N+1查询问题,并组装响应VO列表。
+     *
+     * @param records 地磅记录列表
+     * @return 分页响应VO列表
+     */
+    private List<WeighbridgePageResVo> buildPageRes(List<KwsWeighbridge> records) {
+        if (records == null || records.isEmpty()) {
+            return Collections.emptyList();
+        }
+        // 批量获取企业名称映射
+        Map<Long, String> entNameMap = enterpriseNameMap(records.stream().map(KwsWeighbridge::getEntId).collect(Collectors.toSet()));
+        // 批量获取打印机名称映射(过滤null ID)
+        Map<Long, String> printerNameMap = printerNameMap(records.stream().map(KwsWeighbridge::getPrinterId)
+                .filter(Objects::nonNull).collect(Collectors.toSet()));
+        List<WeighbridgePageResVo> result = new ArrayList<>(records.size());
+        for (KwsWeighbridge record : records) {
+            WeighbridgePageResVo resVo = new WeighbridgePageResVo();
+            // 填充单个记录的基础信息
+            fillBaseRes(record, entNameMap, printerNameMap, resVo);
+            result.add(resVo);
+        }
+        return result;
+    }
+
+    /**
+     * 填充基础响应信息
+     * <p>
+     * 将地磅实体数据及关联的名称映射填充到响应VO中,并转换状态枚举为中文描述。
+     *
+     * @param record        地磅实体
+     * @param entNameMap    企业名称映射
+     * @param printerNameMap 打印机名称映射
+     * @param resVo         响应VO
+     */
+    private void fillBaseRes(KwsWeighbridge record, Map<Long, String> entNameMap, Map<Long, String> printerNameMap,
+                             WeighbridgePageResVo resVo) {
+        resVo.setId(record.getId());
+        resVo.setEntId(record.getEntId());
+        resVo.setEnterpriseName(entNameMap.getOrDefault(record.getEntId(), ""));
+        resVo.setWeighbridgeName(record.getWeighbridgeName());
+        resVo.setUniqueCode(record.getUniqueCode());
+        resVo.setOnlineStatus(record.getOnlineStatus());
+        // 转换在线状态:1-在线,0-离线
+        resVo.setOnlineStatusName(Objects.equals(record.getOnlineStatus(), 1) ? "在线" : "离线");
+        resVo.setPrinterId(record.getPrinterId());
+        resVo.setPrinterName(printerNameMap.getOrDefault(record.getPrinterId(), ""));
+        resVo.setDescription(record.getDescription());
+        resVo.setStatus(record.getStatus());
+        // 转换启用状态:0-启用,1-停用
+        resVo.setStatusName(Objects.equals(record.getStatus(), 0) ? "启用" : "停用");
+        resVo.setCreateTime(record.getCreateTime());
+        resVo.setLastRestartTime(record.getLastRestartTime());
+    }
+
+    /**
+     * 获取企业名称映射
+     * <p>
+     * 根据企业ID集合批量查询企业名称,返回ID到名称的Map。
+     *
+     * @param entIds 企业ID集合
+     * @return 企业ID到名称的映射
+     */
+    private Map<Long, String> enterpriseNameMap(Collection<Long> entIds) {
+        if (entIds == null || entIds.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return kwsEnterpriseRepository.listByIds(entIds).stream()
+                .collect(Collectors.toMap(KwsEnterprise::getId, KwsEnterprise::getFirmName, (a, b) -> a));
+    }
+
+    /**
+     * 获取打印机名称映射
+     * <p>
+     * 根据打印机ID集合批量查询打印机名称,返回ID到名称的Map。
+     *
+     * @param printerIds 打印机ID集合
+     * @return 打印机ID到名称的映射
+     */
+    private Map<Long, String> printerNameMap(Collection<Long> printerIds) {
+        if (printerIds == null || printerIds.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        return kwsPrinterRepository.listByIds(printerIds).stream()
+                .collect(Collectors.toMap(KwsPrinter::getId, KwsPrinter::getPrinterName, (a, b) -> a));
+    }
+
+    /**
+     * 校验地磅保存请求参数
+     * <p>
+     * 检查必填项、企业权限、打印机有效性及唯一编码冲突。
+     *
+     * @param reqVo     保存请求参数
+     * @param excludeId 排除的地磅ID(用于更新时排除自身,新增时为null)
+     */
+    private void validateSaveReq(WeighbridgeSaveReqVo reqVo, Long excludeId) {
+        if (reqVo.getEntId() == null) {
+            throw new SystemException("企业ID不能为空");
+        }
+        if (StringUtils.isBlank(reqVo.getWeighbridgeName())) {
+            throw new SystemException("地磅名称不能为空");
+        }
+        if (StringUtils.isBlank(reqVo.getUniqueCode())) {
+            throw new SystemException("唯一编码不能为空");
+        }
+        // 校验企业是否存在且有权限操作
+        checkEnterprise(reqVo.getEntId());
+        // 校验打印机是否属于该企业且可用
+        checkPrinter(reqVo.getEntId(), reqVo.getPrinterId());
+        // 校验唯一编码是否冲突
+        KwsWeighbridge exists = kwsWeighbridgeRepository.findByUniqueCode(reqVo.getUniqueCode().trim());
+        if (exists != null && !Objects.equals(exists.getId(), excludeId)) {
+            throw new SystemException("唯一编码已存在");
+        }
+    }
+
+    /**
+     * 校验地磅误差配置请求参数
+     * <p>
+     * 检查企业ID及各项误差值是否为空。
+     *
+     * @param reqVo 误差配置请求参数
+     */
+    private void validateDiffConfigReq(WeighbridgeDiffConfigReqVo reqVo) {
+        if (reqVo.getEntId() == null) {
+            throw new SystemException("企业ID不能为空");
+        }
+        if (reqVo.getTareErrorValue() == null) {
+            throw new SystemException("皮重误差值不能为空");
+        }
+        if (reqVo.getLoadErrorValue() == null) {
+            throw new SystemException("载重误差值不能为空");
+        }
+        if (reqVo.getEmptyLoadValue() == null) {
+            throw new SystemException("空载值不能为空");
+        }
+    }
+
+    /**
+     * 校验打印机权限及状态
+     * <p>
+     * 确保指定的打印机ID属于当前企业且处于可用状态。
+     *
+     * @param entId     企业ID
+     * @param printerId 打印机ID
+     */
+    private void checkPrinter(Long entId, Long printerId) {
+        if (printerId == null) {
+            return;
+        }
+        KwsPrinter printer = kwsPrinterRepository.findAvailableById(printerId);
+        if (printer == null || !Objects.equals(printer.getEntId(), entId)) {
+            throw new SystemException("该企业的打印机不可用");
+        }
+    }
+
+    /**
+     * 校验企业权限及状态
+     * <p>
+     * 确保当前用户有权限操作该企业,且企业未被删除或停用。
+     *
+     * @param entId 企业ID
+     */
+    private void checkEnterprise(Long entId) {
+        // 判断是否有权限:管理员或企业在授权列表中
+        boolean allowed = LoginUserHolder.isManager() || resolveAuthorizedEntIds().contains(entId);
+        if (!allowed) {
+            throw new SystemException("无权操作该企业");
+        }
+        // 检查企业实体状态
+        KwsEnterprise enterprise = kwsEnterpriseRepository.getById(entId);
+        if (enterprise == null || Objects.equals(enterprise.getDelFlag(), 1) || Objects.equals(enterprise.getStatus(), 1)) {
+            throw new SystemException(HttpStatus.ENT_NOT_EXISTS);
+        }
+    }
+
+    /**
+     * 获取并校验地磅信息
+     * <p>
+     * 根据地磅ID查询地磅,并校验其存在性及当前用户的操作权限。
+     *
+     * @param id 地磅ID
+     * @return 地磅实体
+     */
+    private KwsWeighbridge getAndCheck(Long id) {
+        KwsWeighbridge weighbridge = kwsWeighbridgeRepository.findAvailableById(id);
+        if (weighbridge == null) {
+            throw new SystemException("地磅不存在");
+        }
+        // 非管理员需校验地磅所属企业是否在授权范围内
+        if (!LoginUserHolder.isManager() && !resolveAuthorizedEntIds().contains(weighbridge.getEntId())) {
+            throw new SystemException("无权操作该地磅");
+        }
+        return weighbridge;
+    }
+
+    /**
+     * 解析查询条件中的企业ID集合
+     * <p>
+     * 根据企业名称关键词和用户权限,解析出可用于查询的地磅所属企业ID集合。
+     *
+     * @param enterpriseName 企业名称关键词
+     * @return 企业ID集合
+     */
+    private Set<Long> resolveQueryEntIds(String enterpriseName) {
+        Set<Long> authEntIds = resolveAuthorizedEntIds();
+        List<KwsEnterprise> enterpriseList;
+        if (LoginUserHolder.isManager()) {
+            // 管理员可根据名称模糊查询所有企业
+            enterpriseList = kwsEnterpriseRepository.queryByEntIdAndName(null, enterpriseName);
+        } else if (authEntIds.isEmpty()) {
+            // 普通用户无授权企业,返回空集合
+            return Collections.emptySet();
+        } else {
+            // 普通用户在授权企业中根据名称模糊查询
+            enterpriseList = kwsEnterpriseRepository.queryByEntIds(authEntIds, enterpriseName);
+        }
+        // 提取ID并去重,保持插入顺序
+        return enterpriseList.stream().map(KwsEnterprise::getId).collect(Collectors.toCollection(LinkedHashSet::new));
+    }
+
+    /**
+     * 解析当前用户授权的企业ID集合
+     * <p>
+     * 获取当前登录用户有权访问的所有企业ID,包括当前企业、授权企业及子企业。
+     * 管理员返回空集合,表示拥有所有权限(通常在业务逻辑中特殊处理)。
+     *
+     * @return 企业ID集合
+     */
+    private Set<Long> resolveAuthorizedEntIds() {
+        if (LoginUserHolder.isManager()) {
+            return Collections.emptySet();
+        }
+        Set<Long> result = new LinkedHashSet<>();
+        // 添加当前登录用户所属企业
+        if (LoginUserHolder.getEntId() != null) {
+            result.add(LoginUserHolder.getEntId());
+        }
+        // 添加额外授权的企业
+        result.addAll(LoginUserHolder.getAuthEntIdList());
+        // 添加子企业
+        result.addAll(LoginUserHolder.getChildEntList());
+        return result;
+    }
+
+    /**
+     * 获取默认的在线状态
+     * <p>
+     * 若传入状态为1则返回1,否则默认返回0(离线)。
+     *
+     * @param onlineStatus 传入的在线状态
+     * @return 默认在线状态(1为在线,0为离线)
+     */
+    private Integer defaultOnlineStatus(Integer onlineStatus) {
+        return Objects.equals(onlineStatus, 1) ? 1 : 0;
+    }
+
+    /**
+     * 构建误差配置JSON字符串
+     * <p>
+     * 将误差配置请求参数转换为JSON字符串,用于存储在地磅表的diff_config字段中。
+     *
+     * @param reqVo 误差配置请求参数
+     * @return JSON字符串
+     */
+    private String buildDiffConfigJson(WeighbridgeDiffConfigReqVo reqVo) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("entId", reqVo.getEntId());
+        jsonObject.put("tareErrorValue", reqVo.getTareErrorValue());
+        jsonObject.put("loadErrorValue", reqVo.getLoadErrorValue());
+        jsonObject.put("emptyLoadValue", reqVo.getEmptyLoadValue());
+        return jsonObject.toJSONString();
+    }
+}