2025/4/21团队开发进度报告
实现设备表的增删改查,保修故障提交:

DeviceController:
package com.example.demo.controller;
import com.example.demo.common.Result;
import com.example.demo.entity.Device;
import com.example.demo.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* Qi
* 25/4/21
* 设备的增删改查
* */
@RestController
@RequestMapping("/device")
public class DeviceController {
@Autowired
private DeviceService deviceService;
@GetMapping("/list")
public Result getAllDevices() {
return Result.success(deviceService.getAllDevices());
}
@GetMapping("/{deviceId}")
public Result getDevice(@PathVariable String deviceId) {
Device device = deviceService.getDeviceById(deviceId);
return device != null ? Result.success(device) : Result.fail("设备不存在");
}
@PostMapping("/add")
public Result addDevice(@RequestBody Device device) {
if (deviceService.addDevice(device)) {
return Result.success(device);
}
return Result.fail("添加设备失败,设备ID可能已存在");
}
@PutMapping("/update")
public Result updateDevice(@RequestBody Device device) {
if (deviceService.updateDevice(device)) {
return Result.success(device);
}
return Result.fail("更新设备失败,设备可能不存在");
}
@PutMapping("/status")
public Result updateDeviceStatus(@RequestBody Device device) {
if (deviceService.updateDeviceStatus(device.getDeviceId(), device.getStatus())) {
return Result.success(device);
}
return Result.fail("更新设备状态失败,设备可能不存在");
}
@DeleteMapping("/{deviceId}")
public Result deleteDevice(@PathVariable String deviceId) {
if (deviceService.deleteDevice(deviceId)) {
return Result.success();
}
return Result.fail("删除设备失败,设备可能不存在");
}
/**
* 获取设备状态统计
* @return 各状态设备数量
*/
@GetMapping("/stats")
public Result getDeviceStats() {
List<Device> devices = deviceService.getAllDevices();
Map<String, Integer> stats = new HashMap<>();
// 初始化统计数据
stats.put("normal", 0);
stats.put("fault", 0);
stats.put("maintenance", 0);
stats.put("scrapped", 0);
// 统计各状态设备数量
for (Device device : devices) {
String status = device.getStatus();
if ("正常".equals(status)) {
stats.put("normal", stats.get("normal") + 1);
} else if ("故障".equals(status)) {
stats.put("fault", stats.get("fault") + 1);
} else if ("维护中".equals(status)) {
stats.put("maintenance", stats.get("maintenance") + 1);
} else if ("报废".equals(status)) {
stats.put("scrapped", stats.get("scrapped") + 1);
}
}
return Result.success(stats);
}
}
Device:
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("devices")
public class Device {
@TableId
private String deviceId;
private String deviceType;
private String location;
private String status;
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Device;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DeviceMapper extends BaseMapper<Device> {
}
DeviceServiceImpl:
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Device;
import com.example.demo.mapper.DeviceMapper;
import com.example.demo.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/*
* Qi
* 25/4/21
* 设备的增删改查
* */
@Service
public class DeviceServiceImpl implements DeviceService {
@Autowired
private DeviceMapper deviceMapper;
@Override
public List<Device> getAllDevices() {
return deviceMapper.selectList(null);
}
@Override
public Device getDeviceById(String deviceId) {
return deviceMapper.selectById(deviceId);
}
@Override
public boolean addDevice(Device device) {
// 检查设备ID是否已存在
Device existingDevice = deviceMapper.selectById(device.getDeviceId());
if (existingDevice != null) {
return false;
}
return deviceMapper.insert(device) > 0;
}
@Override
public boolean updateDevice(Device device) {
return deviceMapper.updateById(device) > 0;
}
@Override
public boolean deleteDevice(String deviceId) {
return deviceMapper.deleteById(deviceId) > 0;
}
@Override
public boolean updateDeviceStatus(String deviceId, String status) {
Device device = deviceMapper.selectById(deviceId);
if (device != null) {
device.setStatus(status);
return deviceMapper.updateById(device) > 0;
}
return false;
}
}
DeviceService:
package com.example.demo.service;
import com.example.demo.entity.Device;
import java.util.List;
/*
* Qi
* 25/4/21
* 设备的增删改查
* */
public interface DeviceService {
List<Device> getAllDevices();
Device getDeviceById(String deviceId);
boolean addDevice(Device device);
boolean updateDevice(Device device);
boolean deleteDevice(String deviceId);
boolean updateDeviceStatus(String deviceId, String status); // 新增此方法
}
DeviceList.vue:
<!-- Qi -->
<!-- 05/4/21 -->
<template>
<div class="device-container">
<!-- 修改标题部分 -->
<div class="header">
<h2>设备设置</h2>
<el-button type="primary" @click="showAddDialog">添加设备</el-button>
</div>
<div class="table-container">
<el-table :data="deviceList" border style="width: 100%" height="450">
<el-table-column prop="deviceId" label="设备编号" width="120" />
<el-table-column prop="deviceType" label="设备类型" width="150" />
<el-table-column prop="location" label="设备位置" min-width="150" />
<el-table-column prop="status" label="设备状态" width="100">
<template #default="scope">
<el-tag :type="getStatusType(scope.row.status)" effect="plain">
{{ scope.row.status }}
</el-tag>
</template>
</el-table-column>
// 修改操作列按钮的条件渲染
<el-table-column label="操作" width="160" fixed="right">
<template #default="scope">
<el-button
size="small"
@click="showEditDialog(scope.row)"
:disabled="scope.row.status !== '正常'"
>编辑</el-button>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.row)"
:disabled="scope.row.status !== '报废'"
>删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</div>
<!-- 添加/编辑设备对话框 -->
<el-dialog
:title="dialogTitle"
v-model="dialogVisible"
width="550px"
>
<el-form :model="deviceForm" label-width="100px">
<el-form-item label="设备编号">
<el-input v-model="deviceForm.deviceId" :disabled="isEdit" style="width: 400px" />
</el-form-item>
<el-form-item label="设备类型">
<el-input v-model="deviceForm.deviceType" style="width: 400px" />
</el-form-item>
<el-form-item label="设备位置">
<el-input v-model="deviceForm.location" style="width: 400px" />
</el-form-item>
<el-form-item label="设备状态">
<el-select v-model="deviceForm.status" style="width: 400px">
<el-option label="正常" value="正常" />
<el-option label="故障" value="故障" />
<el-option label="维护中" value="维护中" />
<el-option label="报废" value="报废" />
</el-select>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">确定</el-button>
</span>
</template>
</el-dialog>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDeviceList, addDevice, updateDevice, deleteDevice } from '@/api/device'
import FaultReportForm from './components/FaultReportForm.vue'
const deviceList = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const dialogTitle = ref('')
const deviceForm = ref({
deviceId: '',
deviceType: '',
location: '',
status: '正常'
})
// 获取设备列表
const fetchDeviceList = async () => {
try {
const res = await getDeviceList()
if (res.code === 200) { // 注意这里改为数字类型的 200
deviceList.value = res.data || [] // 添加空数组作为默认值
} else {
ElMessage.error(res.msg || '获取设备列表失败')
}
} catch (error) {
console.error('获取设备列表错误:', error)
ElMessage.error('获取设备列表失败')
}
}
// 获取状态标签类型
const getStatusType = (status) => {
const statusMap = {
'正常': 'success',
'故障': 'danger',
'维护中': 'warning',
'报废': 'info'
}
return statusMap[status] || ''
}
// 添加这些方法
// 生成设备编号
const generateDeviceId = () => {
const date = new Date()
const year = date.getFullYear().toString().slice(-2) // 获取年份后两位
const month = (date.getMonth() + 1).toString().padStart(2, '0') // 获取月份,补零
const sequence = deviceList.value.length + 1 // 序号从1开始
const sequenceStr = sequence.toString().padStart(3, '0') // 序号补零到3位
return `${year}${month}${sequenceStr}`
}
const showAddDialog = () => {
isEdit.value = false
dialogTitle.value = '添加设备'
deviceForm.value = {
deviceId: generateDeviceId(),
deviceType: '',
location: '',
status: '正常'
}
dialogVisible.value = true
}
// 修改 showEditDialog 方法
const showEditDialog = (row) => {
if (row.status !== '正常') {
ElMessage.warning('只能编辑正常状态的设备')
return
}
isEdit.value = true
dialogTitle.value = '编辑设备'
deviceForm.value = { ...row }
dialogVisible.value = true
}
// 删除这些重复的方法
// handleAdd 和 handleEdit 可以删除,因为已经被上面的方法替代
// 删除设备
const handleDelete = (row) => {
ElMessageBox.confirm('确认删除该设备吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async () => {
try {
const res = await deleteDevice(row.deviceId)
if (res.code === 200) { // 修改为数字类型的 200
ElMessage.success('删除成功')
fetchDeviceList()
} else {
ElMessage.error(res.msg || '删除失败')
}
} catch (error) {
console.error('删除失败:', error)
ElMessage.error('删除失败')
}
}).catch(() => {
// 用户点击取消按钮时的处理
ElMessage.info('已取消删除')
})
}
// 提交表单
const handleSubmit = async () => {
try {
const api = isEdit.value ? updateDevice : addDevice
const res = await api(deviceForm.value)
if (res.code === 200) { // 修改为数字类型的 200
ElMessage.success(isEdit.value ? '更新成功' : '添加成功')
dialogVisible.value = false
fetchDeviceList()
} else {
ElMessage.error(res.msg || (isEdit.value ? '更新失败' : '添加失败'))
}
} catch (error) {
console.error('操作失败:', error)
ElMessage.error(isEdit.value ? '更新失败' : '添加失败')
}
}
onMounted(() => {
fetchDeviceList()
})
const faultReportFormRef = ref(null)
const handleFaultReport = (row) => {
faultReportFormRef.value.showDialog(row.deviceId)
}
</script>
<style scoped>
.device-container {
padding: 20px;
background-color: #fff;
border-radius: 4px;
}
.header {
margin-bottom: 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.header h2 {
margin: 0;
font-size: 18px;
font-weight: 500;
color: #1f2f3d;
}
.table-container {
width: 100%;
overflow: hidden;
}
.dialog-footer {
display: flex;
justify-content: flex-end;
gap: 10px;
}
:deep(.el-dialog__body) {
padding: 15px 20px;
}
:deep(.el-form-item) {
margin-bottom: 18px;
}
:deep(.el-form-item__label) {
font-weight: bold;
}
</style>
<!-- 添加故障报修表单组件 -->
<FaultReportForm ref="faultReportFormRef" @success="fetchDeviceList" />

浙公网安备 33010602011771号