无关风月
2025-03-18 da81542b6dc8984f639687f30e7e610dc139b085
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TInvoiceServiceImpl.java
@@ -3,7 +3,9 @@
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;
@@ -12,7 +14,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
 * <p>
@@ -28,6 +33,8 @@
    TInvoiceToBillServiceImpl tInvoiceToBillService;
    @Autowired
    TBillService tBillService;
    @Resource
    TencentMailUtil mailUtil;
    @Override
    public PageInfo<TInvoice> pageList(TInvoiceQuery query) {
@@ -49,4 +56,94 @@
                .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<Map<String, String>> 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<Map<String, String>> 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<Map<String, String>> attachments = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            Map<String, String> 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<Map<String, String>> mapArrayList = new ArrayList<>();
    //     String invoiceVoucher = query.getInvoiceVoucher();
    //     String invoiceVoucherName = query.getInvoiceVoucherName();
    //
    //     List<String> list = Arrays.stream(invoiceVoucher.split(",")).collect(Collectors.toList());
    //     for (int i = 0; i < list.size()-1; i++) {
    //         Map<String, String> 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);
    // }
}