| | |
| | | package com.vincent.rsf.server.manager.service; |
| | | |
| | | import com.vincent.rsf.server.common.datasource.DataSourceNames; |
| | | import com.vincent.rsf.server.common.datasource.UseDataSource; |
| | | 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 org.springframework.beans.factory.ObjectProvider; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | |
| | | @Slf4j |
| | | public class CusItemSyncViewQueryService { |
| | | |
| | | @Autowired(required = false) |
| | | @Qualifier("cusItemSyncJdbcTemplate") |
| | | private JdbcTemplate cusItemSyncJdbcTemplate; |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | |
| | | @Autowired |
| | | @Qualifier("cusItemSyncDataSource") |
| | | private ObjectProvider<DataSource> cusItemSyncDataSourceProvider; |
| | | |
| | | /** 当前视图查询实际使用的数据源说明 */ |
| | | public String effectiveDataSourceLabel() { |
| | | return cusItemSyncJdbcTemplate != null ? "cus-item-sync" : "none"; |
| | | return cusItemSyncDataSourceProvider.getIfAvailable() != null ? "cus-item-sync" : "none"; |
| | | } |
| | | |
| | | /** |
| | | * 取视图前若干行 |
| | | */ |
| | | @UseDataSource(DataSourceNames.CUS_ITEM_SYNC) |
| | | public List<Map<String, Object>> probeSample(int limit) { |
| | | if (cusItemSyncJdbcTemplate == null) { |
| | | if (cusItemSyncDataSourceProvider.getIfAvailable() == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | int n = Math.min(50, Math.max(1, limit)); |
| | | return cusItemSyncJdbcTemplate.queryForList( |
| | | return jdbcTemplate.queryForList( |
| | | "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view LIMIT " + n); |
| | | } |
| | | |
| | | @UseDataSource(DataSourceNames.CUS_ITEM_SYNC) |
| | | public List<Map<String, Object>> listByItemNos(Collection<String> itemNos) { |
| | | if (itemNos == null || itemNos.isEmpty()) { |
| | | return Collections.emptyList(); |
| | |
| | | if (codes.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | if (cusItemSyncJdbcTemplate == null) { |
| | | if (cusItemSyncDataSourceProvider.getIfAvailable() == null) { |
| | | log.warn("cus-item-sync 数据源未配置,跳过视图查询"); |
| | | return Collections.emptyList(); |
| | | } |
| | | 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()); |
| | | return jdbcTemplate.queryForList(sql, codes.toArray()); |
| | | } |
| | | } |