mdf:用户,部门去jpa

This commit is contained in:
zhangzhiqiang
2022-11-30 10:19:18 +08:00
parent 7f395f4ae6
commit 99d78828a1
11 changed files with 194 additions and 122 deletions

View File

@@ -1,41 +1,41 @@
package org.nl.modules.common.base;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;
import javax.persistence.Column;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Timestamp;
/**
* @author Zheng Jie
* @date 2019年10月24日20:48:53
*/
@Getter
@Setter
@Data
public class BaseDTO implements Serializable {
private String createBy;
private String create_name;
private String updatedBy;
private Long create_id;
private Timestamp createTime;
private Timestamp updateTime;
private String update_optname;
private Long update_optid;
private Date create_time;
private Date update_time;
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
Field[] fields = this.getClass().getDeclaredFields();
try {
for (Field f : fields) {
f.setAccessible(true);
builder.append(f.getName(), f.get(this)).append("\n");
}
} catch (Exception e) {
builder.append("toString builder encounter an error");
}
return builder.toString();
}
}

View File

@@ -40,7 +40,7 @@ public class Dept extends BaseEntity implements Serializable {
@ApiModelProperty(value = "排序")
private Integer deptSort;
private Integer dept_sort;
@NotBlank
@ApiModelProperty(value = "部门名称")
@@ -48,12 +48,12 @@ public class Dept extends BaseEntity implements Serializable {
@NotNull
@ApiModelProperty(value = "是否启用")
private Boolean enabled;
private Boolean is_used;
@ApiModelProperty(value = "上级部门")
private Long pid;
@ApiModelProperty(value = "子节点数目", hidden = true)
private Integer subCount = 0;
private Integer sub_count = 0;
}

View File

@@ -21,6 +21,7 @@ import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.PageUtil;
import org.nl.modules.logging.annotation.Log;
@@ -28,6 +29,7 @@ import org.nl.modules.system.domain.Dept;
import org.nl.modules.system.service.DeptService;
import org.nl.modules.system.service.dto.DeptDto;
import org.nl.modules.system.service.dto.DeptQueryCriteria;
import org.nl.modules.system.service.dto.DeptTree;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
@@ -52,7 +54,7 @@ public class DeptController {
@GetMapping
@SaCheckPermission(value = {"user:list", "dept:list"}, mode = SaMode.AND)
public ResponseEntity<Object> query(DeptQueryCriteria criteria) throws Exception {
List<DeptDto> deptDtos = deptService.queryAll(criteria, true);
List<Dept> deptDtos = deptService.queryAll(criteria, true);
return new ResponseEntity<>(PageUtil.toPage(deptDtos, deptDtos.size()),HttpStatus.OK);
}
@@ -60,11 +62,11 @@ public class DeptController {
@PostMapping("/superior")
@SaCheckPermission(value = {"user:list", "dept:list"}, mode = SaMode.AND)
public ResponseEntity<Object> getSuperior(@RequestBody List<Long> ids) {
Set<DeptDto> deptDtos = new LinkedHashSet<>();
Set<DeptTree> deptDtos = new LinkedHashSet<>();
for (Long id : ids) {
DeptDto deptDto = deptService.findById(id);
List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
deptDtos.addAll(depts);
DeptTree deptTree = deptService.findById(id, DeptTree.class);
List<DeptTree> superior = deptService.getSuperior(deptTree, new ArrayList<>());
deptDtos.addAll(superior);
}
return new ResponseEntity<>(deptService.buildTree(new ArrayList<>(deptDtos)),HttpStatus.OK);
}
@@ -95,7 +97,7 @@ public class DeptController {
@DeleteMapping
@SaCheckPermission("dept:del")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
Set<DeptDto> deptDtos = new HashSet<>();
Set<Dept> deptDtos = new HashSet<>();
for (Long id : ids) {
List<Dept> deptList = deptService.findByPid(id);
deptDtos.add(deptService.findById(id));

View File

@@ -112,10 +112,11 @@ public class UserController {
public ResponseEntity<Object> create(@Validated @RequestBody User resources){
checkLevel(resources);
// 默认密码 123456
if (ObjectUtil.isEmpty(resources.getPassword()))
if (ObjectUtil.isEmpty(resources.getPassword())) {
resources.setPassword(SaSecureUtil.md5BySalt("123456", "salt"));
else
} else {
resources.setPassword(SaSecureUtil.md5BySalt(resources.getPassword(), "salt"));
}
userService.create(resources);
return new ResponseEntity<>(HttpStatus.CREATED);
}

View File

@@ -15,9 +15,11 @@
*/
package org.nl.modules.system.service;
import org.apache.poi.ss.formula.functions.T;
import org.nl.modules.system.domain.Dept;
import org.nl.modules.system.service.dto.DeptDto;
import org.nl.modules.system.service.dto.DeptQueryCriteria;
import org.nl.modules.system.service.dto.DeptTree;
import java.util.List;
import java.util.Set;
@@ -36,7 +38,7 @@ public interface DeptService {
* @return /
* @throws Exception /
*/
List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception;
List<Dept> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception;
/**
* 获取
@@ -52,7 +54,9 @@ public interface DeptService {
* @param id /
* @return /
*/
DeptDto findById(Long id);
Dept findById(Long id);
List<Dept> findByPid(Long pid);
/**
* 创建
@@ -73,17 +77,15 @@ public interface DeptService {
*
* @param deptDtos /
*/
void delete(Set<DeptDto> deptDtos);
void delete(Set<Dept> deptDtos);
/**
* 根据PID查询
*
* @param pid /
* @return /
*/
List<Dept> findByPid(long pid);
/**
<T> T findById(Long id, Class<T> tagert);
/**
* 根据角色ID查询
*
* @param id /
@@ -96,10 +98,9 @@ public interface DeptService {
* 获取待删除的部门
*
* @param deptList /
* @param deptDtos /
* @return /
*/
Set<DeptDto> getDeleteDepts(List<Dept> deptList, Set<DeptDto> deptDtos);
Set<Dept> getDeleteDepts(List<Dept> deptList, Set<Dept> depts);
/**
* 根据ID获取同级与上级数据
@@ -108,7 +109,7 @@ public interface DeptService {
* @param depts /
* @return /
*/
List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts);
List<DeptTree> getSuperior(DeptTree deptDto, List<DeptTree> depts);
/**
* 构建树形数据
@@ -116,7 +117,7 @@ public interface DeptService {
* @param deptDtos /
* @return /
*/
Object buildTree(List<DeptDto> deptDtos);
Object buildTree(List<DeptTree> deptDtos);
/**
@@ -124,7 +125,7 @@ public interface DeptService {
*
* @param deptDtos /
*/
void verification(Set<DeptDto> deptDtos);
void verification(Set<Dept> deptDtos);
/**
* 获取当前节点的所有子类节点集合数据

View File

@@ -20,6 +20,7 @@ import org.nl.modules.common.annotation.DataPermission;
import org.nl.modules.common.annotation.Query;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
/**
@@ -37,7 +38,7 @@ public class DeptQueryCriteria{
private String code;
@Query
private Boolean enabled;
private Boolean is_used;
@Query
private Long pid;
@@ -46,5 +47,7 @@ public class DeptQueryCriteria{
private Boolean pidIsNull;
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> createTime;
private Date startTime;
private Date endTime;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nl.modules.system.service.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import org.nl.modules.common.base.BaseDTO;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
/**
* @author Zheng Jie
* @date 2019-03-25
*/
@Getter
@Setter
public class DeptTree extends BaseDTO implements Serializable {
private Long Dept_id;
private String name;
private Boolean is_used;
private Integer dept_sort;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<DeptTree> children;
private Long pid;
private Integer sub_count;
private String versionId;
public Boolean getHasChildren() {
return sub_count > 0;
}
public Boolean getLeaf() {
return sub_count <= 0;
}
public String getLabel() {
return name;
}
}

View File

@@ -16,12 +16,16 @@
package org.nl.modules.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.CacheKey;
import org.nl.modules.common.utils.QueryHelp;
@@ -35,7 +39,11 @@ import org.nl.modules.system.repository.UserRepository;
import org.nl.modules.system.service.DeptService;
import org.nl.modules.system.service.dto.DeptDto;
import org.nl.modules.system.service.dto.DeptQueryCriteria;
import org.nl.modules.system.service.dto.DeptTree;
import org.nl.modules.system.service.mapstruct.DeptMapper;
import org.nl.modules.tools.MapOf;
import org.nl.modules.wql.WQL;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.SpringContextHolder;
import org.springframework.cache.annotation.CacheConfig;
@@ -43,11 +51,13 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -66,52 +76,42 @@ public class DeptServiceImpl implements DeptService {
private final RoleRepository roleRepository;
@Override
public List<DeptDto> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
Sort sort = Sort.by(Sort.Direction.ASC, "deptSort");
// String dataScopeType = SecurityUtils.getDataScopeType();
if (isQuery) {
// if (dataScopeType.equals(DataScopeEnum.ALL.getValue())) {
criteria.setPidIsNull(true);
// }
List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>());
List<String> fieldNames = new ArrayList<String>() {{
add("pidIsNull");
add("enabled");
}};
for (Field field : fields) {
//设置对象的访问权限保证对private的属性的访问
field.setAccessible(true);
Object val = field.get(criteria);
if (fieldNames.contains(field.getName())) {
continue;
}
if (ObjectUtil.isNotNull(val)) {
criteria.setPidIsNull(null);
break;
}
}
public List<Dept> queryAll(DeptQueryCriteria criteria, Boolean isQuery) throws Exception {
JSONObject o = (JSONObject) JSON.toJSON(criteria);
HashMap map = MapOf.of("name", MapUtil.getStr(o, "name")
, "code", MapUtil.getStr(o, "code")
, "is_used", MapUtil.getStr(o, "is_used")
, "startTime", MapUtil.getStr(o, "startTime")
, "endTime", MapUtil.getStr(o, "endTime")
, "pid", MapUtil.getStr(o, "pid")
, "pidIsNull", MapUtil.getStr(o, "pidIsNull")
);
JSONArray array = WQL.getWO("SYS_DEPT").addParamMap(map).process().getResultJSONArray(0);
return array.toJavaList(Dept.class);
}
@Override
public <T> T findById(Long id,Class<T> tagert) {
if (id==null){
return null;
}
List<DeptDto> list = deptMapper.toDto(deptRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), sort));
// 如果为空,就代表为自定义权限或者本级权限,就需要去重,不理解可以注释掉,看查询结果
// if (StrUtil.isEmpty(dataScopeType)) {
// return deduplication(list);
// }
return list;
JSONObject result = WQLObject.getWQLObject("sys_dept").query("dept_id ='" + id + "'").uniqueResult(0);
return result.toJavaObject(tagert);
}
@Override
@Cacheable(key = "'id:' + #p0")
public DeptDto findById(Long id) {
Dept dept = deptRepository.findById(id).orElseGet(Dept::new);
ValidationUtil.isNull(dept.getDept_id(), "Dept", "id", id);
return deptMapper.toDto(dept);
public Dept findById(Long id) {
JSONObject result = WQLObject.getWQLObject("sys_dept").query("dept_id ='" + id + "'").uniqueResult(0);
return result.toJavaObject(Dept.class);
}
@Override
public List<Dept> findByPid(long pid) {
return deptRepository.findByPid(pid);
public List<Dept> findByPid(Long pid) {
JSONArray result = WQLObject.getWQLObject("sys_dept").query("pid ='" + pid + "'").getResultJSONArray(0);
return result.toJavaList(Dept.class);
}
@Override
public Set<Dept> findByRoleId(Long id) {
return deptRepository.findByRoleId(id);
@@ -120,9 +120,9 @@ public class DeptServiceImpl implements DeptService {
@Override
@Transactional(rollbackFor = Exception.class)
public void create(Dept resources) {
deptRepository.save(resources);
WQLObject.getWQLObject("sys_user").insert((JSONObject)JSON.toJSON(resources));
// 计算子节点数目
resources.setSubCount(0);
resources.setSub_count(0);
// 清理缓存
updateSubCnt(resources.getPid());
}
@@ -136,10 +136,9 @@ public class DeptServiceImpl implements DeptService {
if (resources.getPid() != null && resources.getDept_id().equals(resources.getPid())) {
throw new BadRequestException("上级不能为自己");
}
Dept dept = deptRepository.findById(resources.getDept_id()).orElseGet(Dept::new);
ValidationUtil.isNull(dept.getDept_id(), "Dept", "id", resources.getDept_id());
Dept dept = findById(resources.getDept_id());
resources.setDept_id(dept.getDept_id());
deptRepository.save(resources);
WQLObject.getWQLObject("sys_user").update((JSONObject)JSON.toJSON(resources),"dept_id = '"+resources.getDept_id()+"'");
// 更新父节点中子节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
@@ -149,19 +148,22 @@ public class DeptServiceImpl implements DeptService {
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<DeptDto> deptDtos) {
for (DeptDto deptDto : deptDtos) {
// 清理缓存
delCaches(deptDto.getId());
deptRepository.deleteById(deptDto.getId());
updateSubCnt(deptDto.getPid());
public void delete(Set<Dept> deptDtos) {
if (!CollectionUtils.isEmpty(deptDtos)){
String collectSql = deptDtos.stream().map(a -> String.valueOf(a.getDept_id())).collect(Collectors.joining("','"));
WQLObject.getWQLObject("sys_user").delete("dept_id in ('" + collectSql + "')");
for (Dept deptDto : deptDtos) {
// 清理缓存
delCaches(deptDto.getDept_id());
updateSubCnt(deptDto.getPid());
}
}
}
@Override
public Set<DeptDto> getDeleteDepts(List<Dept> menuList, Set<DeptDto> deptDtos) {
public Set<Dept> getDeleteDepts(List<Dept> menuList, Set<Dept> deptDtos) {
for (Dept dept : menuList) {
deptDtos.add(deptMapper.toDto(dept));
deptDtos.add(dept);
List<Dept> depts = deptRepository.findByPid(dept.getDept_id());
if (depts != null && depts.size() != 0) {
getDeleteDepts(depts, deptDtos);
@@ -171,28 +173,27 @@ public class DeptServiceImpl implements DeptService {
}
@Override
public List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts) {
public List<DeptTree> getSuperior(DeptTree deptDto, List<DeptTree> depts) {
depts.add(deptDto);
if (deptDto.getPid() == null) {
depts.addAll(deptRepository.findByPidIsNull());
return deptMapper.toDto(depts);
return depts;
}
depts.addAll(deptRepository.findByPid(deptDto.getPid()));
return getSuperior(findById(deptDto.getPid()), depts);
return getSuperior(findById(deptDto.getPid(),DeptTree.class), depts);
}
@Override
public Object buildTree(List<DeptDto> deptDtos) {
Set<DeptDto> trees = new LinkedHashSet<>();
Set<DeptDto> depts = new LinkedHashSet<>();
List<String> deptNames = deptDtos.stream().map(DeptDto::getName).collect(Collectors.toList());
public Object buildTree(List<DeptTree> deptDtos) {
Set<DeptTree> trees = new LinkedHashSet<>();
Set<DeptTree> depts = new LinkedHashSet<>();
List<String> deptNames = deptDtos.stream().map(DeptTree::getName).collect(Collectors.toList());
boolean isChild;
for (DeptDto deptDTO : deptDtos) {
for (DeptTree deptDTO : deptDtos) {
isChild = false;
if (deptDTO.getPid() == null) {
trees.add(deptDTO);
}
for (DeptDto it : deptDtos) {
if (it.getPid() != null && deptDTO.getId().equals(it.getPid())) {
for (DeptTree it : deptDtos) {
if (it.getPid() != null && deptDTO.getDept_id().equals(it.getPid())) {
isChild = true;
if (deptDTO.getChildren() == null) {
deptDTO.setChildren(new ArrayList<>());
@@ -217,21 +218,21 @@ public class DeptServiceImpl implements DeptService {
}
@Override
public void verification(Set<DeptDto> deptDtos) {
Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
if (userRepository.countByDepts(deptIds) > 0) {
throw new BadRequestException("所选部门存在用户关联,请解除后再试!");
}
if (roleRepository.countByDepts(deptIds) > 0) {
throw new BadRequestException("所选部门存在角色关联,请解除后再试!");
public void verification(Set<Dept> deptDtos) {
if (!CollectionUtils.isEmpty(deptDtos)){
String collectSql = deptDtos.stream().map(a -> String.valueOf(a.getDept_id())).collect(Collectors.joining("','"));
ResultBean result = WQLObject.getWQLObject("sys_user_dept").query("dept_id in ('" + collectSql + "')");
if (result.getResultCount()>0){
throw new BadRequestException("部门存在绑定的人员,请先解绑人员对应部门");
}
}
}
private void updateSubCnt(Long deptId) {
if (deptId != null) {
int count = deptRepository.countByPid(deptId);
deptRepository.updateSubCntById(count, deptId);
List<Dept> byPid = findByPid(deptId);
WQLObject.getWQLObject("sys_user").update(MapOf.of("sub_count",byPid.size()),"dept_id = '"+deptId+"'");
}
}
@@ -268,7 +269,7 @@ public class DeptServiceImpl implements DeptService {
public List<Long> getDeptChildren(List<Dept> deptList) {
List<Long> list = new ArrayList<>();
deptList.forEach(dept -> {
if (dept != null && dept.getEnabled()) {
if (dept != null && dept.getIs_used()) {
List<Dept> depts = deptRepository.findByPid(dept.getDept_id());
if (deptList.size() != 0) {
list.addAll(getDeptChildren(depts));

View File

@@ -100,7 +100,7 @@ public class DictServiceImpl implements DictService {
map.put("字典描述", dictDTO.getDescription());
map.put("字典标签", dictDetail.getLabel());
map.put("字典值", dictDetail.getValue());
map.put("创建日期", dictDetail.getCreateTime());
map.put("创建日期", dictDetail.getCreate_time());
list.add(map);
}
} else {
@@ -109,7 +109,7 @@ public class DictServiceImpl implements DictService {
map.put("字典描述", dictDTO.getDescription());
map.put("字典标签", null);
map.put("字典值", null);
map.put("创建日期", dictDTO.getCreateTime());
map.put("创建日期", dictDTO.getCreate_time());
list.add(map);
}
}

View File

@@ -199,7 +199,7 @@ public class RoleServiceImpl implements RoleService {
map.put("角色名称", role.getName());
map.put("角色级别", role.getLevel());
map.put("描述", role.getDescription());
map.put("创建日期", role.getCreateTime());
map.put("创建日期", role.getCreate_time());
list.add(map);
}
FileUtil.downloadExcel(list, response);

View File

@@ -131,8 +131,8 @@ public class LocalStorageServiceImpl implements LocalStorageService {
map.put("备注名", localStorageDTO.getName());
map.put("文件类型", localStorageDTO.getType());
map.put("文件大小", localStorageDTO.getSize());
map.put("创建者", localStorageDTO.getCreateBy());
map.put("创建日期", localStorageDTO.getCreateTime());
map.put("创建者", localStorageDTO.getCreate_name());
map.put("创建日期", localStorageDTO.getCreate_time());
list.add(map);
}
FileUtil.downloadExcel(list, response);