DepartmentController.java 3.2 KB
Newer Older
1 2
package com.kwan.springbootkwan.controller;

3
import com.kwan.springbootkwan.annotation.RedisLock;
4 5 6 7 8
import com.kwan.springbootkwan.entity.Department;
import com.kwan.springbootkwan.service.DepartmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
9 10 11 12 13 14 15
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门(Department)表控制层
 *
 * @author makejava
 * @since 2023-08-28 22:17:33
 */
@RestController
@RequestMapping("department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 分页查询
     *
     * @param department  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Department>> queryByPage(Department department, PageRequest pageRequest) {
        return ResponseEntity.ok(this.departmentService.queryByPage(department, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Department> queryById(@PathVariable("id") Integer id) {
        //已知id=16,查询树结构
        return ResponseEntity.ok(this.departmentService.queryById(id));
    }

    /**
     * 已知id查询树结构
     *
     * @param id
     * @return
     */
    @GetMapping("/tree/{id}")
    public ResponseEntity<Department> queryTreeById(@PathVariable("id") Integer id) {
        //已知id=16,查询树结构
        return ResponseEntity.ok(this.departmentService.queryTreeById(id));
    }


    @GetMapping("/all")
73
    @RedisLock(key = "myLock_queryTreeAll", expire = 30000, timeout = 3000)
74
//    @RedisLock(key = "myLock_queryTreeAll", timeout = 10000)
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 107 108 109 110 111
    public ResponseEntity<List<Department>> queryTreeAll() {
        return ResponseEntity.ok(this.departmentService.queryTreeAll());
    }

    /**
     * 新增数据
     *
     * @param department 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Department> add(Department department) {
        return ResponseEntity.ok(this.departmentService.insert(department));
    }

    /**
     * 编辑数据
     *
     * @param department 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Department> edit(Department department) {
        return ResponseEntity.ok(this.departmentService.update(department));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.departmentService.deleteById(id));
    }
}