导入检验规格
This commit is contained in:
@ -3,6 +3,7 @@ package com.xkrs.service;
|
||||
import com.xkrs.model.qo.QcSpecQoDelete;
|
||||
import com.xkrs.model.qo.QcSpecQoInsert;
|
||||
import com.xkrs.model.qo.QcSpecQoUpdate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface QcSpecService {
|
||||
|
||||
@ -26,4 +27,14 @@ public interface QcSpecService {
|
||||
*/
|
||||
String queryQcSpec(String varietyNo, String craftItemNo, String qcItemNo);
|
||||
|
||||
/**
|
||||
* 导入检验规格
|
||||
*/
|
||||
String importSpecExcel(MultipartFile specExcel);
|
||||
|
||||
/**
|
||||
* 导出检验规格
|
||||
*/
|
||||
String exportSpecExcel();
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.xkrs.dao.CraftItemDao;
|
||||
import com.xkrs.dao.QcItemDao;
|
||||
import com.xkrs.dao.QcSpecDao;
|
||||
import com.xkrs.encapsulation.PromptMessageEnum;
|
||||
import com.xkrs.model.bean.ReadSpecHeadBean;
|
||||
import com.xkrs.model.entity.CraftItemEntity;
|
||||
import com.xkrs.model.entity.QcItemEntity;
|
||||
import com.xkrs.model.entity.QcSpecEntity;
|
||||
@ -14,15 +15,18 @@ import com.xkrs.service.QcSpecService;
|
||||
import com.xkrs.util.LocalDateUtils;
|
||||
import com.xkrs.util.LocalStringUtils;
|
||||
import org.apache.http.util.TextUtils;
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||||
|
||||
@ -105,7 +109,6 @@ public class QcSpecServiceImpl implements QcSpecService {
|
||||
/**
|
||||
* 更新检验规格
|
||||
*/
|
||||
@Transactional(rollbackOn = Exception.class)
|
||||
@Override
|
||||
public String updateQcSpec(QcSpecQoUpdate updateQo) {
|
||||
|
||||
@ -146,4 +149,186 @@ public class QcSpecServiceImpl implements QcSpecService {
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入检验规格
|
||||
*/
|
||||
@Override
|
||||
public String importSpecExcel(MultipartFile specExcel) {
|
||||
try {
|
||||
//1、读取测量规格数据表文件
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(specExcel.getInputStream());
|
||||
//2、获取工作表对象
|
||||
XSSFSheet sheet = workbook.getSheetAt(0);
|
||||
//3、获取行的迭代器
|
||||
Iterator<Row> iterator = sheet.iterator();
|
||||
long rowNum = 0;
|
||||
long colNum = -1;
|
||||
//4、表格数据容器
|
||||
List<List<String>> sheetDataList = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
Row row = iterator.next();
|
||||
if (rowNum == 0) {
|
||||
ReadSpecHeadBean readResult = readSpecHead(row);
|
||||
if (readResult.isSuccess()) {
|
||||
colNum = readResult.getHeadCount();
|
||||
rowNum++;
|
||||
continue;
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, readResult.getErrorMessage(), locale);
|
||||
}
|
||||
List<String> rowDataList = new ArrayList<>();
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
String value = getValue(row.getCell(i));
|
||||
if (i == 0) {//机种号数据直接添加
|
||||
if (TextUtils.isEmpty(value)) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "第" + rowNum + "行机种号为空,请先编辑机种号!", locale);
|
||||
}
|
||||
rowDataList.add(value);
|
||||
} else if (i == 1) {//工艺项目数据要添加对应的编号
|
||||
Optional<CraftItemEntity> craftItemOptional = craftItemDao.findByCraftItemName(value);
|
||||
if (craftItemOptional.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "第" + rowNum + "行工艺项目(" + value + ")不存在,请先添加工艺项目!", locale);
|
||||
}
|
||||
rowDataList.add(craftItemOptional.get().getCraftItemNo());
|
||||
} else if (i == 2) {//检验项目数据要添加对应的编号
|
||||
Optional<QcItemEntity> qcItemOptional = qcItemDao.findByQcItemName(value);
|
||||
if (qcItemOptional.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "第" + rowNum + "行检验项目(" + value + ")不存在,请先添加检验项目!", locale);
|
||||
}
|
||||
rowDataList.add(qcItemOptional.get().getQcItemNo());
|
||||
} else {//其他数据直接添加
|
||||
rowDataList.add(value);
|
||||
}
|
||||
}
|
||||
sheetDataList.add(rowDataList);
|
||||
rowNum++;
|
||||
}
|
||||
//5、表格数据自重复检查
|
||||
for (int i = 0; i < sheetDataList.size(); i++) {
|
||||
List<String> rowDataList = sheetDataList.get(i);
|
||||
List<Integer> appearIndexList = checkAppearIndex(sheetDataList, rowDataList.get(0), rowDataList.get(1), rowDataList.get(2));
|
||||
if (appearIndexList.size() == 0) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "系统错误!", locale);
|
||||
}
|
||||
if (appearIndexList.size() > 1) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
response.append("测量规格表中:机种号、工艺项目、检验项目三项存在全部相同项,行数:");
|
||||
for (Integer appearIndex : appearIndexList) {
|
||||
response.append("第").append(appearIndex + 2).append("行、");
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, response.toString(), locale);
|
||||
}
|
||||
}
|
||||
//6、表格的数据已经全部读取完成,并且自检通过,接下来遍历入库即可
|
||||
int insertCount = 0;
|
||||
int updateCount = 0;
|
||||
for (List<String> rowDataList : sheetDataList) {
|
||||
String varietyNo = rowDataList.get(0);
|
||||
String craftItemNo = rowDataList.get(1);
|
||||
String qcItemNo = rowDataList.get(2);
|
||||
String max = rowDataList.get(3);
|
||||
String min = rowDataList.get(4);
|
||||
String method = rowDataList.get(5);
|
||||
Optional<QcSpecEntity> existsQcSpecOptional = qcSpecDao.findExistsQcSpec(varietyNo, craftItemNo, qcItemNo);
|
||||
if (existsQcSpecOptional.isPresent()) {
|
||||
qcSpecDao.updateQcSpecByVCQ(LocalStringUtils.formatEmptyValue(varietyNo), LocalStringUtils.formatEmptyValue(craftItemNo), LocalStringUtils.formatEmptyValue(qcItemNo), LocalDateUtils.getCurrentSecond(), LocalStringUtils.formatEmptyValue(max), "", LocalStringUtils.formatEmptyValue(min), "", LocalStringUtils.formatEmptyValue(method), "", "");
|
||||
updateCount++;
|
||||
} else {
|
||||
QcSpecEntity entity = new QcSpecEntity();
|
||||
entity.setCreateTime(LocalDateUtils.getCurrentSecond());
|
||||
entity.setUpdateTime("");
|
||||
entity.setVarietyNo(LocalStringUtils.formatEmptyValue(varietyNo));
|
||||
entity.setCraftItemNo(LocalStringUtils.formatEmptyValue(craftItemNo));
|
||||
entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo));
|
||||
entity.setMax(LocalStringUtils.formatEmptyValue(max));
|
||||
entity.setMean("");
|
||||
entity.setMin(LocalStringUtils.formatEmptyValue(min));
|
||||
entity.setUnit("");
|
||||
entity.setMethod(LocalStringUtils.formatEmptyValue(method));
|
||||
entity.setStandard("");
|
||||
entity.setRemark("");
|
||||
qcSpecDao.save(entity);
|
||||
insertCount++;
|
||||
}
|
||||
}
|
||||
StringBuilder response = new StringBuilder();
|
||||
response.append("导入成功");
|
||||
if (insertCount > 0) {
|
||||
response.append(",新增").append(insertCount).append("条测量规格");
|
||||
}
|
||||
if (updateCount > 0) {
|
||||
response.append(",更新").append(updateCount).append("条测量规格");
|
||||
}
|
||||
response.append("。");
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, response.toString(), locale);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "测量规格数据导入失败!", locale);
|
||||
}
|
||||
|
||||
private List<Integer> checkAppearIndex(List<List<String>> sheetDataList, String varietyNo, String craftItemNo, String qcItemNo) {
|
||||
List<Integer> appearIndexList = new ArrayList<>();
|
||||
for (int i = 0; i < sheetDataList.size(); i++) {
|
||||
List<String> rowDataList = sheetDataList.get(i);
|
||||
if (rowDataList.get(0).equals(varietyNo) && rowDataList.get(1).equals(craftItemNo) && rowDataList.get(2).equals(qcItemNo)) {
|
||||
appearIndexList.add(i);
|
||||
}
|
||||
}
|
||||
return appearIndexList;
|
||||
}
|
||||
|
||||
List<String> QC_SPEC_HEAD = Arrays.asList("机种号", "工艺项目", "检验项目", "最大值", "最小值", "检验方法");
|
||||
|
||||
/**
|
||||
* 读取表头数据
|
||||
*/
|
||||
private ReadSpecHeadBean readSpecHead(Row headRow) {
|
||||
ReadSpecHeadBean readResult = new ReadSpecHeadBean();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (!QC_SPEC_HEAD.get(i).equals(getValue(headRow.getCell(i)))) {
|
||||
readResult.setSuccess(false);
|
||||
readResult.setHeadCount(0);
|
||||
readResult.setErrorMessage("表头第" + (i + 1) + "列应该是:" + QC_SPEC_HEAD.get(i));
|
||||
return readResult;
|
||||
}
|
||||
}
|
||||
readResult.setSuccess(true);
|
||||
readResult.setHeadCount(6);
|
||||
readResult.setErrorMessage("");
|
||||
return readResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单元格内的数据,并进行格式转换
|
||||
*/
|
||||
private String getValue(Cell cell) {
|
||||
if (cell == null) {
|
||||
return "";
|
||||
}
|
||||
if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
|
||||
return cell.getStringCellValue();
|
||||
}
|
||||
if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
|
||||
return String.valueOf(cell.getBooleanCellValue());
|
||||
}
|
||||
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {//数值和日期均是此类型,需进一步判断
|
||||
if (HSSFDateUtil.isCellDateFormatted(cell)) {
|
||||
//是日期类型
|
||||
return String.valueOf(cell.getDateCellValue().getTime());
|
||||
} else {
|
||||
//是数值类型
|
||||
return String.valueOf(cell.getNumericCellValue());
|
||||
}
|
||||
}
|
||||
return "";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String exportSpecExcel() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user