| | |
| | | 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; |
| | |
| | | * @since 2025-06-04 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService { |
| | | |
| | |
| | | @Transactional |
| | | public boolean addPending(PendingInventoryDto pendingInventoryDto) { |
| | | try { |
| | | CoalPlan coalPlan = coalPlanMapper.selectOne(new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "配煤计算器方案")); |
| | | // 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()); |
| | | |
| | | for (Map<String, Object> map : pendingInventoryDto.getFieldsResultList()) { |
| | | pendingInventory.setSupplierName("配煤计算器方案入库"); |
| | | // 3. 安全获取字段结果 |
| | | Map<String, Object> map = Optional.ofNullable(pendingInventoryDto.getFieldsResultList()) |
| | | .orElseThrow(() -> new BaseException("字段结果列表不能为空")); |
| | | |
| | | // 处理煤炭ID |
| | | if (map.get("coalId") == null) { |
| | | CoalInfo coalInfo = new CoalInfo(); |
| | | coalInfo.setCoal((String) map.get("createCoal")); |
| | | coalInfo.setMaintainerId(userId); |
| | | coalInfo.setMaintenanceDate(LocalDate.now()); |
| | | if (coalInfoMapper.insert(coalInfo) <= 0) { |
| | | return false; |
| | | } |
| | | pendingInventory.setCoalId(coalInfo.getId()); |
| | | } else { |
| | | pendingInventory.setCoalId((Long) map.get("coalId")); |
| | | // 4. 处理煤炭ID |
| | | if (map.get("coalId") == null) { |
| | | String coalName = safeGetString(map, "createCoal"); |
| | | if (coalName == null || coalName.isEmpty()) { |
| | | throw new BaseException("煤炭名称不能为空"); |
| | | } |
| | | |
| | | // 设置价格和数量 |
| | | BigDecimal cost = BigDecimal.valueOf((Double) map.get("cost")); |
| | | BigDecimal tonnage = BigDecimal.valueOf((Double) map.get("totalTonnage")); |
| | | 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); |
| | | } |
| | | |
| | | pendingInventory.setPriceIncludingTax(cost); |
| | | pendingInventory.setInventoryQuantity(tonnage); |
| | | pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage)); |
| | | // 5. 设置价格和数量 |
| | | BigDecimal cost = safeGetBigDecimal(map, "cost"); |
| | | BigDecimal tonnage = safeGetBigDecimal(map, "totalTonnage"); |
| | | |
| | | BigDecimal costExcludingTax = cost.divide(BigDecimal.valueOf(1.13), 2, RoundingMode.HALF_UP); |
| | | pendingInventory.setPriceExcludingTax(costExcludingTax); |
| | | pendingInventory.setTotalPriceExcludingTax(costExcludingTax.multiply(tonnage)); |
| | | pendingInventory.setCoalPlanId(coalPlan.getId()); |
| | | if (cost == null || tonnage == null) { |
| | | throw new BaseException("成本或吨位不能为空"); |
| | | } |
| | | |
| | | // 煤质字段值 |
| | | String coalFields = coalPlan.getCoalFields(); |
| | | List<String> coalFieldList = Arrays.asList(coalFields.split(",")); |
| | | pendingInventory.setPriceIncludingTax(cost); |
| | | pendingInventory.setInventoryQuantity(tonnage); |
| | | pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage)); |
| | | |
| | | for (String field : coalFieldList) { |
| | | CoalField coalField = coalFieldMapper.selectOne( |
| | | new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field)); |
| | | if (coalField == null) { |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | CoalValue coalValue = new CoalValue(); |
| | | coalValue.setPlanId(coalPlan.getId()); |
| | | coalValue.setFields(field); |
| | | coalValue.setFieldName(coalField.getFieldName()); |
| | | coalValue.setType("1"); |
| | | |
| | | switch (coalField.getFieldName()) { |
| | | case "发热量": |
| | | coalValue.setCoalValue((String) map.get("cv")); |
| | | break; |
| | | case "硫分": |
| | | coalValue.setCoalValue((String) map.get("sulfur")); |
| | | break; |
| | | case "灰分": |
| | | coalValue.setCoalValue((String) map.get("ash")); |
| | | break; |
| | | case "水分": |
| | | coalValue.setCoalValue((String) map.get("moisture")); |
| | | break; |
| | | } |
| | | |
| | | // 保存煤质值 |
| | | if (coalValueMapper.insert(coalValue) <= 0) { |
| | | return false; |
| | | } |
| | | if (coalValueMapper.insert(coalValue) <= 0) { |
| | | log.error("保存煤质值失败: {}", field); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | // 插入待入库记录 |
| | | // 7. 插入待入库记录 |
| | | if (pendingInventoryMapper.insert(pendingInventory) <= 0) { |
| | | log.error("插入待入库记录失败"); |
| | | return false; |
| | | } |
| | | |
| | | // 更新正式库 |
| | | for (Map<String, Object> coalResult : pendingInventoryDto.getCoalResultList()) { |
| | | Long officialId = (Long) coalResult.get("officialId"); |
| | | BigDecimal quantity = (BigDecimal) coalResult.get("quantity"); |
| | | OfficialInventory officialInventory = officialInventoryMapper.selectById(officialId); |
| | | // 8. 更新正式库 |
| | | List<Map<String, Object>> coalResults = Optional.ofNullable(pendingInventoryDto.getCoalResultList()) |
| | | .orElseThrow(() -> new BaseException("煤炭结果列表不能为空")); |
| | | |
| | | if (officialInventory == null || officialInventory.getInventoryQuantity().compareTo(quantity) < 0) { |
| | | for (Map<String, Object> coalResult : coalResults) { |
| | | Long officialId = safeGetLong(coalResult, "officialId"); |
| | | BigDecimal quantity = safeGetBigDecimal(coalResult, "quantity"); |
| | | |
| | | if (officialId == null || quantity == null) { |
| | | throw new BaseException("正式库存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("更新正式库存失败: {}", 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("无法转换为Long: 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("无法转换为BigDecimal: key={}, value={}", key, value); |
| | | return null; |
| | | } |
| | | } |
| | | } |