package com.ruoyi.sales.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.basic.dto.SupplierManageDto; import com.ruoyi.basic.excel.SupplierManageExcelDto; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.utils.file.FileUploadUtils; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.vo.FileVo; import com.ruoyi.sales.dto.InvoiceLedgerDto; import com.ruoyi.sales.excel.InvoiceLedgerExcelDto; import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper; import com.ruoyi.sales.mapper.InvoiceLedgerMapper; import com.ruoyi.sales.pojo.InvoiceLedger; import com.ruoyi.sales.pojo.InvoiceLedgerFile; import com.ruoyi.sales.service.InvoiceLedgerService; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; import java.util.stream.Collectors; @Service public class InvoiceLedgerServiceImpl extends ServiceImpl implements InvoiceLedgerService { @Value("${ruoyi.profile}") private String uploadFile; @Autowired private InvoiceLedgerMapper invoiceLedgerMapper; @Autowired private InvoiceLedgerFileMapper invoiceLedgerFileMapper; /** * 开票台账新增 * @param invoiceLedgerDto * @return */ @Override @Transactional(rollbackFor = Exception.class) public int invoiceLedgerAdd(InvoiceLedgerDto invoiceLedgerDto) { InvoiceLedger invoiceLedger = new InvoiceLedger(); BeanUtils.copyProperties(invoiceLedgerDto, invoiceLedger); int result = invoiceLedgerMapper.insert(invoiceLedger); List fileList = invoiceLedgerDto.getFileList(); if(CollectionUtils.isNotEmpty(fileList)){ fileList.forEach(fileVo -> { InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile(); BeanUtils.copyProperties(fileVo, invoiceLedgerFile); invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId()); invoiceLedgerFileMapper.insert(invoiceLedgerFile); }); } return result; } /** * 开票台账删除 * @param ids * @return */ @Override public int invoiceLedgerDel(List ids) { LambdaQueryWrapper delWrapper = new LambdaQueryWrapper<>(); delWrapper.in(InvoiceLedger::getId, ids); return invoiceLedgerMapper.delete(delWrapper); } /** * 开票台账修改 * @param invoiceLedgerDto * @return */ @Override @Transactional(rollbackFor = Exception.class) public int invoiceLedgerUpdate(InvoiceLedgerDto invoiceLedgerDto) { InvoiceLedger invoiceLedger = new InvoiceLedger(); BeanUtils.copyProperties(invoiceLedgerDto, invoiceLedger); int result = invoiceLedgerMapper.updateById(invoiceLedger); // 删除关联附件 LambdaQueryWrapper delWrapper = new LambdaQueryWrapper<>(); delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedger.getId()); invoiceLedgerFileMapper.delete(delWrapper); // 重新插入附件关联表 List fileList = invoiceLedgerDto.getFileList(); if(CollectionUtils.isNotEmpty(fileList)){ fileList.forEach(fileVo -> { InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile(); BeanUtils.copyProperties(fileVo, invoiceLedgerFile); invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId()); invoiceLedgerFileMapper.insert(invoiceLedgerFile); }); } return result; } /** * 开票台账分页查询 * @param page * @param invoiceLedgerDto * @return */ @Override public IPage invoiceLedgerPage(Page page, InvoiceLedgerDto invoiceLedgerDto) { return invoiceLedgerMapper.invoiceLedgerPage(page, invoiceLedgerDto); } /** * 开票台账文件查询 * @param invoiceLedgerId * @return */ @Override public List invoiceLedgerFileList(Integer invoiceLedgerId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedgerId); return invoiceLedgerFileMapper.selectList(queryWrapper); } /** * 开票台账文件上传 * @param file * @return */ @Override public FileVo invoiceLedgerUploadFile(MultipartFile file) { FileVo fileVo = new FileVo(); try { String baseDir = uploadFile + File.separatorChar + "invoiceLedger"; String filePath = FileUploadUtils.upload(baseDir, file); fileVo.setFileName(file.getOriginalFilename()); fileVo.setFilePath(filePath); fileVo.setFileSize((int)file.getSize()); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException("文件上传失败"); } return fileVo; } /** * 附件下载 * @param response * @param invoiceLedgerDto * @return */ @Override public void invoiceLedgerDownload(HttpServletResponse response, InvoiceLedgerDto invoiceLedgerDto) { List invoiceLedgerDtoList = invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto); List invoiceLedgerExcelDtoList = invoiceLedgerDtoList.stream().map(item -> { InvoiceLedgerExcelDto invoiceLedgerExcelDto = new InvoiceLedgerExcelDto(); BeanUtils.copyProperties(item, invoiceLedgerExcelDto); return invoiceLedgerExcelDto; }).collect(Collectors.toList()); ExcelUtil util = new ExcelUtil(InvoiceLedgerExcelDto.class); util.exportExcel(response, invoiceLedgerExcelDtoList, "供应商导出"); } }