| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package com.lianda.backend.service;
- import com.lianda.backend.config.DataSource;
- import com.lianda.backend.config.RoutingDataSourceConfig;
- import com.lianda.backend.dto.PortDTO;
- import com.lianda.backend.dto.ShippingCompanyDTO;
- import com.lianda.backend.model.BusCustomerCompany;
- import com.lianda.backend.model.BusCustomerCompanyBusiness;
- import com.lianda.backend.model.BusCustomerCustomerType;
- import com.lianda.backend.model.DispPort;
- import com.lianda.backend.repository.BusCustomerCompanyBusinessRepository;
- import com.lianda.backend.repository.BusCustomerCompanyRepository;
- import com.lianda.backend.repository.BusCustomerCustomerTypeRepository;
- import com.lianda.backend.repository.DispPortRepository;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- /**
- * 港口和船公司服务类
- */
- @Service
- @DataSource(value = RoutingDataSourceConfig.DataSourceType.BRANCH) // 读取分支机构业务数据
- public class CommonDataService {
- @Autowired
- private DispPortRepository dispPortRepository;
- @Autowired
- private BusCustomerCompanyBusinessRepository busCustomerCompanyBusinessRepository;
- @Autowired
- private BusCustomerCompanyRepository busCustomerCompanyRepository;
- @Autowired
- private BusCustomerCustomerTypeRepository busCustomerCustomerTypeRepository;
- /**
- * 获取有效的港口列表(RecordStatus=1)
- *
- * @return 港口DTO列表
- */
- public List<PortDTO> getValidPorts() {
- // 查询所有有效港口(RecordStatus=1)
- List<DispPort> validPorts = dispPortRepository.findAll().stream()
- .filter(port -> port.getRecordStatus() != null && port.getRecordStatus() == 1)
- .collect(Collectors.toList());
- return validPorts.stream()
- .map(port -> new PortDTO(port.getPortId(), port.getName(), port.getPortType()))
- .collect(Collectors.toList());
- }
- /**
- * 获取有效的船公司列表(RecordStatus=1且关联到CustomerType=2的记录)
- *
- * @return 船公司DTO列表
- */
- public List<ShippingCompanyDTO> getValidShippingCompanies() {
- // 查询CustomerType=2的所有客户ID
- List<BusCustomerCustomerType> customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2);
- Set<String> shippingCompanyCustomerIds = customerTypeRecords.stream()
- .map(BusCustomerCustomerType::getCustomerId)
- .collect(Collectors.toSet());
- // 查询所有有效的船公司业务记录(RecordStatus=1)
- List<BusCustomerCompanyBusiness> validBusinessRecords = busCustomerCompanyBusinessRepository.findAll().stream()
- .filter(business -> business.getRecordStatus() != null && business.getRecordStatus() == 1)
- .filter(business -> shippingCompanyCustomerIds.contains(business.getCustomerCompanyId())) // 确保是船公司类型
- .collect(Collectors.toList());
- // 获取这些业务记录对应的客户公司ID
- Set<String> customerCompanyIds = validBusinessRecords.stream()
- .map(BusCustomerCompanyBusiness::getCustomerCompanyId)
- .collect(Collectors.toSet());
- // 查询对应的客户公司信息
- List<BusCustomerCompany> shippingCompanies = busCustomerCompanyRepository.findAll().stream()
- .filter(company -> customerCompanyIds.contains(company.getCustomerCompanyId()))
- .collect(Collectors.toList());
- return shippingCompanies.stream()
- .map(company -> new ShippingCompanyDTO(
- company.getCustomerCompanyId(),
- company.getName(),
- company.getContact(),
- company.getTel(),
- company.getMobile()))
- .collect(Collectors.toList());
- }
- /**
- * 搜索船公司(支持分页和模糊搜索)
- *
- * @param companyName 船公司名称(支持模糊搜索)
- * @param pageable 分页参数
- * @return 船公司分页列表
- */
- public Page<ShippingCompanyDTO> searchShippingCompanies(String companyName, Pageable pageable) {
- // 查询CustomerType=2的所有客户ID
- List<BusCustomerCustomerType> customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2);
- Set<String> shippingCompanyCustomerIds = customerTypeRecords.stream()
- .map(BusCustomerCustomerType::getCustomerId)
- .collect(Collectors.toSet());
- // 查询所有有效的船公司业务记录(RecordStatus=1)
- List<BusCustomerCompanyBusiness> validBusinessRecords = busCustomerCompanyBusinessRepository.findAll().stream()
- .filter(business -> business.getRecordStatus() != null && business.getRecordStatus() == 1)
- .filter(business -> shippingCompanyCustomerIds.contains(business.getCustomerCompanyId()))
- .collect(Collectors.toList());
- // 获取这些业务记录对应的客户公司ID
- Set<String> customerCompanyIds = validBusinessRecords.stream()
- .map(BusCustomerCompanyBusiness::getCustomerCompanyId)
- .collect(Collectors.toSet());
- // 查询对应的客户公司信息(支持模糊搜索)
- List<BusCustomerCompany> allCompanies;
- if (companyName != null && !companyName.isEmpty()) {
- allCompanies = busCustomerCompanyRepository.findByNameContainingAndRecordStatus(
- companyName, 1);
- } else {
- allCompanies = busCustomerCompanyRepository.findAll();
- }
- // 过滤出船公司类型的数据并转换为DTO
- List<ShippingCompanyDTO> filteredCompanies = allCompanies.stream()
- .filter(company -> customerCompanyIds.contains(company.getCustomerCompanyId()))
- .map(company -> new ShippingCompanyDTO(
- company.getCustomerCompanyId(),
- company.getName(),
- company.getContact(),
- company.getTel(),
- company.getMobile()))
- .collect(Collectors.toList());
- // 手动分页
- int pageSize = pageable.getPageSize();
- int currentPage = pageable.getPageNumber();
- int start = currentPage * pageSize;
- int end = Math.min(start + pageSize, filteredCompanies.size());
- List<ShippingCompanyDTO> pageContent = filteredCompanies.subList(start, end);
- // 创建新的Page对象
- return new org.springframework.data.domain.PageImpl<>(
- pageContent,
- pageable,
- filteredCompanies.size()
- );
- }
- }
|