| | |
| | | import com.ruoyi.basic.mapper.CoalValueMapper; |
| | | import com.ruoyi.basic.mapper.SupplyMapper; |
| | | import com.ruoyi.business.dto.OfficialInventoryDto; |
| | | import com.ruoyi.business.dto.SalesRecordDto; |
| | | import com.ruoyi.business.entity.OfficialInventory; |
| | | import com.ruoyi.business.entity.SalesRecord; |
| | | import com.ruoyi.business.mapper.OfficialInventoryMapper; |
| | | import com.ruoyi.business.service.OfficialInventoryService; |
| | | import com.ruoyi.business.utils.DynamicExcelUtil; |
| | | import com.ruoyi.business.vo.OfficialInventoryExportVo; |
| | | import com.ruoyi.business.vo.OfficialInventoryVo; |
| | | import com.ruoyi.business.vo.SalesRecordExportVo; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | * @since 2025-06-04 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | public class OfficialInventoryServiceImpl extends ServiceImpl<OfficialInventoryMapper, OfficialInventory> implements OfficialInventoryService { |
| | | |
| | |
| | | |
| | | private final SupplyMapper supplyMapper; |
| | | |
| | | private final SysUserMapper sysUserMapper; |
| | | |
| | | |
| | | @Override |
| | | public IPage<OfficialInventoryDto> selectOfficialInventoryList(Page page, OfficialInventoryDto officialInventoryDto) { |
| | | public IPage<OfficialInventoryDto> selectOfficialInventoryList(Page<OfficialInventory> page, OfficialInventoryDto officialInventoryDto) { |
| | | |
| | | // 先查出原始数据(OfficialInventory) |
| | | LambdaQueryWrapper<OfficialInventory> queryWrapper = new LambdaQueryWrapper<>(); |
| | |
| | | .map(OfficialInventory::getSupplierId) |
| | | .toList(); |
| | | |
| | | List<Long> registrantIds = entityPage.getRecords().stream() |
| | | .map(OfficialInventory::getRegistrantId) |
| | | .toList(); |
| | | |
| | | Map<Long, Supply> supplyMap; |
| | | if (!supplierIds.isEmpty()) { |
| | | List<Supply> infos = supplyMapper.selectList(new LambdaQueryWrapper<Supply>().in(Supply::getId, supplierIds)); |
| | |
| | | supplyMap = new HashMap<>(); |
| | | } |
| | | |
| | | //登记人 |
| | | Map<Long, SysUser> userMap; |
| | | if (!registrantIds.isEmpty()) { |
| | | List<SysUser> sysUsers = sysUserMapper.selectList(registrantIds); |
| | | userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity())); |
| | | } else { |
| | | userMap = new HashMap<>(); |
| | | } |
| | | |
| | | // 查询所有可用字段(CoalField) |
| | | List<CoalField> coalFields = coalFieldMapper.selectList(null); |
| | | List<String> allFieldNames = coalFields.stream() |
| | | .map(CoalField::getFields) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | .toList(); |
| | | |
| | | //查询煤种ids |
| | | List<Long> coalIds = entityPage.getRecords().stream() |
| | | .map(OfficialInventory::getCoalId) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | .toList(); |
| | | |
| | | // 批量查询CoalInfo |
| | | Map<Long, CoalInfo> coalInfoMap; |
| | |
| | | Supply supply = supplyMap.get(entity.getSupplierId()); |
| | | if (supply != null) { |
| | | dto.setSupplierName(supply.getSupplierName()); |
| | | } |
| | | |
| | | // 登记人 |
| | | SysUser sysUser = userMap.get(entity.getRegistrantId()); |
| | | if (sysUser != null) { |
| | | dto.setRegistrant(sysUser.getNickName()); |
| | | } |
| | | |
| | | List<CoalValue> coalValues; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void officialInventoryExport(HttpServletResponse response, OfficialInventoryDto officialInventoryDto) { |
| | | try { |
| | | // 获取煤质字段配置 |
| | | List<CoalField> allCoalFields = coalFieldMapper.selectList(null); |
| | | List<String> dynamicHeaders = allCoalFields.stream() |
| | | .map(CoalField::getFieldName) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 获取数据 |
| | | List<OfficialInventory> list = officialInventoryDto.getExportIds() != null && !officialInventoryDto.getExportIds().isEmpty() |
| | | ? officialInventoryMapper.selectByIds(officialInventoryDto.getExportIds()) |
| | | : officialInventoryMapper.selectList(null); |
| | | |
| | | // 转换为导出VO |
| | | List<OfficialInventoryExportVo> exportData = convertToExportVo(list, allCoalFields); |
| | | |
| | | // 使用增强的Excel工具导出 |
| | | DynamicExcelUtil<OfficialInventoryExportVo> util = |
| | | new DynamicExcelUtil<>(OfficialInventoryExportVo.class, dynamicHeaders); |
| | | |
| | | // 导出Excel |
| | | util.exportExcel(response, exportData, "库存数据"); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("库存导出失败", e); |
| | | // 可以考虑返回更友好的错误信息给前端 |
| | | throw new RuntimeException("导出失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private List<OfficialInventoryExportVo> convertToExportVo(List<OfficialInventory> list, List<CoalField> coalFields) { |
| | | if (CollectionUtils.isEmpty(list)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | // 收集所有需要查询的ID |
| | | Set<Long> coalIds = list.stream() |
| | | .map(OfficialInventory::getCoalId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Set<Long> supplierIds = list.stream() |
| | | .map(OfficialInventory::getSupplierId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Set<Long> registrantIds = list.stream() |
| | | .map(OfficialInventory::getRegistrantId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Set<Long> planIds = list.stream() |
| | | .map(OfficialInventory::getCoalPlanId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | // 批量查询关联数据 |
| | | Map<Long, CoalInfo> coalInfoMap = getCoalInfoMap(coalIds); |
| | | Map<Long, Supply> supplyMap = getSupplyMap(supplierIds); |
| | | Map<Long, SysUser> userMap = getUserMap(registrantIds); |
| | | Map<Long, List<CoalValue>> coalValuesMap = getCoalValuesMap(planIds); |
| | | |
| | | // 构建字段ID到字段名称的映射,提高查找效率 |
| | | Map<String, String> fieldIdToNameMap = coalFields.stream() |
| | | .collect(Collectors.toMap(CoalField::getFields, CoalField::getFieldName)); |
| | | |
| | | // 转换数据 |
| | | return list.stream().map(record -> { |
| | | OfficialInventoryExportVo vo = new OfficialInventoryExportVo(); |
| | | vo.initDynamicFields(); // 初始化 |
| | | |
| | | // 设置基础属性 |
| | | BeanUtils.copyProperties(record, vo); |
| | | |
| | | // 设置关联信息 |
| | | setCoalInfo(vo, record.getCoalId(), coalInfoMap); |
| | | setSupplierInfo(vo, record.getSupplierId(), supplyMap); |
| | | setUserMapInfo(vo, record.getRegistrantId(), userMap); |
| | | |
| | | // 动态字段处理 |
| | | if (record.getCoalPlanId() != null) { |
| | | List<CoalValue> values = coalValuesMap.getOrDefault(record.getCoalPlanId(), Collections.emptyList()); |
| | | setDynamicFields(vo, values, fieldIdToNameMap); |
| | | } |
| | | |
| | | return vo; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | private void setCoalInfo(OfficialInventoryExportVo vo, Long coalId, Map<Long, CoalInfo> coalInfoMap) { |
| | | if (coalId != null && coalInfoMap.containsKey(coalId)) { |
| | | vo.setCoal(coalInfoMap.get(coalId).getCoal()); |
| | | } |
| | | } |
| | | |
| | | private void setSupplierInfo(OfficialInventoryExportVo vo, Long supplierId, Map<Long, Supply> supplyMap) { |
| | | if (supplierId != null && supplyMap.containsKey(supplierId)) { |
| | | vo.setSupplierName(supplyMap.get(supplierId).getSupplierName()); |
| | | } |
| | | } |
| | | |
| | | private void setUserMapInfo(OfficialInventoryExportVo vo, Long registrantId, Map<Long, SysUser> userMap) { |
| | | if (registrantId != null && userMap.containsKey(registrantId)) { |
| | | vo.setRegistrant(userMap.get(registrantId).getNickName()); |
| | | } |
| | | } |
| | | |
| | | private void setDynamicFields(OfficialInventoryExportVo vo, List<CoalValue> values, Map<String, String> fieldIdToNameMap) { |
| | | for (CoalValue value : values) { |
| | | String fieldName = fieldIdToNameMap.get(value.getFields()); |
| | | if (fieldName != null) { |
| | | vo.getCoalQualityProperties().put(fieldName, value.getCoalValue()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private Map<Long, List<CoalValue>> getCoalValuesMap(Set<Long> planIds) { |
| | | if (CollectionUtils.isEmpty(planIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | List<CoalValue> allValues = coalValueMapper.selectList( |
| | | new LambdaQueryWrapper<CoalValue>() |
| | | .in(CoalValue::getPlanId, planIds)); |
| | | |
| | | return allValues.stream() |
| | | .collect(Collectors.groupingBy(CoalValue::getPlanId)); |
| | | } |
| | | |
| | | private Map<Long, CoalInfo> getCoalInfoMap(Set<Long> coalIds) { |
| | | if (CollectionUtils.isEmpty(coalIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return coalInfoMapper.selectByIds(coalIds).stream() |
| | | .collect(Collectors.toMap(CoalInfo::getId, Function.identity())); |
| | | } |
| | | |
| | | private Map<Long, Supply> getSupplyMap(Set<Long> supplierIds) { |
| | | if (CollectionUtils.isEmpty(supplierIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return supplyMapper.selectByIds(supplierIds).stream() |
| | | .collect(Collectors.toMap(Supply::getId, Function.identity())); |
| | | } |
| | | |
| | | private Map<Long, SysUser> getUserMap(Set<Long> registrantIds) { |
| | | if (CollectionUtils.isEmpty(registrantIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | return sysUserMapper.selectBatchIds(registrantIds).stream() |
| | | .collect(Collectors.toMap(SysUser::getUserId, Function.identity())); |
| | | } |
| | | |
| | | @Override |
| | | public List<OfficialInventoryDto> coalBlendingList() { |
| | | // 1. 查询基础库存数据 |
| | | List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(null); |
| | | // 2. 收集所有需要查询的ID |
| | | Set<Long> coalIds = new HashSet<>(); |
| | | Set<Long> supplierIds = new HashSet<>(); |
| | | Set<Long> planIds = new HashSet<>(); |
| | | |
| | | officialInventories.forEach(inventory -> { |
| | | coalIds.add(inventory.getCoalId()); |
| | | supplierIds.add(inventory.getSupplierId()); |
| | | planIds.add(inventory.getCoalPlanId()); |
| | | }); |
| | | // 3. 批量查询关联数据 |
| | | Map<Long, CoalInfo> coalInfoMap; |
| | | if (!coalIds.isEmpty()) { |
| | | coalInfoMap = coalInfoMapper.selectByIds(coalIds).stream() |
| | | .collect(Collectors.toMap(CoalInfo::getId, Function.identity())); |
| | | } else { |
| | | coalInfoMap = new HashMap<>(); |
| | | } |
| | | |
| | | Map<Long, Supply> supplyMap; |
| | | if (!supplierIds.isEmpty()) { |
| | | supplyMap = supplyMapper.selectByIds(supplierIds).stream() |
| | | .collect(Collectors.toMap(Supply::getId, Function.identity())); |
| | | } else { |
| | | supplyMap = new HashMap<>(); |
| | | log.warn("supplierIds 为空,跳过查询 Supply"); |
| | | } |
| | | |
| | | Map<Long, List<CoalValue>> coalValuesMap; |
| | | if (!planIds.isEmpty()) { |
| | | coalValuesMap = coalValueMapper.selectList( |
| | | new LambdaQueryWrapper<CoalValue>().in(CoalValue::getPlanId, planIds)) |
| | | .stream() |
| | | .collect(Collectors.groupingBy(CoalValue::getPlanId)); |
| | | } else { |
| | | coalValuesMap = new HashMap<>(); |
| | | log.warn("planIds 为空,跳过查询 CoalValue"); |
| | | } |
| | | // 4. 组装DTO |
| | | return officialInventories.stream() |
| | | .map(inventory -> { |
| | | OfficialInventoryDto dto = new OfficialInventoryDto(); |
| | | BeanUtils.copyProperties(inventory, dto); |
| | | // 设置煤种信息 |
| | | CoalInfo coalInfo = coalInfoMap.get(inventory.getCoalId()); |
| | | Supply supply = supplyMap.get(inventory.getSupplierId()); |
| | | if (coalInfo != null && supply != null) { |
| | | dto.setSupplierCoal(supply.getSupplierName() + " - " + coalInfo.getCoal()); |
| | | } |
| | | // 设置煤质数据 |
| | | dto.setCoalValues(coalValuesMap.getOrDefault(inventory.getCoalPlanId(), Collections.emptyList()) |
| | | .stream() |
| | | .map(coalValue -> { |
| | | Map<String, String> map = new HashMap<>(); |
| | | map.put("fieldName", coalValue.getFieldName()); |
| | | map.put("coalValue", coalValue.getCoalValue()); |
| | | return map; |
| | | }) |
| | | .collect(Collectors.toList())); |
| | | return dto; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, BigDecimal> selectOfficialAllInfo() { |
| | | // 1. 查询 official_inventory 表数据 |
| | | List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(null); |