parent
ff80fbe56d
commit
bca940a8c5
Binary file not shown.
@ -0,0 +1,83 @@
|
|||||||
|
package au.com.royalpay.payment.manage.task;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
|
import org.apache.poi.ss.usermodel.*;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create by yixian at 2018-05-01 17:15
|
||||||
|
*/
|
||||||
|
public class FillExcelTask {
|
||||||
|
|
||||||
|
private File rpayCat = new File("C:\\Users\\yixian\\develop\\royalpayv2\\manage\\src\\main\\ui\\static\\data\\royalpayindustry.json");
|
||||||
|
private File wxCat = new File("C:\\Users\\yixian\\develop\\royalpayv2\\manage\\src\\main\\resources\\wx_industry.json");
|
||||||
|
|
||||||
|
private Map<String, String> royalCatMap;
|
||||||
|
private Map<String, String> wxCatMap;
|
||||||
|
private File excel = new File("C:\\Users\\yixian\\Documents\\bd_merchants.xlsx");
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initCategory() throws IOException {
|
||||||
|
String rpayCatStr = FileUtils.readFileToString(rpayCat, "utf-8");
|
||||||
|
JSONArray rpayCatJson = JSON.parseArray(rpayCatStr);
|
||||||
|
List<JSONObject> flatCollect = rpayCatJson.stream().map(item -> (JSONObject) item)
|
||||||
|
.flatMap(item -> loadChildren(item).stream()).collect(Collectors.toList());
|
||||||
|
royalCatMap = rpayCatJson.stream().map(item -> (JSONObject) item)
|
||||||
|
.flatMap(item -> loadChildren(item).stream())
|
||||||
|
.collect(Collectors.toMap(item -> item.getString("mccCode"), item -> item.getString("label"), (a, b) -> a));
|
||||||
|
|
||||||
|
String wxCatStr = FileUtils.readFileToString(wxCat, "utf-8");
|
||||||
|
JSONObject wxCatJson = JSON.parseObject(wxCatStr);
|
||||||
|
wxCatMap = wxCatJson.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> (String) entry.getValue()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<JSONObject> loadChildren(JSONObject entity) {
|
||||||
|
if (entity.containsKey("children") && !entity.getJSONArray("children").isEmpty()) {
|
||||||
|
List<JSONObject> arr = new ArrayList<>(Arrays.asList(entity));
|
||||||
|
arr.addAll(entity.getJSONArray("children").stream().map(item -> (JSONObject) item).flatMap(item -> loadChildren(item).stream()).collect(Collectors.toList()));
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
return Collections.singletonList(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void replaceCat() throws IOException, InvalidFormatException {
|
||||||
|
Workbook wb = WorkbookFactory.create(excel);
|
||||||
|
Sheet sheet = wb.getSheetAt(0);
|
||||||
|
for (int rn = 1; rn <= sheet.getLastRowNum(); rn++) {
|
||||||
|
Row row = sheet.getRow(rn);
|
||||||
|
Cell wxCatCell = row.getCell(2);
|
||||||
|
Cell rpCatCell = row.getCell(3);
|
||||||
|
fillCateLabel(wxCatCell, wxCatMap);
|
||||||
|
fillCateLabel(rpCatCell, royalCatMap);
|
||||||
|
}
|
||||||
|
wb.write(new FileOutputStream("C:\\Users\\yixian\\Documents\\bd_merchants_final.xlsx"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillCateLabel(Cell rpCatCell, Map<String, String> royalCatMap) {
|
||||||
|
if (rpCatCell == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (rpCatCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
|
||||||
|
String typeNum = (int) rpCatCell.getNumericCellValue() + "";
|
||||||
|
if (royalCatMap.get(typeNum) != null) {
|
||||||
|
rpCatCell.setCellType(Cell.CELL_TYPE_STRING);
|
||||||
|
rpCatCell.setCellValue(royalCatMap.get(typeNum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue