package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import com.whyc.dto.FileDirPath;
|
import com.whyc.dto.Response;
|
import com.whyc.mapper.MaterialMapper;
|
import com.whyc.pojo.DocUser;
|
import com.whyc.pojo.Material;
|
import com.whyc.pojo.ProductBom;
|
import com.whyc.util.ActionUtil;
|
import com.whyc.util.CommonUtil;
|
import com.whyc.util.FileUtil;
|
import com.whyc.util.Zip4jUtil;
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
import org.apache.poi.ss.usermodel.*;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.FileCopyUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.LinkedList;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class MaterialService {
|
@Autowired(required = false)
|
private MaterialMapper mapper;
|
//查询所有的散装件信息加设置了替换关系的子件信息
|
/*public Response getAllMaterial(String category,String subCode,String subName,String subModel,int pageCurr,int pageSize) {
|
PageHelper.startPage(pageCurr,pageSize);
|
*//*QueryWrapper wrapper=new QueryWrapper();
|
wrapper.orderByAsc("id");
|
List<Material> list=mapper.selectList(wrapper);*//*
|
List<Material> list=mapper.getAllMaterial(category,subCode,subName,subModel);
|
PageInfo pageInfo=new PageInfo(list);
|
return new Response().setII(1,list.size()>0,pageInfo,"返回所有的散装件");
|
}*/
|
//查询所有的散装件信息不分页
|
/*public Response getMaterialWithoutSub(String parentModel, String subName,int version) {
|
List<Material> list=mapper.getMaterialWithoutSub(parentModel,subName,version);
|
return new Response().setII(1,list.size()>0,list,"返回子件没有添加过联系的散装件");
|
}*/
|
//删除散装件(将散装件标识为不可用)
|
/*public Response deleteMaterial(int id) {
|
UpdateWrapper wrapper=new UpdateWrapper();
|
wrapper.set("status",0);
|
wrapper.eq("id",id);
|
int bl=mapper.update(null,wrapper);
|
return new Response().setII(1,bl>0,bl,"将散装件标识为不可用");
|
}*/
|
|
public List<Material> getList() {
|
return mapper.selectList(null);
|
}
|
|
public void insertBatch(List<Material> MaterialList) {
|
mapper.insertBatchSomeColumn(MaterialList);
|
}
|
|
/**mybatis-plus逻辑删除*/
|
public void deleteBatch(List<Integer> MaterialIdList) {
|
mapper.deleteBatchIds(MaterialIdList);
|
}
|
|
//查询所有物料(分页,模糊查询)
|
public Response getMaterialLimit(String subCode, String subName, String subModel, int pageCurr, int pageSize) {
|
PageHelper.startPage(pageCurr,pageSize);
|
List<Material> list=mapper.getMaterialLimit(subCode,subName,subModel);
|
PageInfo pageInfo=new PageInfo(list);
|
return new Response().setII(1,list.size()>0,pageInfo,"查询所有物料(分页,模糊查询)");
|
}
|
//建立关联时查询所有的物料(不分页)
|
public Response getAllMaterialNoLimit() {
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.ne("status",0);
|
wrapper.orderByAsc("id");
|
List<Material> list=mapper.selectList(wrapper);
|
return new Response().setII(1,list.size()>0,list,"查询所有物料(不分页)");
|
}
|
|
public List<String> getListByCodeList(List<String> codeList) {
|
return mapper.getListByCodeList(codeList);
|
}
|
|
public void updateDwgUrl(List<Material> materialDwgUrlNameList) {
|
mapper.updateDwgUrl(materialDwgUrlNameList);
|
}
|
|
@Transactional
|
public Response add(List<Material> materialList) {
|
String fileUrl = materialList.get(0).getFileUrl();
|
//校验物料合理性,非空校验:物料名称/物料编号/物料型号
|
for (int i = 0; i < materialList.size(); i++) {
|
Material material = materialList.get(i);
|
if(material.getSubCode()==null ||material.getSubCode().equals("")
|
|| material.getSubName()==null ||material.getSubName().equals("")
|
|| material.getSubModel()==null ||material.getSubModel().equals("")){
|
return new Response().set(1,false,"拒绝新增,物料名称/物料编号/物料型号存在空值");
|
}
|
}
|
//检验物料重复性,已存在的物料,拒绝重复提交,判断依据:物料编码+物料型号
|
List<Material> existList = getListByCodeAndModelList(materialList);
|
if(existList!=null && existList.size()!=0){
|
return new Response().setII(1,false,existList,"拒绝新增,物料重复提交");
|
}
|
|
//获取路径下的所有文件
|
List<String> fileUrlList = new LinkedList<>();
|
List<String> dwgUrlList = null;
|
List<String> picUrlList = null;
|
List<String> materialUrlList = new LinkedList<>();
|
|
fileUrlList = FileUtil.getStaticFilePath(new File(fileUrl),fileUrlList);
|
dwgUrlList = fileUrlList.stream().filter(url->url.contains(".dwg")).collect(Collectors.toList());
|
picUrlList = fileUrlList.stream().filter(url->url.contains(".png") || url.contains(".jpeg")).collect(Collectors.toList());
|
//图纸的url预设
|
dwgUrlList.forEach(dwgUrl-> {
|
materialList.forEach(material -> {
|
String filename = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1, dwgUrl.length() - 4);
|
String fileFullName = dwgUrl.substring(dwgUrl.lastIndexOf(File.separator) + 1);
|
if (material.getSubModel().toUpperCase().equals(filename.toUpperCase())) {
|
materialUrlList.add(dwgUrl);
|
|
material.setDwgUrl("doc_file" + File.separator + "material"
|
+ File.separator + fileFullName);
|
}
|
});
|
});
|
//图片的url预设
|
//一定是有对应物料的,从excel内剥离的
|
picUrlList.forEach(picUrl->{
|
for (Material material : materialList){
|
String filename = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1,picUrl.lastIndexOf("."));
|
String fileFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
|
if(material.getSubModel().toUpperCase().equals(filename.toUpperCase())){
|
material.setPictureUrl("doc_file" + File.separator + "material"
|
+ File.separator + fileFullName);
|
break;
|
}
|
}
|
});
|
//图纸文件转移到正式文件夹
|
String projectDir = CommonUtil.getProjectDir();
|
String materialDir = projectDir + File.separator + "doc_file" + File.separator + "material";
|
File materialFile = new File(materialDir);
|
if(!materialFile.exists()){
|
materialFile.mkdirs();
|
}
|
materialUrlList.forEach(materialUrl->{
|
String dwgFullName = materialUrl.substring(materialUrl.lastIndexOf(File.separator) + 1);
|
try {
|
FileCopyUtils.copy(new File(materialUrl),new File(materialDir + File.separator + dwgFullName));
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
});
|
picUrlList.forEach(picUrl->{
|
String picFullName = picUrl.substring(picUrl.lastIndexOf(File.separator) + 1);
|
try {
|
FileCopyUtils.copy(new File(picUrl),new File(materialDir+File.separator+picFullName));
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
});
|
|
//新增,同时对于物料编码相同,物料型号不同的,禁用旧物料状态
|
mapper.disableStatus(materialList);
|
materialList.forEach(material -> {
|
material.setStatus(1);
|
material.setFileUrl(null);
|
});
|
mapper.insertBatchSomeColumn(materialList);
|
return new Response().set(1,true,"新增完成");
|
}
|
|
private List<Material> getListByCodeAndModelList(List<Material> materialList) {
|
return mapper.getListByCodeAndModelList(materialList);
|
}
|
|
public List<Material> getListByCodeAndModelList2(List<ProductBom> bomList) {
|
return mapper.getListByCodeAndModelList2(bomList);
|
}
|
|
public Material getByCodeAndModel(String subCode, String subModel) {
|
QueryWrapper<Material> query = Wrappers.query();
|
query.eq("sub_code",subCode).eq("sub_model",subModel).last(" limit 1");
|
return mapper.selectOne(query);
|
}
|
|
public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException {
|
List<Material> list = new LinkedList<>();
|
Response response = new Response();
|
//检查是否为zip
|
boolean isZip = Zip4jUtil.checkZipFileParam(file);
|
if(!isZip){
|
return response.set(1,false,"上传的文件格式不是zip");
|
}
|
//zip存储路径:doc_file/material_submit/${username}/{dateFormat}/${timeStamp}
|
String rootFile = CommonUtil.getRootFile();
|
DocUser user = ActionUtil.getUser();
|
String dateFormat = new SimpleDateFormat("YYYY-MM").format(new Date());
|
long timeStamp = System.currentTimeMillis();
|
String filePath = rootFile + File.separator + "material_submit" + File.separator + user.getName() + File.separator + dateFormat+ File.separator + timeStamp;
|
File parentFile = new File(filePath);
|
String originalFilename = file.getOriginalFilename();
|
File zipFile = new File(filePath+File.separator+ originalFilename);
|
if(!zipFile.exists()){
|
zipFile.mkdirs();
|
}
|
file.transferTo(zipFile);
|
//解压文件夹
|
Zip4jUtil.unPackZip(zipFile,null,filePath);
|
//遍历解压后的文件夹路径,解析excel
|
System.out.println(filePath);
|
List<String> fileList = new ArrayList<>();
|
fileList = FileUtil.getStaticFilePath(parentFile,fileList);
|
for (int i = 0; i < fileList.size(); i++) {
|
String fileTempUrl = fileList.get(i);
|
//查询到xls数据
|
if(fileTempUrl.contains(".xls")){
|
File fileTemp = new File(fileTempUrl);
|
Workbook workbook = null;
|
workbook = WorkbookFactory.create(fileTemp);
|
List<? extends PictureData> allPictures = workbook.getAllPictures();
|
//取第一个sheet表
|
Sheet sheet = workbook.getSheetAt(0);
|
int lastRowNum = sheet.getLastRowNum();
|
//取第三行,并以第三行开始
|
Row rowTemp = sheet.getRow(1);
|
short lastCellNum = rowTemp.getLastCellNum();
|
//先单独获取产品型号和版本号/分类
|
Row row = sheet.getRow(1);
|
String type = row.getCell(1).getStringCellValue();
|
|
Row row2 = sheet.getRow(2);
|
String productModel = row2.getCell(5).getStringCellValue();
|
Row row3 = sheet.getRow(3);
|
String productVersion = row3.getCell(5).getStringCellValue();
|
for (int l = 8; l < lastRowNum-1; l++) {
|
Cell cellTemp = sheet.getRow(l).getCell(1);
|
cellTemp.setCellType(Cell.CELL_TYPE_STRING);
|
if(cellTemp.getStringCellValue().equals("")){
|
break;
|
}
|
Material material = new Material();
|
material.setCreateDate(new Date());
|
|
for (int m = 2; m < lastCellNum; m++) {
|
row = sheet.getRow(l);
|
Cell cell = row.getCell(m);
|
String cellValue = null;
|
int cellValueInt = 0;
|
Double cellValueDouble = null;
|
if(m == 3 || m==7){
|
//cellValueDouble = cell.getNumericCellValue();
|
//DecimalFormat decimalFormat = new DecimalFormat("0");
|
//cellValue = decimalFormat.format(cellValueDouble);
|
cell.setCellType(Cell.CELL_TYPE_STRING);
|
}
|
cellValue = cell.getStringCellValue();
|
|
switch (m){
|
case 2:{material.setCategory(cellValue);}break;
|
case 3:{material.setSubCode(cellValue);}break;
|
case 4:{material.setSubName(cellValue);}break;
|
case 5:{material.setSubModel(cellValue);}break;
|
case 6:{material.setMaterial(cellValue);}break;
|
case 7:{material.setThickness(cellValue);}break;
|
//case 8:{material.setProducer(cellValue);}break;
|
case 9:{material.setSurfaceDetail(cellValue);}break;
|
case 10:{material.setNotes(cellValue);}break;
|
case 11:{
|
//图片从1开始,0是yc logo
|
PictureData pictureData = allPictures.get(l - 7);
|
String suffix = pictureData.suggestFileExtension();
|
String picturePath = filePath + File.separator + material.getSubModel() + "." + suffix;
|
String picturePathFront = "doc_file" + File.separator + "material_submit" + File.separator + user.getName() + File.separator + dateFormat + File.separator + timeStamp + File.separator + material.getSubModel() + "." + suffix;
|
byte[] data = pictureData.getData();
|
FileOutputStream fileOutputStream = null;
|
File pictureFile = new File(picturePath);
|
fileOutputStream = new FileOutputStream(pictureFile);
|
fileOutputStream.write(data);
|
|
material.setPictureUrl(picturePathFront);
|
}break;
|
}
|
}
|
list.add(material);
|
}
|
}
|
}
|
for (Material material : list) {
|
if(material.getSubName()==null ||material.getSubName().equals("")){
|
return response.set(1,false,"拒绝解析,excel文件中记录包含名称为空的记录");
|
}
|
}
|
return response.setII(1,true,list,filePath);
|
}
|
//根据物料id查询返回附件文件夹下所有的文件列表
|
public Response getAttachByMaterialId(int materialId) {
|
String fileDirName = FileDirPath.getFileDirName();
|
Material material=mapper.getAttachByMaterialId(materialId);
|
boolean flag=false;
|
List list=new ArrayList();
|
if(material.getFileUrl()!=null&&!material.getFileUrl().isEmpty()){
|
File file = new File(fileDirName+File.separator+material.getFileUrl());
|
if(!file.exists()) {
|
flag=false;
|
}else{
|
String[] fileNames = file.list();//获取该文件夹下的所有文件以及目录的名字
|
if(fileNames.length>0){
|
flag=true;
|
for (int i=0;i<fileNames.length;i++){
|
if(!fileNames[i].contains("-dwg.pdf")&&!fileNames[i].contains("-doc.pdf"))
|
list.add(material.getFileUrl()+fileNames[i]);
|
}
|
}
|
|
}
|
}
|
return new Response().setII(1,flag,list,"文件列表返回");
|
}
|
}
|