| | |
| | | package com.smartearth.poiexcel.service; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.google.common.collect.Lists; |
| | | import com.smartearth.poiexcel.entity.*; |
| | | import com.smartearth.poiexcel.mapper.BasicMapper; |
| | | import com.smartearth.poiexcel.mapper.EntMapper; |
| | | import com.smartearth.poiexcel.utils.RestHelper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| | | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClientBuilder; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.ssl.SSLContexts; |
| | | import org.apache.http.ssl.TrustStrategy; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.net.ssl.SSLContext; |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | |
| | | * @date 2023-10-05 |
| | | */ |
| | | @Service |
| | | public class EntService implements EntMapper { |
| | | @SuppressWarnings("ALL") |
| | | public class EntService { |
| | | @Resource |
| | | EntMapper entMapper; |
| | | |
| | | @Value("${qylweb.url}") |
| | | String url; |
| | | @Value("${qylweb.host}") |
| | | String host; |
| | | |
| | | @Value("${qylweb.user}") |
| | | String user; |
| | |
| | | @Value("${qylweb.pwd}") |
| | | String pwd; |
| | | |
| | | private static RestTemplate restTemplate; |
| | | |
| | | private final static Log log = LogFactory.getLog(EntService.class); |
| | | |
| | | private final static String generateToken = "%s/yqfwg/app/generateToken"; |
| | | private final static String GENERATE_TOKEN = "%s/yqfwg/app/generateToken"; |
| | | |
| | | private final static String getEntBaseInfo = "%s/yqfwg/api/project/getEntBaseInfoForOtherSysListPage?ctoken=%s&buildDate_startdate=%s&buildDate_enddate=%s&qylabel=%s&showCount=%d¤tPage=%d"; |
| | | private final static String GET_ENT_BASE_INFO = "%s/yqfwg/api/project/getEntBaseInfoForOtherSysListPage?ctoken=%s&showCount=%d¤tPage=%d"; |
| | | |
| | | /** |
| | | * 获取令牌 |
| | | */ |
| | | public String selectToken() { |
| | | try { |
| | | String url = String.format(GENERATE_TOKEN, host); |
| | | |
| | | //Map<String, String> map = new HashMap<>(2); |
| | | //map.put("userName", user); |
| | | //map.put("passWord", pwd); |
| | | |
| | | //String map = "userName=shikong001&passWord=123!@#qwe"; |
| | | |
| | | // 请求头信息 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.valueOf("application/x-www-form-urlencoded")); |
| | | |
| | | //设置为表单提交,按需求加 |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | // 组装请求信息 |
| | | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); |
| | | map.add("userName", user); |
| | | map.add("passWord", pwd); |
| | | HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers); |
| | | |
| | | // String str = RestHelper.postForRest(url, map) |
| | | Result rs = postForRest(url, map, Result.class); |
| | | if (null == rs || StaticData.I200 != rs.getCode()) { |
| | | return null; |
| | | } |
| | | |
| | | // TokenResult tr = JSONObject.parseObject(rs.getData().toJSONString(), TokenResult.class) |
| | | TokenResult tr = rs.getData().toJavaObject(TokenResult.class); |
| | | |
| | | return tr.getCtoken(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查询企业 |
| | | */ |
| | | public List<EntEntity> selectEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) { |
| | | try { |
| | | String url = String.format(GET_ENT_BASE_INFO, host, token, showCount, currentPage); |
| | | if (null != startDate) { |
| | | url += "&buildDate_startdate=" + startDate; |
| | | } |
| | | if (null != endDate) { |
| | | url += "&buildDate_enddate=" + endDate; |
| | | } |
| | | if (null != qylabel) { |
| | | url += "&qylabel=" + qylabel; |
| | | } |
| | | |
| | | Result rs = getForRest(url, Result.class); |
| | | if (null == rs || StaticData.I200 != rs.getCode()) { |
| | | return null; |
| | | } |
| | | |
| | | // EntResult er = JSONObject.parseObject(rs.getData().toJSONString(), EntResult.class) |
| | | EntResult er = rs.getData().toJavaObject(EntResult.class); |
| | | |
| | | return er.getPd().getList(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 插入企业 |
| | | */ |
| | | public Integer insertEnts(List<EntEntity> list) { |
| | | try { |
| | | int rows = 0; |
| | | List<List<EntEntity>> subLists = Lists.partition(list, StaticData.I200); |
| | | for (List<EntEntity> sub : subLists) { |
| | | try { |
| | | rows += entMapper.insertBatch(sub); |
| | | } catch (Exception ex) { |
| | | log.error(ex); |
| | | } |
| | | } |
| | | |
| | | return rows; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * get请求(Rest) |
| | | */ |
| | | public <T> T getForRest(String url, Class<T> clazz) { |
| | | // RestTemplate rest = RestHelper.getRestTemplate() |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(url, clazz); |
| | | } |
| | | |
| | | /** |
| | | * post请求(Rest) |
| | | */ |
| | | public <T> T postForRest(String url, Map<String, Object> map, Class<T> clazz) { |
| | | public <T> T postForRest(String url, Object map, Class<T> clazz) { |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | // RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(url, map, clazz); |
| | | } |
| | |
| | | /** |
| | | * delete请求(Rest) |
| | | */ |
| | | public Object deleteForRest(String url, Map<String, Object> map) { |
| | | public <T> T deleteForRest(String url, Map<String, T> map, Class<T> clazz) { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | |
| | | HttpEntity<?> entity = new HttpEntity<>(map, headers); |
| | | |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | ResponseEntity<Object> rs = rest.exchange(url, HttpMethod.DELETE, entity, Object.class); |
| | | // RestTemplate rest = RestHelper.getRestTemplate() |
| | | RestTemplate rest = getRestTemplate(); |
| | | ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz); |
| | | |
| | | return rs.getBody(); |
| | | } |
| | |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 获取RestTemplate |
| | | */ |
| | | public static RestTemplate getRestTemplate() { |
| | | if (null == restTemplate) { |
| | | try { |
| | | TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; |
| | | SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); |
| | | SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); |
| | | |
| | | HttpClientBuilder clientBuilder = HttpClients.custom(); |
| | | |
| | | CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslFactory).build(); |
| | | |
| | | HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); |
| | | requestFactory.setHttpClient(httpClient); |
| | | |
| | | restTemplate = new RestTemplate(requestFactory); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | return restTemplate; |
| | | } |
| | | } |