Commit 116da754 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

省市脚本

parent 3c466c7d
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.duanledexianxianxian.data.process</groupId>
<artifactId>data-process</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.duanledexianxianxian.data.process;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* @author duanledexianxianxian
*/
public class Application {
private static Set<Model> errorSet = Sets.newHashSet();
public static void main(String[] args) throws IOException {
List<String> lines = readData();
// 中文数据
String zh = processData(lines, 0);
writeData("F:\\@project@\\@dianli@\\java-demo\\data-process\\src\\main\\resources\\zh.sql", zh);
// 英文数据
String en = processData(lines, 1);
writeData("F:\\@project@\\@dianli@\\java-demo\\data-process\\src\\main\\resources\\en.sql", en);
System.out.println("error data");
for (Model model:errorSet){
System.out.println(model);
}
}
private static void writeData(String fileName, String content) throws IOException {
FileUtils.write(new File(fileName), content, "UTF-8");
}
private static List<String> readData() throws IOException {
File file = new File("F:\\@project@\\@dianli@\\java-demo\\data-process\\src\\main\\resources\\省市区码表.csv");
List<String> lines = FileUtils.readLines(file, "UTF-8");
System.out.println("lines:" + lines);
return lines;
}
private static String processData(List<String> lines, int lang) {
List<Model> modelList = Lists.newLinkedList();
Map<String, Model> provinceMap = Maps.newTreeMap();
Map<String, Model> allMap = Maps.newHashMap();
int[] index = {lang == 0 ? 100000 : 200000};
if (CollectionUtils.isNotEmpty(lines)) {
lines.forEach(x -> {
String[] words = StringUtils.split(x, "@");
System.out.println("words:" + Arrays.toString(words));
Model model;
model = new Model();
model.setId((long) index[0]);
model.setGrade(words[0]);
model.setType(Integer.parseInt(words[0]));
model.setNameCn(words[1]);
model.setNameEn(words[2]);
model.setOrders(words[3]);
model.setRegCode(words[4]);
if (words.length <= 5) {
model.setPId(lang == 0 ? 1L : 242L);
provinceMap.put(model.getRegCode(), model);
} else {
model.setBelongTo(words[5]);
model.setPRegCode(words[6]);
}
allMap.put(model.getRegCode(), model);
modelList.add(model);
index[0]++;
});
}
System.out.println("modelList:" + modelList);
System.out.println("provinceMap:" + provinceMap);
System.out.println("allMap:" + allMap);
modelList.forEach(x -> {
if (allMap.containsKey(x.getPRegCode())) {
x.setPId(allMap.get(x.getPRegCode()).getId());
}
});
// 排个序
System.out.println("modelList:" + modelList);
Collections.sort(modelList, (o1, o2) -> {
Integer result = o1.getType() - o2.getType();
if (result.equals(0)) {
return o1.getRegCode().compareTo(o2.getRegCode());
}
return result;
});
System.out.println("sort modelList" + modelList);
StringBuffer sb = new StringBuffer();
modelList.forEach(x -> {
if (null != x.getPId()) {
sb.append(String.format("INSERT INTO `tsys_business_parameter_geography`(`id`, `pId`, `local`, `district_code`, `district_name`, `type`, `is_delete`, `creator_id`, `create_time`, `editor_id`, `edit_time`) VALUES " +
"(\"%d\", \"%d\", \"%s\", \"%s\", \"%s\", \"%d\", 0, 1, now(), NULL, NULL);\n",
x.getId(), x.getPId(), lang == 0 ? "zh-cn" : "en-us", x.getRegCode(), lang == 0 ? x.getNameCn() : x.getNameEn(), x.getType()));
} else {
errorSet.add(x);
}
});
System.out.println(sb);
return sb.toString();
}
}
package com.duanledexianxianxian.data.process;
import com.google.common.base.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* The type Model.
*
* @author duanledexianxianxian
*/
@Data
public class Model implements Serializable {
private Long id;
private Long pId;
private String grade;
private String nameCn;
private String nameEn;
private String orders;
private String regCode;
private String belongTo;
private String pRegCode;
/**
* 0-国家
* 1-省
* 2-地级市/区
* 3-县/区
*/
private Integer type;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Model model = (Model) o;
return Objects.equal(grade, model.grade) &&
Objects.equal(nameCn, model.nameCn) &&
Objects.equal(nameEn, model.nameEn) &&
Objects.equal(orders, model.orders) &&
Objects.equal(regCode, model.regCode) &&
Objects.equal(belongTo, model.belongTo) &&
Objects.equal(pRegCode, model.pRegCode) &&
Objects.equal(type, model.type);
}
@Override
public int hashCode() {
return Objects.hashCode(grade, nameCn, nameEn, orders, regCode, belongTo, pRegCode, type);
}
}
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