#
cl
18 小时以前 c6938bcb89091596edab2740f7bf0b218956b4b0
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.vincent.rsf.server.manager.service;
 
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.controller.erp.params.SyncOrdersItem;
import com.vincent.rsf.server.manager.entity.Matnr;
import com.vincent.rsf.server.manager.enums.CusItemSyncMode;
import com.vincent.rsf.server.system.constant.GlobalConfigCode;
import com.vincent.rsf.server.system.entity.Config;
import com.vincent.rsf.server.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
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.Objects;
import java.util.stream.Collectors;
 
/**
 * cus_barcode_sync_view 与 man_matnr 同步(云仓通知单、无订单组托等共用)。
 * 分支仅由 {@link GlobalConfigCode#CUS_ITEM_SYNC_MODE}(sys_config.val)解析为 {@link CusItemSyncMode} 决定。
 * 副库读无事务({@link CusBarcodeSyncViewQueryService} {@code NOT_SUPPORTED}),主库写 {@link CusBarcodeSyncMatnrApplyService} {@code REQUIRES_NEW}。
 */
@Service
@Slf4j
public class CusBarcodeSyncMatnrService {
 
    @Autowired
    private CusBarcodeSyncViewQueryService cusBarcodeSyncViewQueryService;
    @Autowired
    private MatnrService matnrService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private CusBarcodeSyncMatnrApplyService cusBarcodeSyncMatnrApplyService;
 
    /** 与云仓通知单同步:按明细 matnr 查视图并写入/更新物料 */
    public void syncFromOrderItems(List<SyncOrdersItem> orderItems, Long loginUserId) {
        if (orderItems == null || orderItems.isEmpty()) {
            return;
        }
        List<String> matnrCodes = orderItems.stream()
                .map(SyncOrdersItem::getMatnr)
                .filter(StringUtils::isNotBlank)
                .map(String::trim)
                .distinct()
                .collect(Collectors.toList());
        if (matnrCodes.isEmpty()) {
            return;
        }
        syncAlignedWithBarcodeView(matnrCodes, buildOrderItemByMatnrCode(orderItems), loginUserId);
    }
 
    /** 无订单组托等:仅按物料号(与视图 barcode 一致)走同一套视图策略 */
    public void syncFromMatnrCodes(Collection<String> matnrCodes, Long loginUserId) {
        if (matnrCodes == null || matnrCodes.isEmpty()) {
            return;
        }
        List<String> codes = matnrCodes.stream()
                .map(StringUtils::trimToNull)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (codes.isEmpty()) {
            return;
        }
        syncAlignedWithBarcodeView(codes, Collections.emptyMap(), loginUserId);
    }
 
    private static Map<String, SyncOrdersItem> buildOrderItemByMatnrCode(List<SyncOrdersItem> orderItems) {
        Map<String, SyncOrdersItem> map = new LinkedHashMap<>();
        if (orderItems == null) {
            return map;
        }
        for (SyncOrdersItem item : orderItems) {
            if (StringUtils.isBlank(item.getMatnr())) {
                continue;
            }
            String key = StringUtils.trimToNull(item.getMatnr().trim());
            if (key == null) {
                continue;
            }
            map.putIfAbsent(key, item);
        }
        return map;
    }
 
    /** 与分支解析共用同一份 Config(来自 ConfigService 全局缓存,避免每次同步打库) */
    private CusItemSyncConfigSnapshot resolveCusItemSyncConfig() {
        Config c = configService.getCachedOrLoad(GlobalConfigCode.CUS_ITEM_SYNC_MODE);
        if (c == null) {
            return new CusItemSyncConfigSnapshot(CusItemSyncMode.NONE, null);
        }
        return new CusItemSyncConfigSnapshot(CusItemSyncMode.fromConfig(c.getVal()), c.getVal());
    }
 
    private static String formatCfgVal(String rawVal) {
        if (rawVal == null) {
            return "(无配置)";
        }
        String t = rawVal.trim();
        return t.isEmpty() ? "(空)" : t;
    }
 
    private void syncAlignedWithBarcodeView(List<String> matnrCodes, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) {
        CusItemSyncConfigSnapshot cfg = resolveCusItemSyncConfig();
        log.info("[cus_barcode_sync] 同步入口 CUS_ITEM_SYNC_MODE.val={} resolved={} ds={} matnrCount={} matnrs=[{}]",
                formatCfgVal(cfg.rawVal),
                cfg.mode,
                cusBarcodeSyncViewQueryService.effectiveDataSourceLabel(),
                matnrCodes.size(),
                joinCodesForLog(matnrCodes));
        if (cfg.mode == CusItemSyncMode.NONE) {
            syncMatnrNonForceFromView(cfg, matnrCodes, orderItemByCode, loginUserId);
            return;
        }
        List<Map<String, Object>> viewItems = new ArrayList<>(matnrCodes.size());
        for (String code : matnrCodes) {
            List<Map<String, Object>> one = cusBarcodeSyncViewQueryService.listMapsForBarcode(code);
            boolean hit = CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, one);
            log.info("[cus_barcode_sync] 强制校验 code={} viewHit={}", code, hit);
            if (!hit) {
                throw new CoolException("物料未在视图 cus_barcode_sync_view 中:" + code);
            }
            viewItems.addAll(one);
        }
        log.info("[cus_barcode_sync] FORCE_VIEW 分支 CUS_ITEM_SYNC_MODE.val={} viewRows={} viewBarcodes=[{}]",
                formatCfgVal(cfg.rawVal),
                viewItems.size(),
                summarizeViewBarcodes(viewItems));
        cusBarcodeSyncMatnrApplyService.applyFromViewRows(viewItems, orderItemByCode, loginUserId);
    }
 
    private void syncMatnrNonForceFromView(CusItemSyncConfigSnapshot cfg, List<String> matnrCodes, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) {
        Map<String, List<Map<String, Object>>> viewByCode = null;
        try {
            viewByCode = new LinkedHashMap<>();
            for (String code : matnrCodes) {
                viewByCode.put(code, cusBarcodeSyncViewQueryService.listMapsForBarcode(code));
            }
        } catch (Exception ex) {
            log.warn("[cus_barcode_sync] 查询 cus_barcode_sync_view 异常,将仅按物料表校验", ex);
        }
        int viewRowTotal = viewByCode == null ? 0 : viewByCode.values().stream().mapToInt(l -> l == null ? 0 : l.size()).sum();
        log.info("[cus_barcode_sync] NONE 分支 CUS_ITEM_SYNC_MODE.val={} 副库视图 rows={} barcodesInView=[{}]",
                formatCfgVal(cfg.rawVal),
                viewRowTotal,
                summarizeBarcodesWithHits(viewByCode));
        if (viewByCode != null) {
            try {
                for (String code : matnrCodes) {
                    List<Map<String, Object>> one = viewByCode.get(code);
                    if (one != null && !one.isEmpty()) {
                        cusBarcodeSyncMatnrApplyService.applyFromViewRows(one, orderItemByCode, loginUserId);
                    }
                }
            } catch (Exception ex) {
                log.warn("[cus_barcode_sync] 按条码 applyFromViewRows 失败", ex);
            }
        }
        // 视图有条码但本地仍无:按行补建档
        for (String code : matnrCodes) {
            Matnr m = findLocalMatnrForOrderCode(code);
            if (m != null) {
                log.info("[cus_barcode_sync] 校验通过 code={} localMatnrId={}", code, m.getId());
                continue;
            }
            List<Map<String, Object>> one = viewByCode == null
                    ? Collections.emptyList()
                    : viewByCode.getOrDefault(code, Collections.emptyList());
            boolean viewHit = CusBarcodeSyncViewQueryService.orderMatnrHitsBarcodeView(code, one);
            int viewRowCount = one.size();
            log.info("[cus_barcode_sync] 本地无记录 code={} viewHit={} viewRowCount={}", code, viewHit, viewRowCount);
            Exception applyEx = null;
            if (viewHit && !one.isEmpty()) {
                try {
                    log.info("[cus_barcode_sync] 按条码补档 apply rows={} code={}", one.size(), code);
                    cusBarcodeSyncMatnrApplyService.applyFromViewRows(one, orderItemByCode, loginUserId);
                } catch (Exception ex) {
                    applyEx = ex;
                    log.warn("[cus_barcode_sync] 按视图补全物料失败 code={}", code, ex);
                }
                m = findLocalMatnrForOrderCode(code);
            }
            if (m == null) {
                ManMatnrFailReason fr = resolveManMatnrFailReason(viewByCode, viewHit, applyEx);
                log.warn("[cus_barcode_sync] 无法落地 man_matnr code={} reason={} viewRowCount={} viewBarcodes=[{}]",
                        code, fr.name(), viewRowCount, summarizeViewBarcodes(one));
                throw new CoolException("物料不存在:" + code);
            }
        }
    }
 
    private enum ManMatnrFailReason {
        VIEW_QUERY_ERROR,
        VIEW_ZERO_ROWS,
        VIEW_CODE_NOT_IN_RESULT,
        APPLY_ERROR,
        APPLY_STILL_EMPTY
    }
 
    private static ManMatnrFailReason resolveManMatnrFailReason(
            Map<String, List<Map<String, Object>>> viewByCode,
            boolean viewHit,
            Exception applyEx) {
        if (viewByCode == null) {
            return ManMatnrFailReason.VIEW_QUERY_ERROR;
        }
        boolean anyHit = viewByCode.values().stream().anyMatch(list -> list != null && !list.isEmpty());
        if (!anyHit) {
            return ManMatnrFailReason.VIEW_ZERO_ROWS;
        }
        if (!viewHit) {
            return ManMatnrFailReason.VIEW_CODE_NOT_IN_RESULT;
        }
        if (applyEx != null) {
            return ManMatnrFailReason.APPLY_ERROR;
        }
        return ManMatnrFailReason.APPLY_STILL_EMPTY;
    }
 
    private static String joinCodesForLog(List<String> matnrCodes) {
        if (matnrCodes == null || matnrCodes.isEmpty()) {
            return "";
        }
        String s = String.join(",", matnrCodes);
        return s.length() > 1200 ? s.substring(0, 1200) + "..." : s;
    }
 
    private static String summarizeViewBarcodes(List<Map<String, Object>> viewItems) {
        if (viewItems == null || viewItems.isEmpty()) {
            return "";
        }
        String s = viewItems.stream()
                .map(r -> Objects.toString(r.get("barcode"), ""))
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.joining(","));
        return s.length() > 1200 ? s.substring(0, 1200) + "..." : s;
    }
 
    /** 仅汇总本次查询有命中的单据物料号 */
    private static String summarizeBarcodesWithHits(Map<String, List<Map<String, Object>>> viewByCode) {
        if (viewByCode == null || viewByCode.isEmpty()) {
            return "";
        }
        String s = viewByCode.entrySet().stream()
                .filter(e -> e.getValue() != null && !e.getValue().isEmpty())
                .map(Map.Entry::getKey)
                .collect(Collectors.joining(","));
        return s.length() > 1200 ? s.substring(0, 1200) + "..." : s;
    }
 
    private Matnr findLocalMatnrForOrderCode(String orderMatnr) {
        String t = StringUtils.trimToNull(orderMatnr);
        if (t == null) {
            return null;
        }
        return matnrService.getOneByCodeAndBatch(t, "");
    }
 
    private static final class CusItemSyncConfigSnapshot {
        final CusItemSyncMode mode;
        /** sys_config.CUS_ITEM_SYNC_MODE 的 val,无配置行为 null */
        final String rawVal;
 
        CusItemSyncConfigSnapshot(CusItemSyncMode mode, String rawVal) {
            this.mode = mode;
            this.rawVal = rawVal;
        }
    }
 
}