InvoiceController.java 3.67 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.core.watermark.Watermark;
import com.github.liaochong.myexcel.utils.WatermarkUtil;
import io.github.linpeilie.Converter;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.web.bind.annotation.*;
import vion.dto.InvoiceDTO;
import vion.service.IInvoiceService;
import vion.utils.excel.AttachmentExportUtil;
import vion.vo.InvoiceVO;
import vion.vo.UserVO;

import java.awt.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 发票记录管理
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class InvoiceController {

    private final IInvoiceService invoiceService;
    private final Converter converter;

    @GetMapping("/invoice")
    @SaCheckPermission(value = "invoice:list", orRole = "admin")
    public Page<InvoiceVO> list(InvoiceDTO dto) {
        return invoiceService.list(dto);
    }

    @GetMapping("/invoice/{id}")
    @SaCheckPermission(value = "invoice:query", orRole = "admin")
    public InvoiceVO getById(@PathVariable Long id) {
        return converter.convert(invoiceService.getById(id), InvoiceVO.class);
    }

    @PostMapping("/invoice")
    @SaCheckPermission(value = "invoice:save", orRole = "admin")
    public String save(@RequestBody List<InvoiceDTO> dto) {
        return invoiceService.save(dto);
    }

    @PostMapping("/invoice/{id}")
    @SaCheckPermission(value = "invoice:edit", orRole = "admin")
    public String update(@PathVariable Long id, @RequestBody InvoiceDTO dto) {
        dto.setId(id);
        return invoiceService.update(id, dto);
    }

    @DeleteMapping("/invoice/{id}")
    @SaCheckPermission(value = "invoice:remove", orRole = "admin")
    public String delById(@PathVariable Long id) {
        return invoiceService.delById(id);
    }

    @GetMapping("/invoice/export")
    @SaCheckPermission(value = "invoice:export", orRole = "admin")
    public void invoiceExport(InvoiceDTO dto, HttpServletResponse response) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        dto.setPageSize(30000);
        Page<InvoiceVO> voPage = invoiceService.list(dto);
        try (DefaultExcelBuilder<InvoiceVO> defaultExcelBuilder = DefaultExcelBuilder.of(InvoiceVO.class)) {
            Workbook workbook = defaultExcelBuilder.build(voPage.getRecords());
            // 水印添加指定字体,并在服务器上安装 SimSun 字体,解决中文字体变成方块的问题
            Watermark watermark = new Watermark();
            watermark.setText(user.getUsername() + "-" + user.getPhone());
            watermark.setFont(new Font("SimSun", Font.PLAIN, 16));
            WatermarkUtil.addWatermark(workbook, watermark);
            AttachmentExportUtil.export(workbook, StrUtil.format("发票列表_{}", DateUtil.formatDateTime(new Date())), response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 计算合同发票金额
     *
     * @param noList 合同编号列表,为空表示计算所有合同
     * @return java.lang.String
     */
    @GetMapping("/invoice/calMoney")
    @SaCheckPermission(value = "invoice:calMoney", orRole = "admin")
    public String calMoney(String[] noList) {
        return invoiceService.calMoney(noList);
    }

}