CommonDataService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.lianda.backend.service;
  2. import com.lianda.backend.config.DataSource;
  3. import com.lianda.backend.config.RoutingDataSourceConfig;
  4. import com.lianda.backend.dto.PortDTO;
  5. import com.lianda.backend.dto.ShippingCompanyDTO;
  6. import com.lianda.backend.model.BusCustomerCompany;
  7. import com.lianda.backend.model.BusCustomerCompanyBusiness;
  8. import com.lianda.backend.model.BusCustomerCustomerType;
  9. import com.lianda.backend.model.DispPort;
  10. import com.lianda.backend.repository.BusCustomerCompanyBusinessRepository;
  11. import com.lianda.backend.repository.BusCustomerCompanyRepository;
  12. import com.lianda.backend.repository.BusCustomerCustomerTypeRepository;
  13. import com.lianda.backend.repository.DispPortRepository;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.PageRequest;
  17. import org.springframework.data.domain.Pageable;
  18. import org.springframework.data.domain.Sort;
  19. import org.springframework.stereotype.Service;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.stream.Collectors;
  24. /**
  25. * 港口和船公司服务类
  26. */
  27. @Service
  28. @DataSource(value = RoutingDataSourceConfig.DataSourceType.BRANCH) // 读取分支机构业务数据
  29. public class CommonDataService {
  30. @Autowired
  31. private DispPortRepository dispPortRepository;
  32. @Autowired
  33. private BusCustomerCompanyBusinessRepository busCustomerCompanyBusinessRepository;
  34. @Autowired
  35. private BusCustomerCompanyRepository busCustomerCompanyRepository;
  36. @Autowired
  37. private BusCustomerCustomerTypeRepository busCustomerCustomerTypeRepository;
  38. /**
  39. * 获取有效的港口列表(RecordStatus=1)
  40. *
  41. * @return 港口DTO列表
  42. */
  43. public List<PortDTO> getValidPorts() {
  44. // 查询所有有效港口(RecordStatus=1)
  45. List<DispPort> validPorts = dispPortRepository.findAll().stream()
  46. .filter(port -> port.getRecordStatus() != null && port.getRecordStatus() == 1)
  47. .collect(Collectors.toList());
  48. return validPorts.stream()
  49. .map(port -> new PortDTO(port.getPortId(), port.getName(), port.getPortType()))
  50. .collect(Collectors.toList());
  51. }
  52. /**
  53. * 获取有效的船公司列表(RecordStatus=1且关联到CustomerType=2的记录)
  54. *
  55. * @return 船公司DTO列表
  56. */
  57. public List<ShippingCompanyDTO> getValidShippingCompanies() {
  58. // 查询CustomerType=2的所有客户ID
  59. List<BusCustomerCustomerType> customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2);
  60. Set<String> shippingCompanyCustomerIds = customerTypeRecords.stream()
  61. .map(BusCustomerCustomerType::getCustomerId)
  62. .collect(Collectors.toSet());
  63. // 查询所有有效的船公司业务记录(RecordStatus=1)
  64. List<BusCustomerCompanyBusiness> validBusinessRecords = busCustomerCompanyBusinessRepository.findAll().stream()
  65. .filter(business -> business.getRecordStatus() != null && business.getRecordStatus() == 1)
  66. .filter(business -> shippingCompanyCustomerIds.contains(business.getCustomerCompanyId())) // 确保是船公司类型
  67. .collect(Collectors.toList());
  68. // 获取这些业务记录对应的客户公司ID
  69. Set<String> customerCompanyIds = validBusinessRecords.stream()
  70. .map(BusCustomerCompanyBusiness::getCustomerCompanyId)
  71. .collect(Collectors.toSet());
  72. // 查询对应的客户公司信息
  73. List<BusCustomerCompany> shippingCompanies = busCustomerCompanyRepository.findAll().stream()
  74. .filter(company -> customerCompanyIds.contains(company.getCustomerCompanyId()))
  75. .collect(Collectors.toList());
  76. return shippingCompanies.stream()
  77. .map(company -> new ShippingCompanyDTO(
  78. company.getCustomerCompanyId(),
  79. company.getName(),
  80. company.getContact(),
  81. company.getTel(),
  82. company.getMobile()))
  83. .collect(Collectors.toList());
  84. }
  85. /**
  86. * 搜索船公司(支持分页和模糊搜索)
  87. *
  88. * @param companyName 船公司名称(支持模糊搜索)
  89. * @param pageable 分页参数
  90. * @return 船公司分页列表
  91. */
  92. public Page<ShippingCompanyDTO> searchShippingCompanies(String companyName, Pageable pageable) {
  93. // 查询CustomerType=2的所有客户ID
  94. List<BusCustomerCustomerType> customerTypeRecords = busCustomerCustomerTypeRepository.findByCustomerType(2);
  95. Set<String> shippingCompanyCustomerIds = customerTypeRecords.stream()
  96. .map(BusCustomerCustomerType::getCustomerId)
  97. .collect(Collectors.toSet());
  98. // 查询对应的客户公司信息(支持模糊搜索和分页)
  99. Page<BusCustomerCompany> companyPage;
  100. if (companyName != null && !companyName.isEmpty()) {
  101. companyPage = busCustomerCompanyRepository.findByNameContainingAndRecordStatus(
  102. companyName, 1, pageable);
  103. } else {
  104. Sort sort = Sort.by(Sort.Direction.ASC, "name");
  105. Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
  106. companyPage = busCustomerCompanyRepository.findByRecordStatus(1, sortedPageable);
  107. }
  108. // 过滤出船公司类型的数据并转换为DTO
  109. List<ShippingCompanyDTO> filteredCompanies = companyPage.getContent().stream()
  110. .filter(company -> shippingCompanyCustomerIds.contains(company.getCustomerCompanyId()))
  111. .map(company -> new ShippingCompanyDTO(
  112. company.getCustomerCompanyId(),
  113. company.getName(),
  114. company.getContact(),
  115. company.getTel(),
  116. company.getMobile()))
  117. .collect(Collectors.toList());
  118. // 创建新的Page对象
  119. return new org.springframework.data.domain.PageImpl<>(
  120. filteredCompanies,
  121. pageable,
  122. companyPage.getTotalElements()
  123. );
  124. }
  125. }