package com.linghu.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.linghu.model.common.ResponseResult;
|
import com.linghu.model.entity.Type;
|
import com.linghu.service.TypeService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.Date;
|
import java.util.List;
|
|
@RestController
|
@RequestMapping("/type")
|
@Api(value = "类型相关接口", tags = "设置-类型")
|
public class TypeController {
|
|
@Autowired
|
private TypeService typeService;
|
|
@PostMapping
|
@ApiOperation(value = "添加类型")
|
public ResponseResult<Type> add(@RequestBody Type type) {
|
type.setDel_flag(0);
|
boolean success = typeService.save(type);
|
if (success) {
|
return ResponseResult.success(type);
|
}
|
return ResponseResult.error("添加类型失败");
|
}
|
|
@PostMapping("/batch")
|
@ApiOperation(value = "批量添加类型")
|
public ResponseResult<Void> batchAdd(@RequestBody List<Type> types) {
|
types.forEach(type -> type.setDel_flag(0));
|
|
boolean success = typeService.saveBatch(types);
|
if (success) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("批量添加类型失败");
|
}
|
|
@DeleteMapping("/{typeId}")
|
@ApiOperation(value = "删除类型")
|
public ResponseResult<Void> delete(@PathVariable Integer typeId) {
|
Type type = new Type();
|
type.setType_id(typeId);
|
type.setDel_flag(1);
|
boolean success = typeService.updateById(type);
|
if (success) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("删除类型失败");
|
}
|
|
@PutMapping
|
@ApiOperation(value = "更新类型")
|
public ResponseResult<Void> update(@RequestBody Type type) {
|
boolean success = typeService.updateById(type);
|
if (success) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("更新类型失败");
|
}
|
|
@PutMapping("/batch")
|
@ApiOperation(value = "批量更新类型")
|
public ResponseResult<Void> batchUpdate(@RequestBody List<Type> types) {
|
boolean success = typeService.updateBatchById(types);
|
if (success) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("批量更新类型失败");
|
}
|
|
@GetMapping("/{typeId}")
|
@ApiOperation(value = "根据ID查询类型")
|
public ResponseResult<Type> getById(@PathVariable Integer typeId) {
|
Type type = typeService.getById(typeId);
|
if (type != null && type.getDel_flag() != 1) {
|
return ResponseResult.success(type);
|
}
|
return ResponseResult.error("类型不存在");
|
}
|
|
@DeleteMapping("/batch")
|
@ApiOperation(value = "批量删除类型")
|
public ResponseResult<Void> batchDelete(@RequestBody List<Integer> typeIds) {
|
List<Type> types = typeIds.stream().map(id -> {
|
Type type = new Type();
|
type.setType_id(id);
|
type.setDel_flag(1);
|
return type;
|
}).collect(java.util.stream.Collectors.toList());
|
boolean success = typeService.updateBatchById(types);
|
if (success) {
|
return ResponseResult.success();
|
}
|
return ResponseResult.error("批量删除类型失败");
|
}
|
|
@GetMapping("/list")
|
@ApiOperation(value = "查询类型列表,不传页数和大小就查全部")
|
public ResponseResult<List<Type>> list(
|
@RequestParam(required = false) Integer page,
|
@RequestParam(required = false) Integer pageSize) {
|
LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(Type::getDel_flag, 0);
|
|
if (page != null && pageSize != null) {
|
Page<Type> pageInfo = new Page<>(page, pageSize);
|
Page<Type> result = typeService.page(pageInfo, queryWrapper);
|
return ResponseResult.success(result.getRecords());
|
} else {
|
List<Type> list = typeService.list(queryWrapper);
|
return ResponseResult.success(list);
|
}
|
}
|
}
|