gongchunyi
7 小时以前 263b034b4058bb7a36c709278abdc88ca1ba26c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.ruoyi.production.service.impl;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.production.dto.SettlementImportDto;
import com.ruoyi.production.pojo.ProductionSettlementBatches;
import com.ruoyi.production.mapper.ProductionSettlementBatchesMapper;
import com.ruoyi.production.pojo.ProductionSettlementDetails;
import com.ruoyi.production.service.IProductionSettlementBatchesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.production.service.IProductionSettlementDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 生产成本核算批次主表 服务实现类
 * </p>
 *
 * @author deslrey
 * @since 2026-03-30
 */
@Service
public class ProductionSettlementBatchesServiceImpl extends ServiceImpl<ProductionSettlementBatchesMapper, ProductionSettlementBatches> implements IProductionSettlementBatchesService {
 
    @Autowired
    private IProductionSettlementDetailsService productionSettlementDetailsService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importProductionSettlement(MultipartFile file, LocalDate periodTime, String batchName) {
        if (file == null || file.isEmpty()) {
            throw new ServiceException("导入失败,文件不能为空");
        }
 
        List<SettlementImportDto> list;
        try {
            ExcelUtil<SettlementImportDto> util = new ExcelUtil<>(SettlementImportDto.class);
            list = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            log.error("导入生产成本核算失败", e);
            throw new ServiceException("解析Excel文件失败:" + e.getMessage());
        }
 
        if (StringUtils.isEmpty(list)) {
            throw new ServiceException("导入数据不能为空!");
        }
 
        ProductionSettlementBatches batch = new ProductionSettlementBatches();
        batch.setBatchName(StringUtils.isNotEmpty(batchName) ? batchName : periodTime + "成本核算导入");
        batch.setPeriodTime(periodTime != null ? periodTime : LocalDate.now());
        batch.setStatus(0);
        batch.setCreateTime(LocalDateTime.now());
        batch.setCreateUser(SecurityUtils.getUsername());
        this.save(batch);
 
        List<ProductionSettlementDetails> detailList = list.stream().map(dto -> {
            ProductionSettlementDetails detail = new ProductionSettlementDetails();
            detail.setBatchId(batch.getId());
            detail.setProductType(dto.getProductType());
            detail.setCategory(dto.getCategory());
            detail.setSubjectName(dto.getSubjectName());
            detail.setBudgetQty(dto.getBudgetQty());
            detail.setBudgetPrice(dto.getBudgetPrice());
            detail.setBudgetTotal(dto.getBudgetTotal());
            detail.setActualQty(BigDecimal.ZERO);
            detail.setActualPrice(BigDecimal.ZERO);
            detail.setActualTotal(BigDecimal.ZERO);
            detail.setDiffQty(BigDecimal.ZERO);
            detail.setDiffPrice(BigDecimal.ZERO);
            detail.setDiffTotal(BigDecimal.ZERO);
 
            return detail;
        }).collect(Collectors.toList());
 
        productionSettlementDetailsService.saveBatch(detailList);
    }
}