You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 line
2.9KB

  1. package com.example.webapi.service;
  2. import com.example.webapi.dto.VehicleInfoQueryDTO;
  3. import com.example.webapi.entity.VehicleInfo;
  4. import com.example.webapi.repository.VehicleInfoRepository;
  5. import com.example.webapi.util.PageUtil;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.domain.Example;
  8. import org.springframework.data.domain.ExampleMatcher;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.data.domain.Pageable;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.util.StringUtils;
  13. import java.util.Optional;
  14. @Service
  15. public class VehicleInfoService {
  16. @Autowired
  17. private VehicleInfoRepository repository;
  18. public Page<VehicleInfo> findAll(Pageable pageable) {
  19. return repository.findAll(pageable);
  20. }
  21. /**
  22. * 根据条件查询车辆信息(分页)
  23. *
  24. * @param queryDTO 查询条件(包含分页参数)
  25. * @return 分页结果
  26. */
  27. public Page<VehicleInfo> findByConditions(VehicleInfoQueryDTO queryDTO) {
  28. // 从QueryDTO中提取分页参数
  29. Pageable pageable = PageUtil.createPageable(queryDTO);
  30. // 创建实体对象作为查询条件
  31. VehicleInfo probe = new VehicleInfo();
  32. // 设置查询条件(只设置非空值)
  33. if (StringUtils.hasText(queryDTO.getLicensePlate())) {
  34. probe.setLicensePlate(queryDTO.getLicensePlate());
  35. }
  36. if (StringUtils.hasText(queryDTO.getType())) {
  37. probe.setType(queryDTO.getType());
  38. }
  39. if (queryDTO.getOutage() != null) {
  40. probe.setOutage(queryDTO.getOutage());
  41. }
  42. if (queryDTO.getLock() != null) {
  43. probe.setLock(queryDTO.getLock());
  44. }
  45. if (queryDTO.getTransCompany() != null) {
  46. probe.setTransCompany(queryDTO.getTransCompany());
  47. }
  48. if (queryDTO.getReviewed() != null) {
  49. probe.setReviewed(queryDTO.getReviewed());
  50. }
  51. if (StringUtils.hasText(queryDTO.getRemarks())) {
  52. probe.setRemarks(queryDTO.getRemarks());
  53. }
  54. // 创建ExampleMatcher,配置模糊查询
  55. ExampleMatcher matcher = ExampleMatcher.matching()
  56. .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 字符串使用包含匹配
  57. .withIgnoreCase() // 忽略大小写
  58. .withIgnoreNullValues(); // 忽略空值
  59. // 创建Example对象
  60. Example<VehicleInfo> example = Example.of(probe, matcher);
  61. // 执行查询
  62. return repository.findAll(example, pageable);
  63. }
  64. public Optional<VehicleInfo> findById(Integer id) {
  65. return repository.findById(id);
  66. }
  67. public VehicleInfo save(VehicleInfo entity) {
  68. return repository.save(entity);
  69. }
  70. public void deleteById(Integer id) {
  71. repository.deleteById(id);
  72. }
  73. }