|
|
package com.ccl.tt.service.impl;
import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Map;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;
import com.ccl.tt.dao.BasicDao;import com.ccl.tt.dao.FinalRollDao;import com.ccl.tt.dao.PrepressDao;import com.ccl.tt.data.PartData;import com.ccl.tt.entity.Sequence;import com.ccl.tt.entity.SoInfo;import com.ccl.tt.model.SoOrderFileHist;import com.ccl.tt.repository.SequenceRepository;import com.ccl.tt.repository.SoInfoRepository;import com.ccl.tt.service.PrepressService;import com.ccl.tt.utils.DateUtils;import com.ccl.tt.utils.SerialUtil;
/** * * @ClassName: PrepressServiceImpl * @Description: 印前的方法实现 * @author: LR * @date: 2024年6月18日 下午5:59:42 * @Copyright: */@Servicepublic class PrepressServiceImpl implements PrepressService {
private static final Logger logger = LoggerFactory.getLogger(PrepressServiceImpl.class);
@Value("${ifs.datasource.sql.order}") String ifsOrderSql; @Value("${item.filePath}") private String filePath;
@Autowired private PrepressDao prepressDao; @Autowired private BasicDao basicDao; @Autowired private SoInfoRepository soInfoRepo; @Autowired private SequenceRepository seqRepo; @Autowired private FinalRollDao finalRollDao;
@Override public Map<String, Object> getIfsOrder(String orderNo) { Map<String, Object> resultMap = prepressDao.getResultMapBySql(ifsOrderSql, orderNo); //判断是否非空
if (resultMap == null || resultMap.isEmpty()) { throw new RuntimeException("Order information not found for " + orderNo + ":"); } //校验工单是否存在当前的物料编码
String partNo = String.valueOf(resultMap.get("part_no")); //判断物料是否存在
if(partNo == null || "".equals(partNo)) { throw new RuntimeException("Material information not found for " + orderNo + "!"); } //查询物料信息
PartData part = basicDao.getPartDataByPartNo(partNo); //判断是否存在
if(null == part) { throw new RuntimeException("Please maintain material code first:"+partNo+"!"); } //填充物料信息
resultMap.put("revNo", part.getRevNo()); resultMap.put("partNo", part.getPartNo()); resultMap.put("partDesc", part.getPartDesc()); resultMap.put("apn", part.getSerialApn()); resultMap.put("customerPartNo", part.getCustomerPartNo()); resultMap.put("standardRollQty", part.getPackageQty()); resultMap.put("noOfCross", part.getCross()); resultMap.put("projectCode", part.getProjectCode()); return resultMap; }
@Override @Transactional public void addSoInfo(SoInfo soInfo) throws Exception { String orderNo = soInfo.getOrderNo(); soInfo.setCreatedDate(new Timestamp(System.currentTimeMillis())); soInfo.setStatus(SoInfo.CREATED); soInfo.setIsrework("N"); String needDateStr = soInfo.getNeedDate(); Date needDate = DateUtils.getDateByParten(needDateStr, "yyyy-MM-dd"); String currentDateStr = DateUtils.getStringDate(new Date(), "yyyy-MM-dd"); Date currentDate = DateUtils.getAddNumsDateByType(DateUtils.getDateByParten(currentDateStr, "yyyy-MM-dd"), 3, "DAY"); //判断是否需要
if(needDate.getTime() > currentDate.getTime()) { throw new RuntimeException("Order planned start date is too late, please contact planning to modify!"); } //判断是否存在
SoInfo tempData = soInfoRepo.findByOrderNo(orderNo); if(tempData != null) { throw new RuntimeException("Current order already exists!"); } soInfo.setFirstRollTime(needDate); //需要判断当前的数量是否能够整排
int inputLotSize = soInfo.getInputLotSize(); int cross = soInfo.getNoOfCross(); if(inputLotSize % cross > 0) { inputLotSize = ((inputLotSize / cross) + 1) * cross; //重置工单得生产数量
soInfo.setInputLotSize(inputLotSize); } soInfo.validate(); //查询是否物料编码是否存在
String partNo = soInfo.getPartNo(); //判断物料是否存在
if(partNo == null || "".equals(partNo)) { throw new RuntimeException("Material code cannot be empty!"); } //查询物料信息
PartData part = basicDao.getPartDataByPartNo(partNo); //判断是否存在
if(null == part) { throw new RuntimeException("Please maintain material code first:"+partNo+"!"); }
//设置参数
soInfo.setLaminationFlag("Y"); //查询长度是否符合要求
String apn = soInfo.getApn(); if(apn.length() != 11) { throw new RuntimeException("Length of apn does not comply with the rules!"); } String revNo = soInfo.getRevNo(); if(revNo.length() != 1) { throw new RuntimeException("revNo length does not comply with the rules!"); } //设置参数
soInfo.setCustomerPartNoAll(part.getCustomerPartNo());
soInfoRepo.save(soInfo); }
@Override @Transactional public void genSerials(Integer id) { //查询工单得信息
SoInfo soInfo = soInfoRepo.findOne(id); //校验工单得信息
if (soInfo == null){ throw new RuntimeException("Order not found!"); } if (soInfo.getSerialsGenDate() != null){ throw new RuntimeException("Already generated!"); } //产生码包的模板
String snTemplate = genSerialTemplate(soInfo); if(snTemplate == null || "".equals(snTemplate)){ throw new RuntimeException("eeeecode not found!"); } soInfo.setSerialsTemplate(snTemplate);
//find existing
Sequence seq = seqRepo.findByTag(snTemplate); if (seq == null){ seq = new Sequence(); seq.setTag(snTemplate); seq.setSeq(0); }
soInfo.setSerialStart(seq.getSeq() + 1); soInfo.setSerialEnd(soInfo.getSerialStart() + soInfo.getInputLotSize() - 1); //调用方法产生当前码包文件
List<String> serialList = this.genSoOrderSerialList(soInfo);
//公共参数
String orderNo = soInfo.getOrderNo(); String partNo = soInfo.getPartNo(); //生成单排的文件
this.genSingleRowZipSerialFile(orderNo, partNo, serialList); //生成多排的文件
this.genMultiRowZipSerialFile(orderNo, partNo, serialList);
//保存工单得信息
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); soInfo.setRolls(0); soInfo.setStatus(SoInfo.GENERATED); soInfo.setSerialsGenBy(authentication.getName()); soInfo.setSerialsGenDate(new Timestamp(System.currentTimeMillis())); //保存sn的记录数据 以前2023RQ添加的
finalRollDao.deleteSNTemplateCopy(soInfo); finalRollDao.saveSNTemplateCopy(soInfo); //保存工单得信息
soInfoRepo.save(soInfo); //保存序列号的信息
seq.setSeq(soInfo.getSerialEnd()); seqRepo.save(seq); }
/** * * @Title: genMultiRowZipSerialFile * @Description: 创建多排的文件 * @author: LR * @date 2024年5月22日 下午2:48:25 * @return: void * @throws */ public void genMultiRowZipSerialFile(String orderNo, String partNo, List<String> serialList) { //查询物料的信息
PartData part = basicDao.getPartDataByPartNo(partNo); int crossNumber = part.getCross(); String apn = part.getApn(); String revNo = part.getRevNo(); int serialSize = serialList.size(); int rows = serialSize / crossNumber; String baseDatePath = DateUtils.getStringDate(new Date(), "yyyy-MM-dd"); String sourceFilePath = filePath + "\\" +baseDatePath; File sourceFile = new File(sourceFilePath); //判断是否存在不存在创建目录
if(!sourceFile.exists()) { sourceFile.mkdirs(); } //创建文件
long start = System.currentTimeMillis(); String zipFileName = orderNo+"-"+partNo+".zip"; File zipFile = new File(sourceFile, zipFileName); try (FileOutputStream fos = new FileOutputStream(zipFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos);) { String fileName = orderNo+"-"+partNo+".csv"; ZipEntry entry = new ZipEntry(fileName); zos.putNextEntry(entry); //写入ID
zos.write(( "ID-1").getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); //创建表头
for(int i = 1; i <= crossNumber; i++) { //首先循环生成表头的文件
zos.write(("SN"+i).getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); } //换行写数据
zos.write(("\r\n").getBytes("UTF-8")); //双重for循环开始写入数据
//外层for循环写行
for(int i = 0; i < rows; i++) { //写入行数
zos.write((i + 1+"").getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); //内层循环循环列
for(int j = 0; j < crossNumber; j++) { int serialIndex = (j * rows) + i; //写数据
zos.write((serialList.get(serialIndex)).getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); } //换行写数据
zos.write(("\r\n").getBytes("UTF-8")); } //把数据刷入缓存中
zos.flush(); bos.flush(); fos.flush(); } catch (Exception e) { e.printStackTrace(); } //记录时间
long duration = System.currentTimeMillis() - start; logger.info("Generated SN for order [{}], size={}, duration={}.", orderNo, serialList.size(), duration); //创建文件记录
//创建文件记录
SoOrderFileHist fileHist = new SoOrderFileHist(); fileHist.setSite("41"); fileHist.setOrderNo(orderNo); fileHist.setFileName(zipFileName); fileHist.setFilePath(zipFile.getAbsolutePath()); fileHist.setIsMulti("Y"); //保存当前的文件记录
prepressDao.insertSoOrderFileHist(fileHist); }
/** * * @Title: genSingleRowZipSerialFile * @Description: * @author: LR * @date 2024年5月22日 下午2:00:30 * @return: void * @throws */ public void genSingleRowZipSerialFile(String orderNo, String partNo, List<String> serialList) { //公共参数
String baseDatePath = DateUtils.getStringDate(new Date(), "yyyy-MM-dd"); String sourceFilePath = filePath + "\\" +baseDatePath; File sourceFile = new File(sourceFilePath); //判断是否存在不存在创建目录
if(!sourceFile.exists()) { sourceFile.mkdirs(); } //创建文件
long start = System.currentTimeMillis(); String zipFileName = orderNo+"single.zip"; File zipFile = new File(sourceFile, zipFileName); try (FileOutputStream fos = new FileOutputStream(zipFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos);) { String fileName = orderNo+"single.csv"; ZipEntry entry = new ZipEntry(fileName); zos.putNextEntry(entry); //创建表头
zos.write(("SN").getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); zos.write(( "ID" + "\r\n").getBytes("UTF-8")); //循环开始写入数据
for(int i = 0; i < serialList.size(); i++) { //写数据
zos.write((serialList.get(i)).getBytes("UTF-8")); zos.write((",").getBytes("UTF-8")); zos.write(((i + 1) + "\r\n").getBytes("UTF-8")); } //把数据刷入缓存中
zos.flush(); bos.flush(); fos.flush(); } catch (Exception e) { e.printStackTrace(); }
long duration = System.currentTimeMillis() - start; logger.info("Generated SN for order [{}], size={}, duration={}.", orderNo, serialList.size(), duration); //创建文件记录
SoOrderFileHist fileHist = new SoOrderFileHist(); fileHist.setSite("41"); fileHist.setOrderNo(orderNo); fileHist.setFileName(zipFileName); fileHist.setFilePath(zipFile.getAbsolutePath()); fileHist.setIsMulti("N"); //保存当前的文件记录
prepressDao.insertSoOrderFileHist(fileHist); }
/** * * @Title: genSoOrderSerialList * @Description: 产生按照要求符合条件的码包数据 * @author: LR * @date 2024年6月19日 下午3:01:40 * @return: List<String> * @throws */ public List<String> genSoOrderSerialList(SoInfo soInfo) { List<String> snList = new ArrayList<>(); String partNo = soInfo.getPartNo(); //查询当前物料的数据
PartData part = basicDao.getPartDataByPartNo(partNo); String codeType = part.getCodeType(); for (int snIndex = soInfo.getSerialStart(); snIndex <= soInfo.getSerialEnd(); snIndex++) { String currentSn = SerialUtil.getSerialNo(soInfo.getSerialsTemplate(), snIndex, codeType); snList.add(currentSn); } return snList; }
/** * * @Title: genSerialTemplate * @Description: 产生码包的模板的方法 * @author: LR * @date 2024年6月19日 下午2:50:09 * @return: String * @throws */ public String genSerialTemplate(SoInfo soInfo) { //公共参数
String partNo = soInfo.getPartNo(); //查询物料的属性
PartData part = basicDao.getPartDataByPartNo(partNo); //查询相关的参数
String codeType = part.getCodeType();
Calendar cal = Calendar.getInstance(); cal.setTime(soInfo.getSerialsDate()); String platCode3 = "J4K"; String year1 = String.valueOf(cal.get(Calendar.YEAR) % 10); String week2 = org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(cal.get(Calendar.WEEK_OF_YEAR)), 2, '0'); String day1 = String.valueOf(cal.get(Calendar.DAY_OF_WEEK)); String code4 = "____"; String appleEEEE4 = part.getEeeeCode(); String rev1 = part.getRevNo(); String delimiter1 = "+"; String apn = part.getSerialApn(); String checkSum1 = "_"; String snLeft7 = platCode3 + year1 + week2 + day1; String snMiddle5 = appleEEEE4 + rev1; String snRight = delimiter1 + apn; String snFormat = null; //区分不同的类型
if ("2024".equals(codeType)) { snFormat = snLeft7 + code4 + snMiddle5 + checkSum1 + snRight; }else if("2025".equals(codeType)) { snFormat = snLeft7 + code4 + snMiddle5 + checkSum1; } return snFormat; }
}
|