cl
2026-04-17 1839c303fe4b9a3562c8b4cb83e31da8fb316444
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.vincent.rsf.server.manager.service;
 
import com.vincent.rsf.server.manager.mapper.MatnrMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
/**
 * cus_item_sync_view 查询;配置了副库 url 时走 JdbcTemplate,否则走主库 Mapper
 */
@Service
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";
    }
 
    /**
     * 取视图前若干行,用于连通性验证(与业务查询同一套路由:副库优先)
     */
    public List<Map<String, Object>> probeSample(int limit) {
        int n = Math.min(50, Math.max(1, limit));
        JdbcTemplate tpl = cusItemSyncJdbcTemplate != null ? cusItemSyncJdbcTemplate : jdbcTemplate;
        return tpl.queryForList(
                "SELECT item_no, item_spec, unit_no FROM cus_item_sync_view LIMIT " + n);
    }
 
    public List<Map<String, Object>> listByItemNos(Collection<String> itemNos) {
        if (itemNos == null || itemNos.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> codes = new ArrayList<>();
        for (String code : itemNos) {
            String c = StringUtils.trimToNull(code);
            if (c != null) {
                codes.add(c);
            }
        }
        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());
        }
        return matnrMapper.selectByCusItemSyncView(codes);
    }
}