Commit 0406ed29f37050d896d1063fde8a25e5ea656dc4

Authored by zlz
1 parent 69d4f740

运管处基础数据更新

src/main/java/com/bsth/data/schedule/thread/SubmitToTrafficManage.java
1 package com.bsth.data.schedule.thread; 1 package com.bsth.data.schedule.thread;
2 2
3 import com.bsth.service.TrafficManageService; 3 import com.bsth.service.TrafficManageService;
  4 +import com.bsth.service.traffic.YgcBasicDataService;
4 import org.slf4j.Logger; 5 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +19,9 @@ public class SubmitToTrafficManage extends Thread{ @@ -18,6 +19,9 @@ public class SubmitToTrafficManage extends Thread{
18 @Autowired 19 @Autowired
19 TrafficManageService trafficManageService; 20 TrafficManageService trafficManageService;
20 21
  22 + @Autowired
  23 + YgcBasicDataService ygcBasicDataService;
  24 +
21 @Override 25 @Override
22 public void run() { 26 public void run() {
23 logger.info("开始提交数据到运管处..."); 27 logger.info("开始提交数据到运管处...");
@@ -46,6 +50,12 @@ public class SubmitToTrafficManage extends Thread{ @@ -46,6 +50,12 @@ public class SubmitToTrafficManage extends Thread{
46 } catch (Exception e) { 50 } catch (Exception e) {
47 logger.error("提交线路计划班次表到运管处失败", e); 51 logger.error("提交线路计划班次表到运管处失败", e);
48 } 52 }
  53 + try {
  54 + // 运管处基础数据更新
  55 + ygcBasicDataService.updateYgcBasicData();
  56 + } catch (Exception e) {
  57 + logger.error("运管处基础数据更新失败", e);
  58 + }
49 logger.info("提交数据到运管处结束!"); 59 logger.info("提交数据到运管处结束!");
50 } 60 }
51 } 61 }
src/main/java/com/bsth/service/traffic/YgcBasicDataService.java 0 → 100644
  1 +package com.bsth.service.traffic;
  2 +
  3 +/**
  4 + * 运管处基础数据操作接口类
  5 + * Created by zlz on 2017/6/16.
  6 + */
  7 +public interface YgcBasicDataService {
  8 + /**
  9 + * 更新运管处基础数据
  10 + */
  11 + void updateYgcBasicData();
  12 +}
  13 +
src/main/java/com/bsth/service/traffic/impl/YgcBasicDataServiceImpl.java 0 → 100644
  1 +package com.bsth.service.traffic.impl;
  2 +
  3 +import com.bsth.service.impl.TrafficManageServiceImpl;
  4 +import com.bsth.service.traffic.YgcBasicDataService;
  5 +import com.bsth.util.db.DBUtils_oldSystem;
  6 +import org.apache.axiom.om.*;
  7 +import org.apache.axis2.addressing.EndpointReference;
  8 +import org.apache.axis2.client.Options;
  9 +import org.apache.axis2.client.ServiceClient;
  10 +import org.apache.commons.codec.digest.DigestUtils;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.jdbc.core.BatchPreparedStatementSetter;
  15 +import org.springframework.jdbc.core.JdbcTemplate;
  16 +import org.springframework.stereotype.Service;
  17 +import org.w3c.dom.Document;
  18 +import org.w3c.dom.NodeList;
  19 +
  20 +import javax.activation.DataHandler;
  21 +import javax.xml.parsers.DocumentBuilder;
  22 +import javax.xml.parsers.DocumentBuilderFactory;
  23 +import java.io.*;
  24 +import java.sql.PreparedStatement;
  25 +import java.sql.SQLException;
  26 +import java.util.ArrayList;
  27 +import java.util.HashMap;
  28 +import java.util.List;
  29 +import java.util.zip.ZipEntry;
  30 +import java.util.zip.ZipFile;
  31 +import java.util.zip.ZipInputStream;
  32 +
  33 +/**
  34 + * 运管处基础数据操作类
  35 + * Created by zq on 2017/6/15.
  36 + */
  37 +@Service
  38 +public class YgcBasicDataServiceImpl implements YgcBasicDataService{
  39 + Logger logger = LoggerFactory.getLogger(TrafficManageServiceImpl.class);
  40 +
  41 + @Autowired
  42 + JdbcTemplate jdbcTemplate;
  43 +
  44 + private static String IP = "218.242.195.76:9091";
  45 + private static String targetEndpoint = "http://" + IP +"/ygc.TransManager.Basicdown?wsdl";
  46 + private static String namespace = "http://service.shygc.com";
  47 + private static String userName = "admin";
  48 + private static String passWord = "000000";
  49 + private static EndpointReference targetEPR;
  50 + {
  51 + try {
  52 + targetEPR = new EndpointReference(targetEndpoint);
  53 + } catch (Exception e) {
  54 + e.printStackTrace();
  55 + }
  56 + }
  57 +
  58 + /**
  59 + * 更新运管处基础数据
  60 + */
  61 + public void updateYgcBasicData(){
  62 + String saveFile = "ygcBasicData.zip";
  63 + String flag = "failure";
  64 + try {
  65 + // 调用接口下载基础数据
  66 + if(download(userName, DigestUtils.md5Hex(passWord), saveFile)){
  67 + // 读取基础数据zip
  68 + StringBuffer sb = readZipFile(saveFile);
  69 + // 解析xml
  70 + List<HashMap<String,String>> result = parseXml(sb);
  71 + // 插入数据库
  72 + if(result != null && result.size() > 0){
  73 + insertRecord(result);
  74 + }
  75 + }
  76 + flag = "success";
  77 + }catch (Exception e){
  78 + e.printStackTrace();
  79 + }
  80 + finally {
  81 + // 删除文件
  82 + File fill = new File(saveFile);
  83 + logger.info("updateYgcBasicData:"+fill.getAbsolutePath());
  84 + logger.info("updateYgcBasicData:"+flag);
  85 + if(fill.exists()){
  86 + fill.delete();
  87 + }
  88 + }
  89 + }
  90 + /**
  91 + * 下载基础数据,并生成rar文件
  92 + * @param userName
  93 + * @param password
  94 + * @param saveFile
  95 + * @return
  96 + */
  97 + private boolean download(String userName, String password, String saveFile)
  98 + {
  99 + try
  100 + {
  101 + OMElement data = buildDownloadEnvelope(userName, password);
  102 + ServiceClient sender = new ServiceClient();
  103 + Options options = sender.getOptions();
  104 + options.setProperty("SO_TIMEOUT", Integer.valueOf(1800000));
  105 + options.setTo(targetEPR);
  106 + sender.setOptions(options);
  107 + System.out.println("The data in method download: " + data);
  108 + data.build();
  109 + OMElement ome = sender.sendReceive(data);
  110 +
  111 + OMText binaryNode = (OMText)ome.getFirstOMChild();
  112 + binaryNode.setOptimize(true);
  113 + DataHandler actualDH = (DataHandler)binaryNode.getDataHandler();
  114 + FileOutputStream imageOutStream = new FileOutputStream(saveFile);
  115 + InputStream is = actualDH.getInputStream();
  116 + int read ;
  117 + byte[] buffer = new byte[1024];
  118 + while ((read = is.read(buffer)) != -1) {
  119 + imageOutStream.write(buffer, 0, read);
  120 + }
  121 + return true;
  122 + } catch (Exception e) {
  123 + e.printStackTrace();
  124 + }return false;
  125 + }
  126 +
  127 + /**
  128 + * 构建webservice接口参数
  129 + * @param userName
  130 + * @param password
  131 + * @return
  132 + */
  133 + private OMElement buildDownloadEnvelope(String userName, String password)
  134 + {
  135 + OMFactory fac = OMAbstractFactory.getOMFactory();
  136 + OMNamespace omNs = fac.createOMNamespace(namespace,
  137 + "ser");
  138 + OMElement data = fac.createOMElement("downloadAllDataFile", omNs);
  139 + OMElement _userName = fac.createOMElement("userName", omNs);
  140 + _userName.setText(userName);
  141 + OMElement _passWord = fac.createOMElement("passWord", omNs);
  142 + _passWord.setText(String.valueOf(password));
  143 +
  144 + data.addChild(_userName);
  145 + data.addChild(_passWord);
  146 +
  147 + OMElement soap = fac.createOMElement("downloadAllDataFile", omNs);
  148 + soap.addChild(data);
  149 + return soap;
  150 + }
  151 +
  152 + /**
  153 + * 读取zip文件的内容
  154 + * @param file
  155 + * @return
  156 + * @throws Exception
  157 + */
  158 + public StringBuffer readZipFile(String file) throws Exception {
  159 + ZipFile zf = new ZipFile(file);
  160 + InputStream in = new BufferedInputStream(new FileInputStream(file));
  161 + ZipInputStream zin = new ZipInputStream(in);
  162 + ZipEntry ze;
  163 + StringBuffer sb = new StringBuffer();
  164 + while ((ze = zin.getNextEntry()) != null) {
  165 + System.out.print(ze);
  166 + if (ze.isDirectory()) {
  167 + } else {
  168 + System.err.println("file - " + ze.getName() + " : "
  169 + + ze.getSize() + " bytes");
  170 + long size = ze.getSize();
  171 + if (size > 0) {
  172 + BufferedReader br = new BufferedReader(
  173 + new InputStreamReader(zf.getInputStream(ze),"GBK"));
  174 + String line;
  175 + boolean addFlag = false;
  176 + while ((line = br.readLine()) != null) {
  177 + if(line.indexOf("BusLineList") != -1){
  178 + addFlag = true;
  179 + }
  180 + if(addFlag){
  181 + sb.append(line);
  182 + }
  183 +
  184 + if(line.indexOf("/BusLineList") != -1){
  185 + break;
  186 + }
  187 + }
  188 + br.close();
  189 + }
  190 + }
  191 + }
  192 + zin.closeEntry();
  193 + return sb;
  194 + }
  195 +
  196 + /**
  197 + * 解析xml
  198 + * @param sb
  199 + */
  200 + private List<HashMap<String,String>> parseXml(StringBuffer sb){
  201 + List<HashMap<String,String>> list = new ArrayList<>();
  202 + HashMap<String,String> map;
  203 + try {
  204 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  205 + DocumentBuilder builder = factory.newDocumentBuilder();
  206 + Document doc = builder.parse(new ByteArrayInputStream(sb.toString().getBytes()));
  207 + NodeList nl = doc.getElementsByTagName("BusLine");
  208 + String lineName; //线路名称
  209 + String lineId; // 线路ID
  210 + String abbreviation ; // 公司名称
  211 + String ddfs ; // 调度方式
  212 + String lineStandardCode; // 上海市线路编码
  213 + String parentUnitName ; // 总公司
  214 + for (int i = 0; i < nl.getLength(); i++) {
  215 + parentUnitName = doc.getElementsByTagName("ParentUnitName").item(i).getFirstChild() == null ?"":
  216 + doc.getElementsByTagName("ParentUnitName").item(i).getFirstChild().getNodeValue();
  217 + if(parentUnitName.indexOf("浦东") != -1){// 只查找浦东的数据
  218 + lineName = doc.getElementsByTagName("LineName").item(i).getFirstChild() == null ?"":
  219 + doc.getElementsByTagName("LineName").item(i).getFirstChild().getNodeValue();
  220 + lineId = doc.getElementsByTagName("LineId").item(i).getFirstChild() == null ?"":
  221 + doc.getElementsByTagName("LineId").item(i).getFirstChild().getNodeValue();
  222 + abbreviation = doc.getElementsByTagName("Abbreviation").item(i).getFirstChild() == null ?"":
  223 + doc.getElementsByTagName("Abbreviation").item(i).getFirstChild().getNodeValue();
  224 + ddfs = doc.getElementsByTagName("DDFS").item(i).getFirstChild() == null ?"":
  225 + doc.getElementsByTagName("DDFS").item(i).getFirstChild().getNodeValue();
  226 + lineStandardCode = doc.getElementsByTagName("LineStandardCode").item(i).getFirstChild() == null ?"":
  227 + doc.getElementsByTagName("LineStandardCode").item(i).getFirstChild().getNodeValue();
  228 + map = new HashMap<>();
  229 + map.put("lineName",lineName);
  230 + map.put("lineId",lineId);
  231 + map.put("abbreviation",abbreviation);
  232 + map.put("ddfs",ddfs);
  233 + map.put("lineStandardCode",lineStandardCode);
  234 + list.add(map);
  235 + }
  236 + }
  237 + }catch (Exception e){
  238 + e.printStackTrace();;
  239 + }
  240 + return list;
  241 + }
  242 +
  243 + /**
  244 + * 批量插入用法
  245 + * @param list
  246 + */
  247 + public boolean insertRecord(List<HashMap<String,String>> list)
  248 + {
  249 + boolean flag = false;
  250 + final List<HashMap<String,String>> tempList=list;
  251 + String sql="INSERT INTO JJWGPS_T_SHSXLFILE(XLID,SHSXLBM,XLMC,GSMC,DDFS,CREATETIME,XLMC_ALL) VALUES (?,?,?,?,?,SYSDATE,?)";
  252 + jdbcTemplate = new JdbcTemplate(DBUtils_oldSystem.getDataSource());
  253 + jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter()
  254 + {
  255 + public void setValues(PreparedStatement ps, int i)throws SQLException
  256 + {
  257 + String lineId =tempList.get(i).get("lineId");
  258 + String lineStandardCode =tempList.get(i).get("lineStandardCode");
  259 + String lineName =tempList.get(i).get("lineName");
  260 + String abbreviation =tempList.get(i).get("abbreviation");
  261 + String ddfs =tempList.get(i).get("ddfs");
  262 + String lineNameAll = lineName;
  263 + if(ddfs.equals("2")){
  264 + lineNameAll = lineName+"区间(走向部分在全程线路之外)";
  265 + }else if(ddfs.equals("7")){
  266 + lineNameAll = lineName+"区间(走向在全程线路之内)";
  267 + }
  268 + ps.setString(1, lineId);
  269 + ps.setString(2, lineStandardCode);
  270 + ps.setString(3, lineName);
  271 + ps.setString(4, abbreviation);
  272 + ps.setString(5, ddfs);
  273 + ps.setString(6, lineNameAll);
  274 + }
  275 + public int getBatchSize()
  276 + {
  277 + return tempList.size();
  278 + }
  279 + });
  280 + flag = true;
  281 + return flag;
  282 + }
  283 +}