SparePartController.java 4.3 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.github.liaochong.myexcel.core.DefaultStreamExcelBuilder;
import com.github.liaochong.myexcel.core.watermark.Watermark;
import com.github.liaochong.myexcel.utils.WatermarkUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.web.bind.annotation.*;
import vion.dto.SparePartDTO;
import vion.model.RRepairDevice;
import vion.service.ISparePartService;
import vion.utils.excel.AttachmentExportUtil;
import vion.vo.SparePartVO;
import vion.vo.UserVO;

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

/**
 * 备件申请
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/sparePart")
public class SparePartController {

    private final ISparePartService sparePartService;

    @PostMapping("/frontSubmit")
    public Object frontSubmit(SparePartDTO dto) {
        return sparePartService.frontSubmit(dto);
    }

    @GetMapping
    @SaCheckPermission(value = "sparePart:list", orRole = "admin")
    public List<SparePartVO> list(SparePartDTO dto) {
        Assert.isTrue(ArrayUtil.isAllNotNull(dto.getCreateTimeStart(), dto.getCreateTimeEnd()), "查询时间范围不能为空");
        return sparePartService.list(dto);
    }

    @GetMapping("/{id}")
    @SaCheckPermission(value = "sparePart:query", orRole = "admin")
    public SparePartVO getById(@PathVariable Long id) {
        return sparePartService.getSparePartDetail(id, null);
    }

    @GetMapping("/get/{uuid}")
    public SparePartVO getByUuid(@PathVariable String uuid) {
        return sparePartService.getSparePartDetail(null, uuid);
    }

    @PostMapping("/{id}")
    @SaCheckPermission(value = "sparePart:edit", orRole = "admin")
    public String updById(@PathVariable Long id, SparePartDTO dto) {
        return sparePartService.updById(id, dto);
    }

    @DeleteMapping("/{id}")
    @SaCheckPermission(value = "sparePart:remove", orRole = "admin")
    public String delById(@PathVariable Long id) {
        return sparePartService.removeById(id) ? "删除成功" : "删除失败";
    }

    @GetMapping("/export")
    @SaCheckPermission(value = "sparePart:export", orRole = "admin")
    public void sparePartExport(SparePartDTO dto, HttpServletResponse response) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        dto.setPageSize(30000);
        List<SparePartVO> voPage = sparePartService.list(dto);
        try (DefaultStreamExcelBuilder<SparePartVO> defaultStreamExcelBuilder  = DefaultStreamExcelBuilder.of(SparePartVO.class).autoMerge().start()) {
            voPage.forEach(v -> {
                var name = v.getRepairDeviceList().stream().map(RRepairDevice::getDeviceName).toList();
                var no = v.getRepairDeviceList().stream().map(RRepairDevice::getDeviceNo).toList();
                var rRepairDevice = new RRepairDevice();
                rRepairDevice.setDeviceNameList(name);
                rRepairDevice.setDeviceNoList(no);
                v.setRRepairDevice(rRepairDevice);
            });
            defaultStreamExcelBuilder.append(voPage);
            Workbook workbook = defaultStreamExcelBuilder.build();
            // 水印添加指定字体,并在服务器上安装 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("/notify")
    @SaCheckPermission(value = "sparePart:notify", orRole = "admin")
    public String notify(Long id, String userid) {
        return sparePartService.notify(id, userid) ? "钉钉提醒成功" : "钉钉提醒失败";
    }

}