yangys
2025-09-20 fcee672452c02cc29e0e17ebc27a8c51698c6d0d
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
package org.springblade.mdm.basesetting.machine.service;
 
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.basesetting.machine.mapper.MachineMapper;
import org.springblade.mdm.basesetting.machine.vo.MachineExcel;
import org.springblade.mdm.basesetting.producedivision.entity.MdmDept;
import org.springblade.mdm.basesetting.producedivision.service.MdmDeptService;
import org.springblade.mdm.basesetting.producedivision.vo.ImportResult;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.beans.BeanUtils;
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.Optional;
 
@Service
public class MachineImportService extends BizServiceImpl<MachineMapper, Machine> {
    @Autowired
    private ISysClient sysClient;
    @Autowired
    private IDictBizClient dictBizClient;
    @Autowired
    private MdmDeptService mdmDeptService;
    @Autowired
    private MachineService machineService;
    @Transactional
    public ImportResult importMachines(MultipartFile file) {
        ImportResult res1 = new ImportResult();
        if(file == null || file.isEmpty()){
            res1.setMessage("文件为空");
            return res1;
        }
 
        List<MachineExcel> list = ExcelUtil.read(file, MachineExcel.class);
        return transAndSave(list);
    }
 
    @Transactional
    ImportResult transAndSave(List<MachineExcel> list){
        ImportResult result1= checkImport( list);
        if(result1.getFailure()>0){
            return result1;
        }
        //machine_spec
            //machine_control_system
        //machine_control_system
        R<List<DictBiz>> machineSpecListRes = dictBizClient.getList("machine_spec");
        List<DictBiz> machineSpecList = machineSpecListRes.isSuccess() ? machineSpecListRes.getData() : Collections.emptyList();
 
        R<List<DictBiz>> machineSystemRes = dictBizClient.getList("machine_control_system");
        List<DictBiz> machineSystemList = machineSystemRes.isSuccess() ? machineSystemRes.getData() : Collections.emptyList();
 
        R<List<DictBiz>> machineGroupRes = dictBizClient.getList("machine_group");
        List<DictBiz>  machineGroupList = machineGroupRes.isSuccess() ? machineGroupRes.getData() : Collections.emptyList();
        DictBiz emptyDit = new DictBiz();
 
        List<Machine> entityList = new ArrayList<>();
        MdmDept ownerDept;
        ImportResult ir = new ImportResult();
        int failCount = 0;
        int successCount = 0;
        StringBuilder msg = new StringBuilder();
        for(MachineExcel excelVO : list){
            Machine machine = new Machine();
 
            BeanUtils.copyProperties(excelVO, machine);
            ownerDept = mdmDeptService.getByName(excelVO.getOwnerDeptName());
            if(ownerDept != null){
                machine.setOwnerDept(ownerDept.getId());
            }
            //类型
            Optional<DictBiz> specOpt = machineSpecList.stream().filter(s -> StringUtils.equals(s.getDictValue(),excelVO.getMachineSpecName())).findFirst();
            machine.setMachineSpec(specOpt.orElse(emptyDit).getDictKey());
            //系统
            Optional<DictBiz> sysOpt = machineSystemList.stream().filter(s -> StringUtils.equals(s.getDictValue(),excelVO.getControlSystemName())).findFirst();
            machine.setControlSystem(sysOpt.orElse(emptyDit).getDictKey());
 
            //机床组
            //key = FANUK,
            Optional<DictBiz> groupOpt = machineGroupList.stream().filter(s -> StringUtils.equals(s.getDictValue(),excelVO.getMachineGroupName())).findFirst();
            machine.setMachineGroupCode(groupOpt.orElse(emptyDit).getDictKey());
 
            //entityList.add(machine);
            Machine oriMachine = machineService.getByCode(machine.getCode());
 
            if(oriMachine != null){
                msg.append(machine.getCode()).append("机床编码重复:").append(machine.getCode());
                failCount ++;
            }else if(machine.getMachineGroupCode() == null){
                msg.append(excelVO.getCode()+"机床组不匹配"+excelVO.getMachineGroupName()).append("<br/>");
                failCount ++;
            }else if(machine.getMachineSpec() == null){
                msg.append(excelVO.getCode()+"机床类型不匹配"+excelVO.getMachineSpecName()).append("<br/>");
                failCount ++;
            }else {
                try {
                    machineService.save(machine);
                    successCount++;
                }catch(Exception e){
                    log.error("入库失败",e);
                    failCount ++;
                    msg.append(excelVO.getCode()+"入库失败");
                }
            }
        }
 
        ir.setFailure(failCount);
        ir.setSuccess(successCount);
 
        if(ir.getFailure()==0){
            ir.setMessage("导入成功");
        }else{
            ir.setMessage(msg.toString());
        }
        return ir;
    }
 
    private ImportResult checkImport(List<MachineExcel> list) {
        StringBuffer msg = new StringBuffer();;
        int failCount = 0;
        MachineExcel excelVO;
        String temp;
        for(int i=0;i<list.size();i++){
            excelVO = list.get(0);
            temp ="";
            if(StringUtil.isBlank(excelVO.getCode())){
                //msg.append("第"+(i+1)+"行机床编码为空,");
                temp = "机床编码为空,";
            }
            if(StringUtil.isBlank(excelVO.getName())){
                //msg.append("第"+(i+1)+"行机床编码为空,");
                temp += "机床型号为空,";
            }
            if(StringUtil.isBlank(excelVO.getMachineGroupName())){
                temp += "机床组为空,";
            }
 
            if(StringUtil.isBlank(excelVO.getMachineSpecName())){
                temp += "机床类型为空,";
            }
 
 
            if(StringUtil.isNotBlank(temp)){
                msg.append("行"+(i+1)+" "+StringUtils.removeEnd(temp,",")).append(";");
                failCount ++;
            }
        }
 
        ImportResult res= new ImportResult();
        res.setFailure(failCount);
        res.setMessage(msg.toString());
 
        return res;
    }
}