| | |
| | | package com.vincent.rsf.server.manager.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.vincent.rsf.server.common.datasource.DataSourceNames; |
| | | import com.vincent.rsf.server.common.datasource.UseDataSource; |
| | | import com.vincent.rsf.server.manager.entity.CusItemSyncView; |
| | | import com.vincent.rsf.server.manager.mapper.CusItemSyncViewMapper; |
| | | 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 org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.sql.DataSource; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * cus_item_sync_view 查询;仅使用副库视图 |
| | | * cus_item_sync_view 查询;仅使用副库视图。查询不启事务,{@code NOT_SUPPORTED} 挂起外层以利路由副库。 |
| | | */ |
| | | @UseDataSource(DataSourceNames.DJ_CLOUD_WMS) |
| | | @Service |
| | | @Slf4j |
| | | public class CusItemSyncViewQueryService { |
| | | |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | private CusItemSyncViewMapper cusItemSyncViewMapper; |
| | | |
| | | @Autowired |
| | | @Qualifier("cusItemSyncDataSource") |
| | |
| | | |
| | | /** 当前视图查询实际使用的数据源说明 */ |
| | | public String effectiveDataSourceLabel() { |
| | | return cusItemSyncDataSourceProvider.getIfAvailable() != null ? "cus-item-sync" : "none"; |
| | | return cusItemSyncDataSourceProvider.getIfAvailable() != null ? DataSourceNames.DJ_CLOUD_WMS : "none"; |
| | | } |
| | | |
| | | /** |
| | | * 取视图前若干行 |
| | | */ |
| | | @UseDataSource(DataSourceNames.CUS_ITEM_SYNC) |
| | | @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| | | public List<Map<String, Object>> probeSample(int limit) { |
| | | if (cusItemSyncDataSourceProvider.getIfAvailable() == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | int n = Math.min(50, Math.max(1, limit)); |
| | | return jdbcTemplate.queryForList( |
| | | "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view LIMIT " + n); |
| | | IPage<CusItemSyncView> page = cusItemSyncViewMapper.selectPage(new Page<>(1, n), viewSelectWrapper()); |
| | | return toViewMaps(page.getRecords()); |
| | | } |
| | | |
| | | @UseDataSource(DataSourceNames.CUS_ITEM_SYNC) |
| | | @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true) |
| | | public List<Map<String, Object>> listByItemNos(Collection<String> itemNos) { |
| | | if (itemNos == null || itemNos.isEmpty()) { |
| | | return Collections.emptyList(); |
| | |
| | | return Collections.emptyList(); |
| | | } |
| | | if (cusItemSyncDataSourceProvider.getIfAvailable() == null) { |
| | | log.warn("cus-item-sync 数据源未配置,跳过视图查询"); |
| | | log.warn("dj-cloud-wms 数据源未配置,跳过视图查询"); |
| | | 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 jdbcTemplate.queryForList(sql, codes.toArray()); |
| | | List<CusItemSyncView> rows = cusItemSyncViewMapper.selectList( |
| | | viewSelectWrapper().in(CusItemSyncView::getItemNo, codes)); |
| | | return toViewMaps(rows); |
| | | } |
| | | |
| | | private static LambdaQueryWrapper<CusItemSyncView> viewSelectWrapper() { |
| | | return Wrappers.<CusItemSyncView>lambdaQuery() |
| | | .select(CusItemSyncView::getItemNo, CusItemSyncView::getItemSpec, CusItemSyncView::getUnitNo); |
| | | } |
| | | |
| | | private List<Map<String, Object>> toViewMaps(List<CusItemSyncView> rows) { |
| | | if (rows == null || rows.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return rows.stream().map(this::toViewRowMap).collect(Collectors.toList()); |
| | | } |
| | | |
| | | private Map<String, Object> toViewRowMap(CusItemSyncView row) { |
| | | Map<String, Object> m = new LinkedHashMap<>(3); |
| | | m.put("item_no", row.getItemNo()); |
| | | m.put("item_spec", row.getItemSpec()); |
| | | m.put("unit_no", row.getUnitNo()); |
| | | return m; |
| | | } |
| | | } |