|
|
@@ -2,160 +2,242 @@ 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.CustomerCompanyBusinessSearchResult;
|
|
|
import com.lianda.backend.dto.ShippingCompanyDTO;
|
|
|
-import com.lianda.backend.model.BusCustomerCompanyBusiness;
|
|
|
+import com.lianda.backend.model.DispBerthage;
|
|
|
+import com.lianda.backend.model.DispBerthageDictionary;
|
|
|
import com.lianda.backend.model.DispPort;
|
|
|
+import com.lianda.backend.model.DispPortDictionary;
|
|
|
import com.lianda.backend.repository.BusCustomerCompanyBusinessRepository;
|
|
|
+import com.lianda.backend.repository.DispBerthageDictionaryRepository;
|
|
|
+import com.lianda.backend.repository.DispBerthageRepository;
|
|
|
+import com.lianda.backend.repository.DispPortDictionaryRepository;
|
|
|
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.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-/**
|
|
|
- * 港口和船公司服务类
|
|
|
- */
|
|
|
@Service
|
|
|
-@DataSource(value = RoutingDataSourceConfig.DataSourceType.BRANCH) // 读取分支机构业务数据
|
|
|
public class CommonDataService {
|
|
|
|
|
|
@Autowired
|
|
|
- private DispPortRepository dispPortRepository;
|
|
|
+ private DispBerthageDictionaryRepository dispBerthageDictionaryRepository;
|
|
|
|
|
|
@Autowired
|
|
|
- private BusCustomerCompanyBusinessRepository busCustomerCompanyBusinessRepository;
|
|
|
+ private DispPortDictionaryRepository dispPortDictionaryRepository;
|
|
|
|
|
|
@Autowired
|
|
|
- @Qualifier("readJdbcTemplate")
|
|
|
- private JdbcTemplate jdbcTemplate;
|
|
|
+ private DispBerthageRepository dispBerthageRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DispPortRepository dispPortRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BusCustomerCompanyBusinessRepository busCustomerCompanyBusinessRepository;
|
|
|
|
|
|
/**
|
|
|
- * 获取有效的港口列表(RecordStatus=1)
|
|
|
- *
|
|
|
- * @return 港口DTO列表
|
|
|
+ * 泊位信息内部类,包含泊位ID和港口ID
|
|
|
*/
|
|
|
- public List<PortDTO> getValidPorts() {
|
|
|
- // 查询所有有效港口(RecordStatus=1)
|
|
|
- List<DispPort> 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());
|
|
|
+ public static class BerthageInfo {
|
|
|
+ String berthageId;
|
|
|
+ String portId;
|
|
|
+
|
|
|
+ BerthageInfo(String berthageId, String portId) {
|
|
|
+ this.berthageId = berthageId;
|
|
|
+ this.portId = portId;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取有效的船公司列表(RecordStatus=1且关联到CustomerType=2的记录)
|
|
|
- *
|
|
|
- * @return 船公司DTO列表
|
|
|
+ * 查找或创建泊位信息
|
|
|
+ * @param berthName 泊位名称
|
|
|
+ * @return 泊位信息
|
|
|
*/
|
|
|
- public List<ShippingCompanyDTO> getValidShippingCompanies() {
|
|
|
- // 查询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());
|
|
|
-
|
|
|
- // 查询对应的客户公司信息
|
|
|
- List<BusCustomerCompany> 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());
|
|
|
+ @DataSource(value = RoutingDataSourceConfig.DataSourceType.COMMON)
|
|
|
+ @Transactional
|
|
|
+ public BerthageInfo findOrCreateBerthageInfo(String berthName) {
|
|
|
+ String portId = null;
|
|
|
+ String berthageId = null;
|
|
|
+ String portName = null;
|
|
|
+ String actualBerthName = null;
|
|
|
+
|
|
|
+ // 第一步:通过Disp_BerthageDictionary的Keyword完全匹配
|
|
|
+ List<DispBerthageDictionary> berthageDictionaries = dispBerthageDictionaryRepository.findByKeyword(berthName);
|
|
|
+ if (!berthageDictionaries.isEmpty()) {
|
|
|
+ DispBerthageDictionary dict = berthageDictionaries.get(0);
|
|
|
+ berthageId = dict.getBerthageId();
|
|
|
+
|
|
|
+ // 通过BerthageID查找对应的泊位记录
|
|
|
+ DispBerthage berthage = dispBerthageRepository.findById(berthageId).orElse(null);
|
|
|
+ if (berthage != null && berthage.getPortId() != null) {
|
|
|
+ DispPort port = dispPortRepository.findById(berthage.getPortId()).orElse(null);
|
|
|
+ if (port != null && port.getPortType() != null && port.getPortType() == 1) {
|
|
|
+ // 是码头,使用这个PortID
|
|
|
+ portId = berthage.getPortId();
|
|
|
+ portName = port.getName();
|
|
|
+ actualBerthName = berthage.getName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第二步:如果第一步无法匹配,用Disp_PortDictionary的PortKeyword模糊匹配
|
|
|
+ if (portId == null) {
|
|
|
+ List<DispPortDictionary> portDictionaries = dispPortDictionaryRepository.findByPortKeywordContaining(berthName);
|
|
|
+ if (!portDictionaries.isEmpty()) {
|
|
|
+ DispPortDictionary portDict = portDictionaries.get(0);
|
|
|
+ portName = portDict.getName();
|
|
|
+ // 泊位名称就是除港口名称外的剩余部分
|
|
|
+ if (berthName.startsWith(portName)) {
|
|
|
+ actualBerthName = berthName.substring(portName.length()).trim();
|
|
|
+ } else {
|
|
|
+ actualBerthName = berthName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找对应的Port和Berthage记录
|
|
|
+ List<DispPort> ports = dispPortRepository.findByName(portName);
|
|
|
+ if (!ports.isEmpty()) {
|
|
|
+ portId = ports.get(0).getPortId();
|
|
|
+ List<DispBerthage> berthages = dispBerthageRepository.findByNameAndPortId(actualBerthName, portId);
|
|
|
+ if (!berthages.isEmpty()) {
|
|
|
+ berthageId = berthages.get(0).getBerthageId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第三步:如果第二步没有找到,检查是否为"数字+#号"格式
|
|
|
+ if (portId == null) {
|
|
|
+ java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^(.+?)\\s*(\\d+#)$");
|
|
|
+ java.util.regex.Matcher matcher = pattern.matcher(berthName);
|
|
|
+ if (matcher.matches()) {
|
|
|
+ portName = matcher.group(1).trim();
|
|
|
+ actualBerthName = matcher.group(2);
|
|
|
+
|
|
|
+ // 查找对应的Port和Berthage记录
|
|
|
+ List<DispPort> ports = dispPortRepository.findByName(portName);
|
|
|
+ if (!ports.isEmpty()) {
|
|
|
+ DispPort port = ports.get(0);
|
|
|
+ if (port.getPortType() != null && port.getPortType() == 1) {
|
|
|
+ // 确保是码头
|
|
|
+ portId = port.getPortId();
|
|
|
+ List<DispBerthage> berthages = dispBerthageRepository.findByNameAndPortId(actualBerthName, portId);
|
|
|
+ if (!berthages.isEmpty()) {
|
|
|
+ berthageId = berthages.get(0).getBerthageId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第四步:如果还没找到,说明是锚地
|
|
|
+ if (portId == null) {
|
|
|
+ portName = berthName;
|
|
|
+ actualBerthName = berthName;
|
|
|
+
|
|
|
+ // 查找或创建锚地Port记录(PortType=2)
|
|
|
+ List<DispPort> ports = dispPortRepository.findByName(portName);
|
|
|
+ if (!ports.isEmpty()) {
|
|
|
+ portId = ports.get(0).getPortId();
|
|
|
+ } else {
|
|
|
+ DispPort port = new DispPort();
|
|
|
+ port.setPortId(UUID.randomUUID().toString().replace("-", ""));
|
|
|
+ port.setName(portName);
|
|
|
+ port.setPortType(2); // 锚地
|
|
|
+ port.setRecordStatus(1);
|
|
|
+ port.setCreateTime(new Date());
|
|
|
+ port.setCreateUserId(getCurrentUserId());
|
|
|
+ port = dispPortRepository.save(port);
|
|
|
+ portId = port.getPortId();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找或创建锚地Berthage记录
|
|
|
+ List<DispBerthage> berthages = dispBerthageRepository.findByNameAndPortId(actualBerthName, portId);
|
|
|
+ if (!berthages.isEmpty()) {
|
|
|
+ berthageId = berthages.get(0).getBerthageId();
|
|
|
+ } else {
|
|
|
+ DispBerthage berthage = new DispBerthage();
|
|
|
+ berthage.setBerthageId(UUID.randomUUID().toString().replace("-", ""));
|
|
|
+ berthage.setName(actualBerthName);
|
|
|
+ berthage.setPortId(portId);
|
|
|
+ berthage.setRecordStatus(1);
|
|
|
+ berthage.setCreateTime(new Date());
|
|
|
+ berthage.setCreateUserId(getCurrentUserId());
|
|
|
+ berthage = dispBerthageRepository.save(berthage);
|
|
|
+ berthageId = berthage.getBerthageId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第五步:如果找到了portName和actualBerthName,但数据库中没有记录,则新增
|
|
|
+ if (portId != null && berthageId == null && portName != null && actualBerthName != null) {
|
|
|
+ // 创建Port记录(如果不存在)
|
|
|
+ List<DispPort> ports = dispPortRepository.findByName(portName);
|
|
|
+ if (!ports.isEmpty()) {
|
|
|
+ portId = ports.get(0).getPortId();
|
|
|
+ } else {
|
|
|
+ DispPort port = new DispPort();
|
|
|
+ port.setPortId(UUID.randomUUID().toString().replace("-", ""));
|
|
|
+ port.setName(portName);
|
|
|
+ port.setPortType(1); // 码头
|
|
|
+ port.setRecordStatus(1);
|
|
|
+ port.setCreateTime(new Date());
|
|
|
+ port.setCreateUserId("system");
|
|
|
+ port = dispPortRepository.save(port);
|
|
|
+ portId = port.getPortId();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建Berthage记录
|
|
|
+ DispBerthage berthage = new DispBerthage();
|
|
|
+ berthage.setBerthageId(UUID.randomUUID().toString().replace("-", ""));
|
|
|
+ berthage.setName(actualBerthName);
|
|
|
+ berthage.setPortId(portId);
|
|
|
+ berthage.setRecordStatus(1);
|
|
|
+ berthage.setCreateTime(new Date());
|
|
|
+ berthage.setCreateUserId("system");
|
|
|
+ berthage = dispBerthageRepository.save(berthage);
|
|
|
+ berthageId = berthage.getBerthageId();
|
|
|
+ }
|
|
|
+
|
|
|
+ return new BerthageInfo(berthageId, portId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 搜索船公司列表(支持分页和模糊搜索)
|
|
|
+ * 搜索船公司列表
|
|
|
* @param companyName 船公司名称(支持模糊搜索)
|
|
|
* @param pageable 分页参数
|
|
|
* @return 船公司分页列表
|
|
|
*/
|
|
|
+ @DataSource(value = RoutingDataSourceConfig.DataSourceType.BRANCH)
|
|
|
+ @Transactional(readOnly = true)
|
|
|
public Page<ShippingCompanyDTO> searchShippingCompanies(String companyName, Pageable pageable) {
|
|
|
- // 构建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"
|
|
|
- );
|
|
|
+ Page<CustomerCompanyBusinessSearchResult> results = busCustomerCompanyBusinessRepository.searchShippingCompaniesWithPage(companyName, pageable);
|
|
|
|
|
|
- // 添加模糊搜索条件
|
|
|
- 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()) {
|
|
|
- total = jdbcTemplate.queryForObject(countSql, Integer.class, companyName, companyName);
|
|
|
- } else {
|
|
|
- total = jdbcTemplate.queryForObject(countSql, Integer.class);
|
|
|
- }
|
|
|
+ List<ShippingCompanyDTO> dtos = results.getContent().stream()
|
|
|
+ .map(result -> new ShippingCompanyDTO(
|
|
|
+ result.getCustomerCompanyBusinessID(),
|
|
|
+ result.getCustomerName(),
|
|
|
+ result.getBusinessCode(),
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null
|
|
|
+ ))
|
|
|
+ .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 PageImpl<>(
|
|
|
- shippingCompanies,
|
|
|
- pageable,
|
|
|
- total
|
|
|
- );
|
|
|
+ return new org.springframework.data.domain.PageImpl<>(dtos, pageable, results.getTotalElements());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前登录用户ID
|
|
|
+ * @return 用户ID
|
|
|
+ */
|
|
|
+ private String getCurrentUserId() {
|
|
|
+ String userId = com.lianda.backend.util.CurrentUserUtil.getCurrentUserId();
|
|
|
+ return userId != null ? userId : "admin"; // 如果获取不到当前用户ID,则使用"admin"
|
|
|
}
|
|
|
-}
|
|
|
+}
|