|
- package com.example.webapi.service;
-
- import com.example.webapi.dto.SupplyVarietyQueryDTO;
- import com.example.webapi.entity.SupplyVariety;
- import com.example.webapi.repository.SupplyVarietyRepository;
- 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 java.util.Optional;
-
- @Service
- public class SupplyVarietyService {
-
- @Autowired
- private SupplyVarietyRepository repository;
-
- public Page<SupplyVariety> findAll(Pageable pageable) {
- return repository.findAll(pageable);
- }
-
- /**
- * 根据条件查询供应标识品种信息(分页)
- *
- * @param queryDTO 查询条件(包含分页参数)
- * @return 分页结果
- */
- public Page<SupplyVariety> findByConditions(SupplyVarietyQueryDTO queryDTO) {
- // 从QueryDTO中提取分页参数
- Pageable pageable = PageUtil.createPageable(queryDTO);
-
- // 创建实体对象作为查询条件
- SupplyVariety probe = new SupplyVariety();
-
- // 设置查询条件(只设置非空值)
- if (queryDTO.getSupplyId() != null) {
- probe.setSupplyId(queryDTO.getSupplyId());
- }
- if (queryDTO.getGood() != null) {
- probe.setGood(queryDTO.getGood());
- }
- if (queryDTO.getActive() != null) {
- probe.setActive(queryDTO.getActive());
- }
-
- // 创建ExampleMatcher,配置精确匹配
- ExampleMatcher matcher = ExampleMatcher.matching()
- .withIgnoreNullValues(); // 忽略空值
-
- // 创建Example对象
- Example<SupplyVariety> example = Example.of(probe, matcher);
-
- // 执行查询
- return repository.findAll(example, pageable);
- }
-
- public Optional<SupplyVariety> findById(Integer id) {
- return repository.findById(id);
- }
-
- public SupplyVariety save(SupplyVariety entity) {
- return repository.save(entity);
- }
-
- public void deleteById(Integer id) {
- repository.deleteById(id);
- }
- }
|