package com.ruoyi.business.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.basic.entity.CoalField; import com.ruoyi.basic.entity.CoalInfo; import com.ruoyi.basic.entity.CoalValue; import com.ruoyi.basic.entity.Supply; import com.ruoyi.basic.mapper.CoalFieldMapper; import com.ruoyi.basic.mapper.CoalInfoMapper; import com.ruoyi.basic.mapper.CoalValueMapper; import com.ruoyi.basic.mapper.SupplyMapper; import com.ruoyi.business.dto.OfficialInventoryDto; import com.ruoyi.business.entity.OfficialInventory; import com.ruoyi.business.mapper.OfficialInventoryMapper; import com.ruoyi.business.service.OfficialInventoryService; import com.ruoyi.business.vo.OfficialInventoryVo; import com.ruoyi.common.exception.base.BaseException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.bean.BeanUtils; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** *

* 正式库存表 服务实现类 *

* * @author ruoyi * @since 2025-06-04 */ @Service @RequiredArgsConstructor public class OfficialInventoryServiceImpl extends ServiceImpl implements OfficialInventoryService { private final OfficialInventoryMapper officialInventoryMapper; private final CoalValueMapper coalValueMapper; private final CoalFieldMapper coalFieldMapper; private final CoalInfoMapper coalInfoMapper; private final SupplyMapper supplyMapper; @Override public IPage selectOfficialInventoryList(Page page, OfficialInventoryDto officialInventoryDto) { // 先查出原始数据(OfficialInventory) LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.orderByAsc(OfficialInventory::getCreateTime); IPage entityPage = officialInventoryMapper.selectPage(page, queryWrapper); // 创建一个新的 Dto 分页结果 IPage dtoPage = new Page<>(); BeanUtils.copyProperties(entityPage, dtoPage); List dtoList = new ArrayList<>(); List supplierIds = entityPage.getRecords().stream() .map(OfficialInventory::getSupplierId) .toList(); Map supplyMap; if (!supplierIds.isEmpty()) { List infos = supplyMapper.selectList(new LambdaQueryWrapper().in(Supply::getId, supplierIds)); supplyMap = infos.stream().collect(Collectors.toMap(Supply::getId, Function.identity())); } else { supplyMap = new HashMap<>(); } // 查询所有可用字段(CoalField) List coalFields = coalFieldMapper.selectList(null); List allFieldNames = coalFields.stream() .map(CoalField::getFields) .distinct() .collect(Collectors.toList()); //查询煤种ids List coalIds = entityPage.getRecords().stream() .map(OfficialInventory::getCoalId) .distinct() .collect(Collectors.toList()); // 批量查询CoalInfo Map coalInfoMap; if (!coalIds.isEmpty()) { List coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper().in(CoalInfo::getId, coalIds)); coalInfoMap = coalInfos.stream().collect(Collectors.toMap(CoalInfo::getId, Function.identity())); } else { coalInfoMap = new HashMap<>(); } // 遍历每条记录,进行转换并填充 fields for (OfficialInventory entity : entityPage.getRecords()) { OfficialInventoryDto dto = new OfficialInventoryDto(); BeanUtils.copyProperties(entity, dto); // 供应商信息 Supply supply = supplyMap.get(entity.getSupplierId()); if (supply != null) { dto.setSupplierName(supply.getSupplierName()); } List coalValues; if (entity.getMergeId() == null) { coalValues = coalValueMapper.selectList(new LambdaQueryWrapper() .eq(CoalValue::getPlanId, entity.getPendingId()) .and(wrapper -> wrapper.ne(CoalValue::getType, "2").or().isNull(CoalValue::getType)) ); } else { coalValues = coalValueMapper.selectList(new LambdaQueryWrapper() .eq(CoalValue::getPlanId, entity.getId()) .eq(CoalValue::getType, "2") ); } // 构建 Map Map fieldValueMap = coalValues.stream() .collect(Collectors.toMap( CoalValue::getFields, CoalValue::getCoalValue, (existing, replacement) -> existing // 重复字段保留第一个 )); // 构造最终 fields 列表,包含所有字段名,并设置默认值 "-" List> fields = new ArrayList<>(); for (String field : allFieldNames) { Map fieldMap = new HashMap<>(); fieldMap.put(field, fieldValueMap.getOrDefault(field, "-")); fields.add(fieldMap); } // 设置Coal信息 CoalInfo coalInfo = coalInfoMap.get(entity.getCoalId()); if (coalInfo != null) { dto.setCoal(coalInfo.getCoal()); } // 设置到 DTO 中 dto.setFields(fields); dtoList.add(dto); } dtoPage.setRecords(dtoList); // 设置转换后的 DtoList return dtoPage; } @Override public int editOfficial(OfficialInventoryDto officialInventoryDto) { OfficialInventory officialInventory = new OfficialInventory(); BeanUtils.copyProperties(officialInventoryDto, officialInventory); if (officialInventoryDto.getMergeId() != null) { // 1. 构建查询条件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CoalValue::getPlanId, officialInventoryDto.getId()) .eq(CoalValue::getType, "2"); // 2. 查询多个符合条件的CoalValue记录 List coalValues = coalValueMapper.selectList(queryWrapper); if (!CollectionUtils.isEmpty(coalValues) && !CollectionUtils.isEmpty(officialInventoryDto.getFields())) { // 3. 创建字段映射关系 (field key -> coal_value) Map fieldValueMap = new HashMap<>(); for (Map fieldMap : officialInventoryDto.getFields()) { fieldValueMap.putAll(fieldMap); } // 4. 更新 for (CoalValue coalValue : coalValues) { String fieldKey = coalValue.getFields(); // 数据库中的field key if (fieldValueMap.containsKey(fieldKey)) { String newValue = fieldValueMap.get(fieldKey); if (!Objects.equals(coalValue.getCoalValue(), newValue)) { coalValue.setCoalValue(newValue); coalValueMapper.updateById(coalValue); } } } } } else { // 构建查询条件 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(CoalValue::getPlanId, officialInventoryDto.getPendingId()) .eq(CoalValue::getType, "1"); // 2. 查询多个符合条件的CoalValue记录 List coalValues = coalValueMapper.selectList(queryWrapper); if (!CollectionUtils.isEmpty(coalValues) && !CollectionUtils.isEmpty(officialInventoryDto.getFields())) { // 3. 创建字段映射关系 (field key -> coal_value) Map fieldValueMap = new HashMap<>(); for (Map fieldMap : officialInventoryDto.getFields()) { fieldValueMap.putAll(fieldMap); } // 4. 更新 for (CoalValue coalValue : coalValues) { String fieldKey = coalValue.getFields(); // 数据库中的field key if (fieldValueMap.containsKey(fieldKey)) { String newValue = fieldValueMap.get(fieldKey); if (!Objects.equals(coalValue.getCoalValue(), newValue)) { coalValue.setCoalValue(newValue); coalValueMapper.updateById(coalValue); } } } } } return officialInventoryMapper.updateById(officialInventory); } @Override public List selectOfficialList(OfficialInventoryVo officialInventoryVo) { List officialInventories = officialInventoryMapper.selectList(null); return officialInventories.stream() .map(OI -> { OfficialInventoryVo vo = new OfficialInventoryVo(); BeanUtils.copyProperties(OI, vo); CoalInfo coalInfo = coalInfoMapper.selectById(OI.getCoalId()); vo.setCoal(coalInfo.getCoal()); return vo; }) .collect(Collectors.toList()); } @Override public List selectOfficialAll() { return officialInventoryMapper.selectList(null); } @Transactional @Override public int mergeAll(OfficialInventoryDto officialInventoryDto) { List ids = officialInventoryDto.getIds(); // 校验参数 if (CollectionUtils.isEmpty(ids) || ids.size() < 2) { throw new BaseException("请选中至少两条数据"); } if (CollectionUtils.isEmpty(officialInventoryDto.getFields())) { throw new BaseException("字段值不能为空"); } // 1. 批量标记删除旧数据 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.in(OfficialInventory::getId, ids) .set(OfficialInventory::getDeleted, 1); int rowsAffected = officialInventoryMapper.update(null, updateWrapper); if (rowsAffected == 0) { throw new BaseException("未找到匹配的数据,请确认选择是否正确"); } // 2. 插入新库存记录 OfficialInventory officialInventory = new OfficialInventory(); BeanUtils.copyProperties(officialInventoryDto, officialInventory); officialInventory.setId(null); officialInventory.setMergeId(ids.toString()); officialInventory.setSupplierId(officialInventoryDto.getSupplierId()); officialInventory.setRegistrantId(SecurityUtils.getLoginUser().getUser().getUserId()); if (officialInventoryMapper.insert(officialInventory) <= 0) { throw new BaseException("库存记录创建失败"); } // 3. 批量处理字段值 batchProcessCoalValues(officialInventory.getId(), officialInventoryDto.getFields()); return rowsAffected; } private void batchProcessCoalValues(Long planId, List> fields) { // 1. 提取所有唯一字段标识 Set allFields = fields.stream() .flatMap(map -> map.keySet().stream()) .collect(Collectors.toSet()); // 2. 查询字段映射关系 List coalFields = coalFieldMapper.selectList( new LambdaQueryWrapper().in(CoalField::getFields, allFields) ); Map fieldMap = coalFields.stream() .collect(Collectors.toMap( CoalField::getFields, CoalField::getFieldName )); // 3. 构造并插入每条记录 CoalValue coalValueTemplate = new CoalValue(); coalValueTemplate.setPlanId(planId); coalValueTemplate.setType("2"); for (Map fieldMapEntry : fields) { for (Map.Entry entry : fieldMapEntry.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); String fieldName = fieldMap.get(key); if (fieldName == null) { throw new BaseException("字段名不存在: " + key); } CoalValue coalValue = new CoalValue(); BeanUtils.copyProperties(coalValueTemplate, coalValue); // 复用模板属性 coalValue.setId(null); coalValue.setCoalValue(value); coalValue.setType("2"); coalValue.setPlanId(planId); coalValue.setFields(key); coalValue.setFieldName(fieldName); // 单条插入 int result = coalValueMapper.insert(coalValue); if (result <= 0) { throw new BaseException("字段值保存失败,字段:" + key); } } } } }