guyue
2025-07-02 0a99c3a898976f64b53ca05751514bac8500ad60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/type")
public class TypeController {
 
    @Autowired
    private TypeService typeService;
 
    @PostMapping
    public ResponseResult<Type> add(@RequestBody Type type) {
        type.setDelFlag(0);
        boolean success = typeService.save(type);
        if (success) {
            return ResponseResult.success(type);
        }
        return ResponseResult.error("添加类型失败");
    }
 
    @PostMapping("/batch")
    public ResponseResult<Void> batchAdd(@RequestBody List<Type> types) {
        boolean success = typeService.saveBatch(types);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量添加类型失败");
    }
 
    @DeleteMapping("/{typeId}")
    public ResponseResult<Void> delete(@PathVariable Integer typeId) {
        Type type = new Type();
        type.setTypeId(typeId);
        type.setDelFlag(1);
        boolean success = typeService.updateById(type);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("删除类型失败");
    }
 
    @DeleteMapping("/batch")
    public ResponseResult<Void> batchDelete(@RequestBody List<Integer> typeIds) {
        boolean success = typeService.removeBatchByIds(typeIds);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量删除类型失败");
    }
 
    @PutMapping
    public ResponseResult<Void> update(@RequestBody Type type) {
        boolean success = typeService.updateById(type);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("更新类型失败");
    }
 
    @PutMapping("/batch")
    public ResponseResult<Void> batchUpdate(@RequestBody List<Type> types) {
        boolean success = typeService.updateBatchById(types);
        if (success) {
            return ResponseResult.success();
        }
        return ResponseResult.error("批量更新类型失败");
    }
 
    @GetMapping("/{typeId}")
    public ResponseResult<Type> getById(@PathVariable Integer typeId) {
        Type type = typeService.getById(typeId);
        if (type != null && type.getDelFlag() != 1) {
            return ResponseResult.success(type);
        }
        return ResponseResult.error("类型不存在");
    }
 
    @GetMapping("/list")
    public ResponseResult<?> list(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String typeName) {
 
        // 不传分页参数则返回全部数据
        if (pageNum == null || pageSize == null) {
            List<Type> types = typeService.listAllAvailable();
            return ResponseResult.success(types);
        }
 
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Type::getDelFlag, 0)
                .like(typeName != null, Type::getTypeName, typeName);
 
        Page<Type> page = typeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return ResponseResult.success(page);
    }
}