zhuo
2025-04-23 234b0ac195934b34c06045b2d2ef0f10e239dd8e
cnas-device/src/main/java/com/ruoyi/device/controller/DeviceMaintenanceController.java
@@ -1,98 +1,81 @@
package com.ruoyi.device.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.numgen.NumberGenerator;
import com.ruoyi.device.excel.DeviceMaintenanceExport;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.device.dto.DeviceMaintenanceDto;
import com.ruoyi.device.pojo.DeviceMaintenance;
import com.ruoyi.device.service.DeviceMaintenanceService;
import com.ruoyi.system.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
 * todo: 孙河滨
 */
@RestController
@RequestMapping("/device-maintain")
@Api(tags = "设备维护保养")
@RequestMapping("/deviceMaintain")
public class DeviceMaintenanceController {
    @Autowired
    private DeviceMaintenanceService deviceMaintenanceService;
    @Autowired
    private NumberGenerator<DeviceMaintenance> numberGenerator;
    //增
    @PostMapping()
    public Result create(@RequestBody DeviceMaintenance deviceMaintenance){
        String year = new SimpleDateFormat("yy", Locale.CHINESE).format(new Date());
        String month = new SimpleDateFormat("MM", Locale.CHINESE).format(new Date());
        String processNumber = numberGenerator.generateNumberWithPrefix(3, "DG-TC-23FM " + month + "-" + year + month, DeviceMaintenance::getDeviceNumber);
        deviceMaintenance.setDeviceNumber(processNumber);
        return Result.success(deviceMaintenanceService.save(deviceMaintenance));
    private UserMapper userMapper;
    /**
     * 设备维护分页查询
     * @param deviceMaintenance
     * @return
     */
    @ApiOperation(value = "设备维护分页查询")
    @GetMapping("/selectDeviceMaintenancePage")
    public Result selectDeviceMaintenancePage(Page page, DeviceMaintenanceDto deviceMaintenance){
        return Result.success(deviceMaintenanceService.selectDeviceMaintenancePage(page, deviceMaintenance));
    }
    //通过deviceId查询维护数据
    @GetMapping("/getDeviceMaintenancePage")
    public Result getDeviceMaintenancePage(@RequestParam("deviceId") Integer deviceId, Page page, String deviceNumber){
        return Result.success(deviceMaintenanceService.getDeviceMaintenancePage(page, deviceId, deviceNumber));
    }
    //删
    @DeleteMapping("/delete/{id}")
    public void deleteDeviceFault(@PathVariable Integer id) {
        deviceMaintenanceService.removeById(id);
    }
    /**
     * 新增修改设备维护保养
     * @param deviceMaintenance
     * @return
     */
    @ApiOperation(value = "新增修改设备维护保养")
    @PostMapping("/addDeviceMaintenance")
    public Result addDeviceMaintenance(@RequestBody DeviceMaintenance deviceMaintenance){
        if (deviceMaintenance.getDeviceId() == null) {
            throw new RuntimeException("缺少设备id");
        }
        User user = userMapper.selectById(deviceMaintenance.getMaintenanceUserId());
        deviceMaintenance.setMaintenanceUserName(user.getName());
    @GetMapping("/deviceMaintenanceExport")
    public Result deviceMaintenanceExport(@RequestParam("deviceId") Integer deviceId, HttpServletResponse response) throws IOException {
        List<DeviceMaintenanceExport> list = deviceMaintenanceService.deviceMaintenanceExport(deviceId);
        response.setHeader("requestType","excel");
        response.setHeader("Access-Control-Expose-Headers", "requestType");
        // 设置单元格样式
        // 保存到第一个sheet中
        EasyExcel.write(response.getOutputStream())
                .head(DeviceMaintenanceExport.class)
                .registerWriteHandler(getHorizontalCellStyleStrategy((short) 12))
                .sheet()
                .doWrite(list);
        return Result.success();
    }
    @ApiOperation(value = "设备维护记录导出")
    @GetMapping("/exportMaintenanceRecord")
    public void exportMaintenanceRecord(@RequestParam("deviceId") Integer deviceId, HttpServletResponse response) throws Exception {
        deviceMaintenanceService.exportMaintenanceRecord(deviceId, response);
        return Result.success(deviceMaintenanceService.saveOrUpdate(deviceMaintenance));
    }
    /**
     * 单元格样式策略
     * 删除修改设备维护保养
     * @param id
     * @return
     */
    public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy(Short fontHeightInPoints) {
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 【水平居中需要使用以下两行】
        // 设置文字左右居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 设置文字上下居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        // 样式策略
        return new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
    @ApiOperation(value = "删除修改设备维护保养")
    @DeleteMapping("/deleteDeviceMaintenance")
    public Result deleteDeviceMaintenance(Integer id){
        return Result.success(deviceMaintenanceService.removeById(id));
    }
    /**
     * 导出设备维护保养
     */
    @ApiOperation("导出设备维护保养")
    @GetMapping("/exportDeviceMaintenance")
    public void exportDeviceMaintenance(@RequestParam("deviceId") Integer deviceId, HttpServletResponse response) {
        deviceMaintenanceService.exportDeviceMaintenance(deviceId, response);
    }
}