From 4fedbed9949c6160dcfa216d6660bd3c625f7bce Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期三, 25 六月 2025 11:49:55 +0800 Subject: [PATCH] 优化 --- main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 161 insertions(+), 14 deletions(-) diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java index 7be4276..6e1f4d6 100644 --- a/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java +++ b/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java @@ -1,26 +1,31 @@ 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.mapper.CoalFieldMapper; +import com.ruoyi.basic.mapper.CoalInfoMapper; import com.ruoyi.basic.mapper.CoalValueMapper; import com.ruoyi.business.dto.OfficialInventoryDto; import com.ruoyi.business.entity.OfficialInventory; import com.ruoyi.business.mapper.OfficialInventoryMapper; -import com.ruoyi.business.mapper.PendingInventoryMapper; 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.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -41,7 +46,8 @@ private final CoalFieldMapper coalFieldMapper; - private final PendingInventoryMapper pendingInventoryMapper; + private final CoalInfoMapper coalInfoMapper; + @Override public IPage<OfficialInventoryDto> selectOfficialInventoryList(Page page, OfficialInventoryDto officialInventoryDto) { @@ -63,17 +69,37 @@ .distinct() .collect(Collectors.toList()); + //鏌ヨ鐓ょids + List<Long> coalIds = entityPage.getRecords().stream() + .map(OfficialInventory::getCoalId) + .distinct() + .collect(Collectors.toList()); + + // 鎵归噺鏌ヨCoalInfo + Map<Long, CoalInfo> coalInfoMap; + if (!coalIds.isEmpty()) { + List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().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); - - Long pendingId = entity.getPendingId(); - - // 鏌ヨ璇� pendingId 瀵瑰簲鐨� CoalValue 鏁版嵁 - List<CoalValue> coalValues = coalValueMapper.selectList( - new LambdaQueryWrapper<CoalValue>().eq(CoalValue::getPlanId, pendingId) - ); + BeanUtils.copyProperties(entity, dto); + List<CoalValue> coalValues; + if (entity.getMergeId() == null) { + coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>() + .eq(CoalValue::getPlanId, entity.getPendingId()) + .and(wrapper -> wrapper.ne(CoalValue::getType, "2").or().isNull(CoalValue::getType)) + ); + } else { + coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>() + .eq(CoalValue::getPlanId, entity.getId()) + .eq(CoalValue::getType, "2") + ); + } // 鏋勫缓 Map<fieldName, value> Map<String, String> fieldValueMap = coalValues.stream() @@ -91,6 +117,12 @@ fields.add(fieldMap); } + // 璁剧疆Coal淇℃伅 + CoalInfo coalInfo = coalInfoMap.get(entity.getCoalId()); + if (coalInfo != null) { + dto.setCoal(coalInfo.getCoal()); + } + // 璁剧疆鍒� DTO 涓� dto.setFields(fields); dtoList.add(dto); @@ -99,4 +131,119 @@ dtoPage.setRecords(dtoList); // 璁剧疆杞崲鍚庣殑 DtoList return dtoPage; } + + @Override + public int editOfficial(OfficialInventoryDto officialInventoryDto) { + OfficialInventory officialInventory = new OfficialInventory(); + BeanUtils.copyProperties(officialInventoryDto, officialInventory); + return officialInventoryMapper.updateById(officialInventory); + } + + @Override + public List<OfficialInventoryVo> selectOfficialList(OfficialInventoryVo officialInventoryVo) { + List<OfficialInventory> 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<OfficialInventory> selectOfficialAll() { + return officialInventoryMapper.selectList(null); + } + + @Transactional + @Override + public int mergeAll(OfficialInventoryDto officialInventoryDto) { + List<Long> ids = officialInventoryDto.getIds(); + + // 鏍¢獙鍙傛暟 + if (CollectionUtils.isEmpty(ids) || ids.size() < 2) { + throw new BaseException("璇烽�変腑鑷冲皯涓ゆ潯鏁版嵁"); + } + if (CollectionUtils.isEmpty(officialInventoryDto.getFields())) { + throw new BaseException("瀛楁鍊间笉鑳戒负绌�"); + } + + // 1. 鎵归噺鏍囪鍒犻櫎鏃ф暟鎹� + LambdaUpdateWrapper<OfficialInventory> updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.in(OfficialInventory::getId, ids) + .set(OfficialInventory::getDeleted, 1); + int rowsAffected = officialInventoryMapper.update(null, updateWrapper); + if (rowsAffected == 0) { + throw new BaseException("鏈壘鍒板尮閰嶇殑鏁版嵁锛岃纭閫夋嫨鏄惁姝g‘"); + } + + // 2. 鎻掑叆鏂板簱瀛樿褰� + OfficialInventory officialInventory = new OfficialInventory(); + BeanUtils.copyProperties(officialInventoryDto, officialInventory); + officialInventory.setId(null); + officialInventory.setMergeId(ids.toString()); + officialInventory.setRegistrantId(SecurityUtils.getLoginUser().getUser().getUserName()); + if (officialInventoryMapper.insert(officialInventory) <= 0) { + throw new BaseException("搴撳瓨璁板綍鍒涘缓澶辫触"); + } + + // 3. 鎵归噺澶勭悊瀛楁鍊� + batchProcessCoalValues(officialInventory.getId(), officialInventoryDto.getFields()); + + return rowsAffected; + } + + private void batchProcessCoalValues(Long planId, List<Map<String, String>> fields) { + // 1. 鎻愬彇鎵�鏈夊敮涓�瀛楁鏍囪瘑 + Set<String> allFields = fields.stream() + .flatMap(map -> map.keySet().stream()) + .collect(Collectors.toSet()); + + // 2. 鏌ヨ瀛楁鏄犲皠鍏崇郴 + List<CoalField> coalFields = coalFieldMapper.selectList( + new LambdaQueryWrapper<CoalField>().in(CoalField::getFields, allFields) + ); + + Map<String, String> fieldMap = coalFields.stream() + .collect(Collectors.toMap( + CoalField::getFields, + CoalField::getFieldName + )); + + // 3. 鏋勯�犲苟鎻掑叆姣忔潯璁板綍 + CoalValue coalValueTemplate = new CoalValue(); + coalValueTemplate.setPlanId(planId); + coalValueTemplate.setType("2"); + + for (Map<String, String> fieldMapEntry : fields) { + for (Map.Entry<String, String> 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); + } + } + } + } + } -- Gitblit v1.9.3