zhangmei
2025-03-10 170f393728ec9544e6d58e0eb05182a82f832e3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
 
/**
 * <p>
 * 开票管理 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-01-17
 */
@Service
public class TInvoiceServiceImpl extends ServiceImpl<TInvoiceMapper, TInvoice> implements TInvoiceService {
    @Autowired
    TInvoiceToBillServiceImpl tInvoiceToBillService;
    @Autowired
    TBillService tBillService;
    @Resource
    TencentMailUtil mailUtil;
 
    @Override
    public PageInfo<TInvoice> pageList(TInvoiceQuery query) {
        PageInfo<TInvoice> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TInvoice> list = makeQuery(query);
        pageInfo.setRecords(list);
        pageInfo.setTotal(list.size());
        return pageInfo;
    }
 
    @Override
    public List<TInvoice> makeQuery(TInvoiceQuery query) {
        LambdaQueryWrapper<TInvoice> 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<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);
    // }
}