/*
|
* 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);
|
}
|
}
|
}
|