zhangherong
2025-07-09 c800257cb6c8b45e7edc20e2e9018cd90b230806
lxzn-module-system/lxzn-system-biz/src/main/java/org/jeecg/modules/system/service/impl/BaseFactoryServiceImpl.java
@@ -3,10 +3,12 @@
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.BaseFactory;
@@ -100,32 +102,81 @@
        return Collections.emptyList();
    }
    public BaseFactory searchCenterByKeyWord(String keyWord) {
        if (StringUtils.isBlank(keyWord)) {
            return null;
        }
        if (keyWord.contains("/")) {
            return searchByHierarchy(keyWord);
        } else {
            return searchByCode(keyWord);
        }
    }
    private BaseFactory searchByHierarchy(String path) {
        String[] levels = path.split("/");
        int depth = levels.length;
        // 检查层级深度
        if (depth < 2 || depth > 3) {
            return null; // 无效的层级深度
        }
        // 查询第一级中心
        BaseFactory center = findFactory(levels[0], 1, null);
        if (center == null) return null;
        // 查询第二级工区
        BaseFactory workArea = findFactory(levels[1], null, center.getId());
        if (workArea == null || depth == 2) return workArea;
        // 查询第三级工段
        return findFactory(levels[2], null, workArea.getId());
    }
    private BaseFactory searchByCode(String code) {
        LambdaQueryWrapper<BaseFactory> query = new LambdaQueryWrapper<>();
        query.like(BaseFactory::getFactoryCode, code);
        return this.baseMapper.selectOne(query); // 使用selectOne避免多个结果
    }
    private BaseFactory findFactory(String name, Integer category, String parentId) {
        LambdaQueryWrapper<BaseFactory> query = new LambdaQueryWrapper<>();
        query.eq(BaseFactory::getFactoryName, name);
        if (category != null) query.eq(BaseFactory::getFactoryCategory, category);
        if (parentId != null) query.eq(BaseFactory::getParentId, parentId);
        return this.baseMapper.selectOne(query);
    }
    /**
     * saveProductionData 对应 add 保存用户在页面添加的新的设备车间管理对象数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBaseFactoryData(BaseFactory BaseFactory) {
        if (BaseFactory != null) {
            if (BaseFactory.getParentId() == null|| StrUtil.isEmpty(BaseFactory.getParentId())) {
                BaseFactory.setParentId("");
                BaseFactory.setFactoryCategory("0");
    public void saveBaseFactoryData(BaseFactory baseFactory) {
        if (baseFactory != null) {
            if (baseFactory.getParentId() == null|| StrUtil.isEmpty(baseFactory.getParentId())) {
                baseFactory.setParentId("");
                baseFactory.setFactoryCategory("0");
            }
            if (BaseFactory.getFactoryCategory() == null|| StrUtil.isEmpty(BaseFactory.getFactoryCategory())) {
                BaseFactory.setFactoryCategory("1");
            if (baseFactory.getFactoryCategory() == null|| StrUtil.isEmpty(baseFactory.getFactoryCategory())) {
                baseFactory.setFactoryCategory("1");
            }
            BaseFactory.setId(IdWorker.getIdStr(BaseFactory));
            baseFactory.setId(IdWorker.getIdStr(baseFactory));
            // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级
            // 获取父级ID
            String parentId = BaseFactory.getParentId();
            String parentId = baseFactory.getParentId();
            JSONObject formData = new JSONObject();
            formData.put("parentId",parentId);
            String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.WORKSHOP,formData);
            BaseFactory.setOrgCode(codeArray[0]);
            baseFactory.setOrgCode(codeArray[0]);
            String orgType = codeArray[1];
            BaseFactory.setOrgType(String.valueOf(orgType));
            BaseFactory.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            this.save(BaseFactory);
            baseFactory.setOrgType(String.valueOf(orgType));
            baseFactory.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            this.save(baseFactory);
        }
    }
@@ -145,9 +196,15 @@
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBaseFactoryDataById(BaseFactory BaseFactory) {
        if (BaseFactory != null) {
            this.updateById(BaseFactory);
    public boolean updateBaseFactoryDataById(BaseFactory baseFactory) {
        QueryWrapper<BaseFactory> baseFactoryQueryWrapper = new QueryWrapper<>();
        baseFactoryQueryWrapper.eq(StrUtil.isNotEmpty(baseFactory.getFactoryCode()), "factory_code", baseFactory.getFactoryCode());
        baseFactoryQueryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0.toString());
        if (this.getOne(baseFactoryQueryWrapper) != null) {
            throw new JeecgBootException("机构编码已存在");
        }
        if (baseFactory != null) {
            this.updateById(baseFactory);
            return true;
        }
        return false;