package com.ruoyi.sales.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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.common.utils.poi.ExcelUtil; import com.ruoyi.common.vo.FileVo; import com.ruoyi.sales.dto.InvoiceLedgerDto; import com.ruoyi.sales.dto.InvoiceRegistrationProductDto; import com.ruoyi.sales.excel.InvoiceLedgerExcelDto; import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper; import com.ruoyi.sales.mapper.InvoiceLedgerMapper; import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper; 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.util.ObjectUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.math.BigDecimal; import java.time.LocalDate; import java.time.YearMonth; import java.util.List; import java.util.Objects; import java.util.UUID; 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; @Autowired private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; /** * 开票台账新增 * @param productDto * @return */ @Override @Transactional(rollbackFor = Exception.class) public int invoiceLedgerSaveOrUpdate(InvoiceRegistrationProductDto productDto) { // 判断是否已经新增开票台账 QueryWrapper ledgerQueryWrapper = new QueryWrapper<>(); ledgerQueryWrapper.eq("invoice_registration_product_id", productDto.getId()); InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper); int result; if(ObjectUtils.isEmpty(invoiceLedger)){ invoiceLedger = new InvoiceLedger(); invoiceLedger.setInvoiceRegistrationProductId(productDto.getId()); invoiceLedger.setInvoiceNo(productDto.getInvoiceNo()); invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal()); invoiceLedger.setInvoiceDate(productDto.getInvoiceDate()); invoiceLedger.setInvoicePerson(productDto.getInvoicePerson()); result = invoiceLedgerMapper.insert(invoiceLedger); }else { invoiceLedger.setInvoiceNo(productDto.getInvoiceNo()); invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal()); invoiceLedger.setInvoiceDate(productDto.getInvoiceDate()); invoiceLedger.setInvoicePerson(productDto.getInvoicePerson()); result = invoiceLedgerMapper.updateById(invoiceLedger); //删除所有附件关联 LambdaQueryWrapper delWrapper = new LambdaQueryWrapper<>(); delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedger.getId()); invoiceLedgerFileMapper.delete(delWrapper); } List fileList = productDto.getFileList(); if(CollectionUtils.isNotEmpty(fileList)){ for (FileVo fileVo : fileList) { 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 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"; File dirFile = new File(baseDir); if(!dirFile.exists()){ dirFile.mkdirs(); } String filePath = baseDir + File.separatorChar + UUID.randomUUID() + "_" + file.getOriginalFilename(); file.transferTo(new File(filePath)); fileVo.setName(file.getOriginalFilename()); fileVo.setUrl(filePath); fileVo.setFileSize((int)file.getSize()); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException("文件上传失败"); } return fileVo; } /** * 附件下载 * @param response * @param invoiceLedgerDto * @return */ @Override public void invoiceLedgerExport(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, "供应商导出"); } /** * 开票台账详情 * @param id * @return */ @Override public InvoiceLedgerDto invoiceLedgerDetail(Integer id) { InvoiceLedgerDto invoiceLedgerDto = invoiceLedgerMapper.invoiceLedgerInfo(id); if(ObjectUtils.isEmpty(invoiceLedgerDto)){ throw new RuntimeException("开票台账查找失败"); } // 查询附件 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("invoice_ledger_id", id); List invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper); List fileList = invoiceLedgerFileList.stream().map(item -> { FileVo fileVo = new FileVo(); BeanUtils.copyProperties(item, fileVo); return fileVo; }).collect(Collectors.toList()); invoiceLedgerDto.setFileList(fileList); return invoiceLedgerDto; } /** * 附件提交 * @param invoiceLedgerDto * @return */ @Override public void invoiceLedgerCommitFile(InvoiceLedgerDto invoiceLedgerDto) { if(null == invoiceLedgerDto.getId()){ throw new RuntimeException("缺少发票台账主键"); } if(CollectionUtils.isEmpty(invoiceLedgerDto.getFileList())){ throw new RuntimeException("缺少文件信息"); } QueryWrapper ledgerQueryWrapper = new QueryWrapper<>(); ledgerQueryWrapper.eq("invoice_registration_product_id", invoiceLedgerDto.getId()); InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper); if(ObjectUtils.isEmpty(invoiceLedger)){ throw new RuntimeException("开票台账未登记"); } List fileList = invoiceLedgerDto.getFileList(); fileList.forEach(fileVo -> { InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile(); BeanUtils.copyProperties(fileVo, invoiceLedgerFile); invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId()); invoiceLedgerFileMapper.insert(invoiceLedgerFile); }); } /** * 开票台账查询 * @param invoiceLedgerDto * @return */ @Override public List invoiceLedgerList(InvoiceLedgerDto invoiceLedgerDto) { return invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto); } /** * 客户销售记录 * @param page * @param invoiceLedgerDto * @return */ @Override public IPage invoiceLedgerSalesAccount(Page page, InvoiceLedgerDto invoiceLedgerDto) { IPage invoiceLedgerDtoIPage = invoiceLedgerMapper.invoiceLedgerSalesAccount(page, invoiceLedgerDto); return invoiceLedgerDtoIPage; } @Override public BigDecimal getInvoiceAmount() { LocalDate now = LocalDate.now(); YearMonth currentMonth = YearMonth.from(now); // 创建LambdaQueryWrapper LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.ge(InvoiceLedger::getInvoiceDate, currentMonth.atDay(1).atStartOfDay()) // 大于等于本月第一天 .lt(InvoiceLedger::getInvoiceDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天 // 执行查询并计算总和 List invoiceLedgers = invoiceLedgerMapper.selectList(queryWrapper); BigDecimal totalContractAmount = invoiceLedgers.stream() .map(InvoiceLedger::getInvoiceTotal) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); return totalContractAmount; } /** * 开票登记产品分页查询 * @param page * @param registrationProductDto * @return */ @Override public IPage registrationProductPage(Page page, InvoiceRegistrationProductDto registrationProductDto) { return invoiceRegistrationProductMapper.invoiceRegistrationProductPage(page,registrationProductDto); } /** * 产品开票台账详情 * @param id * @return */ @Override public InvoiceRegistrationProductDto invoiceLedgerProductDetail(Integer id) { InvoiceRegistrationProductDto invoiceRegistrationProductDto = invoiceLedgerMapper.invoiceLedgerProductInfo(id); if(ObjectUtils.isEmpty(invoiceRegistrationProductDto)){ throw new RuntimeException("产品开票台账查找失败"); } // 查询附件 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("invoice_ledger_id", invoiceRegistrationProductDto.getInvoiceLedgerId()); List invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper); List fileList = invoiceLedgerFileList.stream().map(item -> { FileVo fileVo = new FileVo(); BeanUtils.copyProperties(item, fileVo); return fileVo; }).collect(Collectors.toList()); invoiceRegistrationProductDto.setFileList(fileList); return invoiceRegistrationProductDto; } }