skyouc
2025-03-12 4af26c71d9414b8b8f568c0f119f123790cab152
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
package com.vincent.rsf.server.manager.service.impl;
 
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.manager.entity.MatnrGroup;
import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate;
import com.vincent.rsf.server.manager.mapper.MatnrMapper;
import com.vincent.rsf.server.manager.entity.Matnr;
import com.vincent.rsf.server.manager.service.MatnrGroupService;
import com.vincent.rsf.server.manager.service.MatnrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
@Service("matnrService")
public class MatnrServiceImpl extends ServiceImpl<MatnrMapper, Matnr> implements MatnrService {
 
    @Autowired
    private MatnrGroupService matnrGroupService;
 
    /**
     * @return
     * @throws
     * @author Ryan
     * @description 物料数据导入接处理实现
     * @time 2025/3/3 13:08
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R importExcels(MultipartFile file) throws Exception {
        //读取上传文件内容
        ExcelImportResult<MatnrsTemplate> result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams());
        if (result.getList().isEmpty()) {
            throw new CoolException("物料导入失败!!");
        }
        List<Matnr> matnrs = new ArrayList<>();
        result.getList().forEach(template -> {
            Matnr matnr = new Matnr();
            matnr.setBarcode(template.getBarcode())
                    .setCode(template.getErpCode())
                    .setDescrible(template.getDescrible())
                    .setColor(template.getColor())
                    .setFlagCheck(Short.parseShort(template.getFlagCheck()))
                    .setWeight(Double.parseDouble(template.getWeight()))
                    .setValidWarn(Integer.parseInt(template.getValidWarn()))
                    .setValid(Integer.parseInt(template.getValid()))
                    .setUnit(template.getUnit())
                    .setStockUnit(template.getPurUnit())
                    .setSpec(template.getSpec())
                    .setStagn(Integer.parseInt(template.getStagn()))
                    .setModel(template.getModel())
                    .setGroupCode(template.getGroupCode())
                    .setPurUnit(template.getPurUnit())
                    .setStockLevel(Short.parseShort(template.getStockLevel()))
                    .setSafeQty(Double.parseDouble(template.getSafeQty()))
                    .setMinQty(Double.parseDouble(template.getMinQty()));
            if (Objects.isNull(template.getGroupCode()) && Objects.isNull(template.getGroupName())) {
                MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>()
                        .eq(!Objects.isNull(template.getGroupCode()), MatnrGroup::getCode, template.getGroupCode())
                        .eq(!Objects.isNull(template.getGroupName()),MatnrGroup::getName, template.getName()));
                matnr.setGroupId(matnrGroups.getId());
            }
 
            matnrs.add(matnr);
        });
 
        if (!this.saveBatch(matnrs)) {
            throw new CoolException("导入失败!!");
        }
        return R.ok("导入成功!!");
    }
}