feat(device): 实现设备逻辑删除与真实删除功能

- 添加设备逻辑删除接口,更新查询SQL过滤已删除数据
- 添加设备真实删除接口,需管理员权限
- 在DeviceService中实现逻辑删除与真实删除方法
- 在DeviceMapper中添加逻辑删除SQL语句
- 新增设备相关错误码:删除失败、类型删除失败等
- 更新UserController中修改密码与更新用户名接口的请求方式为PUT
- 优化DeviceController中创建设备与查询设备接口的注解使用
- 引入Spring Security注解支持接口权限控制
This commit is contained in:
Chaos
2025-11-21 07:30:17 +08:00
parent 72a1e4d309
commit d4bbaf6715
6 changed files with 78 additions and 10 deletions

View File

@@ -54,7 +54,11 @@ public enum ErrorCode {
// 设备
DEVICE_NOT_FOUND(404, "DEVICE-301", "设备不存在"),
DEVICE_TYPE_NOT_FOUND(404, "DEVICE-302", "设备类型不存在")
DEVICE_TYPE_NOT_FOUND(404, "DEVICE-302", "设备类型不存在"),
DEVICE_DELETE_FAILED(500, "DEVICE-303", "设备删除失败"),
DEVICE_TYPE_DELETE_FAILED(500, "DEVICE-304", "设备类型删除失败"),
DEVICE_TYPE_NOT_EMPTY(400, "DEVICE-305", "设备类型不为空"),
DEVICE_DISABLED(403, "DEVICE-306", "设备已禁用"),
;

View File

@@ -11,7 +11,7 @@ public interface DeviceMapper extends BaseMapper<Device> {
* @param id 设备id
* @return 设备信息
*/
@Select("SELECT * FROM t_device WHERE id = #{id}")
@Select("SELECT * FROM t_device WHERE id = #{id} AND deleted = 0 ")
@Results(id = "DeviceResultMap", value ={
@Result(id = true, property = "id", column = "id"),
@Result(property = "name", column = "name"),
@@ -27,4 +27,14 @@ public interface DeviceMapper extends BaseMapper<Device> {
one = @One(select = "cn.nopj.chaos_api.mapper.DriveTypeMapper.selectById"))
})
Device selectOneWithTypeById(Long id);
/**
* 逻辑删除设备
* @param id 设备id
* @return 逻辑删除影响行数
*/
@Update("UPDATE t_device SET deleted = 1 WHERE id = #{id}")
int deleteDevice(Long id);
}

View File

@@ -17,4 +17,16 @@ public interface DeviceService {
* @return 设备信息
*/
DeviceResponse getDeviceById(Long id);
/**
* 删除设备信息 伪删除
* @param id 设备id
*/
void deleteDevice(Long id);
/**
* 删除设备信息 真删除
* @param id 设备id
*/
void deleteDeviceReal(Long id);
}

View File

@@ -48,4 +48,26 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
return new DeviceResponse(device);
}
@Override
@Transactional
public void deleteDevice(Long id) {
int i = this.baseMapper.deleteDevice(id);
if (i <= 0){
throw new BizException(ErrorCode.DEVICE_DELETE_FAILED);
}
}
@Override
@Transactional
public void deleteDeviceReal(Long id) {
log.info("删除设备id:{}",id);
int i = this.baseMapper.deleteById(id);
if (i <= 0){
throw new BizException(ErrorCode.DEVICE_DELETE_FAILED);
}
log.info("删除设备成功");
}
}

View File

@@ -5,9 +5,8 @@ import cn.nopj.chaos_api.dto.request.CreateDriveRequest;
import cn.nopj.chaos_api.dto.response.DeviceResponse;
import cn.nopj.chaos_api.model.ApiResult;
import cn.nopj.chaos_api.service.DeviceService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
/**
@@ -28,7 +27,7 @@ public class DeviceController {
* @param createDriveRequest 设备信息
* @return 新建设备信息结果
*/
@RequestMapping("/create")
@PostMapping("/create")
public ApiResult<DeviceResponse> createDevice(@RequestBody CreateDriveRequest createDriveRequest){
return ApiResult.success(deviceService.createDevice(createDriveRequest));
}
@@ -37,9 +36,30 @@ public class DeviceController {
* 查询指定设备信息
* @return 指定设备信息
*/
@RequestMapping("/get")
public ApiResult<DeviceResponse> getDeviceById(Long id){
@GetMapping("/{id}")
public ApiResult<DeviceResponse> getDeviceById(@PathVariable("id") Long id){
return ApiResult.success(deviceService.getDeviceById(id));
}
/**
* 逻辑删除指定设备信息
* @return 逻辑删除指定设备信息结果
*/
@DeleteMapping("/{id}")
public ApiResult<String> deleteDevice(@PathVariable("id") Long id){
deviceService.deleteDevice(id);
return ApiResult.success("删除成功");
}
/**
* 真实删除指定设备信息
* @return 真实删除指定设备信息结果
*/
@PreAuthorize("hasAuthority('admin')")
@DeleteMapping("/deleteReal")
public ApiResult<String> deleteDeviceReal(@RequestParam(value = "id") Long id){
deviceService.deleteDeviceReal(id);
return ApiResult.success("删除成功");
}
}

View File

@@ -41,7 +41,7 @@ public class UserController {
* @return 设置用户密码结果
*/
@PreAuthorize("hasAuthority('admin')")
@RequestMapping("/setUserPassword")
@PutMapping("/setUserPassword")
ApiResult<String> setUserPassword(@RequestBody SetUserPasswordRequest request){
userInfoService.setUserPassword(request.getUserId(), request.getPassword());
return ApiResult.success("用户密码修改成功");
@@ -52,7 +52,7 @@ public class UserController {
* @return 更新后用户名
* @deprecated 废弃 修改用户名之后token会失效
*/
@PostMapping("/updateUsername")
@PutMapping("/updateUsername")
ApiResult<String> updateUsername(@RequestAttribute("currentUsername") String username,@RequestBody UpdateUsernameRequest request){
userInfoService.updateUsername(username, request.getUsername());
return ApiResult.success("用户名更新成功");