package com.ruoyi.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.basic.PageInfo; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.TencentMailUtil; import com.ruoyi.system.mapper.TInvoiceMapper; import com.ruoyi.system.model.TInvoice; import com.ruoyi.system.query.TInvoiceQuery; import com.ruoyi.system.service.TBillService; import com.ruoyi.system.service.TInvoiceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; /** *

* 开票管理 服务实现类 *

* * @author xiaochen * @since 2025-01-17 */ @Service public class TInvoiceServiceImpl extends ServiceImpl implements TInvoiceService { @Autowired TInvoiceToBillServiceImpl tInvoiceToBillService; @Autowired TBillService tBillService; @Resource TencentMailUtil mailUtil; @Override public PageInfo pageList(TInvoiceQuery query) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List list = makeQuery(query); pageInfo.setRecords(list); pageInfo.setTotal(list.size()); return pageInfo; } @Override public List makeQuery(TInvoiceQuery query) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(null != query.getTitleType(),TInvoice::getTitleType,query.getTitleType()) .like(StringUtils.isNotEmpty(query.getTitleName()),TInvoice::getTitleName,query.getTitleName()) .eq(null != query.getStatus(),TInvoice::getStatus,query.getStatus()) .ge(StringUtils.isNotEmpty(query.getInvoiceStartTime()),TInvoice::getInvoiceTime,query.getInvoiceStartTime()) .le(StringUtils.isNotEmpty(query.getInvoiceEndTime()),TInvoice::getInvoiceTime,query.getInvoiceEndTime()) .orderByDesc(TInvoice::getCreateTime); return this.baseMapper.selectList(queryWrapper); } @Override public Boolean uploadVoucher(TInvoiceQuery query) { // 检查是否存在对应的发票记录 TInvoice preexist = getById(query.getId()); if (preexist == null) { log.error("未找到对应的发票记录,ID: {}"+query.getId()); return false; } // 更新发票信息 TInvoice tInvoice = new TInvoice(); tInvoice.setId(query.getId()); tInvoice.setInvoiceVoucher(query.getInvoiceVoucher()); tInvoice.setInvoiceVoucherName(query.getInvoiceVoucherName()); tInvoice.setInvoiceTime(query.getInvoiceTime()); tInvoice.setStatus(2); // 处理附件信息 List> attachments = buildAttachments(query.getInvoiceVoucher(), query.getInvoiceVoucherName()); if (attachments.isEmpty()) { log.warn("未找到有效的附件信息"); return updateById(tInvoice); } // 异步发送邮件 CompletableFuture.runAsync(() -> { try { mailUtil.sendInvoice(preexist.getEmail(), attachments); } catch (ServiceException e) { log.error("邮件发送失败", e); } }); // 更新数据库 return updateById(tInvoice); } private List> buildAttachments(String invoiceVoucher, String invoiceVoucherName) { if (invoiceVoucher == null || invoiceVoucherName == null) { return Collections.emptyList(); } String[] voucherUrls = invoiceVoucher.split(","); String[] voucherNames = invoiceVoucherName.split(","); // 确保两个数组长度一致 int length = Math.min(voucherUrls.length, voucherNames.length); if (length == 0) { return Collections.emptyList(); } // 构建附件列表 List> attachments = new ArrayList<>(length); for (int i = 0; i < length; i++) { Map attachment = new HashMap<>(2); // 初始容量为2,避免扩容 attachment.put("url", voucherUrls[i]); attachment.put("fileName", voucherNames[i]); attachments.add(attachment); } return attachments; } // @Override // public Boolean uploadVoucher(TInvoiceQuery query) { // TInvoice preexist = getById(query.getId()); // if (preexist == null){ // return false; // } // TInvoice tInvoice = new TInvoice(); // tInvoice.setId(query.getId()); // tInvoice.setInvoiceVoucher(query.getInvoiceVoucher()); // tInvoice.setInvoiceVoucherName(query.getInvoiceVoucherName()); // tInvoice.setInvoiceTime(query.getInvoiceTime()); // tInvoice.setStatus(2); // List> mapArrayList = new ArrayList<>(); // String invoiceVoucher = query.getInvoiceVoucher(); // String invoiceVoucherName = query.getInvoiceVoucherName(); // // List list = Arrays.stream(invoiceVoucher.split(",")).collect(Collectors.toList()); // for (int i = 0; i < list.size()-1; i++) { // Map map = new HashMap<>(); // map.put("url", list.get(i)); // map.put("fileName",invoiceVoucherName.split(",")[i]); // mapArrayList.add(map); // } // mailUtil.sendInvoice(preexist.getEmail(),mapArrayList); // return updateById(tInvoice); // } }