PaymentController.java 3.6 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.AttachmentExportUtil;
import com.github.liaochong.myexcel.utils.WatermarkUtil;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import vion.dto.PaymentDTO;
import vion.service.IPaymentService;
import vion.vo.PaymentVO;
import vion.vo.UserVO;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* 收款记录管理
*/
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class PaymentController {

    private final IPaymentService paymentService;
    private final Converter converter;

    @GetMapping("/payment")
    @SaCheckPermission(value = "payment:list", orRole = "admin")
    public Page<PaymentVO> list(PaymentDTO dto) {
        return paymentService.list(dto);
    }

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

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

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

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

    @GetMapping("/payment/export")
    @SaCheckPermission(value = "contract:export", orRole = "admin")
    public void pointExport(PaymentDTO dto, HttpServletResponse response) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        dto.setPageSize(30000);
        Page<PaymentVO> voPage = paymentService.list(dto);
        try (DefaultExcelBuilder<PaymentVO> defaultExcelBuilder = DefaultExcelBuilder.of(PaymentVO.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);
        }
    }

    @GetMapping("/openSafe")
    public String openSafe(String code) {
        // 校验 redis 里查询 code 相关信息
        if (true) {
            StpUtil.openSafe("payment", 600);
            return "认证成功";
        }
        return "认证失败";
    }

}