|
- package com.example.webapi.service;
-
- import com.example.webapi.dto.VehicleInfoQueryDTO;
- import com.example.webapi.entity.VehicleInfo;
- import com.example.webapi.repository.VehicleInfoRepository;
- import com.example.webapi.util.PageUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Example;
- import org.springframework.data.domain.ExampleMatcher;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
-
- import java.util.Optional;
-
- @Service
- public class VehicleInfoService {
-
- @Autowired
- private VehicleInfoRepository repository;
-
- public Page<VehicleInfo> findAll(Pageable pageable) {
- return repository.findAll(pageable);
- }
-
- /**
- * 根据条件查询车辆信息(分页)
- *
- * @param queryDTO 查询条件(包含分页参数)
- * @return 分页结果
- */
- public Page<VehicleInfo> findByConditions(VehicleInfoQueryDTO queryDTO) {
- // 从QueryDTO中提取分页参数
- Pageable pageable = PageUtil.createPageable(queryDTO);
-
- // 创建实体对象作为查询条件
- VehicleInfo probe = new VehicleInfo();
-
- // 设置查询条件(只设置非空值)
- if (StringUtils.hasText(queryDTO.getLicensePlate())) {
- probe.setLicensePlate(queryDTO.getLicensePlate());
- }
- if (StringUtils.hasText(queryDTO.getType())) {
- probe.setType(queryDTO.getType());
- }
- if (queryDTO.getOutage() != null) {
- probe.setOutage(queryDTO.getOutage());
- }
- if (queryDTO.getLock() != null) {
- probe.setLock(queryDTO.getLock());
- }
- if (queryDTO.getTransCompany() != null) {
- probe.setTransCompany(queryDTO.getTransCompany());
- }
- if (queryDTO.getReviewed() != null) {
- probe.setReviewed(queryDTO.getReviewed());
- }
- if (StringUtils.hasText(queryDTO.getRemarks())) {
- probe.setRemarks(queryDTO.getRemarks());
- }
-
- // 创建ExampleMatcher,配置模糊查询
- ExampleMatcher matcher = ExampleMatcher.matching()
- .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 字符串使用包含匹配
- .withIgnoreCase() // 忽略大小写
- .withIgnoreNullValues(); // 忽略空值
-
- // 创建Example对象
- Example<VehicleInfo> example = Example.of(probe, matcher);
-
- // 执行查询
- return repository.findAll(example, pageable);
- }
-
- public Optional<VehicleInfo> findById(Integer id) {
- return repository.findById(id);
- }
-
- public VehicleInfo save(VehicleInfo entity) {
- return repository.save(entity);
- }
-
- public void deleteById(Integer id) {
- repository.deleteById(id);
- }
- }
|