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;
/**
*
* 物料信息表配置接口实现类
*
*
* @author deslrey
* @version 1.0
* @since 2026/03/11 16:59
*/
@Slf4j
@Service
public class ProductMaterialConfigServiceImpl extends ServiceImpl implements ProductMaterialConfigService {
@Override
public List materialTypeList() {
return getByType(MaterialConfigTypeEnum.MATERIAL_TYPE);
}
@Override
public List inventoryCategoryList() {
return getByType(MaterialConfigTypeEnum.INVENTORY_CAT);
}
private List getByType(MaterialConfigTypeEnum type) {
return list(new LambdaQueryWrapper().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 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 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;
}
}