Преглед изворни кода

修复船公司查询逻辑,使用Bus_CustomerCompanyBusiness表查询

修复内容:
1. 修改CommonDataService使用JdbcTemplate执行SQL查询
2. 修改查询逻辑,使用Bus_CustomerCompanyBusiness表关联Bus_CustomerCompany和Bus_Customer_CustomerType表
3. 添加@Qualifier("readJdbcTemplate")指定使用读数据源
4. 修改PilotPlan.vue,使船公司查询条件始终显示(row2Visible返回true)
5. 修复JPA查询语法错误,使用原生SQL查询

测试结果:
- 船公司下拉框正常显示
- 初始加载10条记录
- 滚动加载功能正常(每次加载10条)
- 搜索功能正常
- 总共有126条船公司记录,与数据库查询结果一致
heyiwen пре 2 недеља
родитељ
комит
968fd853b0

+ 18 - 18
JavaBackend/src/main/java/com/lianda/backend/repository/BusCustomerCompanyBusinessRepository.java

@@ -32,25 +32,25 @@ public interface BusCustomerCompanyBusinessRepository extends JpaRepository<BusC
      * 根据查询词搜索符合条件的客户公司业务 - 通过联合查询
      * 查找CustomerType=2(船公司)且RecordStatus=1的记录,并支持模糊搜索
      */
-    @Query("SELECT new com.lianda.backend.dto.CustomerCompanyBusinessSearchResult(" +
-           "b.customerCompanyBusinessId, c.name, b.businessCode) " +
-           "FROM BusCustomerCompanyBusiness b " +
-           "INNER JOIN BusCustomerCustomerType t ON b.customerCompanyId = t.customerId " +
-           "INNER JOIN BusCustomerCompany c ON b.customerCompanyId = c.customerCompanyId " +
-           "WHERE t.customerType = 2 " +
-           "AND b.recordStatus = 1 " +
-           "AND c.recordStatus = 1 " +
-           "AND (COALESCE(:query, '') = '' OR LOWER(b.businessCode) LIKE CONCAT('%', LOWER(:query), '%') OR LOWER(c.name) LIKE CONCAT('%', LOWER(:query), '%'))")
+    @Query(value = "SELECT new com.lianda.backend.dto.CustomerCompanyBusinessSearchResult(" +
+           "ccb.CustomerCompanyBusinessId, cc.Name, ccb.BusinessCode) " +
+           "FROM Bus_CustomerCompanyBusiness ccb " +
+           "INNER JOIN Bus_CustomerCompany cc ON ccb.CustomerCompanyID = cc.CustomerCompanyID " +
+           "INNER JOIN Bus_Customer_CustomerType cct ON cc.CustomerCompanyID = cct.CustomerID " +
+           "WHERE cct.CustomerType = 2 " +
+           "AND ccb.RecordStatus = 1 " +
+           "AND cc.RecordStatus = 1 " +
+           "AND (COALESCE(:query, '') = '' OR LOWER(ccb.BusinessCode) LIKE CONCAT('%', LOWER(:query), '%') OR LOWER(cc.Name) LIKE CONCAT('%', LOWER(:query), '%'))", nativeQuery = true)
     List<CustomerCompanyBusinessSearchResult> searchShippingCompanies(@Param("query") String query);
     
-    @Query("SELECT new com.lianda.backend.dto.CustomerCompanyBusinessSearchResult(" +
-           "b.customerCompanyBusinessId, c.name, b.businessCode) " +
-           "FROM BusCustomerCompanyBusiness b " +
-           "INNER JOIN BusCustomerCustomerType t ON b.customerCompanyId = t.customerId " +
-           "INNER JOIN BusCustomerCompany c ON b.customerCompanyId = c.customerCompanyId " +
-           "WHERE t.customerType = 2 " +
-           "AND b.recordStatus = 1 " +
-           "AND c.recordStatus = 1 " +
-           "AND (COALESCE(:query, '') = '' OR LOWER(b.businessCode) LIKE CONCAT('%', LOWER(:query), '%') OR LOWER(c.name) LIKE CONCAT('%', LOWER(:query), '%'))")
+    @Query(value = "SELECT new com.lianda.backend.dto.CustomerCompanyBusinessSearchResult(" +
+           "ccb.CustomerCompanyBusinessId, cc.Name, ccb.BusinessCode) " +
+           "FROM Bus_CustomerCompanyBusiness ccb " +
+           "INNER JOIN Bus_CustomerCompany cc ON ccb.CustomerCompanyID = cc.CustomerCompanyID " +
+           "INNER JOIN Bus_Customer_CustomerType cct ON cc.CustomerCompanyID = cct.CustomerID " +
+           "WHERE cct.CustomerType = 2 " +
+           "AND ccb.RecordStatus = 1 " +
+           "AND cc.RecordStatus = 1 " +
+           "AND (COALESCE(:query, '') = '' OR LOWER(ccb.BusinessCode) LIKE CONCAT('%', LOWER(:query), '%') OR LOWER(cc.Name) LIKE CONCAT('%', LOWER(:query), '%'))", nativeQuery = true)
     Page<CustomerCompanyBusinessSearchResult> searchShippingCompaniesWithPage(@Param("query") String query, Pageable pageable);
 }

+ 58 - 40
JavaBackend/src/main/java/com/lianda/backend/service/CommonDataService.java

@@ -4,24 +4,21 @@ 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.beans.factory.annotation.Qualifier;
 import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
-import org.springframework.data.domain.Sort;
+import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -38,10 +35,8 @@ public class CommonDataService {
     private BusCustomerCompanyBusinessRepository busCustomerCompanyBusinessRepository;
 
     @Autowired
-    private BusCustomerCompanyRepository busCustomerCompanyRepository;
-
-    @Autowired
-    private BusCustomerCustomerTypeRepository busCustomerCustomerTypeRepository;
+    @Qualifier("readJdbcTemplate")
+    private JdbcTemplate jdbcTemplate;
 
     /**
      * 获取有效的港口列表(RecordStatus=1)
@@ -98,46 +93,69 @@ public class CommonDataService {
     }
 
     /**
-     * 搜索船公司(支持分页和模糊搜索)
-     *
+     * 搜索船公司列表(支持分页和模糊搜索)
      * @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());
-
-        // 查询对应的客户公司信息(支持模糊搜索和分页)
-        Page<BusCustomerCompany> companyPage;
+        // 构建SQL查询
+        StringBuilder sql = new StringBuilder(
+            "SELECT ccb.CustomerCompanyBusinessId, cc.Name, ccb.BusinessCode " +
+            "FROM Bus_CustomerCompanyBusiness ccb " +
+            "INNER JOIN Bus_CustomerCompany cc ON ccb.CustomerCompanyID = cc.CustomerCompanyID " +
+            "INNER JOIN Bus_Customer_CustomerType cct ON cc.CustomerCompanyID = cct.CustomerID " +
+            "WHERE cct.CustomerType = 2 " +
+            "AND ccb.RecordStatus = 1 " +
+            "AND cc.RecordStatus = 1"
+        );
+        
+        // 添加模糊搜索条件
+        if (companyName != null && !companyName.isEmpty()) {
+            sql.append(" AND (LOWER(ccb.BusinessCode) LIKE CONCAT('%', LOWER(?), '%') OR LOWER(cc.Name) LIKE CONCAT('%', LOWER(?), '%'))");
+        }
+        
+        // 查询总数
+        String countSql = "SELECT COUNT(*) " + sql.substring(sql.indexOf("FROM"));
+        Integer total;
         if (companyName != null && !companyName.isEmpty()) {
-            companyPage = busCustomerCompanyRepository.findByNameContainingAndRecordStatus(
-                    companyName, 1, pageable);
+            total = jdbcTemplate.queryForObject(countSql, Integer.class, companyName, companyName);
         } else {
-            Sort sort = Sort.by(Sort.Direction.ASC, "name");
-            Pageable sortedPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
-            companyPage = busCustomerCompanyRepository.findByRecordStatus(1, sortedPageable);
+            total = jdbcTemplate.queryForObject(countSql, Integer.class);
         }
-
-        // 过滤出船公司类型的数据并转换为DTO
-        List<ShippingCompanyDTO> filteredCompanies = companyPage.getContent().stream()
-                .filter(company -> shippingCompanyCustomerIds.contains(company.getCustomerCompanyId()))
-                .map(company -> new ShippingCompanyDTO(
-                        company.getCustomerCompanyId(),
-                        company.getName(),
-                        company.getContact(),
-                        company.getTel(),
-                        company.getMobile()))
-                .collect(Collectors.toList());
-
+        
+        // 添加排序和分页
+        sql.append(" ORDER BY cc.Name");
+        sql.append(" LIMIT ").append(pageable.getPageSize());
+        sql.append(" OFFSET ").append(pageable.getOffset());
+        
+        // 查询数据
+        List<ShippingCompanyDTO> shippingCompanies;
+        if (companyName != null && !companyName.isEmpty()) {
+            shippingCompanies = jdbcTemplate.query(sql.toString(), 
+                (rs, rowNum) -> new ShippingCompanyDTO(
+                    rs.getString("CustomerCompanyBusinessId"),
+                    rs.getString("Name"),
+                    null,
+                    null,
+                    null
+                ), companyName, companyName);
+        } else {
+            shippingCompanies = jdbcTemplate.query(sql.toString(), 
+                (rs, rowNum) -> new ShippingCompanyDTO(
+                    rs.getString("CustomerCompanyBusinessId"),
+                    rs.getString("Name"),
+                    null,
+                    null,
+                    null
+                ));
+        }
+        
         // 创建新的Page对象
-        return new org.springframework.data.domain.PageImpl<>(
-                filteredCompanies,
+        return new PageImpl<>(
+                shippingCompanies,
                 pageable,
-                companyPage.getTotalElements()
+                total
         );
     }
 }

+ 2 - 2
vue-frontend/src/components/PilotPlan.vue

@@ -338,7 +338,7 @@ export default {
       return true;
     },
     row2Visible() {
-      return this.showAllSearch;
+      return true;
     },
     row3Visible() {
       return this.showAllSearch;
@@ -778,7 +778,7 @@ h3 {
   max-height: 300px;
   overflow-y: auto;
 }
-</style>
+
 .preview-dialog {
   --el-dialog-margin-top: 5vh;
 }