yangys
2024-03-29 4b479381a65bd3ef526cb92631d3550f2aa17459
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
package com.qianwen.smartman.modules.fms.service.impl;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.redis.cache.BladeRedis;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.constant.ExtCacheConstant;
import com.qianwen.smartman.modules.cps.entity.CommonGroup;
import com.qianwen.smartman.modules.cps.enums.WorkTypeEnum;
import com.qianwen.smartman.modules.cps.service.ICommonGroupService;
import com.qianwen.smartman.modules.cps.service.IProductionCraftVersionService;
import com.qianwen.smartman.modules.cps.service.IWorkstationService;
import com.qianwen.smartman.modules.cps.vo.CraftVersionLineVO;
import com.qianwen.smartman.modules.cps.vo.WorkstationVO;
import com.qianwen.smartman.modules.dnc.entity.DncArtFile;
import com.qianwen.smartman.modules.dnc.service.IDncArtFileService;
import com.qianwen.smartman.modules.fms.dto.ProgramDmpStrDto;
import com.qianwen.smartman.modules.fms.dto.ProgramIssuanceDTO;
import com.qianwen.smartman.modules.fms.entity.FmsOrder;
import com.qianwen.smartman.modules.fms.entity.FmsOrderProcess;
import com.qianwen.smartman.modules.fms.enums.FmsOrderEnum;
import com.qianwen.smartman.modules.fms.forestClient.FmsForestClient;
import com.qianwen.smartman.modules.fms.mapper.FmsOrderMapper;
import com.qianwen.smartman.modules.fms.mapper.FmsOrderProcessMapper;
import com.qianwen.smartman.modules.fms.service.IFmsOrderProcessService;
import com.qianwen.smartman.modules.fms.vo.FmsWorkStationProcessVO;
import com.qianwen.smartman.modules.fms.vo.FmsWorkStationVO;
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/fms/service/impl/FmsOrderProcessServiceImpl.class */
public class FmsOrderProcessServiceImpl extends BaseServiceImpl<FmsOrderProcessMapper, FmsOrderProcess> implements IFmsOrderProcessService {
    private final FmsOrderMapper fmsOrderMapper;
    private final IProductionCraftVersionService productionCraftVersionService;
    private final IWorkstationService workstationService;
    private final ICommonGroupService commonGroupService;
    private final OssBuilder ossBuilder;
    private final IDncArtFileService artFileService;
    private final BladeRedis bladeRedis;
    private final FmsForestClient fmsForestClient;
 
 
    public FmsOrderProcessServiceImpl(final FmsOrderMapper fmsOrderMapper, final IProductionCraftVersionService productionCraftVersionService, final IWorkstationService workstationService, final ICommonGroupService commonGroupService, final OssBuilder ossBuilder, final IDncArtFileService artFileService, final BladeRedis bladeRedis, final FmsForestClient fmsForestClient) {
        this.fmsOrderMapper = fmsOrderMapper;
        this.productionCraftVersionService = productionCraftVersionService;
        this.workstationService = workstationService;
        this.commonGroupService = commonGroupService;
        this.ossBuilder = ossBuilder;
        this.artFileService = artFileService;
        this.bladeRedis = bladeRedis;
        this.fmsForestClient = fmsForestClient;
    }
 
    @Override // org.springblade.modules.fms.service.IFmsOrderProcessService
    public List<FmsWorkStationProcessVO> listWorkstation(String orderId) {
        List<FmsOrderProcess> fmsOrderProcesses = this.baseMapper.selectList(Wrappers.<FmsOrderProcess>query().lambda().eq(FmsOrderProcess::getOrderId, orderId));
        /*
        List<FmsOrderProcess> fmsOrderProcesses = ((FmsOrderProcessMapper) this.baseMapper).selectList((Wrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getOrderId();
        }, orderId));*/
        FmsOrder fmsOrder = this.fmsOrderMapper.selectOne(Wrappers.<FmsOrder>query().lambda().eq(FmsOrder::getId, orderId));
        /*
        FmsOrder fmsOrder = (FmsOrder) this.fmsOrderMapper.selectOne((Wrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getId();
        }, orderId));*/
        CraftVersionLineVO craftVersionLineVO = this.productionCraftVersionService.detailVersion(fmsOrder.getCraftVersionId());
        String groupIds = craftVersionLineVO.getGroupId();
        String[] groupSplit = groupIds.split(",");
        List<Long> commonGroupIds = this.commonGroupService.list(Wrappers.<CommonGroup>query().lambda().in(CommonGroup::getId, (Object[])groupSplit)).stream().filter(commonGroup -> commonGroup.getGroupTag().equals(FmsOrderEnum.FMS_BELTLINE.getCode())).map(CommonGroup::getId).collect(Collectors.toList());
        
        /*
        List<Long> commonGroupIds = (List) this.commonGroupService.list((Wrapper) Wrappers.query().lambda().in((v0) -> {
            return v0.getId();
        }, groupSplit)).stream().filter(commonGroup -> {
            return commonGroup.getGroupTag().equals(FmsOrderEnum.FMS_BELTLINE.getCode());
        }).map((v0) -> {
            return v0.getId();
        }).collect(Collectors.toList());*/
        
        List<Long> listGroupIds = new ArrayList<>();
        for (Long groupId : commonGroupIds) {
            listGroupIds.addAll(this.workstationService.productionLineList(groupId));
        }
        List<String> listGroupIdsOfString = listGroupIds.stream().map((v0) -> {
            return String.valueOf(v0);
        }).collect(Collectors.toList());
        List<WorkstationVO> workstationVOList = this.workstationService.getWorkstationByGroupIds(listGroupIdsOfString);
        List<FmsWorkStationProcessVO> fmsWorkStationProcessVOList = new ArrayList<>();
        fmsOrderProcesses.forEach(fmsOrderProcess -> {
            List<FmsWorkStationVO> fmsWorkStationVOS = new ArrayList<>();
            FmsWorkStationProcessVO fmsWorkStationProcessVO = new FmsWorkStationProcessVO();
            if (Func.isNotEmpty(fmsOrderProcess.getProcessType())) {
                List<WorkstationVO> workstationVOS = workstationVOList.stream().filter(workstationVO -> {
                    if (Objects.equals(fmsOrderProcess.getProcessType(), WorkTypeEnum.MEASURE.getCode())) {
                        return Objects.equals(workstationVO.getDeviceType(), fmsOrderProcess.getProcessType()) || Objects.equals(workstationVO.getDeviceType(), WorkTypeEnum.LOAD_AND_UNLOAD.getCode());
                    }
                    return Objects.equals(workstationVO.getDeviceType(), fmsOrderProcess.getProcessType());
                }).collect(Collectors.toList());
                workstationVOS.forEach(workstationVO2 -> {
                    FmsWorkStationVO fmsWorkStationVO = new FmsWorkStationVO();
                    fmsWorkStationVO.setWorkstationId(workstationVO2.getId());
                    fmsWorkStationVO.setWorkstationName(workstationVO2.getName());
                    fmsWorkStationVOS.add(fmsWorkStationVO);
                });
            }
            fmsWorkStationProcessVO.setProcessId(String.valueOf(fmsOrderProcess.getId()));
            fmsWorkStationProcessVO.setFmsWorkStationVOList(fmsWorkStationVOS);
            fmsWorkStationProcessVOList.add(fmsWorkStationProcessVO);
        });
        return fmsWorkStationProcessVOList;
    }
 
    @Override // org.springblade.modules.fms.service.IFmsOrderProcessService
    public void programIssuanceDmp(ProgramIssuanceDTO programIssuanceDTO) {
        DncArtFile dncArtFile;
        String orderProcessId = programIssuanceDTO.getOrderProcessId();
        FmsOrderProcess fmsOrderProcess = (FmsOrderProcess) getById(Long.valueOf(Func.toLong(orderProcessId)));
        if (fmsOrderProcess == null) {
            return;
        }
        Long programId = fmsOrderProcess.getProgramId();
        if (!Func.isEmpty(programId) && (dncArtFile = (DncArtFile) this.artFileService.getById(programId)) != null) {
            String originalFilename = Func.isNotBlank(dncArtFile.getOriginalFilename()) ? dncArtFile.getOriginalFilename() : dncArtFile.getFilename();
            this.bladeRedis.set(ExtCacheConstant.PROGRAM_ISSUANCE + programIssuanceDTO.getDeviceCode(), originalFilename);
            this.bladeRedis.set(ExtCacheConstant.PROGRAM_ISSUANCE_SWITCH + programIssuanceDTO.getDeviceCode(), originalFilename);
            String objectKey = dncArtFile.getObjectKey();
            String backMessage = "";
            try {
                InputStream inputStream = this.ossBuilder.template().getObject(objectKey);
                if (inputStream != null) {
                    ProgramDmpStrDto programDmpDto = new ProgramDmpStrDto();
                    String inputStreamString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                    programDmpDto.setFileName(originalFilename).setContent(inputStreamString).setDeviceCode(programIssuanceDTO.getDeviceCode());
                    backMessage = this.fmsForestClient.programIssuanceStrFile(programDmpDto);
                }
                JSONObject cncMessage = getCncMessage(backMessage);
                if (cncMessage != null) {
                    errorCode(cncMessage);
                }
            } catch (Exception e) {
                throw new ServiceException("dmp连接机床失败,请查看dmp是否正常运行再尝试连接");
            }
        }
    }
 
    private static String toUTF8(String str) {
        String result = str;
        try {
            result = changeCharSet(str);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    private static String changeCharSet(String str) throws UnsupportedEncodingException {
        if (str != null) {
            byte[] bs = str.getBytes();
            return new String(bs, StandardCharsets.UTF_8);
        }
        return null;
    }
 
    private JSONObject getCncMessage(String json) {
        return JSON.parseObject(json);
    }
 
    private void errorCode(JSONObject cncMessage) {
        String errCode = (String) cncMessage.get("errCode");
        if ("0001".equals(errCode)) {
            throw new ServiceException((String) cncMessage.get("errMsg"));
        }
    }
 
    public static byte[] inputStreamToByteArray(InputStream inputStream) {
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];
            int num;
            while ((num = inputStream.read(buffer)) != -1)
                byteArrayOutputStream.write(buffer, 0, num);
            byteArrayOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[0];
        }
        /*
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            while (true) {
                int num = inputStream.read(buffer);
                if (num == -1) {
                    break;
                }
                byteArrayOutputStream.write(buffer, 0, num);
            }
            byteArrayOutputStream.flush();
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            if (byteArrayOutputStream != null) {
                if (0 != 0) {
                    byteArrayOutputStream.close();
                } else {
                    byteArrayOutputStream.close();
                }
            }
            return byteArray;
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[0];
        }*/
    }
}