2026-04-27 0bc2775e4fd776086c508fd7640bc3d61835cf73
src/main/java/com/ruoyi/measuringinstrumentledger/service/impl/MeasuringInstrumentLedgerRecordServiceImpl.java
@@ -16,14 +16,13 @@
import com.ruoyi.other.pojo.TempFile;
import com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.pojo.CommonFile;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -42,19 +41,13 @@
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MeasuringInstrumentLedgerRecordServiceImpl extends ServiceImpl<MeasuringInstrumentLedgerRecordMapper, MeasuringInstrumentLedgerRecord> implements MeasuringInstrumentLedgerRecordService {
    @Autowired
    private MeasuringInstrumentLedgerRecordMapper measuringInstrumentLedgerRecordMapper;
    @Autowired
    private MeasuringInstrumentLedgerMapper measuringInstrumentLedgerMapper;
    @Autowired
    private CommonFileMapper commonFileMapper;
    @Autowired
    private TempFileMapper tempFileMapper;
    private final MeasuringInstrumentLedgerRecordMapper measuringInstrumentLedgerRecordMapper;
    private final MeasuringInstrumentLedgerMapper measuringInstrumentLedgerMapper;
    private final CommonFileMapper commonFileMapper;
    private final TempFileMapper tempFileMapper;
    @Value("${file.upload-dir}")
    private String uploadDir;
@@ -64,7 +57,8 @@
        IPage<MeasuringInstrumentLedgerRecord> measuringInstrumentLedgerRecordIPage = measuringInstrumentLedgerRecordMapper.listPage(page, measuringInstrumentLedgerRecord);
        measuringInstrumentLedgerRecordIPage.getRecords().forEach(item -> {
            LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
            salesLedgerFileWrapper.eq(CommonFile::getCommonId, item.getId());
            salesLedgerFileWrapper.eq(CommonFile::getCommonId, item.getId())
                    .eq(CommonFile::getType, FileNameType.MEASURINGRecord.getValue());
            List<CommonFile> commonFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
            item.setCommonFiles(commonFiles);
        });
@@ -88,14 +82,12 @@
        if(!measuringInstrumentLedgerRecord1.getValid().equals(measuringInstrumentLedgerRecord.getValid())){
            MeasuringInstrumentLedger measuringInstrumentLedger = measuringInstrumentLedgerMapper.selectById(measuringInstrumentLedgerRecord1.getMeasuringInstrumentLedgerId());
            if(measuringInstrumentLedger != null){
                measuringInstrumentLedger.setValid(measuringInstrumentLedgerRecord.getValid());
                measuringInstrumentLedger.setNextDate(new Date(measuringInstrumentLedger.getMostDate().getTime() + measuringInstrumentLedgerRecord.getValid() * 24 * 60 * 60 * 1000L));
            }
            measuringInstrumentLedgerMapper.updateById(measuringInstrumentLedger);
        }
        // 删除附件
        LambdaQueryWrapper<CommonFile> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.eq(CommonFile::getCommonId, measuringInstrumentLedgerRecord.getId());
        commonFileMapper.delete(delWrapper);
        measuringInstrumentLedgerRecordMapper.updateById(measuringInstrumentLedgerRecord);
        // 记录附件绑定
        migrateTempFilesToFormal(measuringInstrumentLedgerRecord.getId(), measuringInstrumentLedgerRecord.getTempFileIds(), FileNameType.MEASURINGRecord.getValue());
        return true;
@@ -144,12 +136,15 @@
            try {
                // 执行文件迁移(使用原子操作确保安全性)
                Files.move(
                        Paths.get(tempFile.getTempPath()),
                        formalFilePath,
                        StandardCopyOption.REPLACE_EXISTING,
                        StandardCopyOption.ATOMIC_MOVE
                );
//                Files.move(
//                        Paths.get(tempFile.getTempPath()),
//                        formalFilePath,
//                        StandardCopyOption.REPLACE_EXISTING,
//                        StandardCopyOption.ATOMIC_MOVE
//                );
                // 原子移动失败,使用复制+删除
                Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING);
                Files.deleteIfExists(Paths.get(tempFile.getTempPath()));
                log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath);
                // 更新文件记录(关联到业务ID)