PovertyService.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.goafanti.common.service;
  2. import com.goafanti.common.bo.InputPoverty;
  3. import com.goafanti.common.bo.OutPoverty;
  4. import com.goafanti.common.utils.StringUtils;
  5. import com.goafanti.common.utils.excel.NewExcelUtil;
  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.jsoup.nodes.Element;
  9. import org.jsoup.select.Elements;
  10. import org.springframework.cache.annotation.Cacheable;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. @Service
  19. public class PovertyService {
  20. private static final String gx_url="http://fpb.gxxw.cn/index.php?m=check&c=index&a=list_jdlk&name=";
  21. @Cacheable(value = "selectPoverty#300", key = "'selectPoverty:key='+#id")
  22. public Map<String,Object> selectPoverty(MultipartFile file,String id) {
  23. NewExcelUtil<InputPoverty> newExcelUtil=new NewExcelUtil<>(InputPoverty.class);
  24. List<InputPoverty> list = null;
  25. try {
  26. list = newExcelUtil.importExcel(file.getInputStream(),0);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. List<OutPoverty> res=new ArrayList<>();
  31. Integer totalcount =0;
  32. Integer effectiveCount =0;
  33. for (InputPoverty e : list) {
  34. OutPoverty outPoverty = selectHttpPoverty(e);
  35. if (outPoverty.getId()!=null){
  36. effectiveCount++;
  37. }
  38. totalcount++;
  39. res.add(outPoverty);
  40. }
  41. Map<String ,Object> map=new HashMap<>();
  42. map.put("list",res);
  43. map.put("totalcount",totalcount);
  44. map.put("effectiveCount",effectiveCount);
  45. map.put("id",id);
  46. return map;
  47. }
  48. private OutPoverty selectHttpPoverty(InputPoverty in) {
  49. OutPoverty out=new OutPoverty();
  50. try {
  51. String name= StringUtils.deleteWhitespace(in.getName());
  52. String idcard=in.getIdCard();
  53. StringBuffer url=new StringBuffer(gx_url);
  54. url=url.append(name).append("&idcard=").append(idcard);
  55. Document document = Jsoup
  56. .connect(url.toString()).get();
  57. Elements filequery_table = document.getElementsByClass("Filequery_table");
  58. Elements tr = filequery_table.select("tr");
  59. if (tr.size()>1){
  60. Element element = tr.get(1);
  61. Elements td = element.select("td");
  62. out.setId(td.get(0).text());
  63. out.setCity(td.get(1).text());
  64. out.setCounty(td.get(2).text());
  65. out.setTownship(td.get(3).text());
  66. out.setVillage(td.get(4).text());
  67. out.setPeopleSum(td.get(5).text());
  68. out.setName(td.get(6).text());
  69. out.setRelationship(td.get(7).text());
  70. out.setPovertyYear(td.get(8).text());
  71. }else {
  72. out.setName(name);
  73. }
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. return out;
  78. }
  79. }