|
@@ -0,0 +1,309 @@
|
|
|
+package com.nokia.siteinfo.task;
|
|
|
+
|
|
|
+import com.nokia.common.gpload.GploadUtil;
|
|
|
+import com.nokia.common.gpload.entity.GploadResult;
|
|
|
+import com.xxl.job.core.context.XxlJobHelper;
|
|
|
+import com.xxl.job.core.handler.annotation.XxlJob;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.csv.CSVFormat;
|
|
|
+import org.apache.commons.csv.CSVPrinter;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class UpdateTask {
|
|
|
+ private final JdbcTemplate inputJdbcTemplate;
|
|
|
+ private final JdbcTemplate outputJdbcTemplate;
|
|
|
+
|
|
|
+ public UpdateTask(JdbcTemplate inputJdbcTemplate, JdbcTemplate outputJdbcTemplate) {
|
|
|
+ this.inputJdbcTemplate = inputJdbcTemplate;
|
|
|
+ this.outputJdbcTemplate = outputJdbcTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+// 0 0 1 ? * 2
|
|
|
+ @XxlJob("siteInfoJobHandler")
|
|
|
+ public void siteInfoJobHandler() {
|
|
|
+ try {
|
|
|
+ Map<String, Map<String, Object>> map = new HashMap<>();
|
|
|
+ update4g(map);
|
|
|
+ update5g(map);
|
|
|
+ writeCsvs(map);
|
|
|
+ backup("customer_service.cfg_p_netconf_std");
|
|
|
+ gpload("customer_service.cfg_p_netconf_std");
|
|
|
+ backup("customer_service.cfg_cell_info");
|
|
|
+ gpload("customer_service.cfg_cell_info");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发生异常了: {}", e.getMessage(), e);
|
|
|
+ XxlJobHelper.log("发生异常了: {}", e.getMessage(), e);
|
|
|
+ XxlJobHelper.handleFail(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void update4g(Map<String, Map<String, Object>> map) throws IOException {
|
|
|
+ // 查询o2p.cfg_0_4g_siteinfo
|
|
|
+ String sql = "select * from o2p.cfg_0_4g_siteinfo";
|
|
|
+ List<Map<String, Object>> list = inputJdbcTemplate.queryForList(sql);
|
|
|
+ Map<String, Map<String, Object>> temp = new HashMap<>();
|
|
|
+ // 去重
|
|
|
+ list.forEach(t -> {
|
|
|
+ map.put((String) t.get("cgi"), t);
|
|
|
+ temp.put((String) t.get("cgi"), t);
|
|
|
+ });
|
|
|
+ log.info("cfg_0_4g_siteinfo: {} -> {}", list.size(), temp.size());
|
|
|
+ XxlJobHelper.log("cfg_0_4g_siteinfo: {} -> {}", list.size(), temp.size());
|
|
|
+ String table = "cfm.cfg_0_4g_siteinfo";
|
|
|
+ writeCsv(temp, table);
|
|
|
+ backup(table);
|
|
|
+ // o2p.cfg_0_4g_siteinfo -> cfm.cfg_0_4g_siteinfo
|
|
|
+ gpload(table);
|
|
|
+ // 查询cfm.cfg_0_4g_siteinfo_gx
|
|
|
+ sql = "select * from cfm.cfg_0_4g_siteinfo_gx";
|
|
|
+ list = outputJdbcTemplate.queryForList(sql);
|
|
|
+ // 添加新元素
|
|
|
+ list.forEach(t -> map.putIfAbsent((String) t.get("cgi"), t));
|
|
|
+ log.info("cfg_0_4g_siteinfo_gx: {} -> {}", list.size(), map.size());
|
|
|
+ XxlJobHelper.log("cfg_0_4g_siteinfo_gx: {} -> {}", list.size(), map.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void update5g(Map<String, Map<String, Object>> map) throws IOException {
|
|
|
+ // 查询o2p.cfg_0_5g_siteinfo
|
|
|
+ String sql = "select * from o2p.cfg_0_5g_siteinfo";
|
|
|
+ List<Map<String, Object>> list = inputJdbcTemplate.queryForList(sql);
|
|
|
+ Map<String, Map<String, Object>> temp = new HashMap<>();
|
|
|
+ // 去重
|
|
|
+ list.forEach(t -> {
|
|
|
+ map.put((String) t.get("cgisai"), t);
|
|
|
+ temp.put((String) t.get("cgisai"), t);
|
|
|
+ });
|
|
|
+ log.info("cfg_0_5g_siteinfo: {} -> {}", list.size(), temp.size());
|
|
|
+ XxlJobHelper.log("cfg_0_5g_siteinfo: {} -> {}", list.size(), temp.size());
|
|
|
+ String table = "cfm.cfg_0_5g_siteinfo";
|
|
|
+ writeCsv(temp, table);
|
|
|
+ backup(table);
|
|
|
+ // o2p.cfg_0_5g_siteinfo -> cfm.cfg_0_5g_siteinfo
|
|
|
+ gpload(table);
|
|
|
+ // 查询cfm.cfg_0_5g_siteinfo_gx
|
|
|
+ sql = "select * from cfm.cfg_0_5g_siteinfo_gx";
|
|
|
+ list = outputJdbcTemplate.queryForList(sql);
|
|
|
+ // 添加新元素
|
|
|
+ list.forEach(t -> map.putIfAbsent((String) t.get("cgisai"), t));
|
|
|
+ log.info("cfg_0_5g_siteinfo_gx: {} -> {}", list.size(), map.size());
|
|
|
+ XxlJobHelper.log("cfg_0_5g_siteinfo_gx: {} -> {}", list.size(), map.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void writeCsvs(Map<String, Map<String, Object>> map) throws IOException {
|
|
|
+ try (OutputStreamWriter osw1 = new OutputStreamWriter(
|
|
|
+ Files.newOutputStream(Paths.get("customer_service.cfg_p_netconf_std.csv")),
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
+ CSVPrinter printer1 = new CSVPrinter(osw1, CSVFormat.DEFAULT);
|
|
|
+ OutputStreamWriter osw2 = new OutputStreamWriter(
|
|
|
+ Files.newOutputStream(Paths.get("customer_service.cfg_cell_info.csv")),
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
+ CSVPrinter printer2 = new CSVPrinter(osw2, CSVFormat.DEFAULT);) {
|
|
|
+ for (Map<String, Object> t : map.values()) {
|
|
|
+ boolean isFourG = "4G".equals(t.get("network_name"));
|
|
|
+ if (isFourG) {
|
|
|
+ BigDecimal id = print4gCfgPNetconfStd(printer1, t);
|
|
|
+ String siteName = (String) t.get("bbu_name");
|
|
|
+ printCfgCellInfo(printer2, t, id, siteName);
|
|
|
+ } else {
|
|
|
+ BigDecimal id = print5gCfgPNetconfStd(printer1, t);
|
|
|
+ String siteName = (String) t.get("station_name");
|
|
|
+ printCfgCellInfo(printer2, t, id, siteName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void writeCsv(Map<String, Map<String, Object>> map, String table) throws IOException {
|
|
|
+ log.info("writeCsv: {}", table);
|
|
|
+ XxlJobHelper.log("writeCsv: {}", table);
|
|
|
+ try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(table + ".csv")),
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
+ CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT);) {
|
|
|
+ for (Map<String, Object> t : map.values()) {
|
|
|
+ printer.printRecord(t.values());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 备份
|
|
|
+ *
|
|
|
+ * @param table 表
|
|
|
+ */
|
|
|
+ public void backup(String table) {
|
|
|
+ log.info("备份: {}", table);
|
|
|
+ XxlJobHelper.log("备份: {}", table);
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+ String nowString = dateFormat(now);
|
|
|
+ // 备份
|
|
|
+ String sql = "create table " + table + "_bak_" + nowString + " as select * from " + table;
|
|
|
+ outputJdbcTemplate.execute(sql);
|
|
|
+ // 删除之前的备份
|
|
|
+ sql = "DO $$\n" +
|
|
|
+ "DECLARE t varchar(255);\n" +
|
|
|
+ "BEGIN FOR t IN\n" +
|
|
|
+ "select 'drop table ' || schemaname || '.' || tablename || ';'\n" +
|
|
|
+ "from pg_tables\n" +
|
|
|
+ "where schemaname || '.' || tablename like '" + table + "_bak_%' " +
|
|
|
+ "and schemaname || '.' || tablename != '" + table + "_bak_" + nowString + "' loop EXECUTE t;\n" +
|
|
|
+ "END LOOP;\n" +
|
|
|
+ "END;\n" +
|
|
|
+ "$$";
|
|
|
+ outputJdbcTemplate.execute(sql);
|
|
|
+ // 清空表
|
|
|
+ sql = "truncate table " + table;
|
|
|
+ outputJdbcTemplate.execute(sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期格式
|
|
|
+ *
|
|
|
+ * @param localDate 当地日期
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ public String dateFormat(LocalDate localDate) {
|
|
|
+ return localDate.format(DateTimeFormatter.ofPattern("MMdd"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void gpload(String table) throws IOException {
|
|
|
+ String gploadCommand = "sh /data1/site_info/gpload/gpload.sh " + table;
|
|
|
+ GploadResult gpload = GploadUtil.gpload(gploadCommand);
|
|
|
+ if (Boolean.TRUE.equals(gpload.getTaskStatus())) {
|
|
|
+ log.info("gpload {} 完成: {}", table, gpload);
|
|
|
+ XxlJobHelper.log("gpload {} 完成: {}", table, gpload);
|
|
|
+ // 删除文件
|
|
|
+ Files.deleteIfExists(Paths.get(table + ".csv"));
|
|
|
+ } else {
|
|
|
+ log.error("gpload {} 失败: {}", table, gpload.getMessage());
|
|
|
+ XxlJobHelper.log("gpload {} 失败: {}", table, gpload.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal print4gCfgPNetconfStd(CSVPrinter p, Map<String, Object> t) throws IOException {
|
|
|
+ BigDecimal enbid = (BigDecimal) t.get("enbid");
|
|
|
+ BigDecimal cell_id = (BigDecimal) t.get("cell_id");
|
|
|
+ BigDecimal cellId = enbid.multiply(new BigDecimal(256)).add(cell_id);
|
|
|
+ BigDecimal id = cellId.add(new BigDecimal(46001000000000L));
|
|
|
+ p.printRecord(
|
|
|
+ "",
|
|
|
+ t.get("city_code"),
|
|
|
+ t.get("city_name"),
|
|
|
+ t.get("district_name"),
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("bbu_name"),
|
|
|
+ enbid,
|
|
|
+ cell_id,
|
|
|
+ t.get("cell_name"),
|
|
|
+ id,
|
|
|
+ cellId,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ 4,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "宏站".equals(t.get("station_type")) ? "室外" : "室内",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("scene"),
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("lon"),
|
|
|
+ t.get("lat"),
|
|
|
+ t.get("vender"),
|
|
|
+ t.get("height"),
|
|
|
+ ((BigDecimal) t.get("m_downtilt")).add((BigDecimal) t.get("e_downtilt")),
|
|
|
+ t.get("direction"),
|
|
|
+ t.get("down_freq"),
|
|
|
+ "",
|
|
|
+ "河北省",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("construction"),
|
|
|
+ "",
|
|
|
+ ""
|
|
|
+ );
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal print5gCfgPNetconfStd(CSVPrinter p, Map<String, Object> t) throws IOException {
|
|
|
+ BigDecimal gnbid = (BigDecimal) t.get("gnbid");
|
|
|
+ BigDecimal cell_id = (BigDecimal) t.get("cell_id");
|
|
|
+ BigDecimal cellId = gnbid.multiply(new BigDecimal(4096)).add(cell_id);
|
|
|
+ BigDecimal id = cellId.add(new BigDecimal(4600100000000000L));
|
|
|
+ p.printRecord(
|
|
|
+ "",
|
|
|
+ t.get("city_code"),
|
|
|
+ t.get("city_name"),
|
|
|
+ t.get("district_name"),
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("station_name"),
|
|
|
+ gnbid,
|
|
|
+ cell_id,
|
|
|
+ t.get("cell_name"),
|
|
|
+ id,
|
|
|
+ cellId,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ 4,
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "宏站".equals(t.get("station_type")) ? "室外" : "室内",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("scene"),
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("lon"),
|
|
|
+ t.get("lat"),
|
|
|
+ t.get("vender"),
|
|
|
+ t.get("height"),
|
|
|
+ ((BigDecimal) t.get("m_downtilt")).add((BigDecimal) t.get("e_downtilt")),
|
|
|
+ t.get("direction"),
|
|
|
+ t.get("down_freq"),
|
|
|
+ "",
|
|
|
+ "河北省",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ "",
|
|
|
+ t.get("construction"),
|
|
|
+ "",
|
|
|
+ ""
|
|
|
+ );
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void printCfgCellInfo(CSVPrinter p, Map<String, Object> t, BigDecimal id, String siteName) throws IOException {
|
|
|
+ p.printRecord(
|
|
|
+ id,
|
|
|
+ siteName,
|
|
|
+ t.get("city_name"),
|
|
|
+ t.get("lon"),
|
|
|
+ t.get("lat"),
|
|
|
+ ""
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|