cl
1 天以前 bb36bbb0968f6f599e18a651f5e385b98c4e1532
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
package com.vincent.rsf.server.manager.service;
 
import com.vincent.rsf.server.api.controller.erp.params.SyncOrdersItem;
import com.vincent.rsf.server.manager.entity.Matnr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * 主库 man_matnr 按视图结果写入;独立事务,与副库视图查询分离
 */
@Slf4j
@Service
public class CusBarcodeSyncMatnrApplyService {
 
    @Autowired
    private MatnrService matnrService;
 
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void applyFromViewRows(List<Map<String, Object>> viewItems, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) {
        if (viewItems == null || viewItems.isEmpty()) {
            return;
        }
        for (Map<String, Object> row : dedupeCusBarcodeViewRows(viewItems)) {
            String matCode = CusBarcodeSyncViewQueryService.matnrCodeFromBarcode(Objects.toString(row.get("barcode"), null));
            if (matCode == null) {
                continue;
            }
            SyncOrdersItem syncItem = resolveSyncOrderItem(orderItemByCode, matCode);
            String viewSpec = StringUtils.trimToEmpty(Objects.toString(row.get("item_spec"), ""));
            String viewUnit = StringUtils.trimToNull(Objects.toString(row.get("unit_no"), null));
            String viewItemName = StringUtils.trimToNull(Objects.toString(row.get("item_name"), null));
            String incomingName = syncItem == null ? null : StringUtils.trimToNull(syncItem.getMaktx());
            Matnr local = matnrService.getOneByCodeAndBatch(matCode, "");
            if (local == null) {
                Matnr matnr = new Matnr();
                matnr.setCode(matCode)
                        .setBatch("")
                        .setName(incomingName != null ? incomingName : (viewItemName != null ? viewItemName : matCode))
                        .setSpec(viewSpec)
                        .setUnit(viewUnit)
                        .setStockUnit(viewUnit)
                        .setStatus(1)
                        .setCreateBy(loginUserId)
                        .setUpdateBy(loginUserId)
                        .setCreateTime(new Date())
                        .setUpdateTime(new Date());
                matnrService.save(matnr);
                log.info("[cus_barcode_sync] man_matnr 新增 code={}", matCode);
                continue;
            }
            boolean nameDiff = incomingName != null
                    && !StringUtils.equals(StringUtils.trimToEmpty(local.getName()), incomingName);
            boolean specDiff = !StringUtils.equals(StringUtils.trimToEmpty(local.getSpec()), viewSpec);
            boolean unitDiff = viewUnit != null
                    && (!StringUtils.equals(StringUtils.trimToEmpty(local.getUnit()), viewUnit)
                    || !StringUtils.equals(StringUtils.trimToEmpty(local.getStockUnit()), viewUnit));
            if (!nameDiff && !specDiff && !unitDiff) {
                log.debug("[cus_barcode_sync] man_matnr 已存在且无变更 code={} id={}", matCode, local.getId());
                continue;
            }
            Matnr update = new Matnr();
            update.setId(local.getId());
            if (nameDiff) {
                update.setName(incomingName);
            }
            if (specDiff) {
                update.setSpec(viewSpec);
            }
            if (unitDiff) {
                update.setUnit(viewUnit).setStockUnit(viewUnit);
            }
            update.setUpdateBy(loginUserId).setUpdateTime(new Date());
            matnrService.updateById(update);
            log.info("[cus_barcode_sync] man_matnr 更新 code={} id={} nameDiff={} specDiff={} unitDiff={}",
                    matCode, local.getId(), nameDiff, specDiff, unitDiff);
        }
    }
 
    private static SyncOrdersItem resolveSyncOrderItem(Map<String, SyncOrdersItem> orderItemByCode, String matCode) {
        return matCode == null ? null : orderItemByCode.get(matCode);
    }
 
    private static List<Map<String, Object>> dedupeCusBarcodeViewRows(List<Map<String, Object>> viewItems) {
        Map<String, Map<String, Object>> firstByCode = new LinkedHashMap<>();
        if (viewItems == null) {
            return Collections.emptyList();
        }
        for (Map<String, Object> row : viewItems) {
            String matCode = CusBarcodeSyncViewQueryService.matnrCodeFromBarcode(Objects.toString(row.get("barcode"), null));
            if (matCode == null) {
                continue;
            }
            firstByCode.putIfAbsent(matCode, row);
        }
        return new ArrayList<>(firstByCode.values());
    }
}