4.10
科技政策查询网页版
项目结构
PolicyController
package com.example.policyquerysystem.controller;
import com.example.policyquerysystem.dto.PolicyQueryDto;
import com.example.policyquerysystem.dto.PolicyTypeTreeDTO;
import com.example.policyquerysystem.dto.TypeNodeDTO;
import com.example.policyquerysystem.entity.Policy;
import com.example.policyquerysystem.entity.PolicyType;
import com.example.policyquerysystem.service.PolicyService;
import com.example.policyquerysystem.service.PolicyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/policy")
public class PolicyController {
@Autowired
private PolicyService policyService;
@Autowired
private PolicyTypeService policyTypeService;
@GetMapping("/")
public String index(Model model) {
// 获取分类树数据
List
model.addAttribute("typeTree", typeTree);
// 添加空白查询DTO
model.addAttribute("queryDto", new PolicyQueryDto());
return "index";
}
@GetMapping("/detail/{id}")
public String detail(@PathVariable Long id, Model model) {
Policy policy = policyService.getPolicyById(id);
if (policy != null) {
String typeName = policyTypeService.getTypeNameById(policy.getType());
model.addAttribute("policy", policy);
model.addAttribute("typeName", typeName);
}
return "policy-detail";
}
@GetMapping("/search")
public String searchForm(Model model) {
List
model.addAttribute("typeTree", typeTree);
model.addAttribute("queryDto", new PolicyQueryDto());
return "search-form";
}
@PostMapping("/search")
public String search(PolicyQueryDto queryDto, Model model) {
List
String typeName = queryDto.getType() != null && !queryDto.getType().isEmpty()
? policyTypeService.getTypeNameById(queryDto.getType())
: "";
model.addAttribute("policies", policies);
model.addAttribute("queryDto", queryDto);
model.addAttribute("typeName", typeName);
return "policy-list";
}
@GetMapping("/types")
public String showTypeTree(Model model) {
model.addAttribute("typeTree", policyTypeService.getFullTypeTree());
return "policy-type-tree";
}
@GetMapping("/type/{typeId}")
public String listByType(@PathVariable String typeId, Model model) {
// 先获取分类名称
String typeName = policyTypeService.getTypeNameById(typeId);
// 使用分类名称查询政策
List<Policy> policies = policyService.findByTypeName(typeName);
model.addAttribute("policies", policies);
model.addAttribute("typeName", typeName);
return "policy-list";
}
@GetMapping("/advanced-search")
public String advancedSearchForm(Model model) {
model.addAttribute("typeTree", policyTypeService.getSelectableTypeTree());
model.addAttribute("queryDto", new PolicyQueryDto());
return "advanced-search";
}
}
package com.example.policyquerysystem.repository;
import com.example.policyquerysystem.entity.Policy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
@Repository
public interface PolicyRepository extends JpaRepository<Policy, Long>, JpaSpecificationExecutor {
List
// 如果需要查询该分类及其所有子分类的政策
@Query("SELECT p FROM Policy p WHERE p.type LIKE :typeId%")
List
@Query("SELECT p FROM Policy p WHERE " +
"(:name IS NULL OR p.name LIKE %:name%) AND " +
"(:type IS NULL OR p.type = :type) AND " +
"(:organ IS NULL OR p.organ LIKE %:organ%) AND " +
"(:document IS NULL OR p.document LIKE %:document%) AND " +
"(:text IS NULL OR p.text LIKE %:text%)")
List
// 通过分类名称查询
@Query("SELECT p FROM Policy p WHERE p.type = :typeName")
List
// 通过分类ID查询(先获取名称再查询)
default List
return typeRepo.findById(typeId)
.map(type -> findByTypeName(type.getTypeName()))
.orElse(Collections.emptyList());
}
}
package com.example.policyquerysystem.repository;
import com.example.policyquerysystem.entity.PolicyType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PolicyTypeRepository extends JpaRepository<PolicyType, String> {
List
List
@Query("SELECT t FROM PolicyType t WHERE t.typeName LIKE %:keyword%")
List
}
package com.example.policyquerysystem.service;
import com.example.policyquerysystem.dto.PolicyQueryDto;
import com.example.policyquerysystem.entity.Policy;
import com.example.policyquerysystem.repository.PolicyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
@Service
public class PolicyService {
@Autowired
private PolicyRepository policyRepository;
@Autowired
private PolicyTypeService policyTypeService;
public List
return policyRepository.findByType(typeId);
}
public List
return policyRepository.advancedSearch(
queryDto.getName(),
queryDto.getType(),
queryDto.getOrgan(),
queryDto.getDocument(),
queryDto.getText()
);
}
public List
return policyRepository.findAll((Specification
List
if (queryDto.getName() != null && !queryDto.getName().isEmpty()) {
predicates.add(criteriaBuilder.like(root.get("name"), "%" + queryDto.getName() + "%"));
}
if (queryDto.getType() != null && !queryDto.getType().isEmpty()) {
predicates.add(criteriaBuilder.equal(root.get("type"), queryDto.getType()));
}
if (queryDto.getOrgan() != null && !queryDto.getOrgan().isEmpty()) {
predicates.add(criteriaBuilder.like(root.get("organ"), "%" + queryDto.getOrgan() + "%"));
}
if (queryDto.getDocument() != null && !queryDto.getDocument().isEmpty()) {
predicates.add(criteriaBuilder.like(root.get("document"), "%" + queryDto.getDocument() + "%"));
}
if (queryDto.getText() != null && !queryDto.getText().isEmpty()) {
predicates.add(criteriaBuilder.like(root.get("text"), "%" + queryDto.getText() + "%"));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
});
}
public Policy getPolicyById(Long id) {
return policyRepository.findById(id).orElse(null);
}
public List
return policyRepository.findByTypeName(typeName);
}
}
package com.example.policyquerysystem.service;
import com.example.policyquerysystem.dto.PolicyTypeTreeDTO;
import com.example.policyquerysystem.dto.TypeNodeDTO;
import com.example.policyquerysystem.entity.PolicyType;
import com.example.policyquerysystem.repository.PolicyTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class PolicyTypeService {
@Autowired
private PolicyTypeRepository policyTypeRepository;
public List
return policyTypeRepository.findByParentIdIsNull();
}
public List
return policyTypeRepository.findByParentId(parentId);
}
public Map<String, List
List
Map<String, List
for (PolicyType rootType : rootTypes) {
List<PolicyType> children = getChildrenTypes(rootType.getTypeId());
typeTree.put(rootType.getTypeId(), children);
}
return typeTree;
}
public String getTypeNameById(String typeId) {
return policyTypeRepository.findById(typeId)
.map(PolicyType::getTypeName)
.orElse("未知分类");
}
public List
return policyTypeRepository.searchByKeyword(keyword);
}
public Map<String, String> getAllTypeNames() {
return policyTypeRepository.findAll()
.stream()
.collect(Collectors.toMap(PolicyType::getTypeId, PolicyType::getTypeName));
}
public Map<String, Object> getTypeTreeWithNames() {
List
Map<String, Object> typeTree = new LinkedHashMap<>();
for (PolicyType rootType : rootTypes) {
Map<String, Object> node = new HashMap<>();
node.put("name", rootType.getTypeName());
List<Map<String, String>> children = getChildrenTypes(rootType.getTypeId()).stream()
.map(child -> {
Map<String, String> childMap = new HashMap<>();
childMap.put("id", child.getTypeId());
childMap.put("name", child.getTypeName());
return childMap;
})
.collect(Collectors.toList());
node.put("children", children);
typeTree.put(rootType.getTypeId(), node);
}
return typeTree;
}
public List
// 获取所有根节点
List
return rootTypes.stream()
.map(root -> {
TypeNodeDTO rootNode = new TypeNodeDTO(root);
// 获取子节点
List<PolicyType> children = policyTypeRepository.findByParentId(root.getTypeId());
rootNode.setChildren(
children.stream()
.map(TypeNodeDTO::new)
.collect(Collectors.toList())
);
return rootNode;
})
.collect(Collectors.toList());
}
private TypeNodeDTO convertToDTO(PolicyType policyType) {
TypeNodeDTO dto = new TypeNodeDTO(policyType);
List
dto.setChildren(children.stream()
.map(this::convertToDTO)
.collect(Collectors.toList()));
return dto;
}
public List<PolicyTypeTreeDTO> getFullTypeTree() {
// 获取所有分类数据
List<PolicyType> allTypes = policyTypeRepository.findAll();
// 构建ID到DTO的映射
Map<String, PolicyTypeTreeDTO> dtoMap = new HashMap<>();
allTypes.forEach(type -> dtoMap.put(type.getTypeId(), new PolicyTypeTreeDTO(type)));
// 构建树形结构
List<PolicyTypeTreeDTO> result = new ArrayList<>();
for (PolicyType type : allTypes) {
PolicyTypeTreeDTO dto = dtoMap.get(type.getTypeId());
if (type.getParentId() == null || type.getParentId().isEmpty()) {
result.add(dto);
} else {
PolicyTypeTreeDTO parentDto = dtoMap.get(type.getParentId());
if (parentDto != null) {
parentDto.getChildren().add(dto);
}
}
}
return result;
}
public List
List
// 确保所有节点都可选(包括一级分类)
fullTree.forEach(root -> {
root.setSelectable(true);
root.getChildren().forEach(child -> child.setSelectable(true));
});
return fullTree;
}
}
package com.example.policyquerysystem;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PolicyQuerySystemApplication {
public static void main(String[] args) {
SpringApplication.run(PolicyQuerySystemApplication.class, args);
}
}
posted on 2025-04-11 16:12 我爱玩原神(原神大王) 阅读(11) 评论(0) 收藏 举报
浙公网安备 33010602011771号