Commit 67a366b2 by kk

存储

parent 343e87c1
......@@ -9,6 +9,14 @@ import cn.kk.spring_simple_operation.utils.ExcelUtil;
import cn.kk.spring_simple_operation.utils.SqlGenerator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -19,7 +27,11 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
......@@ -58,6 +70,86 @@ public class MyTest2 {
@Test
public void exportSkuHasAPlus() throws Exception {
Path inputPath = Paths.get("22864-羊绒商品认证数据确认.xlsx");
Path outputPath = Paths.get("22864-羊绒商品认证数据确认-是否有A+.xlsx");
DataFormatter dataFormatter = new DataFormatter();
try (InputStream inputStream = Files.newInputStream(inputPath);
Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
Row headerRow = sheet.getRow(0);
if (Objects.isNull(headerRow)) {
throw new IllegalStateException("Excel第一行表头为空");
}
int skuColumnIndex = getColumnIndex(headerRow, "SKU", dataFormatter);
if (skuColumnIndex < 0) {
throw new IllegalStateException("未找到sku列");
}
int siteColumnIndex = getColumnIndex(headerRow, "站点", dataFormatter);
if (siteColumnIndex < 0) {
throw new IllegalStateException("未找到站点列");
}
int resultColumnIndex = getColumnIndex(headerRow, "是否有A+", dataFormatter);
if (resultColumnIndex < 0) {
resultColumnIndex = headerRow.getLastCellNum();
if (resultColumnIndex < 0) {
resultColumnIndex = 0;
}
}
Cell resultHeaderCell = getOrCreateCell(headerRow, resultColumnIndex);
resultHeaderCell.setCellValue("是否有A+");
if (resultColumnIndex > 0) {
copyCellStyle(workbook, headerRow.getCell(resultColumnIndex - 1), resultHeaderCell);
}
Set<String> skuSet = new LinkedHashSet<>();
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (Objects.isNull(row)) {
continue;
}
String sku = getCellString(row.getCell(skuColumnIndex), dataFormatter);
if (!StringUtils.isEmpty(sku)) {
skuSet.add(sku);
}
}
Set<String> hasAPlusSkuSiteSet = new HashSet<>();
Lists.partition(new ArrayList<>(skuSet), 500).forEach(skuList -> {
List<APlusProduct> aPlusProductList = aPlusProductMapper.selectList(Wrappers.<APlusProduct>lambdaQuery()
.in(APlusProduct::getSku, skuList)
.select(APlusProduct::getSku, APlusProduct::getSite));
aPlusProductList.forEach(aPlusProduct -> hasAPlusSkuSiteSet.add(buildSkuSiteKey(aPlusProduct.getSku(), aPlusProduct.getSite())));
});
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (Objects.isNull(row)) {
continue;
}
String sku = getCellString(row.getCell(skuColumnIndex), dataFormatter);
String site = getCellString(row.getCell(siteColumnIndex), dataFormatter);
Cell resultCell = getOrCreateCell(row, resultColumnIndex);
if (resultColumnIndex > 0) {
copyCellStyle(workbook, row.getCell(resultColumnIndex - 1), resultCell);
}
resultCell.setCellValue(!StringUtils.isEmpty(sku) && !StringUtils.isEmpty(site) && hasAPlusSkuSiteSet.contains(buildSkuSiteKey(sku, site)) ? "是" : "否");
}
sheet.autoSizeColumn(resultColumnIndex);
try (OutputStream outputStream = Files.newOutputStream(outputPath)) {
workbook.write(outputStream);
}
}
log.info("导出完成:{}", outputPath.toAbsolutePath());
}
@Test
public void dealSimpleAPlusPlanTime() {
ArrayList<String> skuList = Lists.newArrayList("STY0089", "IFS2835", "DF1061", "IF2216", "VEA0926", "BV4469", "STY1329", "TAY0257", "LOO2703", "XEU1036", "LOM0105", "XUL0975", "UR1221", "ISI0782", "LIC9145", "HOD5141", "LEM6389", "DF4494", "WAR1487", "PIR5107", "QUN2115", "ANB2765", "JY1066", "RY3111", "SIG0377", "TEA2739", "XEU1563", "YOG1732", "NEV2980", "IG2159", "FLO3335", "ZHI0065", "MZ2724", "JUU0439", "HOD2404", "DF2869", "MM0881", "DUN3755", "HAI6132", "JYY2734", "GUR0168", "HUM1645", "GUU2335", "QUQ1018", "GUR3956", "CAS3083", "GOR2056", "QUN3648", "XIN2813", "FS2247", "HUI2255", "YUN2250", "XAO3558", "YOF1784", "JIA0971", "GUI0229", "GOO2940");
List<VisualAPlusPlan> visualAPlusPlans = visualAPlusPlanMapper.selectList(Wrappers.<VisualAPlusPlan>lambdaQuery().in(VisualAPlusPlan::getSku, skuList).select(VisualAPlusPlan::getId, VisualAPlusPlan::getSku));
......@@ -210,4 +302,45 @@ public class MyTest2 {
}
private int getColumnIndex(Row headerRow, String columnName, DataFormatter dataFormatter) {
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
String cellValue = getCellString(headerRow.getCell(i), dataFormatter);
if (columnName.equalsIgnoreCase(cellValue)) {
return i;
}
}
return -1;
}
private String getCellString(Cell cell, DataFormatter dataFormatter) {
if (Objects.isNull(cell)) {
return "";
}
if (CellType.STRING.equals(cell.getCellType())) {
return cell.getStringCellValue().trim();
}
return dataFormatter.formatCellValue(cell).trim();
}
private Cell getOrCreateCell(Row row, int columnIndex) {
Cell cell = row.getCell(columnIndex);
if (Objects.isNull(cell)) {
cell = row.createCell(columnIndex);
}
return cell;
}
private String buildSkuSiteKey(String sku, String site) {
return (sku == null ? "" : sku.trim()) + "#" + (site == null ? "" : site.trim());
}
private void copyCellStyle(Workbook workbook, Cell sourceCell, Cell targetCell) {
if (Objects.isNull(sourceCell)) {
return;
}
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.cloneStyleFrom(sourceCell.getCellStyle());
targetCell.setCellStyle(cellStyle);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment