| | |
| | | package com.vincent.rsf.server.manager.service; |
| | | |
| | | import com.vincent.rsf.server.manager.mapper.MatnrMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * cus_item_sync_view 查询;配置了副库 url 时走 JdbcTemplate,否则走主库 Mapper |
| | | * cus_item_sync_view 查询;仅使用副库视图 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class CusItemSyncViewQueryService { |
| | | |
| | | @Autowired(required = false) |
| | | @Qualifier("cusItemSyncJdbcTemplate") |
| | | private JdbcTemplate cusItemSyncJdbcTemplate; |
| | | |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | |
| | | @Autowired |
| | | private MatnrMapper matnrMapper; |
| | | |
| | | /** 当前视图查询实际使用的数据源说明 */ |
| | | public String effectiveDataSourceLabel() { |
| | | return cusItemSyncJdbcTemplate != null ? "cus-item-sync" : "primary"; |
| | | return cusItemSyncJdbcTemplate != null ? "cus-item-sync" : "none"; |
| | | } |
| | | |
| | | /** |
| | | * 取视图前若干行,用于连通性验证(与业务查询同一套路由:副库优先) |
| | | * 取视图前若干行 |
| | | */ |
| | | public List<Map<String, Object>> probeSample(int limit) { |
| | | if (cusItemSyncJdbcTemplate == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | int n = Math.min(50, Math.max(1, limit)); |
| | | JdbcTemplate tpl = cusItemSyncJdbcTemplate != null ? cusItemSyncJdbcTemplate : jdbcTemplate; |
| | | return tpl.queryForList( |
| | | return cusItemSyncJdbcTemplate.queryForList( |
| | | "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view LIMIT " + n); |
| | | } |
| | | |
| | |
| | | if (codes.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | if (cusItemSyncJdbcTemplate != null) { |
| | | String placeholders = String.join(",", Collections.nCopies(codes.size(), "?")); |
| | | String sql = "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view WHERE item_no IN (" + placeholders + ")"; |
| | | return cusItemSyncJdbcTemplate.queryForList(sql, codes.toArray()); |
| | | if (cusItemSyncJdbcTemplate == null) { |
| | | log.warn("cus-item-sync 数据源未配置,跳过视图查询"); |
| | | return Collections.emptyList(); |
| | | } |
| | | return matnrMapper.selectByCusItemSyncView(codes); |
| | | String placeholders = String.join(",", Collections.nCopies(codes.size(), "?")); |
| | | String sql = "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view WHERE item_no IN (" + placeholders + ")"; |
| | | return cusItemSyncJdbcTemplate.queryForList(sql, codes.toArray()); |
| | | } |
| | | } |