Commit fcb32b39 authored by Quxl's avatar Quxl

x

parent 9a274b7b
package com.egolm.film.util;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.egolm.common.ExcelUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.Util;
import com.egolm.common.jdbc.JUtil;
public class Export {
private String root;
private String db_name;
private Connection conn;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public Export(String root, String db_host, String db_name, String username, String password) throws SQLException {
this.root = root;
this.db_name = db_name;
this.conn = DriverManager.getConnection("jdbc:mysql://" + db_host + "/information_schema?useSSL=false&useUnicode=true&characterEncoding=utf8" , username, password);
}
public List<Map<String, Object>> getTableDesc(String tableName) throws SQLException {
String sql = "SELECT DISTINCT C.COLUMN_NAME, C.COLUMN_TYPE, C.COLUMN_COMMENT, CU.CONSTRAINT_NAME FROM COLUMNS C LEFT JOIN KEY_COLUMN_USAGE CU ON CU.TABLE_SCHEMA = C.TABLE_SCHEMA AND CU.TABLE_NAME = C.TABLE_NAME AND CU.COLUMN_NAME = C.COLUMN_NAME WHERE C.TABLE_SCHEMA = '" + this.db_name + "' AND C.TABLE_NAME = '" + tableName + "'";
return this.queryForList(sql);
}
public List<Map<String, Object>> queryForList(String sql) throws SQLException {
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
return JUtil.resultSetToList(resultSet);
}
static Map<String, String> types = new HashMap<String, String>();
static {
types.put("director", "导演");
types.put("producer", "监制");
types.put("original", "原著");
types.put("screenwriter", "编剧");
types.put("photographer", "摄影师");
types.put("musicAuthor", "音乐");
types.put("filmEditor", "剪辑");
types.put("presenter", "主持人");
types.put("officiatingGuests", "主持嘉宾");
types.put("art", "美术");
types.put("stylist", "造型");
types.put("effectArtist", "特效");
types.put("hero", "男主演");
types.put("heroine", "女主演");
types.put("supportingActor", "男配角");
types.put("supportingActress", "女配角");
}
public List<Map<String, Object>> queryDataList(String tableName) throws SQLException {
String sql = "select t.* from " + this.db_name + "." + tableName + " t";
String sql0 = "select group_concat(fp.playactor_name) text, fp.film_id, fp.playactor_type from " + this.db_name + ".fc_member_film_playactor fp group by fp.film_id, fp.playactor_type";
String sql1 = "select group_concat(fi.issuer_company, ' ', fi.issuer_company_en) text, fi.film_id from " + this.db_name + ".fc_member_film_issuer fi group by fi.film_id";
String sql2 = "select group_concat(fs.show_filmfest) text, fs.film_id from " + this.db_name + ".fc_member_film_show fs group by fs.film_id";
List<Map<String, Object>> list = this.queryForList(sql);
List<Map<String, Object>> playactors = this.queryForList(sql0);
List<Map<String, Object>> issuers = this.queryForList(sql1);
List<Map<String, Object>> shows = this.queryForList(sql2);
Map<String, Map<String, Object>> playactorMM = Util.listToMM(playactors, ",", "film_id", "playactor_type");
Map<String, Map<String, Object>> issuerMM = Util.listToMM(issuers, ",", "film_id");
Map<String, Map<String, Object>> showMM = Util.listToMM(shows, ",", "film_id");
for(Map<String, Object> map : list) {
String id_str = "" + map.get("id");
for(String playactor_type : types.keySet()) {
Map<String, Object> playactorMap = playactorMM.get(id_str + "," + playactor_type);
map.put(playactor_type, playactorMap == null ? null : playactorMap.get("text"));
}
Map<String, Object> issuerMap = issuerMM.get(id_str);
Map<String, Object> showMap = showMM.get(id_str);
map.put("issuer", issuerMap == null ? null : issuerMap.get("text"));
map.put("issuer", showMap == null ? null : showMap.get("text"));
}
return list;
}
public static void main(String[] args) throws Exception {
Export exp = new Export("D:/tv.xls", "10.10.0.111", "tv", "root", "egolm#2018");
List<Map<String, Object>> desc = exp.getTableDesc("fc_member_film");
List<Map<String, Object>> list = exp.queryDataList("fc_member_film");
List<String> argsList = new ArrayList<String>();
List<String> titleList = new ArrayList<String>();
for(Map<String, Object> map : desc) {
String columnName = (String)map.get("COLUMN_NAME");
argsList.add(columnName);
String columnComment = (String)map.get("COLUMN_COMMENT");
if(StringUtil.isBlank(columnComment)) {
titleList.add(columnName);
} else {
titleList.add(columnComment + "" + columnName);
}
}
for(String key : types.keySet()) {
argsList.add(key);
titleList.add(types.get(key));
}
argsList.add("issuser");
titleList.add("发行方");
argsList.add("show");
titleList.add("参加过的电视节");
ExcelUtil.excel(new FileOutputStream(exp.root), argsList.toArray(new String[argsList.size()]), titleList.toArray(new String[titleList.size()]), null, list);
}
}
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