Procházet zdrojové kódy

修复船公司下拉框无法显示的问题

- 修改ShipCompanyController的list接口支持分页和搜索参数
- 添加searchShippingCompanies方法支持分页和模糊搜索
- 在BusCustomerCompanyRepository添加分页查询方法
- 修复前端期望的分页格式响应问题
heyiwen před 6 hodinami
rodič
revize
5e502d1ae7

+ 16 - 4
JavaBackend/src/main/java/com/lianda/backend/controller/ShipCompanyController.java

@@ -3,6 +3,10 @@ package com.lianda.backend.controller;
 import com.lianda.backend.dto.ShippingCompanyDTO;
 import com.lianda.backend.service.CommonDataService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
@@ -20,12 +24,20 @@ public class ShipCompanyController {
 
     /**
      * 获取有效的船公司列表(RecordStatus=1且关联到CustomerType=2的记录)
+     * 支持分页和搜索
      *
-     * @return 船公司列表,包含companyId和companyName字段
+     * @param companyName 船公司名称(支持模糊搜索)
+     * @param page 页码(从1开始)
+     * @param pageSize 每页数量
+     * @return 船公司分页列表,包含companyId和companyName字段
      */
     @GetMapping("/list")
-    public ResponseEntity<List<ShippingCompanyDTO>> getShipCompanyList() {
-        List<ShippingCompanyDTO> companyList = commonDataService.getValidShippingCompanies();
-        return ResponseEntity.ok(companyList);
+    public ResponseEntity<Page<ShippingCompanyDTO>> getShipCompanyList(
+            @RequestParam(required = false) String companyName,
+            @RequestParam(defaultValue = "1") int page,
+            @RequestParam(defaultValue = "10") int pageSize) {
+        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by("companyName").ascending());
+        Page<ShippingCompanyDTO> companyPage = commonDataService.searchShippingCompanies(companyName, pageable);
+        return ResponseEntity.ok(companyPage);
     }
 }

+ 7 - 0
JavaBackend/src/main/java/com/lianda/backend/repository/BusCustomerCompanyRepository.java

@@ -1,6 +1,8 @@
 package com.lianda.backend.repository;
 
 import com.lianda.backend.model.BusCustomerCompany;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 import java.util.List;
@@ -37,4 +39,9 @@ public interface BusCustomerCompanyRepository extends JpaRepository<BusCustomerC
      * 根据名称列表查找客户公司
      */
     List<BusCustomerCompany> findByNameIn(List<String> names);
+    
+    /**
+     * 根据名称模糊查找和记录状态查找客户公司(分页)
+     */
+    Page<BusCustomerCompany> findByNameContainingAndRecordStatus(String name, Integer recordStatus, Pageable pageable);
 }

+ 55 - 0
JavaBackend/src/main/java/com/lianda/backend/service/CommonDataService.java

@@ -13,6 +13,8 @@ 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;
@@ -91,4 +93,57 @@ public class CommonDataService {
                         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());
+
+        // 查询对应的客户公司信息(支持模糊搜索和分页)
+        Page<BusCustomerCompany> companyPage;
+        if (companyName != null && !companyName.isEmpty()) {
+            companyPage = busCustomerCompanyRepository.findByNameContainingAndRecordStatus(
+                    companyName, 1, pageable);
+        } else {
+            companyPage = busCustomerCompanyRepository.findAll(pageable);
+        }
+
+        // 过滤出船公司类型的数据并转换为DTO
+        List<ShippingCompanyDTO> filteredCompanies = companyPage.getContent().stream()
+                .filter(company -> customerCompanyIds.contains(company.getCustomerCompanyId()))
+                .map(company -> new ShippingCompanyDTO(
+                        company.getCustomerCompanyId(),
+                        company.getName(),
+                        company.getContact(),
+                        company.getTel(),
+                        company.getMobile()))
+                .collect(Collectors.toList());
+
+        // 创建新的Page对象,只包含过滤后的数据
+        return new org.springframework.data.domain.PageImpl<>(
+                filteredCompanies,
+                pageable,
+                filteredCompanies.size()
+        );
+    }
 }