提交 038cc1e5 authored 作者: 马晨俊's avatar 马晨俊

mcj:rule方法排版

上级 7ce3fe83
...@@ -13,7 +13,11 @@ import java.util.Map; ...@@ -13,7 +13,11 @@ import java.util.Map;
* 规则接口 * 规则接口
*/ */
public interface RuleService { public interface RuleService {
/**
* 查询所有规则
* @return 规则
*/
List<Rule> findRules();
/** /**
* 新增规则 * 新增规则
* @param ruleQo 规则封装类 * @param ruleQo 规则封装类
...@@ -21,35 +25,36 @@ public interface RuleService { ...@@ -21,35 +25,36 @@ public interface RuleService {
public void addRule(RuleQo ruleQo); public void addRule(RuleQo ruleQo);
public void addRule(List<Rule> rules); public void addRule(List<Rule> rules);
/** /**
* 修改规则 * 修改规则
* @param ruleQo 规则封装类 * @param ruleQo 规则封装类
*/ */
public void upRule(RuleQo ruleQo); public void upRule(RuleQo ruleQo);
public String exportData();
/** /**
* 删除规则 * 删除规则
* @param ruleQo 规则封装类 * @param ruleQo 规则封装类
*/ */
public void deleteRule(RuleQo ruleQo); public void deleteRule(RuleQo ruleQo);
/** List<Rule> findAllByTechnologyIdIn(List<String> technologyIds);
* 查询所有规则
* @return 规则
*/
List<RuleCollection> findAll(); public void testgb();
public String exportData();
/** /**
* 查询所有规则 * 查询所有规则
* @return 规则 * @return 规则
*/ */
List<Rule> findRules(); List<RuleCollection> findAll();
List<RuleCollection> findByName(String name); List<RuleCollection> findByName(String name);
CustomPage<RuleCollection> findSearch(Map searchMap, int page, int size); CustomPage<RuleCollection> findSearch(Map searchMap, int page, int size);
List<Rule> findAllByTechnologyIdIn(List<String> technologyIds);
void importRules(MultipartFile file); void importRules(MultipartFile file);
} }
...@@ -28,6 +28,8 @@ import java.util.*; ...@@ -28,6 +28,8 @@ import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
/** /**
* 规则库 * 规则库
* *
...@@ -50,6 +52,54 @@ public class RuleServiceImpl implements RuleService { ...@@ -50,6 +52,54 @@ public class RuleServiceImpl implements RuleService {
@Autowired @Autowired
private RuleCollectionDao ruleCollectionDao; private RuleCollectionDao ruleCollectionDao;
@Override
public void testgb() {
/*List<Rule> rules = ruleDao.findAll();
Map<String, List<Rule>> ruleMap = rules.stream().collect(groupingBy(Rule::getTarget));
Set<String> strings = ruleMap.keySet();
List<Rule> ruleList=new ArrayList<>();
for (String string : strings) {
List<Rule> rules1 = ruleMap.get(string);
System.out.println(rules1.toArray());
Rule rule=new Rule();
StringBuilder sb=new StringBuilder();
for (int i = 0; i < rules1.size(); i++) {
if(i==0){
rule.setTechnologyName(rules1.get(i).getTechnologyName());
rule.setId(rules1.get(i).getId());
rule.setTarget(rules1.get(i).getTarget());
rule.setTechnologyId(rules1.get(i).getTechnologyId());
sb.append(rules1.get(i).getSuffix());
continue;
}
sb.append(","+rules1.get(i).getSuffix());
}
rule.setSuffix(sb.toString());
ruleList.add(rule);
}
ruleDao.deleteAll();
ruleList.forEach(System.out::println);
ruleDao.saveAll(ruleList);*/
}
public List<Rule> findAllRule() {
List<Rule> rules = ruleDao.findAll();
List<Rule> ruleList = new ArrayList<>();
if (rules != null && rules.size() > 0) {
for (Rule rule : rules) {
String suffix = rule.getSuffix();
String[] split = suffix.split(",");
for (String s : split) {
Rule rule1 = new Rule();
BeanUtils.copyProperties(rule, rule1);
rule1.setSuffix(s);
ruleList.add(rule1);
}
}
}
return ruleList;
}
/** /**
* 新增规则 * 新增规则
* *
...@@ -59,8 +109,8 @@ public class RuleServiceImpl implements RuleService { ...@@ -59,8 +109,8 @@ public class RuleServiceImpl implements RuleService {
public void addRule(RuleQo ruleQo) { public void addRule(RuleQo ruleQo) {
List<String> suffixes = ruleQo.getSuffix(); List<String> suffixes = ruleQo.getSuffix();
for (String suffix : suffixes) { for (String suffix : suffixes) {
List<Rule> rule1 = ruleDao.findByTargetAndSuffixEqualsAndTechnologyIdEquals(ruleQo.getTarget(), suffix,ruleQo.getTechnologyId()); List<Rule> rule1 = ruleDao.findByTargetAndSuffixEqualsAndTechnologyIdEquals(ruleQo.getTarget(), suffix, ruleQo.getTechnologyId());
if (rule1 != null&& rule1.size()>0) { if (rule1 != null && rule1.size() > 0) {
continue; continue;
} }
Rule rule = new Rule(); Rule rule = new Rule();
...@@ -75,11 +125,12 @@ public class RuleServiceImpl implements RuleService { ...@@ -75,11 +125,12 @@ public class RuleServiceImpl implements RuleService {
ruleCollectionDao.save(ruleCollection); ruleCollectionDao.save(ruleCollection);
} }
public void saveRule(Rule rule) { public void saveRule(Rule rule) {
String suffix = rule.getSuffix(); String suffix = rule.getSuffix();
List<Rule> rule1 = ruleDao.findByTargetAndSuffixEqualsAndTechnologyIdEquals(rule.getTarget(), suffix,rule.getTechnologyId()); List<Rule> rule1 = ruleDao.findByTargetAndSuffixEqualsAndTechnologyIdEquals(rule.getTarget(), suffix, rule.getTechnologyId());
if (rule1 != null&&rule1.size()>0) { if (rule1 != null && rule1.size() > 0) {
if(rule1.size()>1){ if (rule1.size() > 1) {
for (int i = 1; i < rule1.size(); i++) { for (int i = 1; i < rule1.size(); i++) {
ruleDao.deleteById(rule1.get(i).getId()); ruleDao.deleteById(rule1.get(i).getId());
} }
...@@ -88,6 +139,7 @@ public class RuleServiceImpl implements RuleService { ...@@ -88,6 +139,7 @@ public class RuleServiceImpl implements RuleService {
} }
ruleDao.save(rule); ruleDao.save(rule);
} }
@Override @Override
public void addRule(List<Rule> rules) { public void addRule(List<Rule> rules) {
ruleDao.saveAll(rules); ruleDao.saveAll(rules);
...@@ -112,7 +164,7 @@ public class RuleServiceImpl implements RuleService { ...@@ -112,7 +164,7 @@ public class RuleServiceImpl implements RuleService {
CustomPage<RuleCollection> myCustomPage = new CustomPage<>(); CustomPage<RuleCollection> myCustomPage = new CustomPage<>();
myCustomPage.setTotalPage(ruleCollections.getTotalPages()); myCustomPage.setTotalPage(ruleCollections.getTotalPages());
myCustomPage.setTotalElement((int)ruleCollections.getTotalElements()); myCustomPage.setTotalElement((int) ruleCollections.getTotalElements());
myCustomPage.setObjects(ruleCollections.getContent()); myCustomPage.setObjects(ruleCollections.getContent());
return myCustomPage; return myCustomPage;
} }
...@@ -122,7 +174,33 @@ public class RuleServiceImpl implements RuleService { ...@@ -122,7 +174,33 @@ public class RuleServiceImpl implements RuleService {
return ruleDao.findAllByTechnologyIdIn(technologyIds); return ruleDao.findAllByTechnologyIdIn(technologyIds);
} }
/**
* 查询所有规则
* 规则唯一 关键字+后缀+技术id
* target:xx,suffix:js:tech:gjfgj
* target:xx,suffix:html:tech:gjfgj
* target:xx,suffix:css:tech:gjfgj
* <p>
* target:xx,suffix:html,css,js:tech:gjfgj
*
* @return RuleVoList
*/
@Override
public List<RuleCollection> findAll() {
List<RuleCollection> rules = ruleCollectionDao.findAll();
return rules;
}
@Override
public List<Rule> findRules() {
return ruleDao.findAll();
}
@Override
public List<RuleCollection> findByName(String name) {
List<RuleCollection> rules = ruleCollectionDao.findAllByTargetLike("%" + name + "%");
return rules;
}
/** /**
* 动态条件构建 * 动态条件构建
...@@ -154,7 +232,7 @@ public class RuleServiceImpl implements RuleService { ...@@ -154,7 +232,7 @@ public class RuleServiceImpl implements RuleService {
for (Rule rule : rules) { for (Rule rule : rules) {
ruleDao.deleteById(rule.getId()); ruleDao.deleteById(rule.getId());
} }
if(ruleCollection!=null){ if (ruleCollection != null) {
ruleCollectionDao.deleteById(ruleCollection.getId()); ruleCollectionDao.deleteById(ruleCollection.getId());
} }
addRule(ruleQo); addRule(ruleQo);
...@@ -169,40 +247,13 @@ public class RuleServiceImpl implements RuleService { ...@@ -169,40 +247,13 @@ public class RuleServiceImpl implements RuleService {
ruleDao.deleteById(rule.getId()); ruleDao.deleteById(rule.getId());
} }
RuleCollection ruleCollection = ruleCollectionDao.findAllByTechnologyIdEqualsAndTargetEquals(ruleQo.getTechnologyId(), ruleQo.getTarget()); RuleCollection ruleCollection = ruleCollectionDao.findAllByTechnologyIdEqualsAndTargetEquals(ruleQo.getTechnologyId(), ruleQo.getTarget());
if(ruleCollection!=null){ if (ruleCollection != null) {
ruleCollectionDao.deleteById(ruleCollection.getId()); ruleCollectionDao.deleteById(ruleCollection.getId());
} }
} }
/** private RuleCollection dataQo2RuleCollection(RuleQo ruleQo) {
* 查询所有规则
* 规则唯一 关键字+后缀+技术id
* target:xx,suffix:js:tech:gjfgj
* target:xx,suffix:html:tech:gjfgj
* target:xx,suffix:css:tech:gjfgj
*
* target:xx,suffix:html,css,js:tech:gjfgj
* @return RuleVoList
*/
@Override
public List<RuleCollection> findAll() {
List<RuleCollection> rules = ruleCollectionDao.findAll();
return rules;
}
@Override
public List<Rule> findRules() {
return ruleDao.findAll();
}
@Override
public List<RuleCollection> findByName(String name) {
List<RuleCollection> rules = ruleCollectionDao.findAllByTargetLike("%" + name + "%");
return rules;
}
private RuleCollection dataQo2RuleCollection(RuleQo ruleQo){
RuleCollection ruleCollection = new RuleCollection(); RuleCollection ruleCollection = new RuleCollection();
ruleCollection.setTechnologyName(ruleQo.getTechnologyName()); ruleCollection.setTechnologyName(ruleQo.getTechnologyName());
ruleCollection.setTechnologyId(ruleQo.getTechnologyId()); ruleCollection.setTechnologyId(ruleQo.getTechnologyId());
...@@ -210,9 +261,9 @@ public class RuleServiceImpl implements RuleService { ...@@ -210,9 +261,9 @@ public class RuleServiceImpl implements RuleService {
ruleCollection.setId(UUIDUtil.getUUID()); ruleCollection.setId(UUIDUtil.getUUID());
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (String suffix : ruleQo.getSuffix()) { for (String suffix : ruleQo.getSuffix()) {
if(stringBuilder.length()==0){ if (stringBuilder.length() == 0) {
stringBuilder.append(suffix); stringBuilder.append(suffix);
}else{ } else {
stringBuilder.append(",").append(suffix); stringBuilder.append(",").append(suffix);
} }
} }
...@@ -220,19 +271,19 @@ public class RuleServiceImpl implements RuleService { ...@@ -220,19 +271,19 @@ public class RuleServiceImpl implements RuleService {
return ruleCollection; return ruleCollection;
} }
private List<RuleCollection> dataList2RuleCollection(List<Rule> rules){ private List<RuleCollection> dataList2RuleCollection(List<Rule> rules) {
ArrayList<RuleCollection> ruleCollections = new ArrayList<>(); ArrayList<RuleCollection> ruleCollections = new ArrayList<>();
HashMap<String, ArrayList<String>> map = new HashMap<>(); HashMap<String, ArrayList<String>> map = new HashMap<>();
HashMap<String, Rule> ruleMap = new HashMap<>(); HashMap<String, Rule> ruleMap = new HashMap<>();
for (Rule rule : rules) { for (Rule rule : rules) {
ruleMap.put(rule.getTarget()+":"+rule.getTechnologyId(),rule); ruleMap.put(rule.getTarget() + ":" + rule.getTechnologyId(), rule);
if(map.containsKey(rule.getTarget()+":"+rule.getTechnologyId())){ if (map.containsKey(rule.getTarget() + ":" + rule.getTechnologyId())) {
ArrayList<String> suffix = map.get(rule.getTarget()+":"+rule.getTechnologyId()); ArrayList<String> suffix = map.get(rule.getTarget() + ":" + rule.getTechnologyId());
suffix.add(rule.getSuffix()); suffix.add(rule.getSuffix());
}else{ } else {
ArrayList<String> suffix = new ArrayList<>(); ArrayList<String> suffix = new ArrayList<>();
suffix.add(rule.getSuffix()); suffix.add(rule.getSuffix());
map.put(rule.getTarget()+":"+rule.getTechnologyId(),suffix); map.put(rule.getTarget() + ":" + rule.getTechnologyId(), suffix);
} }
} }
...@@ -245,9 +296,9 @@ public class RuleServiceImpl implements RuleService { ...@@ -245,9 +296,9 @@ public class RuleServiceImpl implements RuleService {
ruleCollection.setId(UUIDUtil.getUUID()); ruleCollection.setId(UUIDUtil.getUUID());
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (String suffix : map.get(target)) { for (String suffix : map.get(target)) {
if(stringBuilder.length()==0){ if (stringBuilder.length() == 0) {
stringBuilder.append(suffix); stringBuilder.append(suffix);
}else{ } else {
stringBuilder.append(",").append(suffix); stringBuilder.append(",").append(suffix);
} }
} }
...@@ -257,33 +308,35 @@ public class RuleServiceImpl implements RuleService { ...@@ -257,33 +308,35 @@ public class RuleServiceImpl implements RuleService {
return ruleCollections; return ruleCollections;
} }
public String exportData(){ @Override
SyncData syncData=new SyncData(); public String exportData() {
List<TechnologySyn> technologySyns=new ArrayList<>(); SyncData syncData = new SyncData();
List<TechnologySyn> technologySyns = new ArrayList<>();
//1.生成数据 //1.生成数据
List<Technology> technologies = technologyService.findAllTechnology(); List<Technology> technologies = technologyService.findAllTechnology();
for (Technology technology : technologies) { for (Technology technology : technologies) {
TechnologySyn technologySyn=new TechnologySyn(); TechnologySyn technologySyn = new TechnologySyn();
BeanUtils.copyProperties(technology,technologySyn); BeanUtils.copyProperties(technology, technologySyn);
List<Rule> rules = ruleDao.findAllByTechnologyId(technology.getId()); List<Rule> rules = ruleDao.findAllByTechnologyId(technology.getId());
technologySyn.setRules(rules); technologySyn.setRules(rules);
technologySyns.add(technologySyn); technologySyns.add(technologySyn);
} }
syncData.setTechnologies(technologySyns); syncData.setTechnologies(technologySyns);
String s = JSON.toJSONString(syncData); String s = JSON.toJSONString(syncData);
String path=System.getProperty("user.dir")+ File.separator+"inspect"+File.separator+"inspect.txt"; String path = System.getProperty("user.dir") + File.separator + "inspect" + File.separator + "inspect.txt";
//2.生成json文件 //2.生成json文件
FileUtil.write(s,path); FileUtil.write(s, path);
return path; return path;
} }
@Override @Override
public void importRules(MultipartFile file) { public void importRules(MultipartFile file) {
if (file.isEmpty()) { if (file.isEmpty()) {
return; return;
} }
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
String fileType = fileName.substring(0,fileName.lastIndexOf(".")); String fileType = fileName.substring(0, fileName.lastIndexOf("."));
File dest = new File(System.getProperty("user.dir") +File.separator+"inspect"+File.separator+UUIDUtil.getUUID()+File.separator+fileType); File dest = new File(System.getProperty("user.dir") + File.separator + "inspect" + File.separator + UUIDUtil.getUUID() + File.separator + fileType);
if (!dest.getParentFile().exists()) { if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();
} }
...@@ -295,16 +348,16 @@ public class RuleServiceImpl implements RuleService { ...@@ -295,16 +348,16 @@ public class RuleServiceImpl implements RuleService {
//1.导入json文件 //1.导入json文件
String s = readTxt(dest.getAbsolutePath()); String s = readTxt(dest.getAbsolutePath());
System.out.println(s); System.out.println(s);
if(!StringUtils.isEmpty(s)){ if (!StringUtils.isEmpty(s)) {
//2.清洗规则数据 //2.清洗规则数据
//3.将数据添加到数据库中 //3.将数据添加到数据库中
SyncData syncData = JSON.parseObject(s, SyncData.class); SyncData syncData = JSON.parseObject(s, SyncData.class);
List<TechnologySyn> technologies = syncData.getTechnologies(); List<TechnologySyn> technologies = syncData.getTechnologies();
if(technologies!=null&&technologies.size()>0) { if (technologies != null && technologies.size() > 0) {
for (TechnologySyn technology : technologies) { for (TechnologySyn technology : technologies) {
List<Rule> rules = technology.getRules(); List<Rule> rules = technology.getRules();
if(rules!=null&&rules.size()>0){ if (rules != null && rules.size() > 0) {
for (Rule rule : rules) { for (Rule rule : rules) {
saveRule(rule); saveRule(rule);
} }
...@@ -319,13 +372,15 @@ public class RuleServiceImpl implements RuleService { ...@@ -319,13 +372,15 @@ public class RuleServiceImpl implements RuleService {
} }
public String readTxt(String filePath){ public String readTxt(String filePath) {
try { try {
String encoding = "UTF-8"; String encoding = "UTF-8";
File file = new File(filePath); File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在 // 判断文件是否存在
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader( InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式 // 考虑到编码格式
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read); BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null; String lineTxt = null;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -345,7 +400,8 @@ public class RuleServiceImpl implements RuleService { ...@@ -345,7 +400,8 @@ public class RuleServiceImpl implements RuleService {
} }
return null; return null;
} }
public void syncData(){
public void syncData() {
//1.导入json数据 //1.导入json数据
//2.清空数据库 //2.清空数据库
//3.导入到标准库 //3.导入到标准库
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论