SyncJob.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package com.qlm.job;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import org.quartz.Job;
  9. import org.quartz.JobExecutionContext;
  10. import org.quartz.JobExecutionException;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import com.jfinal.kit.HttpKit;
  16. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  17. import com.jfinal.plugin.activerecord.Db;
  18. import com.jfinal.plugin.activerecord.Record;
  19. import com.jfinal.plugin.druid.DruidPlugin;
  20. import com.qlm.tools.WxUtil;
  21. public class SyncJob implements Job {
  22. private static void con(){
  23. DruidPlugin dp = new DruidPlugin("jdbc:mysql://58.20.133.135:6177/jinzai?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "jinzai", "rTdBsjA636XkarRa");
  24. ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
  25. // ��web����Ψһ�IJ�ͬ��Ҫ�ֶ�����һ����ز����start()����
  26. dp.start();
  27. arp.start();
  28. }
  29. static Map<String,String> codeNameMap = new HashMap<String, String>();
  30. private static final Logger logger = LoggerFactory.getLogger(MyJob.class);
  31. public static void main(String[] args) {
  32. con();
  33. // doSync();
  34. // fix();
  35. }
  36. private static void fix() {
  37. // TODO Auto-generated method stub
  38. List<Record> datas = Db.find("select * from jinzai_jxs_area_xian where id = 10284");
  39. codeNameMap.clear();
  40. List<Record> areas = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4");
  41. Map<String,Record> codeMap = new HashMap<String, Record>();
  42. for (Record record : areas) {
  43. String str = record.getStr("code");
  44. String name = record.getStr("name");
  45. codeMap.put(name, record);
  46. codeNameMap.put(str, name);
  47. }
  48. List<Record> list = new ArrayList<Record>();
  49. for (Record data :datas) {
  50. String name = data.getStr("jxs");
  51. String code = data.getStr("code");
  52. String city = null;
  53. if(WxUtil.isNull(name)){
  54. continue;
  55. }
  56. if(name.length()<6){
  57. continue;
  58. }
  59. Record info = new Record();
  60. String temp = name.substring(0,3);
  61. String lastStr = "";
  62. String province = null;
  63. int index = 2;
  64. if(temp.endsWith("省") || "黑龙江".equals(temp) || "内蒙古".equals(temp)){
  65. province = name.substring(0,3);
  66. lastStr = name.substring(3);
  67. index = 3;
  68. }else{
  69. lastStr = name.substring(2);
  70. province = name.substring(0,2);
  71. }
  72. int length = lastStr.length();
  73. String userName = lastStr.substring(length-2,length);
  74. String cityName = null;
  75. try{
  76. cityName = lastStr.substring(0,length-2);
  77. }catch(Exception e){
  78. continue;
  79. }
  80. if(isBigCity(province)){
  81. province = city = province+"市";
  82. }else{
  83. String containName = containName(codeMap,cityName);
  84. if(containName != null){
  85. String[] find = find(codeMap, containName);
  86. province = find[0];
  87. city = find[1];
  88. }else{
  89. userName = lastStr.substring(length-3);
  90. cityName = lastStr.substring(0,length-3);
  91. containName = containName(codeMap,cityName);
  92. if(containName != null){
  93. String[] find = find(codeMap, containName);
  94. province = find[0];
  95. city = find[1];
  96. }else{
  97. if(cityName.length() >=3){
  98. cityName = cityName.substring(0,2);
  99. }
  100. containName = containName(codeMap,cityName);
  101. if(containName != null){
  102. String[] find = find(codeMap, containName);
  103. province = find[0];
  104. city = find[1];
  105. }else{
  106. System.out.println("无法获取,"+name);
  107. }
  108. }
  109. }
  110. }
  111. info.set("jxs", name).set("code", code).set("city", city).set("province", province);
  112. list.add(info);
  113. }
  114. WxUtil.batchSaveReplace("jinzai_jxs_area", list, "");
  115. }
  116. private static void doSync() {
  117. codeNameMap.clear();
  118. String aiChuangToken = getAiChuangToken();
  119. List<Record> find = Db.find("select code,name,parent_code,type from tb_map_area_copy241023 where type<=4");
  120. Map<String,Record> codeMap = new HashMap<String, Record>();
  121. for (Record record : find) {
  122. String str = record.getStr("code");
  123. String name = record.getStr("name");
  124. codeMap.put(name, record);
  125. codeNameMap.put(str, name);
  126. }
  127. for (int i = 0; i < 10000; i++) {
  128. boolean data = getData(aiChuangToken,i+1,codeMap);
  129. if(!data){
  130. break;
  131. }
  132. }
  133. }
  134. public static boolean getData(String toekn,int page,Map<String,Record> codeMap){
  135. Map<String,String> header = new HashMap<String,String>();
  136. header.put("Content-type", "application/json");
  137. header.put("Authorization", "Bearer "+toekn);
  138. JSONObject obj = new JSONObject();
  139. obj.put("modifiedTime", "1990-01-01 00:00:00");
  140. obj.put("corpType", "2");
  141. obj.put("currentPage", page);
  142. obj.put("itemsPerPage", 1000);
  143. String post = HttpKit.post("http://183.215.13.140:324/openapi/basedata/corp/all/listPage", obj.toJSONString(),header);
  144. JSONObject parseObject = JSONObject.parseObject(post);
  145. JSONObject jsonObject = parseObject.getJSONObject("data");
  146. JSONArray jsonArray = jsonObject.getJSONArray("items");
  147. if(jsonArray.isEmpty()){
  148. return false;
  149. }else{
  150. Integer currentPage = jsonObject.getInteger("currentPage");
  151. if(page>currentPage){
  152. return false;
  153. }
  154. System.out.println("当前页数:"+page);
  155. List<Record> list = new ArrayList<Record>();
  156. for (int i = 0; i < jsonArray.size(); i++) {
  157. JSONObject jsonObject2 = jsonArray.getJSONObject(i);
  158. String name = jsonObject2.getString("name");
  159. String code = jsonObject2.getString("code");
  160. String city = null;
  161. if(WxUtil.isNull(name)){
  162. continue;
  163. }
  164. if(name.length()<6){
  165. continue;
  166. }
  167. Record info = new Record();
  168. String temp = name.substring(0,3);
  169. String lastStr = "";
  170. String province = null;
  171. int index = 2;
  172. if(temp.endsWith("省") || "黑龙江".equals(temp) || "内蒙古".equals(temp)){
  173. province = name.substring(0,3);
  174. lastStr = name.substring(3);
  175. index = 3;
  176. }else{
  177. lastStr = name.substring(2);
  178. province = name.substring(0,2);
  179. }
  180. int length = lastStr.length();
  181. String userName = lastStr.substring(length-2,length);
  182. String cityName = null;
  183. try{
  184. cityName = lastStr.substring(0,length-2);
  185. }catch(Exception e){
  186. continue;
  187. }
  188. if(isBigCity(province)){
  189. province = city = province+"市";
  190. }else{
  191. String containName = containName(codeMap,cityName);
  192. if(containName != null){
  193. String[] find = find(codeMap, containName);
  194. province = find[0];
  195. city = find[1];
  196. }else{
  197. userName = lastStr.substring(length-3);
  198. cityName = lastStr.substring(0,length-3);
  199. containName = containName(codeMap,cityName);
  200. if(containName != null){
  201. String[] find = find(codeMap, containName);
  202. province = find[0];
  203. city = find[1];
  204. }else{
  205. if(cityName.length() >=3){
  206. cityName = cityName.substring(0,2);
  207. }
  208. containName = containName(codeMap,cityName);
  209. if(containName != null){
  210. String[] find = find(codeMap, containName);
  211. province = find[0];
  212. city = find[1];
  213. }else{
  214. System.out.println("无法获取,"+name);
  215. }
  216. }
  217. }
  218. }
  219. info.set("jxs", name).set("code", code).set("city", city).set("province", province);
  220. list.add(info);
  221. }
  222. WxUtil.batchSaveIgnore("jinzai_jxs_area", list, "");
  223. }
  224. return true;
  225. }
  226. private static String[] find(Map<String, Record> codeMap,
  227. String containName) {
  228. String city = null;
  229. String province = null;
  230. String [] cis = {"",""};
  231. Record record = codeMap.get(containName);
  232. int type = WxUtil.getInt("type", record);
  233. String parent_code = record.getStr("parent_code");
  234. if(type == 3){
  235. city = codeNameMap.get(parent_code);
  236. Record cityInfo = codeMap.get(city);
  237. parent_code = cityInfo.getStr("parent_code");
  238. province = codeNameMap.get(parent_code);
  239. }else if(type == 2){
  240. city = containName;
  241. province = codeNameMap.get(parent_code);
  242. }else if(type == 4){
  243. String string = codeNameMap.get(parent_code);
  244. return find(codeMap,string);
  245. }
  246. cis[0] = province;
  247. cis[1] = city;
  248. return cis;
  249. }
  250. public static boolean isBigCity(String province){
  251. String citys [] = {"北京","重庆","天津","上海"};
  252. for (String string : citys) {
  253. if(string.equals(province)){
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. public static String containName(Map<String,Record> codeMap,String name){
  260. Set<Entry<String,Record>> entrySet = codeMap.entrySet();
  261. for (Entry<String, Record> entry : entrySet) {
  262. String key = entry.getKey();
  263. if(key.contains(name)){
  264. return key;
  265. }
  266. }
  267. return null;
  268. }
  269. @Override
  270. public void execute(JobExecutionContext arg0) throws JobExecutionException {
  271. // doSync();
  272. }
  273. private static String getAiChuangToken() {
  274. Map<String,String> header = new HashMap<String,String>();
  275. header.put("Content-type", "application/json");
  276. JSONObject obj = new JSONObject();
  277. obj.put("loginName", "xw");
  278. obj.put("password", "1qaz@WSX");
  279. String post = HttpKit.post("http://183.215.13.140:324/openapi/token", obj.toJSONString(),header);
  280. JSONObject parseObject = JSONObject.parseObject(post);
  281. String token = parseObject.getJSONObject("data").getString("token");
  282. return token;
  283. }
  284. }