科技政策查询系统网页版
项目结构
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<PolicyTypeTreeDTO> typeTree = policyTypeService.getFullTypeTree();
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<TypeNodeDTO> typeTree = policyTypeService.getTypeTreeAsDTO();
model.addAttribute("typeTree", typeTree);
model.addAttribute("queryDto", new PolicyQueryDto());
return "search-form";
}
@PostMapping("/search")
public String search(PolicyQueryDto queryDto, Model model) {
List<Policy> policies = policyService.advancedSearchWithSpecification(queryDto);
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<Policy> findByType(String type);
// 如果需要查询该分类及其所有子分类的政策
@Query("SELECT p FROM Policy p WHERE p.type LIKE :typeId%")
List<Policy> findByTypeAndSubTypes(@Param("typeId") String typeId);
@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<Policy> advancedSearch(String name, String type, String organ, String document, String text);
// 通过分类名称查询
@Query("SELECT p FROM Policy p WHERE p.type = :typeName")
List<Policy> findByTypeName(@Param("typeName") String typeName);
// 通过分类ID查询(先获取名称再查询)
default List<Policy> findByTypeId(PolicyTypeRepository typeRepo, String typeId) {
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<PolicyType> findByParentIdIsNull();
List<PolicyType> findByParentId(String parentId);
@Query("SELECT t FROM PolicyType t WHERE t.typeName LIKE %:keyword%")
List<PolicyType> searchByKeyword(String keyword);
}
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<Policy> findByType(String typeId) {
return policyRepository.findByType(typeId);
}
public List<Policy> advancedSearch(PolicyQueryDto queryDto) {
return policyRepository.advancedSearch(
queryDto.getName(),
queryDto.getType(),
queryDto.getOrgan(),
queryDto.getDocument(),
queryDto.getText()
);
}
public List<Policy> advancedSearchWithSpecification(PolicyQueryDto queryDto) {
return policyRepository.findAll((Specification<Policy>) (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
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<Policy> findByTypeName(String typeName) {
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<PolicyType> getRootTypes() {
return policyTypeRepository.findByParentIdIsNull();
}
public List<PolicyType> getChildrenTypes(String parentId) {
return policyTypeRepository.findByParentId(parentId);
}
public Map<String, List<PolicyType>> getTypeTree() {
List<PolicyType> rootTypes = getRootTypes();
Map<String, List<PolicyType>> typeTree = new HashMap<>();
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<PolicyType> searchTypes(String keyword) {
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<PolicyType> rootTypes = getRootTypes();
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<TypeNodeDTO> getTypeTreeAsDTO() {
// 获取所有根节点
List<PolicyType> rootTypes = policyTypeRepository.findByParentIdIsNull();
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<PolicyType> children = getChildrenTypes(policyType.getTypeId());
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<PolicyTypeTreeDTO> getSelectableTypeTree() {
List<PolicyTypeTreeDTO> fullTree = this.getFullTypeTree();
// 确保所有节点都可选(包括一级分类)
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);
}
}