InterSiService.java 19.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
/**
 * Copyright © 2015-2018 ODM All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 

import org.apache.commons.io.IOUtils; 
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.service.TreeService;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBasePerson;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusQlr;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusFdcq2Service;
import com.thinkgem.jeesite.modules.sys.dao.DictDao;
import com.thinkgem.jeesite.modules.sys.dao.OfficeDao;
import com.thinkgem.jeesite.modules.sys.dao.RegBusInterHisDao;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.RegBusInterHis;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.po.JudgeDto;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 机构Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class InterSiService extends CrudService<RegBusInterHisDao,RegBusInterHis> {
	private Logger logger = LoggerFactory.getLogger(InterSiService.class);
		
		/**
		 * 前端URL后缀
		 */
		@Value("${odm_Si_Url}")
		protected String odm_si_url;
		@Value("${odm_Judge_Url}")
		protected String odm_judge_url;
		@Value("classpath:si_code.json")
		protected Resource  json_si_info;
		
		private Map si_info;
		private String charset = "utf-8";
		
		@Autowired
		private RegBusFdcq2Service regBusFdcq2Service;
		
		public Map getInfoMap() throws JsonParseException, JsonMappingException, IOException {
			if(si_info != null && si_info.size() > 0 ) {
				return si_info;
			} 
			String odm_si_info =  IOUtils.toString(json_si_info.getInputStream(), Charset.forName("UTF-8")); 
			ObjectMapper mapper = new ObjectMapper();
			si_info = mapper.readValue(odm_si_info, new TypeReference<Map>() {}); 
			return si_info; 
		}
		
		public void updatePersonInfo(RegBasePerson regBasePerson,String interhis_id) {
			if(interhis_id == null ) {
				return;
			}
			String[] str_ids = interhis_id.split(",");
			for (String id : str_ids) {
				if(StringUtils.isEmpty(id)) {
					continue;
				}
				RegBusInterHis his = dao.get(id);
				his.setPersonid(regBasePerson.getId());
				his.setPcode(regBasePerson.getPcode());
				dao.update(his);
			}
		}
		
		public RegBusInterHis getInterHisByMc(RegBusQlr qlr,String intermc) {
			RegBusInterHis his = new RegBusInterHis();
			his.setPcode(qlr.getPcode());
			his.setJkmc(intermc);
			List<RegBusInterHis> list = findList(his);
			return list == null||list.size()<= 0 ?null:list.get(0);
		}
		
		
		public Map codeTochinese(Map res_data,String code_name) throws JsonParseException, JsonMappingException, IOException, ServiceException{
			 Iterator it = res_data.keySet().iterator();
			 Map res_map = Maps.newHashMap();
			 Map res_data_str = Maps.newHashMap();
			 
			 if(!getInfoMap().containsKey(code_name)) {
				 throw new ServiceException("市不动产共享数据平台配置错误,未找到【"+code_name+"】的中文配置信息");
			 }
			 Map info_map = (Map)(getInfoMap().get(code_name)) ;
			 if(!info_map.containsKey("data")) {
				 throw new ServiceException("市不动产共享数据平台配置错误,未找到【"+code_name+"】的中文代码配置信息");
			 }
			 Map code_data = (Map)info_map.get("data");
			 
			 Map code_china = info_map.containsKey("code")?(Map)info_map.get("code"):Maps.newHashMap();
			 
			 while(it.hasNext()) {
				 String key = it.next().toString();
				 String value = res_data.get(key) == null ?"": res_data.get(key).toString();
				 String c_key = "未配置";
				 
				 
				 if(code_data.containsKey(key)) {
					 c_key = code_data.get(key).toString();
				 }else {
					//未配置-中文名称 不显示
					continue;
				 }
				 
				 //内容为空,不显示
				 if(!StringUtils.isBlank(value) ){
					 res_map.put(c_key, value); 
				 }
				 
				 if(code_china.containsKey(key)) {
					 Map c_map = (Map)code_china.get(key);
					 String c_value = c_map.get(value) == null?value:c_map.get(value).toString();
					 
					 res_data_str.put(key+"_text", c_value);
					 res_map.put(c_key, c_value);
				 } 
			 }
			 res_data.putAll(res_data_str);
			 return res_map;
			
		}
		
		public  String getValueByMap(Map map,String key,String value){
			return map.get(key)==null?value:map.get(key).toString();
		}	
		
	 public Map si_post(String data,String interdata,String si_code) throws JsonParseException, JsonMappingException, IOException, ServiceException {
		 ObjectMapper mapper = new ObjectMapper();
		 User user = UserUtils.getUser();
		 Map m_map =Maps.newHashMap(); 
		 RegBusInterHis interhis = new RegBusInterHis();
		 Map inter_map= mapper.readValue(interdata, new TypeReference<Map>() {}); 
		 
		 String rylx  = getValueByMap(inter_map, "rylx", "");
		 interhis.setChyw(getValueByMap(inter_map,"chyw",""));
		 interhis.setJklx(si_code);
		 interhis.setJkmc(getValueByMap(inter_map,"jkmc",""));
		 interhis.setRymc(getValueByMap(inter_map,"rymc",""));
		 interhis.setZjhm(getValueByMap(inter_map,"zjhm",""));
		 interhis.setPersonid(getValueByMap(inter_map, "personid", ""));
		 interhis.setPcode(getValueByMap(inter_map, "pcode", ""));
		 interhis.setInparam(data);
		 
		 String url = odm_si_url+si_code;
		 logger.info("http request url:" + url);
		 logger.info("http request data:" + data); 
		 
		 Map header = Maps.newHashMap();
		 // header.put("token", Global.getAreaCode());
		 header.put("uid", Global.getInterUserName());
		 header.put("pwd", Global.getInterPassword());
		 header.put("xmid", "bdc_platSi");
		 header.put("version", "1");
 
		 List<NameValuePair> lst = Lists.newArrayList();
		  
		 Map params= mapper.readValue(data, new TypeReference<Map>() {
			});
		 Iterator it = params.keySet().iterator();
		 String searchType = "";
		 while(it.hasNext()) {
			 String key = it.next().toString();
			 String value = params.get(key) == null ?"": params.get(key).toString();
			 if("search_type".equals(key)) {
				 searchType = value;
			 }
			 NameValuePair pair = new BasicNameValuePair(key, value);
			 lst.add(pair);
		 }
		 String res_str = getReturnResByUrl(url, lst, header);
		 
		 logger.info("http post back:" + res_str);
		 
		 interhis.setOutparam(res_str);
		 
		 Map res_map= mapper.readValue(res_str, new TypeReference<Map>() {}); 
		 String chinfo = "";
		 
		 Boolean res_success = res_map == null || res_map.get("success")== null ?false: Boolean.parseBoolean(res_map.get("success").toString());
			if(res_success) {
				
		    	Map res_data =(Map)res_map.get("data");
				if (res_data == null || res_data.size() <= 0) {
					throw new ServiceException("请求市不动产共享数据平台时,返回信息为空");
				}
		    	
				m_map.put("data",res_data);
				 
				si_code = si_code.substring(si_code.indexOf('/')+1, si_code.length());
				 
				Map map = codeTochinese(res_data, si_code);
				m_map.put("china_info", map);
				interhis.setVeritydatamsg("数据核查通过 数据内容:"+(map.isEmpty()?"未找到数据信息":JsonUtil.toJsonString(map)));
				interhis.setVeritydatazt("1");
				if(si_code.endsWith("bzfwhhxxcx")) {
					interhis.setVeritydatamsg("数据核查通过 数据内容:"+(map.isEmpty()?"未找到民政殡葬服务火化信息":JsonUtil.toJsonString(map)));
				}
				
				if(si_code.endsWith("rxbd")  || si_code.endsWith("rkjzxxcx") ) {
					String xmppddm = getValueByMap(res_data, "XM_PPDDM", ""); 
					String zjhppddm = getValueByMap(res_data, "GMSFHM_PPDDM", ""); 
					if(!"1".equals(zjhppddm) || !"1".equals(xmppddm) ){
						interhis.setVeritydatazt("2");
						interhis.setVeritydatamsg("数据核查未通过,姓名、证件号码不匹配 数据内容:"+(map.isEmpty()?"未找到核查数据信息":JsonUtil.toJsonString(map)));
					} 
					String xsd = getValueByMap(res_data, "XSD", "");
					if(!StringUtils.isBlank(xsd)) {
						BigDecimal b = new BigDecimal(xsd);
						BigDecimal bz = new BigDecimal("90");
						if(b.compareTo(bz)<=0 ) {
							interhis.setVeritydatazt("2");
							interhis.setVeritydatamsg("数据核查未通过,相似度不够  数据内容:"+ (map.isEmpty()?"未找到核查数据信息":JsonUtil.toJsonString(map)));
						} 
					}
				}
				 
			}else {
				String errmsg = res_map.get("resultMsg")== null?"": res_map.get("resultMsg").toString();
				interhis.setVeritydatazt("2");
				interhis.setVeritydatamsg("数据核查未通过,信息:"+errmsg);
				throw new ServiceException("请求市不动产共享数据平台错误,错误信息:"+errmsg);
			}    
			interhis.setCreateby(user.getId());
			interhis.setCreateuser(user.getLoginName());
			interhis.setCreateDate(new Date());
			save(interhis);
			m_map.put("rxbdzt", interhis.getVeritydatazt());
			m_map.put("interhis_id", interhis.getId());
			return m_map;
	 }

	 /**
	  * 权利人保存修改调用婚姻接口
	  * @param data
	  * @param si_code
	  * @return
	  * @throws JsonParseException
	  * @throws JsonMappingException
	  * @throws IOException
	  * @throws ServiceException
	  */
	 public Map si_post1(String data,RegBusInterHis interhis, String si_code) throws JsonParseException, JsonMappingException, IOException {
		 ObjectMapper mapper = new ObjectMapper();
		 String url = odm_si_url+si_code;
		 logger.info("http request url:" + url);
		 logger.info("http request data:" + data); 
		 
		 Map header = Maps.newHashMap();
			// header.put("token", Global.getAreaCode());
		header.put("uid", Global.getInterUserName());
		header.put("pwd", Global.getInterPassword());
		header.put("xmid", "bdc_platSi");
		header.put("version", "1");
			
		 List<NameValuePair> lst = Lists.newArrayList();
		 
		 Map params= mapper.readValue(data, new TypeReference<Map>() {});
		 Iterator it = params.keySet().iterator();
		 while(it.hasNext()) {
			 String key = it.next().toString();
			 String value = params.get(key) == null ?"": params.get(key).toString();
			 NameValuePair pair = new BasicNameValuePair(key, value);
			 lst.add(pair);
		 }
		 String res_str = getReturnResByUrl(url,lst,header);
		 if(StringUtils.isEmpty(res_str)) {
			 throw new ServiceException("网络地址:"+url+"请求失败,未接收到返回结果");
		 }
		 
		 logger.info("http post back:" + res_str);
		 interhis.setOutparam(res_str);
		 Map res_map= mapper.readValue(res_str, new TypeReference<Map>() {}); 
		 
		 Boolean res_success = res_map == null || res_map.get("success")== null ?false: Boolean.parseBoolean(res_map.get("success").toString());
		 Map m_map =Maps.newHashMap();
		 if(res_success) {
			 Map res_data =(Map)res_map.get("data");  
			 m_map.put("data",res_data);
		 }   
		 return m_map;
	 }	 
	 
	 /**
		 * @param url
		 *            post方式请求
		 * @return url返回的数据,以字符串显示
		 * @throws ClientProtocolException
		 * @throws IOException
	 * @throws ServiceException 
		 */
		public  String getReturnResByUrl(String url,
				List<NameValuePair> params){
			Integer URLOUTTIME = 1000 * 60;
			CloseableHttpResponse response = null;
			CloseableHttpClient client = HttpClientBuilder.create().build();
			String res = "";
			HttpPost httppost = new HttpPost(url); 
			try {
				RequestConfig requestConfig = RequestConfig.custom()
						.setConnectionRequestTimeout(URLOUTTIME)
						.setConnectTimeout(URLOUTTIME)
						.setSocketTimeout(URLOUTTIME).build();
				httppost.setConfig(requestConfig);
				if (params != null && params.size() > 0) {
					httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
				}
				httppost.setHeader("ContentType",
						"application/x-www-form-urlencoded;charset=UTF-8");
				httppost.setHeader("x-requested-with", "XMLHttpRequest");
				response = client.execute(httppost);
				if (response.getStatusLine().getStatusCode() == 200) {
					res = EntityUtils.toString(response.getEntity(), "utf-8");
				}
			}catch (HttpHostConnectException e) {
				e.printStackTrace();
				throw new ServiceException("网路地址:"+url+"连接失败"); 
			} catch (ParseException e) {
				e.printStackTrace();
				throw new ServiceException("网路地址:"+url+"连接失败"); 
			} catch (IOException e) {
				e.printStackTrace();
				throw new ServiceException("网路地址:"+url+"连接失败"); 
			}finally {
				if(httppost != null) {
					httppost.releaseConnection();
				}
				if(response != null) {
					try {
						response.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(client != null){
					try {
						client.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
			return res;
		}
		
		/**
		 * @param url post方式请求
		 * @return url返回的数据,以字符串显示
		 * @throws ClientProtocolException
		 * @throws IOException
		 */
		public String getReturnResByUrl(String url, List<NameValuePair> params, Map<String, String> header)
				throws IOException {
			logger.info("http request url:" + url);
			logger.info("http request header:" + JsonMapper.toJsonString(header));
			logger.info("http request data:" + JsonMapper.toJsonString(params));

			Integer URLOUTTIME = 1000 * 60;
			String res = "";
			HttpPost httppost = null;
			CloseableHttpClient client = null;
			CloseableHttpResponse response = null;
			Exception exception = null;
			try {
				client = HttpClientBuilder.create().build();
				httppost = new HttpPost(url);
				RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
						.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
				httppost.setConfig(requestConfig);

				if (params != null && params.size() > 0) {
					httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
				}
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
				httppost.setHeader("x-requested-with", "XMLHttpRequest");
				if (header != null && header.size() > 0) {
					Iterator<String> it = header.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next();
						httppost.setHeader(key, header.get(key));
					}
				}

				response = client.execute(httppost);
				int code = response.getStatusLine().getStatusCode();
				if (code == 200) {
					res = EntityUtils.toString(response.getEntity(), charset);
				} else {
					throw new ServiceException("返回状态代码" + code);
				}
			} catch (Exception s) {
				exception = s;
				throw new ServiceException(" http请求发生错误," + s.getMessage());
			} finally {
				if (httppost != null) {
					httppost.releaseConnection();
				}
				if (response != null) {
					response.close();
				}
				if (client != null) {
					client.close();
				}
			}

			logger.info("http post back:" + res);
			return res;

		}

		public Map ju_post(JudgeDto data, String si_code) throws IOException {
			 ObjectMapper mapper = new ObjectMapper();
			 User user = UserUtils.getUser();
			 Map params= Maps.newHashMap();
			 params.put("name", user.getName());
			 params.put("businessNumber", data.getBusinessNumber());
			 params.put("ajbh", data.getAjbh());
			 params.put("qxdm", Global.getAreaCode());
			 params.put("cxqqdh", data.getCxqqdh());
			 Map m_map =Maps.newHashMap();  
			 String url = odm_judge_url+si_code;
			 logger.info("http request url:" + url);
			 logger.info("http request data:" + params); 
			 
			 Map header = Maps.newHashMap();
			 Integer URLOUTTIME = 1000 * 60;
				String res = "";
				HttpPost httppost = null;
				CloseableHttpClient client = null;
				CloseableHttpResponse response = null;
				Exception exception = null;
				try {
					client = HttpClientBuilder.create().build();
					httppost = new HttpPost(url);
					RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
							.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
					httppost.setConfig(requestConfig);
					List<NameValuePair> urlParameters = Lists.newArrayList();
					
			
			 if(params != null && params.size()>0) {
					Iterator<String> it = params.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next(); 
						urlParameters.add(new BasicNameValuePair(key,(String) params.get(key))); 
					}
					httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
				}
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
				response = client.execute(httppost);
				res = EntityUtils.toString(response.getEntity(), "utf-8");
				logger.info("返回信息:" + res);
			    String cxqqdh = "";
			    	Map resp =JsonUtil.jsonToMap(res);
			    	Map res_data = JsonUtil.jsonToMap(JsonUtil.toJsonString(resp.get("head")));
			    	logger.info("返回信息:" + res_data);
			    	String requestType = (String) res_data.get("requestType");
			    	params.put("ywh", params.get("businessNumber"));
			    	if(StringUtils.isNotBlank(requestType)) {
			    	if(requestType.equals("GF01")) {
			    	if(res_data.containsKey("status") ) {
			    	 cxqqdh = (String) res_data.get("cxqqdh");
			    	
					m_map.put("cxqqdh",cxqqdh);
					params.put("cxqqdh", cxqqdh);
					
			    	}
			    	}else if(requestType.equals("GF99")) {
			    	   Map result =  (Map) resp.get("data");
			    	   String pjnr = (String) result.get("data");
			    	   logger.info("判决内容:" + pjnr);
			    	   if(StringUtils.isNotBlank(pjnr)) {
			    	   params.put("pjnr", pjnr);
			    	   regBusFdcq2Service.updatePjnr(params);
			    	   }
			    	}
			    	}
			    	}catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (httppost != null) {
						httppost.releaseConnection();
					}
					if (response != null) {
						response.close();
					}
					if (client != null) {
						client.close();
					}
				}
				return m_map;
		}
}