luofl
2025-04-11 4f38ccbb1c2b2b05f285c2e4784fed8548e5f156
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
package com.ruoyi.web.controller.api;
 
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TencentMailUtil;
import com.ruoyi.system.dto.TBillDto;
import com.ruoyi.system.model.TBill;
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 com.ruoyi.web.controller.tool.TencentCosUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
 
 
/**
 * <p>
 * 开票管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-01-17
 */
@RestController
@RequestMapping("/t-invoice")
public class TInvoiceController {
    @Autowired
    private TInvoiceService invoiceService;
    @Autowired
    TBillService tBillService;
    @Autowired
    TencentCosUtil tencentCosUtil;
    @PreAuthorize("@ss.hasPermi('invoice:list')")
    @ApiOperation(value = "获取开票列表")
    @PostMapping("/list")
    public R<PageInfo<TInvoice>> list(@RequestBody TInvoiceQuery query) {
        query.setBusinessDeptId(SecurityUtils.getBusinessDeptId());
        return R.ok(invoiceService.pageList(query));
    }
 
    @PreAuthorize("@ss.hasPermi('invoice:list:del')")
    @Log(title = "开票信息-删除开票", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除开票")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(invoiceService.removeById(id));
    }
 
    @ApiOperation(value = "关联账单信息")
    @GetMapping(value = "/getBillByInvoiceId/{invoiceId}")
    public R<PageInfo<TBillDto>> getBillByInvoiceId(@PathVariable String invoiceId){
        return R.ok(tBillService.getBillByInvoiceId(invoiceId));
    }
 
    @ApiOperation(value = "上传开票凭证")
    @PostMapping("/uploadVoucher")
    @PreAuthorize("@ss.hasPermi('invoice:list:payment')")
    public R<Boolean> uploadVoucher(@RequestBody TInvoiceQuery query) {
        String invoiceVoucher = query.getInvoiceVoucher();
        String invoiceVoucherName = query.getInvoiceVoucherName();
        if (invoiceVoucher == null || invoiceVoucherName == null) {
            return R.fail("请上传发票文件");
        }
        String[] voucherUrls = invoiceVoucher.split(",");
        String[] voucherNames = invoiceVoucherName.split(",");
 
        // 确保两个数组长度一致
        int length = Math.min(voucherUrls.length, voucherNames.length);
        if (length == 0) {
            return R.fail("请上传发票文件");
        }
        // 构建附件列表
        List<Map<String, String>> attachments = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            String voucherUrl = voucherUrls[i];
            String fileName = voucherNames[i];
            Map<String, String> attachment = new HashMap<>(2); // 初始容量为2,避免扩容
            String tempDir = System.getProperty("java.io.tmpdir");
            Path filePath = Paths.get(tempDir, "invoice");
            // 保存到临时目录
            tencentCosUtil.download(voucherUrl,filePath.toString(),fileName);
 
            attachment.put("filePath", filePath.toString());
            attachment.put("fileName", fileName);
            attachments.add(attachment);
        }
        return R.ok(invoiceService.uploadVoucher(query,attachments));
    }
}