liding
8 小时以前 94509204d25f7c0ad213ae2322be2bd5bfd17424
main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java
@@ -6,9 +6,11 @@
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;
@@ -16,6 +18,7 @@
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;
@@ -29,6 +32,7 @@
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;
@@ -38,11 +42,13 @@
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.math.BigDecimal;
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;
/**
@@ -72,10 +78,8 @@
    private final InputInventoryRecordService inputInventoryRecordService;
    private final InventorySummaryService inventorySummaryService;
    private final CoalFieldService coalFieldService;
    private final CoalPlanService coalPlanService;
    private final CoalPlanMapper coalPlanMapper;
    private final CoalValueService coalValueService;
    @Override
    public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) {
@@ -297,7 +301,115 @@
    @Override
    @Transactional
    public int addPending(PendingInventoryDto pendingInventoryDto) {
      return 1;
    public boolean addPending(PendingInventoryDto pendingInventoryDto) {
        try {
            CoalPlan coalPlan = coalPlanMapper.selectOne(new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "配煤计算器方案"));
            if (coalPlan == null) {
                return false;
            }
            // 添加到待入库
            Long userId = SecurityUtils.getUserId();
            PendingInventory pendingInventory = new PendingInventory();
            pendingInventory.setUnit("t");
            pendingInventory.setRegistrantId(userId);
            pendingInventory.setRegistrationDate(LocalDate.now());
            for (Map<String, Object> map : pendingInventoryDto.getFieldsResultList()) {
                pendingInventory.setSupplierName("配煤计算器方案入库");
                // 处理煤炭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"));
                }
                // 设置价格和数量
                BigDecimal cost = BigDecimal.valueOf((Double) map.get("cost"));
                BigDecimal tonnage = BigDecimal.valueOf((Double) map.get("totalTonnage"));
                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));
                pendingInventory.setCoalPlanId(coalPlan.getId());
                // 煤质字段值
                String coalFields = coalPlan.getCoalFields();
                List<String> coalFieldList = Arrays.asList(coalFields.split(","));
                for (String field : coalFieldList) {
                    CoalField coalField = coalFieldMapper.selectOne(
                            new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field));
                    if (coalField == null) {
                        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 (pendingInventoryMapper.insert(pendingInventory) <= 0) {
                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);
                if (officialInventory == null || officialInventory.getInventoryQuantity().compareTo(quantity) < 0) {
                    throw new BaseException("库存数量不足,添加至待入库失败");
                }
                officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantity));
                if (officialInventoryMapper.updateById(officialInventory) <= 0) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            // 记录日志
            log.error("添加待入库失败", e);
            throw new BaseException("添加待入库失败: " + e.getMessage());
        }
    }
}