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;
/**
*
* 生产成本核算批次主表 服务实现类
*
*
* @author deslrey
* @since 2026-03-30
*/
@Service
public class ProductionSettlementBatchesServiceImpl extends ServiceImpl 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 list;
try {
ExcelUtil 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 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);
}
}