6 天以前 1e123568c2f561013f5636e45dc0db30b17a3517
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
package com.ruoyi.production.service.impl;
 
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.production.bean.vo.MaterialConsumptionActualVo;
import com.ruoyi.production.bean.vo.MaterialConsumptionOrderVo;
import com.ruoyi.production.bean.vo.MaterialConsumptionStatVo;
import com.ruoyi.production.bean.vo.MaterialConsumptionStructureVo;
import com.ruoyi.production.mapper.MaterialConsumptionMapper;
import com.ruoyi.production.service.MaterialConsumptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class MaterialConsumptionServiceImpl implements MaterialConsumptionService {
 
    private final MaterialConsumptionMapper materialConsumptionMapper;
    private final ProductModelMapper productModelMapper;
    private final ProductMapper productMapper;
 
    @Override
    public List<MaterialConsumptionStatVo> stat(LocalDate startDate, LocalDate endDate,
                                                String npsNo, String productName, Boolean onlyDiff) {
        List<MaterialConsumptionOrderVo> orders =
                materialConsumptionMapper.listOrders(startDate, endDate, npsNo, productName);
        if (orders.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> orderIds = orders.stream().map(MaterialConsumptionOrderVo::getProductionOrderId)
                .filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<MaterialConsumptionStructureVo> structures = materialConsumptionMapper.listStructures(orderIds);
        List<MaterialConsumptionActualVo> actuals = materialConsumptionMapper.listActualInputs(orderIds);
 
        Map<Long, MaterialConsumptionStructureVo> structureById = structures.stream()
                .collect(Collectors.toMap(MaterialConsumptionStructureVo::getId, s -> s, (a, b) -> a));
        Map<String, BigDecimal> actualMap = new HashMap<>();
        for (MaterialConsumptionActualVo actual : actuals) {
            actualMap.merge(actual.getProductionOrderId() + "_" + actual.getProductModelId(),
                    nvl(actual.getActualQty()), BigDecimal::add);
        }
 
        //BOM逐层展开:累计单耗=上级累计×本级单耗,按物料合并
        Map<Long, Map<Long, BigDecimal>> orderStandard = new HashMap<>();
        for (MaterialConsumptionStructureVo row : structures) {
            if (isRoot(row)) {
                continue;
            }
            BigDecimal cumulative = cumulativeUnitQuantity(row, structureById);
            orderStandard.computeIfAbsent(row.getProductionOrderId(), k -> new LinkedHashMap<>())
                    .merge(row.getProductModelId(), cumulative, BigDecimal::add);
        }
 
        Set<Long> materialModelIds = new HashSet<>();
        orderStandard.values().forEach(m -> materialModelIds.addAll(m.keySet()));
        materialModelIds.addAll(actualMap.keySet().stream()
                .map(k -> Long.valueOf(k.substring(k.indexOf('_') + 1))).collect(Collectors.toSet()));
        Map<Long, ProductModel> modelMap = modelIds(materialModelIds);
        Map<Long, Product> productMap = products(modelMap.values());
 
        List<MaterialConsumptionStatVo> result = new ArrayList<>();
        for (MaterialConsumptionOrderVo order : orders) {
            Map<Long, BigDecimal> standardMap = orderStandard.getOrDefault(order.getProductionOrderId(), new LinkedHashMap<>());
            //报工投入了但BOM没有的物料也要展示(超标/误领)
            Set<Long> materialIds = new java.util.LinkedHashSet<>(standardMap.keySet());
            actualMap.keySet().stream()
                    .filter(k -> k.startsWith(order.getProductionOrderId() + "_"))
                    .forEach(k -> materialIds.add(Long.valueOf(k.substring(k.indexOf('_') + 1))));
            if (materialIds.isEmpty()) {
                continue;
            }
            BigDecimal outputQty = nvl(order.getCompleteQty()).compareTo(BigDecimal.ZERO) > 0
                    ? order.getCompleteQty() : nvl(order.getPlanQty());
            for (Long materialId : materialIds) {
                BigDecimal unitQty = standardMap.getOrDefault(materialId, BigDecimal.ZERO);
                BigDecimal standard = unitQty.multiply(outputQty).setScale(4, RoundingMode.HALF_UP);
                BigDecimal actual = actualMap.getOrDefault(
                        order.getProductionOrderId() + "_" + materialId, BigDecimal.ZERO);
                BigDecimal diff = actual.subtract(standard);
                if (Boolean.TRUE.equals(onlyDiff) && diff.compareTo(BigDecimal.ZERO) == 0) {
                    continue;
                }
                MaterialConsumptionStatVo vo = new MaterialConsumptionStatVo();
                vo.setProductionOrderId(order.getProductionOrderId());
                vo.setNpsNo(order.getNpsNo());
                vo.setFinishedProduct(order.getFinishedProduct());
                vo.setMaterialModelId(materialId);
                ProductModel pm = modelMap.get(materialId);
                if (pm != null) {
                    vo.setMaterialModel(pm.getModel());
                    vo.setUnit(pm.getUnit());
                    Product p = productMap.get(pm.getProductId());
                    if (p != null) {
                        vo.setMaterialName(p.getProductName());
                    }
                }
                vo.setUnitQuantity(unitQty);
                vo.setCompleteQty(outputQty);
                vo.setStandardQty(standard);
                vo.setActualQty(actual);
                vo.setDiffQty(diff);
                if (standard.compareTo(BigDecimal.ZERO) > 0) {
                    vo.setDiffRate(diff.multiply(BigDecimal.valueOf(100))
                            .divide(standard, 2, RoundingMode.HALF_UP));
                }
                result.add(vo);
            }
        }
        return result;
    }
 
    private boolean isRoot(MaterialConsumptionStructureVo row) {
        return row.getParentId() == null || row.getParentId() == 0L;
    }
 
    private BigDecimal cumulativeUnitQuantity(MaterialConsumptionStructureVo row,
                                              Map<Long, MaterialConsumptionStructureVo> structureById) {
        BigDecimal cumulative = nvl(row.getUnitQuantity());
        Long parentId = row.getParentId();
        int guard = 0;
        while (parentId != null && parentId != 0L && guard++ < 20) {
            MaterialConsumptionStructureVo parent = structureById.get(parentId);
            if (parent == null) {
                break;
            }
            cumulative = cumulative.multiply(nvl(parent.getUnitQuantity()));
            parentId = parent.getParentId();
        }
        return cumulative.setScale(6, RoundingMode.HALF_UP);
    }
 
    private Map<Long, ProductModel> modelIds(Set<Long> ids) {
        if (ids.isEmpty()) {
            return new HashMap<>();
        }
        return productModelMapper.selectBatchIds(ids).stream()
                .collect(Collectors.toMap(ProductModel::getId, m -> m, (a, b) -> a));
    }
 
    private Map<Long, Product> products(java.util.Collection<ProductModel> models) {
        List<Long> ids = models.stream().map(ProductModel::getProductId)
                .filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (ids.isEmpty()) {
            return new HashMap<>();
        }
        return productMapper.selectBatchIds(ids).stream()
                .collect(Collectors.toMap(Product::getId, p -> p, (a, b) -> a));
    }
 
    private BigDecimal nvl(BigDecimal value) {
        return value == null ? BigDecimal.ZERO : value;
    }
}