Browse Source

工具优化

master
DouDou 6 months ago
parent
commit
19a9e14baa
  1. 22
      src/main/java/com/spring/common/utils/LdapReadUtils.java
  2. 22
      src/main/java/com/spring/config/LdapAccountInitLoad.java
  3. 7
      src/main/java/com/spring/modules/base/dao/PropertiesMapper.java
  4. 45
      src/main/java/com/spring/modules/base/service/Impl/PropertiesServiceImpl.java

22
src/main/java/com/spring/common/utils/LdapReadUtils.java

@ -17,6 +17,7 @@ import javax.naming.ldap.PagedResultsResponseControl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
@ -37,7 +38,7 @@ public class LdapReadUtils {
* @date 2025年9月5日 下午12:07:09
* @return: Map<String,String>
*/
public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase) throws NamingException, IOException {
public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase, RedisTemplate<String, Object> redisTemplate) throws NamingException, IOException {
byte[] cookie = null;
// 设置返回所有属性
SearchControls controls = new SearchControls();
@ -48,6 +49,8 @@ public class LdapReadUtils {
String nameFilter = "(&(objectClass=user)(objectCategory=person)(!(objectClass=computer)))";
Map<String, String> ldapAccountMap = new HashMap<>();
int pageNums = 1;
int countNums = 0;
//循环执行
do{
ctx.setRequestControls(new Control[]
@ -55,21 +58,25 @@ public class LdapReadUtils {
NamingEnumeration<SearchResult> results = null;
try{
results = ctx.search(ldapBase, nameFilter, controls);
countNums = 1;
//获取数据 读取人员组织架构+域控账号
while (results.hasMore()) {
SearchResult result = results.next();
Attributes attrs = result.getAttributes();
String distinguishedName = attrs.get("distinguishedName").get().toString();
String sAMAccountName = attrs.get("sAMAccountName").get().toString();
//logger.info("distinguishedName:"+distinguishedName);
//logger.info("sAMAccountName:"+sAMAccountName);
ldapAccountMap.put(distinguishedName, sAMAccountName);
//logger.warn("分页查询第:{}条, distinguishedName: {}, sAMAccountName: {}", countNums, distinguishedName, sAMAccountName);
redisTemplate.opsForHash().put("ldapAccount", distinguishedName, sAMAccountName);
countNums++;
}
}catch(PartialResultException pre){
logger.warn("Search results: {}", pre.getMessage());
//logger.warn("Search results: {}", pre.getMessage());
throw new PartialResultException(pre.getMessage());
}finally{
ctx.setRequestControls(null);
}
}
pageNums++;
// 处理服务器返回的分页响应
Control[] controlsResp = ctx.getResponseControls();
if (controlsResp != null) {
@ -82,10 +89,9 @@ public class LdapReadUtils {
} else {
cookie = null;
}
//logger.warn("分页查询结束,分码:{}, 数据统计量:{}", pageNums, countNums);
}while(cookie != null && cookie.length > 0);
logger.warn("Query finished, count={}", ldapAccountMap.size());
//返回结果数据
return ldapAccountMap;
}

22
src/main/java/com/spring/config/LdapAccountInitLoad.java

@ -13,8 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@ -29,10 +28,8 @@ import org.springframework.stereotype.Component;
*/
@Component
@Order(value = 12)
public class LdapAccountInitLoad implements ApplicationRunner{
private static final Logger logger = LoggerFactory.getLogger(LdapAccountInitLoad.class);
public class LdapAccountInitLoad implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(LdapReadUtils.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@ -48,7 +45,7 @@ public class LdapAccountInitLoad implements ApplicationRunner{
@Override
public void run(ApplicationArguments args) throws Exception {
public void run(String... args) throws Exception {
//首先查询域控账号的所有数据
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
@ -66,17 +63,16 @@ public class LdapAccountInitLoad implements ApplicationRunner{
// 创建DirContext对象建立与LDAP服务器的连接
ctx = new InitialLdapContext(env, null);
//调用方法读取数据
Map<String, String> ldapAccountMap = LdapReadUtils.getAllLdapAccount(ctx, ldapBase);
Map<String, String> ldapAccountMap = LdapReadUtils.getAllLdapAccount(ctx, ldapBase, redisTemplate);
// 数据放到redis中去
redisTemplate.opsForHash().putAll("ldapAccount", ldapAccountMap);
logger.info("域控账号缓存的数量:"+ldapAccountMap.size());
redisTemplate.opsForHash().putAll("ldapAccountTest", ldapAccountMap);
logger.info("本次域控缓存数据统计:"+ldapAccountMap.size());
} catch (NamingException e) {
logger.error("Failed to connect to the LDAP server.");
System.out.println("Failed to connect to the LDAP server.");
} finally{
if (null != ctx){
ctx.close();
System.out.println("域控账号正常关闭!");
}
}

7
src/main/java/com/spring/modules/base/dao/PropertiesMapper.java

@ -454,4 +454,11 @@ public interface PropertiesMapper extends BaseMapper<PlmPropertiesItemData> {
Double getNewSeqNoForAlone(PartSubPropertiesValueData inData);
/**
* @description: 查询数据列表
* @author DouDou
* @date 2025/9/11 15:18
* @version 1.0
*/
List<PartSubPropertiesValue> getPropertiesList(PartSubPropertiesValue inData);
}

45
src/main/java/com/spring/modules/base/service/Impl/PropertiesServiceImpl.java

@ -465,11 +465,54 @@ public class PropertiesServiceImpl implements PropertiesService {
this.syncToolPropertyValues(data, recordType);
}
}
for (PartSubPropertiesValue itemDate : data){
//首先查询当前需要数据
List<PartSubPropertiesValue> oriList = propertiesMapper.getPropertiesList(data.get(0));
//查询需要修改的参数
List<PartSubPropertiesValue> updateList = getAndCheckNeedUpdateList(oriList, data);
//批量修改
for (PartSubPropertiesValue itemDate : updateList){
propertiesMapper.updatePropertiesList(itemDate);
}
}
/**
* @description: 找出需要更新的数据
* @author DouDou
* @date 2025/9/11 15:49
* @version 1.0
*/
public List<PartSubPropertiesValue> getAndCheckNeedUpdateList(List<PartSubPropertiesValue> oriList, List<PartSubPropertiesValue> newList) {
//数据转Map 键为属性名称
Map<String, PartSubPropertiesValue> newMap = new HashMap<>();
List<PartSubPropertiesValue> updateList = new ArrayList<>();
//循环转换
for(PartSubPropertiesValue bean : newList) {
newMap.put(bean.getPropertiesItemNo(), bean);
}
//循环比较数据是否一直
for (PartSubPropertiesValue oriBean : oriList){
String attribute = oriBean.getPropertiesItemNo(); // 属性编码
String valueText = oriBean.getTextValue(); // 文本值
Double valueNo = oriBean.getNumValue(); // 数字值
//接口路传过来的新值
if (!newMap.containsKey(attribute)){
continue;
}
PartSubPropertiesValue tempBean = newMap.get(attribute);
//临时的参数
String tempValueText = tempBean.getTextValue(); // 文本值
Double tempValueNo = tempBean.getNumValue(); // 数字值
//针对文本和数据使用不同的比较方式
if(Objects.equals(tempValueText, valueText) && Objects.equals(tempValueNo, valueNo)) {
//本次不用保存
} else {
updateList.add(tempBean);
}
}
//返回需要处理的数据
return updateList;
}
/**
* @description: 批量同步工具的参数
* @author LR

Loading…
Cancel
Save