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 getValidPorts() { // 查询所有有效港口(RecordStatus=1) List 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 getValidShippingCompanies() { // 查询CustomerType=2的所有客户ID List customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2); Set shippingCompanyCustomerIds = customerTypeRecords.stream() .map(BusCustomerCustomerType::getCustomerId) .collect(Collectors.toSet()); // 查询所有有效的船公司业务记录(RecordStatus=1) List validBusinessRecords = busCustomerCompanyBusinessRepository.findAll().stream() .filter(business -> business.getRecordStatus() != null && business.getRecordStatus() == 1) .filter(business -> shippingCompanyCustomerIds.contains(business.getCustomerCompanyId())) // 确保是船公司类型 .collect(Collectors.toList()); // 获取这些业务记录对应的客户公司ID Set customerCompanyIds = validBusinessRecords.stream() .map(BusCustomerCompanyBusiness::getCustomerCompanyId) .collect(Collectors.toSet()); // 查询对应的客户公司信息 List 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 searchShippingCompanies(String companyName, Pageable pageable) { // 查询CustomerType=2的所有客户ID List customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2); Set shippingCompanyCustomerIds = customerTypeRecords.stream() .map(BusCustomerCustomerType::getCustomerId) .collect(Collectors.toSet()); // 查询所有有效的船公司业务记录(RecordStatus=1) List validBusinessRecords = busCustomerCompanyBusinessRepository.findAll().stream() .filter(business -> business.getRecordStatus() != null && business.getRecordStatus() == 1) .filter(business -> shippingCompanyCustomerIds.contains(business.getCustomerCompanyId())) .collect(Collectors.toList()); // 获取这些业务记录对应的客户公司ID Set customerCompanyIds = validBusinessRecords.stream() .map(BusCustomerCompanyBusiness::getCustomerCompanyId) .collect(Collectors.toSet()); // 查询对应的客户公司信息(支持模糊搜索) List allCompanies; if (companyName != null && !companyName.isEmpty()) { allCompanies = busCustomerCompanyRepository.findByNameContainingAndRecordStatus( companyName, 1); } else { allCompanies = busCustomerCompanyRepository.findAll(); } // 过滤出船公司类型的数据并转换为DTO List 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 pageContent = filteredCompanies.subList(start, end); // 创建新的Page对象 return new org.springframework.data.domain.PageImpl<>( pageContent, pageable, filteredCompanies.size() ); } }