huminmin
3 天以前 a8a78a1b733914ea97393ccc5a81bf7ea76ed5aa
src/main/java/com/ruoyi/production/service/impl/ProductMaterialConfigServiceImpl.java
@@ -1,11 +1,19 @@
package com.ruoyi.production.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.dto.ProductMaterialConfigDto;
import com.ruoyi.production.enums.MaterialConfigTypeEnum;
import com.ruoyi.production.mapper.ProductMaterialConfigMapper;
import com.ruoyi.production.pojo.ProductMaterialConfig;
import com.ruoyi.production.service.ProductMaterialConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * <br>
@@ -20,5 +28,93 @@
@Service
public class ProductMaterialConfigServiceImpl extends ServiceImpl<ProductMaterialConfigMapper, ProductMaterialConfig> implements ProductMaterialConfigService {
    @Override
    public List<ProductMaterialConfig> materialTypeList() {
        return getByType(MaterialConfigTypeEnum.MATERIAL_TYPE);
    }
    @Override
    public List<ProductMaterialConfig> inventoryCategoryList() {
        return getByType(MaterialConfigTypeEnum.INVENTORY_CAT);
    }
    private List<ProductMaterialConfig> getByType(MaterialConfigTypeEnum type) {
        return list(new LambdaQueryWrapper<ProductMaterialConfig>().eq(ProductMaterialConfig::getConfigType, type.name()));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProductMaterialConfig(ProductMaterialConfigDto config) {
        config.setConfigType(MaterialConfigTypeEnum.getConfigType(config.getType()));
        validateConfig(config, false);
        if (existsConfig(config.getConfigType(), config.getConfigName(), null)) {
            throw new ServiceException("配置名称已存在");
        }
        if (!this.save(config)) {
            throw new ServiceException("新增配置失败");
        }
        log.info("新增物料配置成功 type={}, name={}", config.getConfigType(), config.getConfigName());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductMaterialConfig(ProductMaterialConfigDto config) {
        config.setConfigType(MaterialConfigTypeEnum.getConfigType(config.getType()));
        validateConfig(config, true);
        ProductMaterialConfig exist = this.getById(config.getId());
        if (exist == null) {
            throw new ServiceException("配置不存在");
        }
        if (existsConfig(config.getConfigType(), config.getConfigName(), config.getId())) {
            throw new ServiceException("配置名称已存在");
        }
        if (!this.updateById(config)) {
            throw new ServiceException("修改配置失败");
        }
        log.info("修改物料配置成功 id={}", config.getId());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductMaterialConfig(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择至少一条数据");
        }
        if (!this.removeByIds(ids)) {
            throw new ServiceException("删除配置失败");
        }
        log.info("删除物料配置成功 ids={}", ids);
    }
    private void validateConfig(ProductMaterialConfig config, boolean requireId) {
        if (config == null) {
            throw new ServiceException("参数不能为空");
        }
        if (requireId && config.getId() == null) {
            throw new ServiceException("主键ID不能为空");
        }
        if (StringUtils.isEmpty(config.getConfigType())) {
            throw new ServiceException("配置类型不能为空");
        }
        try {
            MaterialConfigTypeEnum.valueOf(config.getConfigType());
        } catch (IllegalArgumentException e) {
            throw new ServiceException("配置类型不合法");
        }
        if (StringUtils.isEmpty(config.getConfigName())) {
            throw new ServiceException("配置名称不能为空");
        }
    }
    private boolean existsConfig(String configType, String configName, Integer excludeId) {
        if (StringUtils.isEmpty(configName) || StringUtils.isEmpty(configType)) {
            return false;
        }
        LambdaQueryWrapper<ProductMaterialConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductMaterialConfig::getConfigType, configType).eq(ProductMaterialConfig::getConfigName, configName);
        if (excludeId != null) {
            wrapper.ne(ProductMaterialConfig::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }
}