package com.szpg.task; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.szpg.db.dao.PgConRequestMsgDao; import com.szpg.db.dao.impl.PgConRequestMsgDaoImp; import com.szpg.db.data.PgConRequestMsg; import com.szpg.db.data.PgConRequestMsgJson; import com.szpg.util.HttpRequest; import org.apache.log4j.Logger; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; public class ReadConRequestTask implements Runnable{ private org.apache.log4j.Logger logger = Logger.getLogger(this.getClass().getName()); final String tableName="ASSET_INSPECTION_PLAN";// final String url="http://10.10.2.31:9056/GLYW/api/Common/LoadIndexData"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override public void run(){ PgConRequestMsgDao pgConRequestMsgDao=new PgConRequestMsgDaoImp(); try { String endTime = dateFormat.format(new Date());//当前时间 String startTime = pgConRequestMsgDao.findLastRecordTime();//上次同步数据最后记录时间 if (startTime == null) { Date dt = new Date();; startTime = getStartDateStr(dt, -1);; } else { Date dtStart = dateFormat.parse(startTime); startTime=getStartDateStr(dtStart,1); } StringBuilder sb = new StringBuilder(); sb.append("tableName=").append(tableName); sb.append("&page=-1"); sb.append("&strWhere= LOG_DATE");; sb.append(" between '").append(startTime); sb.append("' and '").append(endTime); sb.append("'"); logger.info("开始请求施工请点数据"); String jsonStr = HttpRequest.sendPostByHttp(url, sb.toString()); if (jsonStr.equals("")) logger.error("施工请点数据请求失败"); else { insertDB(pgConRequestMsgDao, jsonStr); logger.error("施工请点数据存储成功"); } } catch(Exception ex) { ex.printStackTrace(); } } public void insertDB(PgConRequestMsgDao pgConRequestMsgDao,String jsonStr) { List<PgConRequestMsg> pgConRequestMsgList= getAssetByJsonString(jsonStr); logger.info("同步施工请点数据条数:"+ String.valueOf(pgConRequestMsgList.size())); pgConRequestMsgDao.addConRequestMsg(pgConRequestMsgList); } private List<PgConRequestMsg> getAssetByJsonString(String jsonStr){ try { String jsonString=String.valueOf(JSON.parse(jsonStr)); PgConRequestMsgJson pgConRequestMsgJson = JSONObject.parseObject(jsonString, new TypeReference<PgConRequestMsgJson>() {}); List<PgConRequestMsg> pgConRequestMsgList = pgConRequestMsgJson.getRows(); return pgConRequestMsgList; } catch(Exception ex){ logger.error(ex.getMessage()); return null; } } public String getStartDateStr(Date dt,int d) { String yesToday = ""; Date dtt = (Date) dt.clone(); Calendar calendar = Calendar.getInstance(); calendar.setTime(dtt); calendar.add(Calendar.DAY_OF_MONTH, d); yesToday = dateFormat.format(calendar.getTime());; return yesToday; } }