tuqin
2026-09-24 0891b6708cc09c2672a19661a8555aae0e742df1
src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -14,6 +14,10 @@
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInRecordImportDto;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.execl.StockInRecordExportData;
@@ -28,11 +32,13 @@
import com.ruoyi.stock.service.StockInRecordService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
@@ -43,6 +49,7 @@
    private StockInventoryMapper stockInventoryMapper;
    private StockUninventoryMapper stockUninventoryMapper;
    private ProductionOrderPickMapper productionOrderPickMapper;
    private ProductModelMapper productModelMapper;
    @Override
    public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -52,12 +59,13 @@
    // 新增入库
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(StockInRecordDto stockInRecordDto) {
    public StockInRecord add(StockInRecordDto stockInRecordDto) {
        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches");
        stockInRecordDto.setInboundBatches(no);
        StockInRecord stockInRecord = new StockInRecord();
        BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
        return stockInRecordMapper.insert(stockInRecord);
        stockInRecordMapper.insert(stockInRecord);
        return stockInRecord;
    }
    @Override
@@ -263,9 +271,9 @@
            if (stockInRecord == null) {
                throw new BaseException("入库记录不存在,无法审批!!!");
            }
            if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) {
            /*if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) {
                throw new BaseException("只有待审批状态的记录才能审批,入库批次:" + stockInRecord.getInboundBatches());
            }
            }*/
            stockInRecord.setApprovalStatus(approvalStatus);
            stockInRecordMapper.updateById(stockInRecord);
@@ -368,4 +376,74 @@
        return ids.size();
    }
    @Override
    public AjaxResult importData(MultipartFile file, String type) {
        List<StockInRecordImportDto> rows;
        try {
            ExcelUtil<StockInRecordImportDto> util = new ExcelUtil<>(StockInRecordImportDto.class);
            rows = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            return AjaxResult.error("解析Excel失败:" + e.getMessage());
        }
        if (CollectionUtils.isEmpty(rows)) {
            return AjaxResult.error("导入文件无数据");
        }
        String stockType = StringUtils.isEmpty(type) ? "0" : type.trim();
        String recordType = "1".equals(stockType)
                ? StockInQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode()
                : StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode();
        int success = 0;
        List<String> errors = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            StockInRecordImportDto row = rows.get(i);
            int rowNo = i + 2; // 首行为表头
            try {
                String productName = trim(row.getProductName());
                String model = trim(row.getModel());
                if (StringUtils.isEmpty(productName)) {
                    errors.add("第" + rowNo + "行:产品大类为空");
                    continue;
                }
                if (StringUtils.isEmpty(model)) {
                    errors.add("第" + rowNo + "行:规格型号为空");
                    continue;
                }
                if (row.getStockInNum() == null) {
                    errors.add("第" + rowNo + "行:入库数量为空");
                    continue;
                }
                ProductModel productModel = productModelMapper.selectByProductNameAndModel(productName, model);
                if (productModel == null) {
                    errors.add("第" + rowNo + "行:未找到产品[" + productName + " / " + model + "]");
                    continue;
                }
                StockInRecordDto dto = new StockInRecordDto();
                dto.setProductModelId(productModel.getId());
                dto.setBatchNo(trim(row.getBatchNo()));
                dto.setStockInNum(row.getStockInNum());
                dto.setRemark(row.getRemark());
                dto.setType(stockType);
                dto.setRecordType(recordType);
                dto.setApprovalStatus(ReviewStatusEnum.PENDING_REVIEW.getCode());
                add(dto);
                success++;
            } catch (Exception e) {
                errors.add("第" + rowNo + "行:" + e.getMessage());
            }
        }
        AjaxResult result = AjaxResult.success();
        result.put("totalCount", rows.size());
        result.put("successCount", success);
        result.put("failCount", rows.size() - success);
        result.put("errors", errors);
        result.put("msg", "导入完成:成功 " + success + " 条,失败 " + (rows.size() - success) + " 条");
        return result;
    }
    private String trim(String value) {
        return value == null ? null : value.trim();
    }
}