| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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<String,String> codeNameMap = new HashMap<String, String>();
- 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<Record> datas = Db.find("select * from jinzai_jxs_area_xian where id = 10284");
- codeNameMap.clear();
- List<Record> areas = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4");
- Map<String,Record> codeMap = new HashMap<String, Record>();
- for (Record record : areas) {
- String str = record.getStr("code");
- String name = record.getStr("name");
- codeMap.put(name, record);
- codeNameMap.put(str, name);
- }
- List<Record> list = new ArrayList<Record>();
- 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<Record> find = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4");
- Map<String,Record> codeMap = new HashMap<String, Record>();
- 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<String,Record> codeMap){
-
- Map<String,String> header = new HashMap<String,String>();
- 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<Record> list = new ArrayList<Record>();
- 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<String, Record> 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<String,Record> codeMap,String name){
- Set<Entry<String,Record>> entrySet = codeMap.entrySet();
- for (Entry<String, Record> 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<String,String> header = new HashMap<String,String>();
- 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;
- }
- }
|