部门列表api返回部门的学员数量

This commit is contained in:
none 2023-04-12 13:55:22 +08:00
parent 3fda4b6926
commit 9344f81772
7 changed files with 63 additions and 36 deletions

View File

@ -47,6 +47,7 @@ public class DepartmentController {
public JsonResponse index() { public JsonResponse index() {
HashMap<String, Object> data = new HashMap<>(); HashMap<String, Object> data = new HashMap<>();
data.put("departments", departmentService.groupByParent()); data.put("departments", departmentService.groupByParent());
data.put("dep_user_count", departmentService.getDepartmentsUserCount());
return JsonResponse.data(data); return JsonResponse.data(data);
} }
@ -133,7 +134,7 @@ public class DepartmentController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException { public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id); Department department = departmentService.findOrFail(id);
departmentService.deleteById(department.getId()); departmentService.destroy(department.getId());
ctx.publishEvent(new DepartmentDestroyEvent(this, BCtx.getId(), department.getId())); ctx.publishEvent(new DepartmentDestroyEvent(this, BCtx.getId(), department.getId()));
return JsonResponse.success(); return JsonResponse.success();
} }

View File

@ -38,6 +38,7 @@ import java.util.stream.Collectors;
/** /**
* @Author 杭州白书科技有限公司 * @Author 杭州白书科技有限公司
*
* @create 2023/2/23 09:48 * @create 2023/2/23 09:48
*/ */
@RestController @RestController
@ -45,17 +46,13 @@ import java.util.stream.Collectors;
@RequestMapping("/backend/v1/user") @RequestMapping("/backend/v1/user")
public class UserController { public class UserController {
@Autowired @Autowired private UserService userService;
private UserService userService;
@Autowired @Autowired private UserDepartmentService userDepartmentService;
private UserDepartmentService userDepartmentService;
@Autowired @Autowired private DepartmentService departmentService;
private DepartmentService departmentService;
@Autowired @Autowired private ApplicationContext context;
private ApplicationContext context;
@BackendPermissionMiddleware(slug = BPermissionConstant.USER_INDEX) @BackendPermissionMiddleware(slug = BPermissionConstant.USER_INDEX)
@GetMapping("/index") @GetMapping("/index")
@ -200,7 +197,7 @@ public class UserController {
String defaultAvatar = BCtx.getConfig().get(CConfig.MEMBER_DEFAULT_AVATAR); String defaultAvatar = BCtx.getConfig().get(CConfig.MEMBER_DEFAULT_AVATAR);
List<String[]> errorLines = new ArrayList<>(); List<String[]> errorLines = new ArrayList<>();
errorLines.add(new String[]{"错误行", "错误信息"}); // 错误表-表头 errorLines.add(new String[] {"错误行", "错误信息"}); // 错误表-表头
// 读取存在的部门 // 读取存在的部门
List<Department> departments = departmentService.all(); List<Department> departments = departmentService.all();
@ -240,14 +237,14 @@ public class UserController {
i++; // 索引值 i++; // 索引值
if (userItem.getEmail() == null || userItem.getEmail().trim().length() == 0) { if (userItem.getEmail() == null || userItem.getEmail().trim().length() == 0) {
errorLines.add(new String[]{"" + (i + startLine) + "", "未输入邮箱账号"}); errorLines.add(new String[] {"" + (i + startLine) + "", "未输入邮箱账号"});
} else { } else {
// 邮箱重复判断 // 邮箱重复判断
Integer repeatLine = emailRepeat.get(userItem.getEmail()); Integer repeatLine = emailRepeat.get(userItem.getEmail());
if (repeatLine != null) { if (repeatLine != null) {
errorLines.add( errorLines.add(
new String[]{ new String[] {
"" + (i + startLine) + "", "与第" + repeatLine + "行邮箱重复" "" + (i + startLine) + "", "与第" + repeatLine + "行邮箱重复"
}); });
} else { } else {
emailRepeat.put(userItem.getEmail(), i + startLine); emailRepeat.put(userItem.getEmail(), i + startLine);
@ -257,7 +254,7 @@ public class UserController {
// 部门数据检测 // 部门数据检测
if (userItem.getDeps() == null || userItem.getDeps().trim().length() == 0) { if (userItem.getDeps() == null || userItem.getDeps().trim().length() == 0) {
errorLines.add(new String[]{"" + (i + startLine) + "", "未选择部门"}); errorLines.add(new String[] {"" + (i + startLine) + "", "未选择部门"});
} else { } else {
String[] tmpDepList = userItem.getDeps().trim().split("\\|"); String[] tmpDepList = userItem.getDeps().trim().split("\\|");
Integer[] tmpDepIds = new Integer[tmpDepList.length]; Integer[] tmpDepIds = new Integer[tmpDepList.length];
@ -267,8 +264,8 @@ public class UserController {
// 判断部门id是否存在 // 判断部门id是否存在
if (tmpDepId == null || tmpDepId == 0) { if (tmpDepId == null || tmpDepId == 0) {
errorLines.add( errorLines.add(
new String[]{ new String[] {
"" + (i + startLine) + "", "部门『" + tmpDepList[j] + "』不存在" "" + (i + startLine) + "", "部门『" + tmpDepList[j] + "』不存在"
}); });
continue; continue;
} }
@ -280,13 +277,13 @@ public class UserController {
// 姓名为空检测 // 姓名为空检测
String tmpName = userItem.getName(); String tmpName = userItem.getName();
if (tmpName == null || tmpName.trim().length() == 0) { if (tmpName == null || tmpName.trim().length() == 0) {
errorLines.add(new String[]{"" + (i + startLine) + "", "昵称为空"}); errorLines.add(new String[] {"" + (i + startLine) + "", "昵称为空"});
} }
// 密码为空检测 // 密码为空检测
String tmpPassword = userItem.getPassword(); String tmpPassword = userItem.getPassword();
if (tmpPassword == null || tmpPassword.trim().length() == 0) { if (tmpPassword == null || tmpPassword.trim().length() == 0) {
errorLines.add(new String[]{"" + (i + startLine) + "", "密码为空"}); errorLines.add(new String[] {"" + (i + startLine) + "", "密码为空"});
} }
// 待插入数据 // 待插入数据
@ -314,7 +311,7 @@ public class UserController {
List<String> existsEmails = userService.existsEmailsByEmails(emails); List<String> existsEmails = userService.existsEmailsByEmails(emails);
if (existsEmails.size() > 0) { if (existsEmails.size() > 0) {
for (String tmpEmail : existsEmails) { for (String tmpEmail : existsEmails) {
errorLines.add(new String[]{"" + emailRepeat.get(tmpEmail) + "", "邮箱已注册"}); errorLines.add(new String[] {"" + emailRepeat.get(tmpEmail) + "", "邮箱已注册"});
} }
} }
if (errorLines.size() > 1) { if (errorLines.size() > 1) {

View File

@ -9,6 +9,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.Department; import xyz.playedu.api.domain.Department;
import xyz.playedu.api.types.mapper.DepartmentsUserCountMapRes;
import java.util.List;
/** /**
* @author tengteng * @author tengteng
@ -16,4 +19,6 @@ import xyz.playedu.api.domain.Department;
* @createDate 2023-02-19 12:19:45 @Entity xyz.playedu.api.domain.Department * @createDate 2023-02-19 12:19:45 @Entity xyz.playedu.api.domain.Department
*/ */
@Mapper @Mapper
public interface DepartmentMapper extends BaseMapper<Department> {} public interface DepartmentMapper extends BaseMapper<Department> {
List<DepartmentsUserCountMapRes> getDepartmentsUserCount();
}

View File

@ -25,13 +25,11 @@ public interface DepartmentService extends IService<Department> {
Department findOrFail(Integer id) throws NotFoundException; Department findOrFail(Integer id) throws NotFoundException;
void deleteById(Integer id) throws NotFoundException; void destroy(Integer id) throws NotFoundException;
void update(Department department, String name, Integer parentId, Integer sort) void update(Department department, String name, Integer parentId, Integer sort)
throws NotFoundException; throws NotFoundException;
List<Integer> allIds();
String compParentChain(Integer parentId) throws NotFoundException; String compParentChain(Integer parentId) throws NotFoundException;
String childrenParentChain(Department department); String childrenParentChain(Department department);
@ -53,4 +51,6 @@ public interface DepartmentService extends IService<Department> {
Map<Integer, String> id2name(); Map<Integer, String> id2name();
Long total(); Long total();
Map<Integer, Integer> getDepartmentsUserCount();
} }

View File

@ -21,6 +21,7 @@ import xyz.playedu.api.mapper.DepartmentMapper;
import xyz.playedu.api.service.CourseDepartmentService; import xyz.playedu.api.service.CourseDepartmentService;
import xyz.playedu.api.service.DepartmentService; import xyz.playedu.api.service.DepartmentService;
import xyz.playedu.api.service.internal.UserDepartmentService; import xyz.playedu.api.service.internal.UserDepartmentService;
import xyz.playedu.api.types.mapper.DepartmentsUserCountMapRes;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -60,7 +61,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
@Override @Override
@Transactional @Transactional
public void deleteById(Integer id) throws NotFoundException { public void destroy(Integer id) throws NotFoundException {
Department department = findOrFail(id); Department department = findOrFail(id);
updateParentChain(department.getParentChain(), childrenParentChain(department)); updateParentChain(department.getParentChain(), childrenParentChain(department));
removeById(department.getId()); removeById(department.getId());
@ -136,16 +137,6 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
updateBatchById(updateRows); updateBatchById(updateRows);
} }
@Override
public List<Integer> allIds() {
List<Department> departments = list(query().getWrapper().eq("1", "1").select("id"));
List<Integer> ids = new ArrayList<>();
for (Department department : departments) {
ids.add(department.getId());
}
return ids;
}
@Override @Override
public String compParentChain(Integer parentId) throws NotFoundException { public String compParentChain(Integer parentId) throws NotFoundException {
String parentChain = ""; String parentChain = "";
@ -257,4 +248,13 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
public Long total() { public Long total() {
return count(); return count();
} }
@Override
public Map<Integer, Integer> getDepartmentsUserCount() {
return getBaseMapper().getDepartmentsUserCount().stream()
.collect(
Collectors.toMap(
DepartmentsUserCountMapRes::getDepId,
DepartmentsUserCountMapRes::getTotal));
}
} }

View File

@ -0,0 +1,19 @@
/**
* This file is part of the PlayEdu.
* (c) 杭州白书科技有限公司
*/
package xyz.playedu.api.types.mapper;
import lombok.Data;
/**
* @Author 杭州白书科技有限公司
*
* @create 2023/4/12 13:46
*/
@Data
public class DepartmentsUserCountMapRes {
private Integer depId;
private Integer total;
}

View File

@ -15,8 +15,13 @@
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id,name,parent_id, id,name,parent_id,
parent_chain,sort,created_at, parent_chain,sort,created_at,
updated_at updated_at
</sql> </sql>
<select id="getDepartmentsUserCount" resultType="xyz.playedu.api.types.mapper.DepartmentsUserCountMapRes">
SELECT `dep_id`, count(1) AS `total`
FROM user_department
GROUP BY dep_id;
</select>
</mapper> </mapper>