package com.qlm.job; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.jfinal.kit.HttpKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.druid.DruidPlugin; import com.qlm.tools.WxUtil; public class SyncJob implements Job { private static void con(){ DruidPlugin dp = new DruidPlugin("jdbc:mysql://58.20.133.135:6177/jinzai?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "jinzai", "rTdBsjA636XkarRa"); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); // ��web����Ψһ�IJ�ͬ��Ҫ�ֶ�����һ����ز����start()���� dp.start(); arp.start(); } static Map codeNameMap = new HashMap(); private static final Logger logger = LoggerFactory.getLogger(MyJob.class); public static void main(String[] args) { con(); // doSync(); // fix(); } private static void fix() { // TODO Auto-generated method stub List datas = Db.find("select * from jinzai_jxs_area_xian where id = 10284"); codeNameMap.clear(); List areas = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4"); Map codeMap = new HashMap(); for (Record record : areas) { String str = record.getStr("code"); String name = record.getStr("name"); codeMap.put(name, record); codeNameMap.put(str, name); } List list = new ArrayList(); for (Record data :datas) { String name = data.getStr("jxs"); String code = data.getStr("code"); String city = null; if(WxUtil.isNull(name)){ continue; } if(name.length()<6){ continue; } Record info = new Record(); String temp = name.substring(0,3); String lastStr = ""; String province = null; int index = 2; if(temp.endsWith("省") || "黑龙江".equals(temp) || "内蒙古".equals(temp)){ province = name.substring(0,3); lastStr = name.substring(3); index = 3; }else{ lastStr = name.substring(2); province = name.substring(0,2); } int length = lastStr.length(); String userName = lastStr.substring(length-2,length); String cityName = null; try{ cityName = lastStr.substring(0,length-2); }catch(Exception e){ continue; } if(isBigCity(province)){ province = city = province+"市"; }else{ String containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ userName = lastStr.substring(length-3); cityName = lastStr.substring(0,length-3); containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ if(cityName.length() >=3){ cityName = cityName.substring(0,2); } containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ System.out.println("无法获取,"+name); } } } } info.set("jxs", name).set("code", code).set("city", city).set("province", province); list.add(info); } WxUtil.batchSaveReplace("jinzai_jxs_area", list, ""); } private static void doSync() { codeNameMap.clear(); String aiChuangToken = getAiChuangToken(); List find = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4"); Map codeMap = new HashMap(); for (Record record : find) { String str = record.getStr("code"); String name = record.getStr("name"); codeMap.put(name, record); codeNameMap.put(str, name); } for (int i = 0; i < 10000; i++) { boolean data = getData(aiChuangToken,i+1,codeMap); if(!data){ break; } } } public static boolean getData(String toekn,int page,Map codeMap){ Map header = new HashMap(); header.put("Content-type", "application/json"); header.put("Authorization", "Bearer "+toekn); JSONObject obj = new JSONObject(); obj.put("modifiedTime", "1990-01-01 00:00:00"); obj.put("corpType", "2"); obj.put("currentPage", page); obj.put("itemsPerPage", 1000); String post = HttpKit.post("http://183.215.13.140:324/openapi/basedata/corp/all/listPage", obj.toJSONString(),header); JSONObject parseObject = JSONObject.parseObject(post); JSONObject jsonObject = parseObject.getJSONObject("data"); JSONArray jsonArray = jsonObject.getJSONArray("items"); if(jsonArray.isEmpty()){ return false; }else{ Integer currentPage = jsonObject.getInteger("currentPage"); if(page>currentPage){ return false; } System.out.println("当前页数:"+page); List list = new ArrayList(); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject2 = jsonArray.getJSONObject(i); String name = jsonObject2.getString("name"); String code = jsonObject2.getString("code"); String city = null; if(WxUtil.isNull(name)){ continue; } if(name.length()<6){ continue; } Record info = new Record(); String temp = name.substring(0,3); String lastStr = ""; String province = null; int index = 2; if(temp.endsWith("省") || "黑龙江".equals(temp) || "内蒙古".equals(temp)){ province = name.substring(0,3); lastStr = name.substring(3); index = 3; }else{ lastStr = name.substring(2); province = name.substring(0,2); } int length = lastStr.length(); String userName = lastStr.substring(length-2,length); String cityName = null; try{ cityName = lastStr.substring(0,length-2); }catch(Exception e){ continue; } if(isBigCity(province)){ province = city = province+"市"; }else{ String containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ userName = lastStr.substring(length-3); cityName = lastStr.substring(0,length-3); containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ if(cityName.length() >=3){ cityName = cityName.substring(0,2); } containName = containName(codeMap,cityName); if(containName != null){ String[] find = find(codeMap, containName); province = find[0]; city = find[1]; }else{ System.out.println("无法获取,"+name); } } } } info.set("jxs", name).set("code", code).set("city", city).set("province", province); list.add(info); } WxUtil.batchSaveIgnore("jinzai_jxs_area", list, ""); } return true; } private static String[] find(Map codeMap, String containName) { String city = null; String province = null; String [] cis = {"",""}; Record record = codeMap.get(containName); int type = WxUtil.getInt("type", record); String parent_code = record.getStr("parent_code"); if(type == 3){ city = codeNameMap.get(parent_code); Record cityInfo = codeMap.get(city); parent_code = cityInfo.getStr("parent_code"); province = codeNameMap.get(parent_code); }else if(type == 2){ city = containName; province = codeNameMap.get(parent_code); }else if(type == 4){ String string = codeNameMap.get(parent_code); return find(codeMap,string); } cis[0] = province; cis[1] = city; return cis; } public static boolean isBigCity(String province){ String citys [] = {"北京","重庆","天津","上海"}; for (String string : citys) { if(string.equals(province)){ return true; } } return false; } public static String containName(Map codeMap,String name){ Set> entrySet = codeMap.entrySet(); for (Entry entry : entrySet) { String key = entry.getKey(); if(key.contains(name)){ return key; } } return null; } @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // doSync(); } private static String getAiChuangToken() { Map header = new HashMap(); header.put("Content-type", "application/json"); JSONObject obj = new JSONObject(); obj.put("loginName", "xw"); obj.put("password", "1qaz@WSX"); String post = HttpKit.post("http://183.215.13.140:324/openapi/token", obj.toJSONString(),header); JSONObject parseObject = JSONObject.parseObject(post); String token = parseObject.getJSONObject("data").getString("token"); return token; } }