gongchunyi
19 小时以前 ddc95002ef4c7593a006443690d5ae6bba693227
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.ProductDto;
import com.ruoyi.basic.dto.ProductImportRowDto;
import com.ruoyi.basic.dto.ProductModelDto;
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.basic.service.IProductModelService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.technology.mapper.TechnologyBomMapper;
import com.ruoyi.technology.pojo.TechnologyBom;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
 
/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
@AllArgsConstructor
public class ProductModelServiceImpl extends ServiceImpl<ProductModelMapper, ProductModel> implements IProductModelService {
 
    private final ProductMapper productMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
    private final TechnologyBomMapper technologyBomMapper;
    private ProductModelMapper productModelMapper;
 
    private static final List<String> IMPORT_SHEET_NAMES = Arrays.asList("原料", "半成品", "成品");
 
    @Override
    public int addOrEditProductModel(ProductModelDto productModelDto) {
        String model = StringUtils.trim(productModelDto.getModel());
        String productCode = StringUtils.trim(productModelDto.getProductCode());
        productModelDto.setModel(model);
        productModelDto.setProductCode(productCode);
        checkModelAndProductCodeUnique(model, productCode, productModelDto.getId());
 
        if (productModelDto.getId() == null) {
            ProductModel productModel = new ProductModel();
            BeanUtils.copyProperties(productModelDto, productModel);
            return productModelMapper.insert(productModel);
        } else {
            return productModelMapper.updateById(productModelDto);
        }
    }
 
    private void checkModelAndProductCodeUnique(String model, String productCode, Long currentId) {
        if (StringUtils.isEmpty(model) || StringUtils.isEmpty(productCode)) {
            return;
        }
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductModel::getModel, model)
                .eq(ProductModel::getProductCode, productCode)
                .ne(currentId != null, ProductModel::getId, currentId)
                .last("limit 1");
        ProductModel duplicateProductModel = productModelMapper.selectOne(queryWrapper);
        if (duplicateProductModel != null) {
            throw new ServiceException("对应的型号" + model + "的产品编码" + productCode + "已经存在");
        }
    }
 
 
    @Override
    public int delProductModel(Long[] ids) {
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
                .lambda().in(SalesLedgerProduct::getProductModelId, ids));
        if (salesLedgerProducts != null && salesLedgerProducts.size() > 0) {
 
            throw new RuntimeException("已经存在该产品的销售台账和采购台账");
        }
 
        // 是否存在BOM
        List<TechnologyBom> technologyBoms = technologyBomMapper.selectList(new QueryWrapper<TechnologyBom>()
                .lambda().in(TechnologyBom::getProductModelId, ids));
        if (CollectionUtils.isNotEmpty(technologyBoms)) {
            throw new RuntimeException("已经存在该产品的BOM数据");
        }
 
        return productModelMapper.deleteBatchIds(Arrays.asList(ids));
    }
 
    @Override
    public List<ProductModel> selectModelList(ProductDto productDto) {
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductModel::getProductId, productDto.getId());
        return productModelMapper.selectList(queryWrapper);
    }
 
    /**
     * 根据id查询产品规格分页查询
     *
     * @param page
     * @param productDto
     * @return
     */
    @Override
    public IPage<ProductModel> modelListPage(Page page, ProductDto productDto) {
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductModel::getProductId, productDto.getId());
        return productModelMapper.selectPage(page, queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importProductModel(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("请选择要导入的文件");
        }
 
        Map<String, List<ProductImportRowDto>> sheetMap;
        try {
            ExcelUtil<ProductImportRowDto> excelUtil = new ExcelUtil<>(ProductImportRowDto.class);
            sheetMap = excelUtil.importExcelMultiSheet(IMPORT_SHEET_NAMES, file.getInputStream(), 0);
        } catch (Exception e) {
            log.error("解析产品导入文件异常", e);
            throw new ServiceException("产品导入文件解析失败");
        }
        if (sheetMap.isEmpty()) {
            return AjaxResult.error("未找到 [原料/半成品/成品] 工作表,或文件中没有数据");
        }
 
        // 去重键:产品名称 + 规格型号 + 编码(空编码按空串参与比较)
        Set<String> seenKeys = loadExistingProductKeys(sheetMap);
 
        int successCount = 0;
        int skipCount = 0;
        Set<String> generatedCodes = new HashSet<>();
        for (String sheetName : IMPORT_SHEET_NAMES) {
            List<ProductImportRowDto> rows = sheetMap.get(sheetName);
            if (CollectionUtils.isEmpty(rows)) {
                continue;
            }
            Long topId = findOrCreateTopProduct(sheetName);
            Map<String, Long> nodeCache = new HashMap<>();
            for (int i = 0; i < rows.size(); i++) {
                ProductImportRowDto row = rows.get(i);
                //  没有名称/编码/规格的行构不成产品数据,按空行跳过
                if (hasNoProductContent(row)) {
                    continue;
                }
                String parentName = trimToNull(row.getProductParentName());
                String categoryName = trimToNull(row.getProductCategoryName());
                String productName = trimToNull(row.getProductName());
                if (productName == null) {
                    throw new ServiceException(String.format("工作表【%s】第 %d 行 [产品名称] 不能为空(该行已填:%s)",
                            sheetName, resolveRowNum(row, i), describeRow(row)));
                }
 
                String model = trimToNull(row.getModel());
                if (model == null) {
                    model = "/";
                }
                String code = trimToNull(row.getProductCode());
                if (!seenKeys.add(dedupKey(productName, model, code))) {
                    skipCount++;
                    continue;
                }
 
                String path = String.valueOf(topId);
                Long currentParentId = topId;
                if (parentName != null) {
                    path = path + ">" + parentName;
                    currentParentId = ensureChildNode(nodeCache, path, currentParentId, parentName);
                }
                if (categoryName != null) {
                    path = path + ">" + categoryName;
                    currentParentId = ensureChildNode(nodeCache, path, currentParentId, categoryName);
                }
                path = path + ">" + productName;
                Long leafId = ensureChildNode(nodeCache, path, currentParentId, productName);
 
                if (code == null) {
                    code = generateUniqueCode(generatedCodes);
                }
 
                ProductModel productModel = new ProductModel();
                productModel.setProductId(leafId);
                productModel.setProductCode(code);
                productModel.setModel(model);
                productModel.setUnit(trimToNull(row.getUnit()));
                productModelMapper.insert(productModel);
                successCount++;
            }
        }
 
        String message = skipCount > 0
                ? String.format("成功导入 %d 条,跳过已存在 %d 条", successCount, skipCount)
                : String.format("成功导入 %d 条", successCount);
        return AjaxResult.success(message);
    }
 
    private Long findOrCreateTopProduct(String name) {
        Product root = productMapper.selectOne(new LambdaQueryWrapper<Product>()
                .isNull(Product::getParentId)
                .eq(Product::getProductName, name)
                .last("limit 1"));
        if (root != null) {
            return root.getId();
        }
        Product product = new Product();
        product.setProductName(name);
        productMapper.insert(product);
        return product.getId();
    }
 
    private Long ensureChildNode(Map<String, Long> nodeCache, String path, Long parentId, String name) {
        Long id = nodeCache.get(path);
        if (id != null) {
            return id;
        }
        Product existing = productMapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getParentId, parentId)
                .eq(Product::getProductName, name)
                .last("limit 1"));
        if (existing != null) {
            nodeCache.put(path, existing.getId());
            return existing.getId();
        }
        Product product = new Product();
        product.setParentId(parentId);
        product.setProductName(name);
        productMapper.insert(product);
        nodeCache.put(path, product.getId());
        return product.getId();
    }
 
    /**
     * 预加载库中已存在的去重键(产品名称 + 规格型号 + 编码),并作为本次导入的初始集合。
     */
    private Set<String> loadExistingProductKeys(Map<String, List<ProductImportRowDto>> sheetMap) {
        Set<String> productNames = new HashSet<>();
        for (List<ProductImportRowDto> rows : sheetMap.values()) {
            if (CollectionUtils.isEmpty(rows)) {
                continue;
            }
            for (ProductImportRowDto row : rows) {
                String productName = trimToNull(row.getProductName());
                if (productName != null) {
                    productNames.add(productName);
                }
            }
        }
        Set<String> keys = new HashSet<>();
        if (productNames.isEmpty()) {
            return keys;
        }
 
        List<Product> products = productMapper.selectList(new LambdaQueryWrapper<Product>()
                .in(Product::getProductName, productNames));
        if (CollectionUtils.isEmpty(products)) {
            return keys;
        }
        Map<Long, String> nameById = new HashMap<>();
        List<Long> productIds = new ArrayList<>();
        for (Product product : products) {
            nameById.put(product.getId(), product.getProductName());
            productIds.add(product.getId());
        }
 
        List<ProductModel> productModels = productModelMapper.selectList(new LambdaQueryWrapper<ProductModel>()
                .in(ProductModel::getProductId, productIds));
        for (ProductModel productModel : productModels) {
            String productName = nameById.get(productModel.getProductId());
            if (productName != null) {
                keys.add(dedupKey(productName, productModel.getModel(), productModel.getProductCode()));
            }
        }
        return keys;
    }
 
    /**
     * 解析时会跳过空行
     */
    private static int resolveRowNum(ProductImportRowDto row, int index) {
        return row.getExcelRowNum() != null ? row.getExcelRowNum() : index + 2;
    }
 
    private static boolean hasNoProductContent(ProductImportRowDto row) {
        return trimToNull(row.getProductName()) == null
                && trimToNull(row.getProductCode()) == null
                && trimToNull(row.getModel()) == null;
    }
 
    private static String describeRow(ProductImportRowDto row) {
        StringBuilder description = new StringBuilder();
        appendFilledColumn(description, "产品父类", row.getProductParentName());
        appendFilledColumn(description, "产品大类", row.getProductCategoryName());
        appendFilledColumn(description, "产品名称", row.getProductName());
        appendFilledColumn(description, "产品编码", row.getProductCode());
        appendFilledColumn(description, "规格型号", row.getModel());
        appendFilledColumn(description, "单位", row.getUnit());
        return description.length() == 0 ? "无" : description.toString();
    }
 
    private static void appendFilledColumn(StringBuilder description, String columnName, String value) {
        String trimmed = trimToNull(value);
        if (trimmed == null) {
            return;
        }
        if (description.length() > 0) {
            description.append(",");
        }
        description.append(columnName).append('=').append(trimmed);
    }
 
    private static String dedupKey(String productName, String model, String productCode) {
        String normalizedModel = trimToNull(model) == null ? "/" : model.trim();
        String normalizedCode = productCode == null ? "" : productCode.trim();
        return productName.trim() + "\u0001" + normalizedModel + "\u0001" + normalizedCode;
    }
 
    private String generateUniqueCode(Set<String> generatedCodes) {
        for (int i = 0; i < 200; i++) {
            String code = randomCode(5);
            if (generatedCodes.contains(code)) {
                continue;
            }
            Long count = productModelMapper.selectCount(new LambdaQueryWrapper<ProductModel>()
                    .eq(ProductModel::getProductCode, code));
            if (count == null || count == 0) {
                generatedCodes.add(code);
                return code;
            }
        }
        throw new ServiceException("生成唯一产品编码失败,请重试");
    }
 
    private static String randomCode(int length) {
        String chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(ThreadLocalRandom.current().nextInt(chars.length())));
        }
        return sb.toString();
    }
 
    private static String trimToNull(String value) {
        if (value == null) {
            return null;
        }
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
}