package com.ruoyi.require.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.numgen.NumberGenerator;
import com.ruoyi.require.excel.DeviceRecordExport;
import com.ruoyi.require.pojo.DeviceRecord;
import com.ruoyi.require.service.DeviceRecordService;
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;
/**
*
* cnas设备使用记录表 前端控制器
*
*
* @author 江苏鵷雏网络科技有限公司
* @since 2024-09-21 11:06:47
*/
@RestController
@RequestMapping("/deviceRecord")
public class DeviceRecordController {
@Autowired
private DeviceRecordService deviceRecordService;
@Autowired
private NumberGenerator numberGenerator;
@GetMapping("/deviceRecordPage")
public Result incidentReportPages(@RequestParam("deviceId") Integer deviceId, Page page, String deviceNumber) {
return Result.success(deviceRecordService.deviceRecordPage(deviceId, page, deviceNumber));
}
@PostMapping("/saveDeviceRecord")
public Result saveDeviceRecords(@RequestBody DeviceRecord deviceRecord) {
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, DeviceRecord::getDeviceNumber);
deviceRecord.setDeviceNumber(processNumber);
return Result.success(deviceRecordService.save(deviceRecord));
}
@DeleteMapping("/deleteDeviceRecord")
public Result deleteDeviceRecords(@RequestParam("id") Integer id) {
return Result.success(deviceRecordService.removeById(id));
}
@GetMapping("/deviceRecordExport")
public Result incidentReportExport(@RequestParam("deviceId") Integer deviceId, HttpServletResponse response) throws IOException {
List list = deviceRecordService.incidentReportExport(deviceId);
response.setHeader("requestType", "excel");
response.setHeader("Access-Control-Expose-Headers", "requestType");
// 设置单元格样式
// 保存到第一个sheet中
EasyExcel.write(response.getOutputStream())
.head(DeviceRecordExport.class)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 自适应列宽
.sheet()
.doWrite(list);
return Result.success();
}
}