PointDesignController.java 5.45 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.text.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 vion.utils.excel.AttachmentExportUtil;
import com.github.liaochong.myexcel.utils.WatermarkUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import vion.dto.PointInfoDTO;
import vion.model.RejectInfo;
import vion.service.IPointInfoService;
import vion.third.WechatMod;
import vion.vo.PointInfoVO;
import vion.vo.UserVO;

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

/**
 * 点位管理
 *
 * @author HlQ
 * @date 2024/1/8
 */
@RestController
@RequestMapping("/api/point")
@RequiredArgsConstructor
@Slf4j
public class PointDesignController {

    private final IPointInfoService pointInfoService;
    private final WechatMod wechatMod;

    @PostMapping("/frontSubmit")
    public Object frontSubmit(PointInfoDTO dto) {
        return pointInfoService.frontSubmit(dto);
    }

    @PostMapping
    @SaCheckPermission(value = "point:save", orRole = "admin")
    public Object save(PointInfoDTO dto) {
        return pointInfoService.save(dto);
    }

    @PostMapping("/{id}")
    @SaCheckPermission(value = "point:edit", orRole = "admin")
    public String updById(@PathVariable Long id, PointInfoDTO dto) {
        return pointInfoService.upd(id, null, dto);
    }

    @PostMapping("/upd/{uuid}")
    public String updById(@PathVariable String uuid, PointInfoDTO dto) {
        return pointInfoService.upd(null, uuid, dto);
    }

    @GetMapping
    @SaCheckPermission(value = "point:list", orRole = "admin")
    public Page<PointInfoVO> list(PointInfoDTO dto) {
        return pointInfoService.list(dto);
    }

    @GetMapping("/{id}")
    @SaCheckPermission(value = "point:query", orRole = "admin")
    public PointInfoVO getPointById(@PathVariable Long id) {
        return pointInfoService.getPointDetail(id, null);
    }

    @GetMapping("/get/{uuid}")
    public PointInfoVO getPointByUuid(@PathVariable String uuid) {
        return pointInfoService.getPointDetail(null, uuid);
    }

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

    @PostMapping("/client/reject")
    public String clientReject(@RequestBody RejectInfo dto) {
        return pointInfoService.reject(dto, "client");
    }

    @PostMapping("/reject")
    @SaCheckPermission(value = "point:reject", orRole = "admin")
    public String reject(@RequestBody RejectInfo dto) {
        return pointInfoService.reject(dto, null);
    }

    @GetMapping("/reject/{pointId}")
    @SaCheckPermission(value = "point:reject:list", orRole = "admin")
    public Page<RejectInfo> rejectInfoList(@PathVariable Long pointId, RejectInfo dto) {
        return pointInfoService.rejectInfoList(pointId, dto);
    }

    @GetMapping("/reject/uuid/{uuid}")
    public Page<RejectInfo> rejectInfoPage(@PathVariable String uuid, RejectInfo dto) {
        return pointInfoService.rejectInfoList(uuid, dto);
    }

    @GetMapping("/desgin/push")
    @SaCheckPermission(value = "point:design:push", orRole = "admin")
    public Object designPush(Long pointId, String pushType) {
        return pointInfoService.designPush(pointId, null, pushType);
    }

    @GetMapping("/export")
    @SaCheckPermission(value = "point:export", orRole = "admin")
    public void pointExport(PointInfoDTO dto, HttpServletResponse response) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        dto.setPageSize(30000);
        Page<PointInfoVO> voPage = pointInfoService.list(dto);
        voPage.getRecords().forEach(v -> v.setPointUrl("https://yunwei.vionyun.com:8443/wap/setup-device?uuid=" + v.getUuid()));
        try (DefaultExcelBuilder<PointInfoVO> defaultExcelBuilder = DefaultExcelBuilder.of(PointInfoVO.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);
        }
    }

    @PostMapping("/install/submit/{uuid}")
    public String installSubmit(@PathVariable String uuid, @RequestBody List<String> deviceList) {
        return pointInfoService.installSubmit(uuid, deviceList);
    }

    @GetMapping("/getBindQRCode")
    public String getBindQRCode(String uuid) {
        return wechatMod.genBindOpenidQRCode(uuid);
    }

    @GetMapping("/getBindOpenid")
    public Object genBindOpenidQRCode(String uuid, String code) {
        return pointInfoService.bindOpenid(uuid, code);
    }

}