Pārlūkot izejas kodu

修改部门接口,增加返回用户信息

Feick 4 gadi atpakaļ
vecāks
revīzija
94927bf63b

+ 3 - 0
src/main/kotlin/com/yaoxiang/planning/action/AnnualAction.kt

@@ -45,12 +45,14 @@ class AnnualAction {
         return Reply(result)
     }
 
+    @ApiOperation("删除年度计划")
     @PostMapping("delete")
     fun delete(id: Long): Reply<Any> {
         val result = annualPlanService.delete(id)
         return Reply(result)
     }
 
+    @ApiOperation("获取年度计划")
     @GetMapping("current")
     fun current(@RequestParam(required = false) year: Int? = null): Reply<AnnualPlan> {
         val y: Int = year ?: DateUtils.year(Date())
@@ -58,6 +60,7 @@ class AnnualAction {
         return Reply(optional.isPresent, "", optional.orElse(null))
     }
 
+    @ApiOperation("获取年度计划列表")
     @GetMapping("list")
     fun list(): Reply<List<AnnualPlan>> {
         val list = annualPlanService.list()

+ 13 - 9
src/main/kotlin/com/yaoxiang/planning/action/DepartmentAction.kt

@@ -26,26 +26,28 @@ class DepartmentAction {
         ApiImplicitParam(name = "remark", value = "备注", paramType = "query"),
         ApiImplicitParam(name = "parentId", value = "上级部门Id", paramType = "query")
     )
-    fun add(name: String, @RequestParam(required = false) duty: String?,
-            @RequestParam(required = false) remark: String?, parentId: Long): Reply<Any> {
+    fun add(
+        name: String, @RequestParam(required = false) duty: String?,
+        @RequestParam(required = false) remark: String?, parentId: Long
+    ): Reply<Any> {
         val result = departmentService.add(name, duty, remark, parentId)
         return if (result) Reply.ok() else Reply.fail("部门名称重复或未找到上级部门")
     }
 
     @ApiOperation("获取部门树")
     @GetMapping("getTree")
-    fun getTree(): Reply<Department> {
+    fun getTree(@RequestParam(defaultValue = "false") withUser: Boolean): Reply<Department> {
         val root = departmentService.findRoot()
         if (!root.isPresent) {
             return Reply.fail("未找到部门列表")
         }
-        val tree = departmentService.getTree(root.get().id!!)
+        val tree = departmentService.getTree(root.get().id!!, withUser)
         return Reply.ok(tree.orElse(null))
     }
 
     @ApiOperation("获取部门列表")
     @GetMapping("list")
-    fun list():Reply<List<Department>>{
+    fun list(): Reply<List<Department>> {
         val result = departmentService.list()
         return Reply.ok(result)
     }
@@ -59,16 +61,18 @@ class DepartmentAction {
         ApiImplicitParam(name = "remark", value = "备注", paramType = "query"),
         ApiImplicitParam(name = "parentId", value = "上级部门Id", paramType = "query")
     )
-    fun update(id: Long, name: String, @RequestParam(required = false) duty: String?,
-               @RequestParam(required = false) remark: String?,
-               @RequestParam(required = false) parentId: Long?): Reply<Any> {
+    fun update(
+        id: Long, name: String, @RequestParam(required = false) duty: String?,
+        @RequestParam(required = false) remark: String?,
+        @RequestParam(required = false) parentId: Long?
+    ): Reply<Any> {
         val result = departmentService.update(id, name, duty, remark, parentId)
         return if (result) Reply.ok() else Reply.fail("更新失败")
     }
 
     @ApiOperation("删除部门及其子部门")
     @PostMapping("delete")
-    @ApiImplicitParam(name = "id",value = "部门id",paramType = "query")
+    @ApiImplicitParam(name = "id", value = "部门id", paramType = "query")
     fun delete(id: Long): Reply<Any> {
         val result = departmentService.delete(id)
         return if (result) Reply.ok() else Reply.fail("删除失败")

+ 5 - 0
src/main/kotlin/com/yaoxiang/planning/action/PlanningItemAction.kt

@@ -28,30 +28,35 @@ class PlanningItemAction {
         return Reply(result)
     }
 
+    @ApiOperation("批量增加计划项")
     @PostMapping("addItems")
     fun addItems(@RequestBody items: List<PlanningItem>): Reply<Any> {
         val result = planningItemService.addItems(items)
         return Reply(result)
     }
 
+    @ApiOperation("查找给定年度计划或者季度计划的所有分计划")
     @PostMapping("findByPlanningIdAndType")
     fun findByPlanningIdAndType(planningId: Long, type: Int): Reply<List<PlanningItem>> {
         val result = planningItemService.findByPlanningIdAndType(planningId, type)
         return Reply.ok(result)
     }
 
+    @ApiOperation("删除分计划")
     @PostMapping("deleteItem")
     fun deleteItem(id: Long): Reply<Any> {
         val result = planningItemService.deleteItem(id)
         return Reply(result)
     }
 
+    @ApiOperation("批量删除分计划")
     @PostMapping("deleteItems")
     fun deleteItems(@RequestBody ids: List<Long>): Reply<Any> {
         val result = planningItemService.deleteItems(ids)
         return Reply(result)
     }
 
+    @ApiOperation("删除给定年度计划或者季度计划的所有分计划")
     @PostMapping("deleteByPlanningIdAndType")
     @ApiImplicitParams(ApiImplicitParam(name = "planningId", value = "计划Id", paramType = "query"),
             ApiImplicitParam(name = "type", value = "计划类型", paramType = "query"))

+ 14 - 8
src/main/kotlin/com/yaoxiang/planning/action/UserAction.kt

@@ -28,8 +28,10 @@ class UserAction {
 
     @ApiOperation("新增用户")
     @PostMapping("add")
-    fun add(username: String, name: String, password: String,
-            @RequestParam(required = false) departmentId: Long?): Reply<Any> {
+    fun add(
+        username: String, name: String, password: String,
+        @RequestParam(required = false) departmentId: Long?
+    ): Reply<Any> {
         val result = userService.add(username, name, password, departmentId)
         return if (result) Reply.ok() else Reply.fail("新增失败")
     }
@@ -42,9 +44,11 @@ class UserAction {
         ApiImplicitParam(name = "email", value = "上级部门Id", paramType = "query")
     )
     @PostMapping("updateInfo")
-    fun updateInfo(id: Long, name: String, @RequestParam(required = false) age: Int?,
+    fun updateInfo(
+        id: Long, name: String, @RequestParam(required = false) age: Int?,
         @RequestParam(required = false) gender: Int?,
-        @RequestParam(required = false) email: String?): Reply<Any> {
+        @RequestParam(required = false) email: String?
+    ): Reply<Any> {
         val result = userService.updateInfo(id, name, age, gender, email)
         return if (result) Reply.ok() else Reply.fail("更新失败")
     }
@@ -67,10 +71,6 @@ class UserAction {
     @GetMapping("cur")
     fun currentUser(): Reply<UserInfo> {
         val result = userService.currentUser()
-        if (result != null) {
-            result.salt = null
-            result.password = null
-        }
         return Reply.ok(result)
     }
 
@@ -88,4 +88,10 @@ class UserAction {
         return if (result) Reply.ok() else Reply.fail("删除失败")
     }
 
+    @ApiOperation("获取用户列表")
+    @GetMapping("list")
+    fun list(): Reply<List<UserInfo>> {
+        val list = userService.list()
+        return Reply.ok(list)
+    }
 }

+ 1 - 1
src/main/kotlin/com/yaoxiang/planning/action/WeeklyAction.kt

@@ -20,7 +20,7 @@ class WeeklyAction {
     @Autowired
     private lateinit var weeklyPlanService: WeeklyPlanService
 
-    @ApiOperation("添加季度计划")
+    @ApiOperation("添加计划")
     @ApiImplicitParams(
         ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
         ApiImplicitParam(name = "content", value = "计划内容", paramType = "query"),

+ 4 - 0
src/main/kotlin/com/yaoxiang/planning/domain/Department.kt

@@ -33,6 +33,10 @@ class Department() : BaseLongEntity(), ToTree {
     @ApiModelProperty("子树")
     var children: List<Department>? = null
 
+    @Transient
+    @ApiModelProperty("用户列表")
+    var userList: List<UserInfo>? = null
+
     override fun id(): Long {
         return id!!
     }

+ 12 - 2
src/main/kotlin/com/yaoxiang/planning/service/DepartmentService.kt

@@ -131,13 +131,23 @@ class DepartmentService {
     }
 
     @Transactional(readOnly = true)
-    fun getTree(id: Long): Optional<Department> {
+    fun getTree(id: Long, withUser: Boolean): Optional<Department> {
         val list = listAllChildren(id)
         if (list.isEmpty()) {
             return Optional.empty()
         }
+        val root = list.firstOrNull { id == it.id!! }
+        if (withUser) {
+            val users = userService.list()
+            val map = users.groupBy { it.departmentId }
+            list.forEach { it.userList = map.getOrDefault(it.id, arrayListOf()) }
+            //只给root部门加上userList
+            if (root != null && root.parentId == null && root.userList == null) {
+                root.userList = map[null]
+            }
+        }
 //        val map = list.associateBy { it.id!! }
-        return Optional.ofNullable(list.firstOrNull { id == it.id!! })
+        return Optional.ofNullable(root)
     }
 
 }

+ 12 - 0
src/main/kotlin/com/yaoxiang/planning/service/UserService.kt

@@ -45,11 +45,17 @@ class UserService {
         val authentication = SecurityContextHolder.getContext().authentication
         if (authentication != null) {
             val user = authentication.principal as AuthUser
+            filter(user.userInfo)
             return user.userInfo
         }
         return null
     }
 
+    private fun filter(userInfo: UserInfo) {
+        userInfo.salt = null
+        userInfo.password = null
+    }
+
     @Transactional(readOnly = true)
     fun existsByUsername(username: String): Boolean {
         return userInfoRepo.existsByUsername(username)
@@ -200,4 +206,10 @@ class UserService {
         userInfoRepo.save(userInfo)
         return true
     }
+
+    fun list(): List<UserInfo> {
+        val list = userInfoRepo.findAll()
+        list.forEach { filter(it) }
+        return list
+    }
 }

+ 19 - 1
src/test/kotlin/com/yaoxiang/planning/utils/TreeUtilTest.kt

@@ -1,6 +1,7 @@
 package com.yaoxiang.planning.utils
 
 import com.yaoxiang.planning.domain.Department
+import com.yaoxiang.planning.domain.UserInfo
 import org.junit.jupiter.api.AfterEach
 import org.junit.jupiter.api.BeforeEach
 
@@ -44,8 +45,25 @@ internal class TreeUtilTest {
         child4.parentId = 4
 
         val list = mutableListOf<ToTree>(root, child1, child2, child3, child4)
-        TreeUtil.listAllChildren(root,list)
+        TreeUtil.listAllChildren(root, list)
 
         println(root)
     }
+
+    @Test
+    fun test1() {
+        val user1 = UserInfo()
+        user1.name = "a"
+        user1.departmentId = 1
+
+        val user2 = UserInfo()
+        user2.name = "b"
+        user2.departmentId = 1
+
+        val user3 = UserInfo()
+        user3.name = "c"
+        val list = listOf(user1, user2, user3)
+        val group = list.groupBy { it.departmentId }
+        println(group)
+    }
 }