From 8238b458d6a3bec147fd4b639ed0427de7cf8be4 Mon Sep 17 00:00:00 2001 From: yaowanxin <3588231647@qq.com> Date: 星期一, 11 八月 2025 15:27:41 +0800 Subject: [PATCH] 添加附件 --- src/main/java/com/ruoyi/measuringinstrumentledger/service/impl/MeasuringInstrumentLedgerServiceImpl.java | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 220 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/ruoyi/measuringinstrumentledger/service/impl/MeasuringInstrumentLedgerServiceImpl.java b/src/main/java/com/ruoyi/measuringinstrumentledger/service/impl/MeasuringInstrumentLedgerServiceImpl.java new file mode 100644 index 0000000..c59f3c1 --- /dev/null +++ b/src/main/java/com/ruoyi/measuringinstrumentledger/service/impl/MeasuringInstrumentLedgerServiceImpl.java @@ -0,0 +1,220 @@ +package com.ruoyi.measuringinstrumentledger.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +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.ruoyi.common.enums.FileNameType; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.framework.web.domain.AjaxResult; +import com.ruoyi.measuringinstrumentledger.dto.MeasuringInstrumentLedgerDto; +import com.ruoyi.measuringinstrumentledger.mapper.MeasuringInstrumentLedgerMapper; +import com.ruoyi.measuringinstrumentledger.mapper.MeasuringInstrumentLedgerRecordMapper; +import com.ruoyi.measuringinstrumentledger.pojo.MeasuringInstrumentLedger; +import com.ruoyi.measuringinstrumentledger.pojo.MeasuringInstrumentLedgerRecord; +import com.ruoyi.measuringinstrumentledger.service.MeasuringInstrumentLedgerService; +import com.ruoyi.other.mapper.TempFileMapper; +import com.ruoyi.other.pojo.TempFile; +import com.ruoyi.project.system.domain.SysUser; +import com.ruoyi.project.system.mapper.SysUserMapper; +import com.ruoyi.sales.mapper.CommonFileMapper; +import com.ruoyi.sales.pojo.CommonFile; +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 javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +/** + * @author :yys + * @date : 2025/8/5 9:23 + */ +@Service +@Slf4j +public class MeasuringInstrumentLedgerServiceImpl extends ServiceImpl<MeasuringInstrumentLedgerMapper, MeasuringInstrumentLedger> implements MeasuringInstrumentLedgerService { + + @Autowired + private MeasuringInstrumentLedgerMapper measuringInstrumentLedgerMapper; + + @Autowired + private MeasuringInstrumentLedgerRecordMapper measuringInstrumentLedgerRecordMapper; + + @Autowired + private TempFileMapper tempFileMapper; + + @Autowired + private CommonFileMapper commonFileMapper; + + @Autowired + private SysUserMapper sysUserMapper; + + @Value("${file.upload-dir}") + private String uploadDir; + + @Override + public IPage<MeasuringInstrumentLedger> listPage(Page page, MeasuringInstrumentLedger measuringInstrumentLedger) { + IPage<MeasuringInstrumentLedger> measuringInstrumentLedgerIPage = measuringInstrumentLedgerMapper.listPage(page, measuringInstrumentLedger); + List<Integer> types = new ArrayList<>(); + types.add(FileNameType.MEASURING.getValue()); + types.add(FileNameType.MEASURINGRecord.getValue()); + measuringInstrumentLedgerIPage.getRecords().forEach(item -> { + LambdaQueryWrapper<MeasuringInstrumentLedgerRecord> measuringInstrumentLedgerRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); + measuringInstrumentLedgerRecordLambdaQueryWrapper.eq(MeasuringInstrumentLedgerRecord::getMeasuringInstrumentLedgerId, item.getId()); + List<MeasuringInstrumentLedgerRecord> measuringInstrumentLedgerRecords = measuringInstrumentLedgerRecordMapper.selectList(measuringInstrumentLedgerRecordLambdaQueryWrapper); + List<Long> collect = new ArrayList<>(); + if(!CollectionUtils.isEmpty(measuringInstrumentLedgerRecords)){ + collect = measuringInstrumentLedgerRecords.stream().map(MeasuringInstrumentLedgerRecord::getId).collect(Collectors.toList()); + } + collect.add(item.getId()); + LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>(); + salesLedgerFileWrapper.in(CommonFile::getCommonId, collect) + .in(CommonFile::getType,types); + List<CommonFile> commonFiles = commonFileMapper.selectList(salesLedgerFileWrapper); + item.setCommonFiles(commonFiles); + + }); + return measuringInstrumentLedgerIPage; + } + + @Override + public boolean verifying(MeasuringInstrumentLedgerDto req) throws IOException { + MeasuringInstrumentLedger measuringInstrumentLedger = measuringInstrumentLedgerMapper.selectById(req.getId()); + if(measuringInstrumentLedger == null) { + return false; + } + SysUser sysUser = sysUserMapper.selectUserById(measuringInstrumentLedger.getUserId()); + measuringInstrumentLedger.setValid(req.getValid()); + measuringInstrumentLedger.setMostDate(req.getRecordDate()); + measuringInstrumentLedger.setNextDate(new Date(req.getRecordDate().getTime() + 1000L * 60 * 60 * 24 * req.getValid())); + MeasuringInstrumentLedgerRecord measuringInstrumentLedgerRecord = new MeasuringInstrumentLedgerRecord(); + if(measuringInstrumentLedgerMapper.updateById(measuringInstrumentLedger) > 0) { + measuringInstrumentLedgerRecord.setMeasuringInstrumentLedgerId(req.getId()); + measuringInstrumentLedgerRecord.setRecordDate(req.getRecordDate()); + measuringInstrumentLedgerRecord.setEntryDate(req.getEntryDate()); + measuringInstrumentLedgerRecord.setValid(req.getValid()); + measuringInstrumentLedgerRecord.setUserId(req.getUserId()); + measuringInstrumentLedgerRecord.setUserName(sysUser.getUserName()); + measuringInstrumentLedgerRecordMapper.insert(measuringInstrumentLedgerRecord); + // 鍙拌处缁戝畾涓�娆� +// if(!CollectionUtils.isEmpty(req.getTempFileIds())){ +// migrateTempFilesToFormal(measuringInstrumentLedger.getId(), req.getTempFileIds(), FileNameType.MEASURING.getValue()); +// } + // 鍙拌处璁板綍缁戝畾涓�娆� + if(!CollectionUtils.isEmpty(req.getTempFileIds())){ + migrateTempFilesToFormal(measuringInstrumentLedgerRecord.getId(), req.getTempFileIds(), FileNameType.MEASURINGRecord.getValue()); + } + return true; + } + return false; + } + + @Override + public void export(HttpServletResponse response) { + List<MeasuringInstrumentLedger> list = measuringInstrumentLedgerMapper.listPage(new MeasuringInstrumentLedger()); + ExcelUtil<MeasuringInstrumentLedger> util = new ExcelUtil<MeasuringInstrumentLedger>(MeasuringInstrumentLedger.class); + util.exportExcel(response, list , "瀹㈡埛妗f鏁版嵁"); + } + + @Override + public boolean add(MeasuringInstrumentLedger measuringInstrumentLedger) throws IOException { + SysUser sysUser = sysUserMapper.selectUserById(measuringInstrumentLedger.getUserId()); + if (sysUser == null) { + return false; + } + measuringInstrumentLedger.setUserName(sysUser.getUserName()); + measuringInstrumentLedgerMapper.insert(measuringInstrumentLedger); + if(!CollectionUtils.isEmpty(measuringInstrumentLedger.getTempFileIds())){ + migrateTempFilesToFormal(measuringInstrumentLedger.getId(), measuringInstrumentLedger.getTempFileIds(), FileNameType.MEASURING.getValue()); + } + return true; + } + + + /** + * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍 + * + * @param businessId 涓氬姟ID锛堥攢鍞彴璐D锛� + * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃 + * @throws IOException 鏂囦欢鎿嶄綔寮傚父 + */ + private void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds,Integer fileType) throws IOException { + if (CollectionUtils.isEmpty(tempFileIds)) { + return; + } + + // 鏋勫缓姝e紡鐩綍璺緞锛堟寜涓氬姟绫诲瀷鍜屾棩鏈熷垎缁勶級 + String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE); + + Path formalDirPath = Paths.get(formalDir); + + // 纭繚姝e紡鐩綍瀛樺湪锛堥�掑綊鍒涘缓锛� + if (!Files.exists(formalDirPath)) { + Files.createDirectories(formalDirPath); + } + + for (String tempFileId : tempFileIds) { + // 鏌ヨ涓存椂鏂囦欢璁板綍 + TempFile tempFile = tempFileMapper.selectById(tempFileId); + if (tempFile == null) { + log.warn("涓存椂鏂囦欢涓嶅瓨鍦紝璺宠繃澶勭悊: {}", tempFileId); + continue; + } + + // 鏋勫缓姝e紡鏂囦欢鍚嶏紙鍖呭惈涓氬姟ID鍜屾椂闂存埑锛岄伩鍏嶅啿绐侊級 + String originalFilename = tempFile.getOriginalName(); + String fileExtension = FilenameUtils.getExtension(originalFilename); + String formalFilename = businessId + "_" + + System.currentTimeMillis() + "_" + + UUID.randomUUID().toString().substring(0, 8) + + (StringUtils.hasText(fileExtension) ? "." + fileExtension : ""); + + Path formalFilePath = formalDirPath.resolve(formalFilename); + + try { + // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級 + Files.move( + Paths.get(tempFile.getTempPath()), + formalFilePath, + StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE + ); + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + + // 鏇存柊鏂囦欢璁板綍锛堝叧鑱斿埌涓氬姟ID锛� + CommonFile fileRecord = new CommonFile(); + fileRecord.setCommonId(businessId); + fileRecord.setName(originalFilename); + fileRecord.setUrl(formalFilePath.toString()); + fileRecord.setCreateTime(LocalDateTime.now()); + fileRecord.setType(fileType); + commonFileMapper.insert(fileRecord); + + // 鍒犻櫎涓存椂鏂囦欢璁板綍 + tempFileMapper.deleteById(tempFile); + + log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath); + } catch (IOException e) { + log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e); + // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠� + throw new IOException("鏂囦欢杩佺Щ寮傚父", e); + } + } + } +} -- Gitblit v1.9.3