From 6733a32d4bcd7ad3ec3f109da0f3d2524766f7bb Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期二, 15 七月 2025 15:54:33 +0800 Subject: [PATCH] 1.数据优化 2.配煤计算器入库优化 --- main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java | 377 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 364 insertions(+), 13 deletions(-) diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java index adbc5e9..1e6c5ad 100644 --- a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java +++ b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java @@ -6,28 +6,51 @@ 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.mchange.v2.lang.SystemUtils; +import com.ruoyi.basic.dto.CoalFieldDto; import com.ruoyi.basic.entity.CoalField; +import com.ruoyi.basic.entity.CoalInfo; +import com.ruoyi.basic.entity.CoalPlan; import com.ruoyi.basic.entity.CoalValue; import com.ruoyi.basic.mapper.CoalFieldMapper; +import com.ruoyi.basic.mapper.CoalInfoMapper; +import com.ruoyi.basic.mapper.CoalPlanMapper; import com.ruoyi.basic.mapper.CoalValueMapper; +import com.ruoyi.basic.service.CoalFieldService; +import com.ruoyi.basic.service.CoalPlanService; +import com.ruoyi.basic.service.CoalValueService; import com.ruoyi.business.dto.PendingInventoryDto; import com.ruoyi.business.entity.OfficialInventory; import com.ruoyi.business.entity.PendingInventory; +import com.ruoyi.business.entity.SalesRecord; +import com.ruoyi.business.mapper.InventorySummaryMapper; import com.ruoyi.business.mapper.OfficialInventoryMapper; import com.ruoyi.business.mapper.PendingInventoryMapper; +import com.ruoyi.business.mapper.SalesRecordMapper; +import com.ruoyi.business.service.InputInventoryRecordService; +import com.ruoyi.business.service.InventorySummaryService; import com.ruoyi.business.service.PendingInventoryService; +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.StringUtils; import com.ruoyi.common.utils.bean.BeanUtils; +import com.ruoyi.system.mapper.SysUserMapper; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.math.RoundingMode; +import java.time.LocalDate; +import java.time.format.DateTimeParseException; +import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; /** * <p> @@ -38,6 +61,7 @@ * @since 2025-06-04 */ @Service +@Slf4j @RequiredArgsConstructor public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService { @@ -49,11 +73,131 @@ private final CoalFieldMapper coalFieldMapper; + private final CoalInfoMapper coalInfoMapper; + + private final SysUserMapper sysUserMapper; + + private final InputInventoryRecordService inputInventoryRecordService; + + private final InventorySummaryService inventorySummaryService; + private final CoalPlanMapper coalPlanMapper; + private final CoalValueService coalValueService; + @Override - public IPage<PendingInventory> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) { + public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) { + // 1. 鏋勫缓涓绘煡璇� LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>(); + if (pendingInventoryDto.getRegistrationDate() != null) { + queryWrapper.eq(PendingInventory::getRegistrationDate, pendingInventoryDto.getRegistrationDate()); + } + + // 鎸夌叅绉嶅悕绉版煡璇� + if (StringUtils.isNotBlank(pendingInventoryDto.getCoal())) { + LambdaQueryWrapper<CoalInfo> coalQueryWrapper = new LambdaQueryWrapper<>(); + coalQueryWrapper.like(CoalInfo::getCoal, pendingInventoryDto.getCoal()); + List<CoalInfo> coalInfos = coalInfoMapper.selectList(coalQueryWrapper); + + if (!coalInfos.isEmpty()) { + List<Long> coalIds = coalInfos.stream() + .map(CoalInfo::getId) + .collect(Collectors.toList()); + queryWrapper.in(PendingInventory::getCoalId, coalIds); + } else { + // 濡傛灉娌℃湁鍖归厤鐨勭叅绉嶏紝鐩存帴杩斿洖绌虹粨鏋� + queryWrapper.eq(PendingInventory::getCoalId, -1L); // 浣跨敤涓嶅彲鑳藉瓨鍦ㄧ殑ID + } + } + queryWrapper.orderByDesc(PendingInventory::getCreateTime); - return pendingInventoryMapper.selectPage(page, queryWrapper); + + // 2. 鎵ц涓昏〃鍒嗛〉鏌ヨ + IPage<PendingInventory> pendingInventoryPage = pendingInventoryMapper.selectPage(page, queryWrapper); + + // 3. 鏃犳暟鎹揩閫熻繑鍥� + if (CollectionUtils.isEmpty(pendingInventoryPage.getRecords())) { + return new Page<>(page.getCurrent(), page.getSize(), pendingInventoryPage.getTotal()); + } + + // 4. 鎻愬彇鎵�鏈夊緟澶勭悊搴撳瓨ID + List<Long> pendingIds = pendingInventoryPage.getRecords().stream() + .map(PendingInventory::getId) + .collect(Collectors.toList()); + + // 5. 鎵归噺鏌ヨ鍏宠仈鐨勭叅鐐俊鎭拰姝e紡搴撳瓨淇℃伅 + List<Long> coalIds = pendingInventoryPage.getRecords().stream() + .map(PendingInventory::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<>(); + } + + // 5. 鎵归噺鏌ヨ鐧昏浜篿d + List<Long> registrantIds = pendingInventoryPage.getRecords().stream() + .map(PendingInventory::getRegistrantId) + .distinct() + .toList(); + // 鎵归噺鏌ヨ鐧昏浜� + Map<Long, SysUser> sysUserMap; + if (!registrantIds.isEmpty()) { + List<SysUser> sysUsers = sysUserMapper.selectList(registrantIds); + sysUserMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity())); + } else { + sysUserMap = new HashMap<>(); + } + + // 鎵归噺鏌ヨ姝e紡搴撳瓨淇℃伅 + Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds); + + // 6. 杞崲DTO骞惰缃浉鍏冲瓧娈� + return pendingInventoryPage.convert(record -> { + PendingInventoryDto dto = new PendingInventoryDto(); + BeanUtils.copyProperties(record, dto); + + // 璁剧疆Coal淇℃伅 + CoalInfo coalInfo = coalInfoMap.get(record.getCoalId()); + if (coalInfo != null) { + dto.setCoal(coalInfo.getCoal()); + } + + // 璁剧疆鐧昏浜� + SysUser sysUser = sysUserMap.get(record.getRegistrantId()); + if (sysUser != null) { + dto.setRegistrant(sysUser.getNickName()); + } + + // 浠庨鍔犺浇鐨凪ap涓幏鍙杘fficialId + dto.setOfficialId(pendingToOfficialMap.getOrDefault(record.getId(), null)); + + return dto; + }); + } + + + // 鎵归噺鑾峰彇寰呭鐞嗗簱瀛樹笌姝e紡搴撳瓨鐨勬槧灏勫叧绯� + private Map<Long, Long> getOfficialInventoryMap(List<Long> pendingIds) { + if (CollectionUtils.isEmpty(pendingIds)) { + return Collections.emptyMap(); + } + + // 鏌ヨ鍏宠仈鐨勬寮忓簱瀛樻暟鎹� + LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>(); + wrapper.select(OfficialInventory::getId, OfficialInventory::getPendingId) + .in(OfficialInventory::getPendingId, pendingIds); + + return officialInventoryMapper.selectList(wrapper) + .stream() + .collect(Collectors.toMap( + OfficialInventory::getPendingId, + OfficialInventory::getId, + (existing, replacement) -> existing // 濡傛灉鏈夐噸澶嶏紝淇濈暀绗竴涓� + )); } @Override @@ -119,6 +263,7 @@ coalValue.setCoalValue(value); coalValue.setFields(key); coalValue.setFieldName(fieldName); + coalValue.setType(String.valueOf(1)); i = coalValueMapper.insert(coalValue); } } @@ -131,18 +276,224 @@ BigDecimal left = pendingInventory.getInventoryQuantity().subtract(quantity); if (left.compareTo(BigDecimal.ZERO) > 0) { pendingInventory.setInventoryQuantity(left); + pendingInventory.setCoalPlanId(pendingInventoryDto.getCoalPlanId()); pendingInventoryMapper.updateById(pendingInventory); } else { pendingInventoryMapper.deleteById(pendingInventoryDto.getPId()); } - officialInventoryMapper.delete(new LambdaQueryWrapper<OfficialInventory>().eq(OfficialInventory::getPendingId, pendingInventoryDto.getPId())); - OfficialInventory officialInventory = new OfficialInventory(); - BeanUtils.copyProperties(pendingInventory, officialInventory); - officialInventory.setId(null); - officialInventory.setPendingId(pendingInventoryDto.getPId()); - officialInventory.setInventoryQuantity(quantity); - officialInventoryMapper.insert(officialInventory); + //姝e紡搴� + if (pendingInventoryDto.getOfficialId() == null) { + OfficialInventory officialInventory = new OfficialInventory(); + BeanUtils.copyProperties(pendingInventory, officialInventory); + officialInventory.setId(null); + officialInventory.setCoalPlanId(pendingInventoryDto.getCoalPlanId()); + officialInventory.setPendingId(pendingInventoryDto.getPId()); + officialInventory.setInventoryQuantity(quantity); + officialInventory.setRegistrantId(1L); + officialInventory.setSupplierId(pendingInventoryDto.getSupplierId()); + officialInventoryMapper.insert(officialInventory); + } else { + OfficialInventory officialInventory = officialInventoryMapper.selectById(pendingInventoryDto.getOfficialId()); + officialInventory.setInventoryQuantity(quantity.add(officialInventory.getInventoryQuantity())); + officialInventoryMapper.updateById(officialInventory); + } } return i; } + + @Override + @Transactional + public boolean addPending(PendingInventoryDto pendingInventoryDto) { + try { + // 1. 鑾峰彇閰嶇叅璁$畻鍣ㄦ柟妗� + CoalPlan coalPlan = coalPlanMapper.selectOne( + new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "閰嶇叅璁$畻鍣ㄦ柟妗�")); + if (coalPlan == null) { + log.error("閰嶇叅璁$畻鍣ㄦ柟妗堜笉瀛樺湪"); + return false; + } + + // 2. 鍑嗗寰呭叆搴撹褰� + Long userId = SecurityUtils.getUserId(); + PendingInventory pendingInventory = new PendingInventory(); + pendingInventory.setUnit("t"); + pendingInventory.setRegistrantId(userId); + pendingInventory.setRegistrationDate(LocalDate.now()); + pendingInventory.setSupplierName("閰嶇叅璁$畻鍣ㄦ柟妗堝叆搴�"); + pendingInventory.setCoalPlanId(coalPlan.getId()); + + // 3. 瀹夊叏鑾峰彇瀛楁缁撴灉 + Map<String, Object> map = Optional.ofNullable(pendingInventoryDto.getFieldsResultList()) + .orElseThrow(() -> new BaseException("瀛楁缁撴灉鍒楄〃涓嶈兘涓虹┖")); + + // 4. 澶勭悊鐓ょ偔ID + if (map.get("coalId") == null) { + String coalName = safeGetString(map, "createCoal"); + if (coalName == null || coalName.isEmpty()) { + throw new BaseException("鐓ょ偔鍚嶇О涓嶈兘涓虹┖"); + } + + CoalInfo coalInfo = new CoalInfo(); + coalInfo.setCoal(coalName); + coalInfo.setMaintainerId(userId); + coalInfo.setMaintenanceDate(LocalDate.now()); + if (coalInfoMapper.insert(coalInfo) <= 0) { + log.error("鍒涘缓鐓ょ偔淇℃伅澶辫触"); + return false; + } + pendingInventory.setCoalId(coalInfo.getId()); + } else { + Long coalId = safeGetLong(map, "coalId"); + if (coalId == null) { + throw new BaseException("鐓ょ偔ID鏍煎紡閿欒"); + } + pendingInventory.setCoalId(coalId); + } + + // 5. 璁剧疆浠锋牸鍜屾暟閲� + BigDecimal cost = safeGetBigDecimal(map, "cost"); + BigDecimal tonnage = safeGetBigDecimal(map, "totalTonnage"); + + if (cost == null || tonnage == null) { + throw new BaseException("鎴愭湰鎴栧惃浣嶄笉鑳戒负绌�"); + } + + pendingInventory.setPriceIncludingTax(cost); + pendingInventory.setInventoryQuantity(tonnage); + pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage)); + + BigDecimal costExcludingTax = cost.divide(BigDecimal.valueOf(1.13), 2, RoundingMode.HALF_UP); + pendingInventory.setPriceExcludingTax(costExcludingTax); + pendingInventory.setTotalPriceExcludingTax(costExcludingTax.multiply(tonnage)); + + // 6. 澶勭悊鐓よ川瀛楁鍊� + String coalFields = coalPlan.getCoalFields(); + if (coalFields == null || coalFields.isEmpty()) { + throw new BaseException("鐓よ川瀛楁閰嶇疆涓嶈兘涓虹┖"); + } + + String[] coalFieldList = coalFields.split(","); + for (String field : coalFieldList) { + CoalField coalField = coalFieldMapper.selectOne( + new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field)); + if (coalField == null) { + log.warn("鏈壘鍒扮叅璐ㄥ瓧娈甸厤缃�: {}", field); + continue; + } + + CoalValue coalValue = new CoalValue(); + coalValue.setPlanId(coalPlan.getId()); + coalValue.setFields(field); + coalValue.setFieldName(coalField.getFieldName()); + coalValue.setType("1"); + + // 瀹夊叏璁剧疆鐓よ川鍊� + switch (coalField.getFieldName()) { + case "鍙戠儹閲�": + coalValue.setCoalValue(safeGetString(map, "cv")); + break; + case "纭垎": + coalValue.setCoalValue(safeGetString(map, "sulfur")); + break; + case "鐏板垎": + coalValue.setCoalValue(safeGetString(map, "ash")); + break; + case "姘村垎": + coalValue.setCoalValue(safeGetString(map, "moisture")); + break; + default: + log.warn("鏈煡鐓よ川瀛楁: {}", coalField.getFieldName()); + continue; + } + + if (coalValueMapper.insert(coalValue) <= 0) { + log.error("淇濆瓨鐓よ川鍊煎け璐�: {}", field); + return false; + } + } + + // 7. 鎻掑叆寰呭叆搴撹褰� + if (pendingInventoryMapper.insert(pendingInventory) <= 0) { + log.error("鎻掑叆寰呭叆搴撹褰曞け璐�"); + return false; + } + + // 8. 鏇存柊姝e紡搴� + List<Map<String, Object>> coalResults = Optional.ofNullable(pendingInventoryDto.getCoalResultList()) + .orElseThrow(() -> new BaseException("鐓ょ偔缁撴灉鍒楄〃涓嶈兘涓虹┖")); + + for (Map<String, Object> coalResult : coalResults) { + Long officialId = safeGetLong(coalResult, "officialId"); + BigDecimal quantity = safeGetBigDecimal(coalResult, "quantity"); + + if (officialId == null || quantity == null) { + throw new BaseException("姝e紡搴撳瓨ID鎴栨暟閲忎笉鑳戒负绌�"); + } + + OfficialInventory officialInventory = officialInventoryMapper.selectById(officialId); + if (officialInventory == null) { + throw new BaseException("鎵句笉鍒版寮忓簱瀛樿褰�: " + officialId); + } + + if (officialInventory.getInventoryQuantity().compareTo(quantity) < 0) { + throw new BaseException("搴撳瓨鏁伴噺涓嶈冻锛屾坊鍔犺嚦寰呭叆搴撳け璐�"); + } + + officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantity)); + if (officialInventoryMapper.updateById(officialInventory) <= 0) { + log.error("鏇存柊姝e紡搴撳瓨澶辫触: {}", officialId); + return false; + } + } + + return true; + } catch (Exception e) { + log.error("娣诲姞寰呭叆搴撳け璐�", e); + throw new BaseException("娣诲姞寰呭叆搴撳け璐�: " + e.getMessage()); + } + } + + // 瀹夊叏绫诲瀷杞崲杈呭姪鏂规硶 + private String safeGetString(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + return value.toString(); + } + + private Long safeGetLong(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + if (value instanceof Number) { + return ((Number) value).longValue(); + } + try { + return Long.parseLong(value.toString()); + } catch (NumberFormatException e) { + log.warn("鏃犳硶杞崲涓篖ong: key={}, value={}", key, value); + return null; + } + } + + private BigDecimal safeGetBigDecimal(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + if (value instanceof BigDecimal) { + return (BigDecimal) value; + } + if (value instanceof Number) { + return BigDecimal.valueOf(((Number) value).doubleValue()); + } + try { + return new BigDecimal(value.toString()); + } catch (NumberFormatException e) { + log.warn("鏃犳硶杞崲涓築igDecimal: key={}, value={}", key, value); + return null; + } + } } -- Gitblit v1.9.3