yangyang
2025-03-21 158ef0f443b9f36f8beb44853ed1f39449c45c1d
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
package com.vincent.rsf.server.manager.service.impl;
 
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.common.utils.CommonUtil;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.common.utils.FieldsUtils;
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 com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
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.Map;
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 result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams());
        if (result.getList().isEmpty()) {
            throw new CoolException("物料导入失败!!");
        }
        List<Matnr> matnrs = new ArrayList<>();
        List<Map<String, String>> list = result.getList();
        list.forEach(template -> {
            Matnr matnr = new Matnr();
            matnr.setBarcode(template.get("barcode"))
                    .setCode(template.get("code"))
                    .setDescrible(template.get("describle"))
                    .setColor(template.get("color"))
                    .setFlagCheck(!Objects.isNull(template.get("flagCheck")) ? Short.parseShort(template.get("flagCheck")) : 0)
                    .setWeight(!Objects.isNull(template.get("weight")) ? Double.parseDouble(template.get("weight")) : 0.0)
                    .setValidWarn(!Objects.isNull(template.get("validWarn")) ? Integer.parseInt(template.get("validWarn")): 0)
                    .setValid(!Objects.isNull(template.get("valid")) ? Integer.parseInt( template.get("valid")) : 0)
                    .setUnit(template.get("unit"))
                    .setStockUnit(template.get("purUnit"))
                    .setSpec(template.get("spec"))
                    .setStagn(!Objects.isNull(template.get("stagn")) ? Integer.parseInt(template.get("stagn")) : 0)
                    .setModel(template.get("model"))
                    .setGroupCode(template.get("groupCode"))
                    .setPurUnit(template.get("purUnit"))
                    .setStockLevel(!Objects.isNull(template.get("stockLevel")) ? Short.parseShort(template.get("stockLevel")) : 0)
                    .setSafeQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("safeQty")) : 0)
                    .setMinQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("minQty")) : 0);
            if (Objects.isNull(template.get("groupCode")) && Objects.isNull(template.get("groupName"))) {
                MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>()
                        .eq(!Objects.isNull(template.get("groupCode")), MatnrGroup::getCode, template.get("groupCode"))
                        .eq(!Objects.isNull(template.get("groupName")),MatnrGroup::getName, template.get("groupName")));
                matnr.setGroupId(matnrGroups.getId());
            }
            //获取动态字段,并保存明细内容
            if (!FieldsUtils.getFieldsSta().isEmpty()) {
                String uuid = CommonUtil.randomUUID16();
                matnr.setFieldsIndex(uuid);
                //保存物料扩展属性值
                FieldsUtils.saveFields(template, uuid);
            }
            matnrs.add(matnr);
        });
 
        if (!this.saveBatch(matnrs)) {
            throw new CoolException("导入失败!!");
        }
        return R.ok("导入成功!!");
    }
 
    @Override
    public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam) {
        IPage<Map<String, Object>> reulst = this.baseMapper.selectMatnrs(pageParam, pageParam.buildWrapper(true));
        /**获取物料分页信息 */
        List<Map<String, Object>> mapList = reulst.getRecords();
        if (!mapList.isEmpty()) {
            mapList.forEach(map -> {
                if (!Objects.isNull(map.get("fieldsIndex"))) {
                   FieldsUtils.mergeFields(map, map.get("fieldsIndex").toString());
                }
            });
        }
 
        PageParam<Matnr, BaseParam> page = this.page(pageParam, pageParam.buildWrapper(true));
        List<Matnr> records = page.getRecords();
        for (Matnr record : records) {
            if (!Objects.isNull(record.getFieldsIndex())) {
                Map<String, String> fields = FieldsUtils.getFields(record.getFieldsIndex());
                record.setExtendFields(fields);
            }
        }
        page.setRecords(records);
        return page;
    }
 
    @Override
    public Matnr selectMatnrById(Long id) {
        Matnr matnr = this.baseMapper.selectById(id);
        if (Objects.isNull(matnr)) {
            throw new CoolException("当前物料不存在!!");
        }
        if (!Objects.isNull(matnr.getFieldsIndex())) {
            Map<String, String> fields = FieldsUtils.getFields(matnr.getFieldsIndex());
            matnr.setExtendFields(fields);
        }
        return matnr;
    }
 
    /**
     * @desc 更新扩展物料扩展字段值
     * @param matnr
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveMatnrs(Map<String, Object> matnr) {
        Matnr matnr1 = JSONObject.parseObject(JSONObject.toJSONString(matnr), Matnr.class);
        if (Objects.isNull(matnr1.getCode())) {
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_MATNR_CODE, null);
            matnr1.setCode(ruleCode);
        }
        /**
         * 扩展字段存入库
         */
        String uuid16 = CommonUtil.randomUUID16();
        if (!FieldsUtils.getFieldsSta().isEmpty()) {
            FieldsUtils.saveFields(matnr, uuid16);
            matnr1.setFieldsIndex(uuid16);
        }
        if (!this.saveOrUpdate(matnr1)) {
            return R.error("Save Fail");
        }
        return R.ok(matnr1);
    }
}