diff --git a/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/base/file/IMesFileService.java b/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/base/file/IMesFileService.java new file mode 100644 index 0000000..0e92929 --- /dev/null +++ b/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/base/file/IMesFileService.java @@ -0,0 +1,33 @@ +package cn.estsh.i3plus.ext.mes.api.base.file; + +import cn.estsh.i3plus.pojo.platform.bean.SysFile; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; + +/** + * @Description : + * @Reference : + * @Author : junsheng.li + * @CreateDate 2024/7/19 11:54 + * @Modify: + **/ +public interface IMesFileService { + + /** + * 上传文件 + * @param file 文件 + * @return 文件信息 + */ + @ApiOperation(value = "上传文件", notes = "上传文件") + SysFile uploadFile(MultipartFile file); + + /** + * 获取文件 + * @param resp + * @param fileUrl 文件url + * @return 文件流 + */ + void getFileByUrl(HttpServletResponse resp, String fileUrl); +} diff --git a/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/busi/IMesInputDefectRecordService.java b/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/busi/IMesInputDefectRecordService.java new file mode 100644 index 0000000..7923660 --- /dev/null +++ b/modules/i3plus-ext-mes-api/src/main/java/cn/estsh/i3plus/ext/mes/api/busi/IMesInputDefectRecordService.java @@ -0,0 +1,17 @@ +package cn.estsh.i3plus.ext.mes.api.busi; + +import cn.estsh.i3plus.pojo.mes.bean.MesDefectType; +import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn; +import io.swagger.annotations.ApiOperation; +/** + * @Description :缺陷记录 + * @Reference : + * @Author : junsheng.li + * @CreateDate 2024/7/19 15:32 + * @Modify: + **/ +public interface IMesInputDefectRecordService { + + @ApiOperation(value = "生成缺陷记录") + void savePartInspection(MesProduceSn mesProduceSn, String userName, MesDefectType mesDefect,String defectLocation); +} diff --git a/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/base/file/MesFileServiceImpl.java b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/base/file/MesFileServiceImpl.java new file mode 100644 index 0000000..b2d97ab --- /dev/null +++ b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/base/file/MesFileServiceImpl.java @@ -0,0 +1,136 @@ +package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base.file; + +import cn.estsh.i3plus.ext.mes.api.base.file.IMesFileService; +import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum; +import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil; +import cn.estsh.i3plus.pojo.platform.bean.SysFile; +import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder; +import cn.estsh.impp.framework.boot.fileservice.ImppFileService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * @Description :文件上传 + * @Reference : + * @Author : junsheng.li + * @CreateDate 2024/7/19 11:55 + * @Modify: + **/ +@Slf4j +@Service +public class MesFileServiceImpl implements IMesFileService { + + @Autowired + private ImppFileService imppFileService; + + @Override + public SysFile uploadFile(MultipartFile file) { + SysFile sysFile; + try { + log.info("文件大小{}", file.getBytes().length); + sysFile = save2FastDfs(file); + log.info("after save2FastDfs {}", sysFile); + } catch (IOException e) { + log.error("FastDFS上传文件出错:{}", e.getMessage() + "\r" + e.getStackTrace()); + throw ImppExceptionBuilder.newInstance() + .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode()) + .setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode()) + .setErrorDetail("上传文件至文件服务器出错了。信息为:%s", e.getMessage()) + .build(); + } + if (null == sysFile) { + log.error("FastDFS上传文件出错"); + throw ImppExceptionBuilder.newInstance() + .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode()) + .setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode()) + .setErrorDetail("FastDFS上传文件出错") + .build(); + } + if (StringUtils.isEmpty(sysFile.getFileTypeId())) { + log.error("FastDFS上传文件出错:{}", "未在字典中定义该文件类型,无法上传成功,请先添加file_type字典"); + throw ImppExceptionBuilder.newInstance() + .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode()) + .setErrorCode(ImppExceptionEnum.IO_EXCEPTION_FILE.getCode()) + .setErrorDetail("未在字典中定义该文件类型,无法上传成功,请先添加file_type字典") + .build(); + } + return sysFile; + } + + /** + * 上传文件至FastDfs,目前私有,因为没有别的地方进行调用。 + * + * @param multipartFile 文件 + * @return 带有URL地址的对象 + * @throws IOException + */ + private SysFile save2FastDfs(MultipartFile multipartFile) throws IOException { + SysFile sysFile = imppFileService.upload(multipartFile, CommonEnumUtil.SOFT_TYPE.MES.getValue()); + return sysFile; + } + + @Override + public void getFileByUrl(HttpServletResponse resp, String fileUrl) { + InputStream inputStream = getInputStream(fileUrl); + writeFile(resp, inputStream); + } + + private static InputStream getInputStream(String urlPath) { + InputStream inputStream = null; + HttpURLConnection httpURLConnection; + try { + URL url = new URL(urlPath); + httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setConnectTimeout(3000); + httpURLConnection.setDoInput(true); + httpURLConnection.setRequestMethod("GET"); + int responseCode = httpURLConnection.getResponseCode(); + System.out.println("responseCode is:" + responseCode); + if (responseCode == HttpURLConnection.HTTP_OK) { + inputStream = httpURLConnection.getInputStream(); + } else { + inputStream = httpURLConnection.getErrorStream(); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return inputStream; + } + + private static void writeFile(HttpServletResponse resp, InputStream inputStream) { + OutputStream out = null; + try { + out = resp.getOutputStream(); + int len; + byte[] b = new byte[1024]; + while ((len = inputStream.read(b)) != -1) { + out.write(b, 0, len); + } + out.flush(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (out != null) { + out.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} diff --git a/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/busi/MesInputDefectRecordServiceImpl.java b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/busi/MesInputDefectRecordServiceImpl.java new file mode 100644 index 0000000..ab637cc --- /dev/null +++ b/modules/i3plus-ext-mes-apiservice/src/main/java/cn/estsh/i3plus/ext/mes/apiservice/serviceimpl/busi/MesInputDefectRecordServiceImpl.java @@ -0,0 +1,139 @@ +package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi; + +import cn.estsh.i3plus.ext.mes.api.base.IMesConfigService; +import cn.estsh.i3plus.ext.mes.api.base.IMesPartSapService; +import cn.estsh.i3plus.ext.mes.api.busi.IMesInputDefectRecordService; +import cn.estsh.i3plus.mes.api.iservice.busi.ISyncFuncService; +import cn.estsh.i3plus.platform.common.convert.ConvertBean; +import cn.estsh.i3plus.platform.common.tool.TimeTool; +import cn.estsh.i3plus.pojo.mes.bean.MesDefectType; +import cn.estsh.i3plus.pojo.mes.bean.MesMove; +import cn.estsh.i3plus.pojo.mes.bean.MesPartSap; +import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn; +import cn.estsh.i3plus.pojo.mes.bean.nc.MesPartInspection; +import cn.estsh.i3plus.pojo.mes.bean.nc.MesPartInspectionDetail; +import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel; +import cn.estsh.i3plus.pojo.mes.repository.MesMoveRepository; +import cn.estsh.i3plus.pojo.mes.repository.MesPartInspectionDetailRepository; +import cn.estsh.i3plus.pojo.mes.repository.MesPartInspectionRepository; +import cn.estsh.i3plus.pojo.mes.repository.MesProduceSnRepository; +import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil; +import cn.estsh.impp.framework.boot.auth.AuthUtil; +import cn.estsh.impp.framework.boot.util.ResultBean; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + +import java.util.Objects; + +/** + * @Description : 缺陷记录 + * @Reference : + * @Author : junsheng.li + * @CreateDate 2024/7/19 15:52 + * @Modify: + **/ + +@Service +@Slf4j +public class MesInputDefectRecordServiceImpl implements IMesInputDefectRecordService { + @Autowired + private MesMoveRepository moveRepository; + + @Autowired + private MesPartInspectionRepository partInspectionRepository; + + @Autowired + private MesPartInspectionDetailRepository partInspectionDetailRepository; + + @Autowired + private IMesConfigService configService; + + @Autowired + private ISyncFuncService syncFuncService; + + @Autowired + private MesProduceSnRepository mesProduceSnRepository; + + @Autowired + private IMesPartSapService mesPartSapService; + + @Override + public void savePartInspection(MesProduceSn mesProduceSn, String userName, MesDefectType mesDefect,String defectLocation) { + MesPartSap mesPartSap = mesPartSapService.getMesPartSapByPartNo(mesProduceSn.getPartNo(), mesProduceSn.getOrganizeCode()); + MesPartInspection mesPartInspection = partInspectionRepository.insert(createPartInspection(mesPartSap, mesProduceSn.getProductSn(), mesProduceSn.getOrganizeCode(), userName)); + saveDetail(mesProduceSn.getOrganizeCode(), mesDefect, mesPartInspection.getId(), userName,defectLocation); + //更新条码状态 + mesProduceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.SUSPICIOUS.getValue()); + ConvertBean.serviceModelInitialize(mesProduceSn, userName); + mesProduceSnRepository.update(mesProduceSn); + //移库 + createMove(mesPartSap, mesPartInspection.getId(), configService.getCfgValue(mesProduceSn.getOrganizeCode(), "LGORT"), configService.getCfgValue(mesProduceSn.getOrganizeCode(), "UMLGO"), mesProduceSn); + } + + private void saveDetail(String organizeCode, MesDefectType mesDefect, long partInspectionId, String userName,String defectLocation) { + MesPartInspectionDetail detail = new MesPartInspectionDetail(); + if (!Objects.isNull(mesDefect)) { + detail.setDefectTypeId(mesDefect.getId()); + BeanUtils.copyProperties(mesDefect, detail); + } + detail.setId(null); + detail.setDefectLocation(defectLocation); + detail.setFrontBack(MesExtEnumUtil.DEFECT_ALARM_CONFIG_SIDES.FRONT.getValue()); + detail.setOrganizeCode(organizeCode); + detail.setPid(partInspectionId); + ConvertBean.serviceModelInitialize(detail, userName); + partInspectionDetailRepository.insert(detail); + } + + private MesPartInspection createPartInspection(MesPartSap mesPartSap, String sn, String org, String userName) { + MesPartInspection partInspection = new MesPartInspection(); + partInspection.setOrganizeCode(org); + partInspection.setPartNo(mesPartSap.getPartNo()); + partInspection.setPartName(mesPartSap.getPartName()); + partInspection.setInspectionDate(TimeTool.getToday()); + partInspection.setInspectionStatus(MesExtEnumUtil.PART_INSPECTION_STATUS.FAIL.getValue()); + partInspection.setNcStatus(MesExtEnumUtil.PART_INSPECTION_NC_STATUS.CREATE.getValue()); + partInspection.setSn(sn); + partInspection.setQty(1); + partInspection.setSourceType(10); + ConvertBean.serviceModelInitialize(partInspection, userName); + return partInspection; + } + + /** + * 移库 + * + * @param source 来源 + * @param target 目标 + * @return + */ + private void createMove(MesPartSap mesPartSap, long partInspectionId, String source, String target, MesProduceSn mesProduceSn) { + GenSerialNoModel serialNoModel = new GenSerialNoModel("INPUT_DEFECT_ZRSUM"); + serialNoModel.setPartNo(mesPartSap.getPartNo()); + ResultBean rb = syncFuncService.syncSerialNo(serialNoModel, AuthUtil.getSessionUser().getUserName(), mesProduceSn.getOrganizeCode(), 1); + String zrsum = ""; + if (null != rb && !CollectionUtils.isEmpty(rb.getResultList())) { + zrsum = (rb.getResultList().get(0)).toString(); + } + + MesMove move = new MesMove(); + move.setMatnr(mesPartSap.getPartNo()); + move.setOrganizeCode(mesProduceSn.getOrganizeCode()); + move.setFactoryCode(mesProduceSn.getOrganizeCode()); + move.setLgort(source); + move.setUmlgo(target); + move.setMenge(1d); + move.setMeins(mesPartSap.getUnit()); + move.setZrsum(zrsum); + move.setPostDate(TimeTool.getToday()); + move.setPostTime(TimeTool.getTimeShortWithColon()); + move.setPartInspectionId(partInspectionId); + move.setMoveType(MesExtEnumUtil.MOVE_TYPE.SUSPICIOUS_MOVE.getValue()); + move.setProductSn(mesProduceSn.getProductSn()); + ConvertBean.serviceModelInitialize(move, AuthUtil.getSessionUser().getUserName()); + moveRepository.insert(move); + } +}