CommonDataService.java 6.8 KB

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