李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.technology.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.ifs.api.feign.IfsFeignClient;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.entity.PartFamily;
import com.chinaztt.mes.basic.entity.Template;
import com.chinaztt.mes.basic.mapper.PartFamilyMapper;
import com.chinaztt.mes.basic.mapper.BasicParamTemplateMapper;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.quality.entity.TestStandard;
import com.chinaztt.mes.quality.entity.TestStandardBinding;
import com.chinaztt.mes.quality.mapper.TestStandardBindingMapper;
import com.chinaztt.mes.quality.mapper.TestStandardMapper;
import com.chinaztt.mes.technology.dto.OperationDTO;
import com.chinaztt.mes.technology.dto.StepDTO;
import com.chinaztt.mes.technology.entity.*;
import com.chinaztt.mes.technology.excel.OperationData;
import com.chinaztt.mes.technology.mapper.*;
import com.chinaztt.mes.technology.service.OperationService;
import com.chinaztt.ztt.common.core.util.R;
import com.chinaztt.ztt.common.oss.OssProperties;
import com.chinaztt.ztt.common.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 工序
 *
 * @author zhangxy
 * @date 2020-08-18 10:22:27
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {
    private final OssProperties ossProperties;
    private final OssTemplate minioTemplate;
    private final OperationAttachmentMapper operationAttachmentMapper;
 
    private BasicParamTemplateMapper templateMapper;
    private IfsFeignClient ifsFeignClient;
    private PartMapper partMapper;
    private PartFamilyMapper partFamilyMapper;
    private OperationJoinStepMapper operationJoinStepMapper;
    private OperationMapper operationMapper;
    private OperationJoinTemplateMapper operationJoinTemplateMapperMapper;
    private TestStandardMapper testStandardMapper;
    private TestStandardBindingMapper testStandardBindingMapper;
 
    @Override
    public IPage<List<Operation>> getOperationPage(Page page, QueryWrapper<Operation> ew) {
        return baseMapper.getOperationPage(page, ew);
    }
 
    @Override
    public OperationDTO getFullById(Long id) {
        OperationDTO operationDTO = baseMapper.getFullById(id);
        List<Template> operationTemplate = templateMapper.getOperationTemplate(id);
        operationDTO.setOperationTemplateList(operationTemplate);
        List<StepDTO> stepList = operationJoinStepMapper.getStep(id);
        operationDTO.setStepList(stepList);
        return operationDTO;
    }
 
    @Override
    public R<Long> fullSave(OperationDTO operation) {
        int noCount = baseMapper.selectCount(Wrappers.<Operation>lambdaQuery().eq(Operation::getOperationNo, operation.getOperationNo()));
        if (noCount > 0) {
            return R.failed("编号重复");
        }
        BigDecimal bigDecimal = operation.getLaborRunFactor().add(operation.getLaborSetupTime()).add(operation.getMachRunFactor()).add(operation.getMachSetupTime());
        if (bigDecimal.compareTo(BigDecimal.ZERO) < 1) {
            return R.failed("人工,机器运转时间,因素不可都为0");
        }
        baseMapper.insert(operation);
        baseMapper.saveOpCapRecord(operation);
        return R.ok(operation.getId());
    }
 
    @Override
    public R<Boolean> fullDelete(Long id) {
        baseMapper.deleteById(id);
        operationJoinTemplateMapperMapper.delTechnology(id);
        operationJoinStepMapper.delete(Wrappers.<OperationJoinStep>lambdaQuery().eq(OperationJoinStep::getTechnologyOperationId, id));
        return R.ok();
    }
 
    @Override
    public R<Boolean> fullUpdate(OperationDTO operation) {
        int noCount = baseMapper.selectCount(Wrappers.<Operation>lambdaQuery().ne(Operation::getId, operation.getId()).eq(Operation::getOperationNo, operation.getOperationNo()));
        if (noCount > 0) {
            return R.failed("编号重复");
        }
        BigDecimal bigDecimal = operation.getLaborRunFactor().add(operation.getLaborSetupTime()).add(operation.getMachRunFactor()).add(operation.getMachSetupTime());
        if (bigDecimal.compareTo(BigDecimal.ZERO) < 1) {
            return R.failed("人工,机器运转时间,因素不可都为0");
        }
        baseMapper.updateById(operation);
        baseMapper.saveOpCapRecord(operation);
        return R.ok();
    }
 
    @Override
    public R uploadFile(MultipartFile file, Long operationId) {
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
        String url = String.format("/mes/operation/%s/%s", ossProperties.getBucketName(), fileName);
        try {
            minioTemplate.putObject(ossProperties.getBucketName(), fileName, file.getInputStream());
 
            OperationAttachment attachment = new OperationAttachment();
            attachment.setOriginal(file.getOriginalFilename());
            attachment.setPath(url);
            attachment.setFileName(fileName);
            attachment.setOperationId(operationId);
            operationAttachmentMapper.insert(attachment);
 
        } catch (Exception e) {
            log.error("上传失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.failed(e.getLocalizedMessage());
        }
        return R.ok();
    }
 
    @Override
    public void getFile(String bucket, String fileName, HttpServletResponse response) {
        try (InputStream inputStream = minioTemplate.getObject(bucket, fileName)) {
            response.setContentType("application/octet-stream; charset=UTF-8");
            IoUtil.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("文件读取异常: {}", e.getLocalizedMessage());
        }
    }
 
    @Override
    @SneakyThrows
    public R<Boolean> deleteFile(String fileName) {
        OperationAttachment attachment = operationAttachmentMapper.selectOne(Wrappers.<OperationAttachment>lambdaQuery().eq(OperationAttachment::getFileName, fileName));
        minioTemplate.removeObject(ossProperties.getBucketName(), attachment.getFileName());
        operationAttachmentMapper.deleteById(attachment.getId());
        return R.ok();
    }
 
    @Override
    public void importExcel(List<OperationData> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        List<PartFamily> partFamilies = partFamilyMapper.selectList(null);
        for (OperationData data : list) {
            Operation operation = new Operation();
            partFamilies.forEach(a -> {
                if (a.getPartFamilyNo().equals(data.getPartFamilyNo())) {
                    operation.setPartFamilyId(a.getId());
                }
 
            });
            if (operation.getPartFamilyId() == null) {
                log.error("零件族:" + data.getPartFamilyNo() + "不存在");
                continue;
            }
            operation.setOperationNo(data.getOperationNo());
            operation.setName(data.getName());
            baseMapper.insert(operation);
        }
    }
 
    @Override
    public boolean deleteOperationTemplate(OperationJoinTemplate operationJoinTemplate) {
        operationJoinTemplateMapperMapper.delete(Wrappers.<OperationJoinTemplate>lambdaQuery().eq(OperationJoinTemplate::getTechnologyOperationId, operationJoinTemplate.getTechnologyOperationId())
                .eq(OperationJoinTemplate::getTechnologyTemplateId, operationJoinTemplate.getTechnologyTemplateId()));
        return true;
    }
 
    @Override
    public R saveOperationTemplate(OperationDTO operationDTO) {
        if (CollectionUtil.isEmpty(operationDTO.getTemplateIds())) {
            return R.failed("添加的模板为空");
        }
        List<Long> newOperationJoinTemplateList = new ArrayList<>();
        List<OperationJoinTemplate> operationJoinTemplateList = operationJoinTemplateMapperMapper.selectList(Wrappers.<OperationJoinTemplate>lambdaQuery().eq(OperationJoinTemplate::getTechnologyOperationId, operationDTO.getId()));
        A:
        for (Long operationTemplateId : operationDTO.getTemplateIds()) {
            for (OperationJoinTemplate operationJoinTemplate : operationJoinTemplateList) {
                if (operationTemplateId.equals(operationJoinTemplate.getTechnologyTemplateId())) {
                    continue A;
                }
            }
            newOperationJoinTemplateList.add(operationTemplateId);
        }
        for (Long operationParamId : newOperationJoinTemplateList) {
            OperationJoinTemplate operationTemplate = new OperationJoinTemplate();
            operationTemplate.setTechnologyOperationId(operationDTO.getId());
            operationTemplate.setTechnologyTemplateId(operationParamId);
            operationJoinTemplateMapperMapper.insert(operationTemplate);
        }
        return R.ok("添加成功");
    }
 
    @Override
    public List<StepDTO> saveOperationStep(List<OperationJoinStep> operationJoinStepList, Long id) {
        List<StepDTO> stepList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(operationJoinStepList)) {
            operationJoinStepMapper.delete(Wrappers.<OperationJoinStep>lambdaQuery().eq(OperationJoinStep::getTechnologyOperationId, operationJoinStepList.get(0).getTechnologyOperationId()));
            for (OperationJoinStep operationJoinStep : operationJoinStepList) {
                operationJoinStepMapper.insert(operationJoinStep);
            }
            stepList = operationJoinStepMapper.getStep(operationJoinStepList.get(0).getTechnologyOperationId());
        } else {
            operationJoinStepMapper.delete(Wrappers.<OperationJoinStep>lambdaQuery().eq(OperationJoinStep::getTechnologyOperationId, id));
        }
        return stepList;
    }
 
    @Override
    public R getIfsLaborClass(String laborClassNo) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotBlank(laborClassNo)) {
            jsonObject.put("LABOR_CLASS_NO", laborClassNo);
        }
        R result = ifsFeignClient.queryLaborClassStd(jsonObject, true);
        if (result.getCode() == 0) {
            JSONObject data = (JSONObject) JSON.toJSON(result.getData());
            return R.ok(data.getJSONArray("LIST_INFO"), "OK");
        } else {
            return R.failed("IFS错误——" + result.getMsg());
        }
    }
 
    @Override
    public void importTestStandardBindingExcel(Map<Integer, String> headMap, List<Map<Integer, String>> dataList) {
        for (Map<Integer, String> map : dataList) {
            String partNo = map.get(0);
            String operationName = map.get(1);
            String standardNo = map.get(2);
 
            TestStandardBinding testStandardBinding = new TestStandardBinding();
            //testStandardBindingData
            Part part = partMapper.selectOne(Wrappers.<Part>lambdaQuery().eq(Part::getPartNo, partNo));
            if (null == part) {
                throw new RuntimeException("找不到零件编号对应的零件,请确认! " + partNo);
            }
            testStandardBinding.setPartId(part.getId());
            testStandardBinding.setPartName(part.getPartName());
            testStandardBinding.setPartNo(part.getPartNo());
 
            Operation operation = operationMapper.selectOne(Wrappers.<Operation>lambdaQuery().eq(Operation::getName
                    , operationName));
            if (null == operation) {
                throw new RuntimeException("找不到工序名称对应的工序,请确认! " + operationName);
            }
            testStandardBinding.setOperationId(operation.getId());
            testStandardBinding.setOperationName(operation.getName());
            testStandardBinding.setOperationNo(operation.getOperationNo());
 
            TestStandard testStandard = testStandardMapper.selectOne(Wrappers.<TestStandard>lambdaQuery()
                    .eq(TestStandard::getStandardNo, standardNo));
            if (null == testStandard) {
                throw new RuntimeException("找不到标准编号对应的标准,请确认! " + standardNo);
            }
            testStandardBinding.setStandardId(testStandard.getId());
            testStandardBinding.setStandardName(testStandard.getStandardName());
            testStandardBinding.setStandardNo(testStandard.getStandardNo());
            testStandardBindingMapper.insert(testStandardBinding);
        }
    }
}