📄 traffichotspotforecastprocess.java
字号:
package com.statistical.process;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.map.LinkedMap;
import com.statistical.StaticInfo;
import com.statistical.tools.DateUtils;
public class TrafficHotspotForecastProcess implements IStatisticalProcessIntf {
public boolean process(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String forecastDate = request.getParameter("forecastDate");
String queryType = request.getParameter("queryType");
if(this.checkForecastDate(forecastDate, queryType)==false){
command.doWithConnection();
request.setAttribute("hintMsg", "此时间内已有数据,无需预测!");
return false;
}
String hotspotArea = request.getParameter("hotspotArea");
String startLimit = null;
String endLimit = null;
List<String> areaList = null;
Map<String,List<String>> resultMap = new LinkedMap();//key = datetime
if("".equals(hotspotArea)){
areaList = StaticInfo.areas;
}else{
areaList = new ArrayList<String>();
areaList.add(hotspotArea);
}
String dateCondition = null;
String datePartStr = null;
if(queryType.equals(StaticInfo.QUERY_TYPE_YEAR)){
startLimit = DateUtils.dateAdd(forecastDate, Calendar.YEAR, -2);
endLimit = DateUtils.dateAdd(forecastDate, Calendar.YEAR, -1);
dateCondition = DateUtils.getYearsString(startLimit, endLimit);
datePartStr = "substr(LOC_TIME,0,4)";
}
if(queryType.equals(StaticInfo.QUERY_TYPE_MONTH)){
startLimit = DateUtils.dateAdd(forecastDate, Calendar.MONTH, -2);
endLimit = DateUtils.dateAdd(forecastDate, Calendar.MONTH, -1);
dateCondition = DateUtils.getMonthsString(startLimit, endLimit);
datePartStr = "substr(LOC_TIME,0,7)";
}
if(queryType.equals(StaticInfo.QUERY_TYPE_DAY)){
startLimit = DateUtils.dateAdd(forecastDate, Calendar.DATE, -2);
endLimit = DateUtils.dateAdd(forecastDate, Calendar.DATE, -1);
dateCondition = DateUtils.getDaysString(startLimit, endLimit);
datePartStr = "LOC_TIME";
}
for(int i=0;i<areaList.size();i++){
//String sql = "select count(distinct TMNL_ID) as count,datepart(year,LOC_TIME) as dateStr from "+StaticInfo.TATISTICAL_TABLE_NAME+" where LOC_DESC like '%"+areaList.get(i)+"%' and datepart(year,LOC_TIME) in "+yearCondition+" group by datepart(year,LOC_TIME)";
String sql ="select count(distinct TMNL_ID) as count,"+datePartStr+" as dateStr from "+StaticInfo.TATISTICAL_TABLE_NAME+" where LOC_DESC like '%"+areaList.get(i)+"%' and "+datePartStr+" in "+dateCondition+" group by "+datePartStr;
List<Map<String, String>> lt = command.executeSql(sql);
for(int resultCount = 0;resultCount<lt.size();resultCount++){
Map<String, String> map = lt.get(resultCount);
String dateTime = map.get("dateStr".toUpperCase());
String count = map.get("count".toUpperCase());
if(resultMap.containsKey(dateTime)){
List<String> tmpLt = resultMap.get(dateTime);
tmpLt.add(count);
}else{
List<String> tmpLt = new ArrayList<String>();
tmpLt.add(count);
resultMap.put(dateTime,tmpLt);
}
}
}
List<String> forecastList = null;
String forecastKey = forecastDate;
if(queryType.equals(StaticInfo.QUERY_TYPE_YEAR)){
forecastList= this.generateForecast(forecastDate, startLimit.substring(0,4), endLimit.substring(0,4), resultMap);
forecastKey= forecastDate.substring(0,4);
}
if(queryType.equals(StaticInfo.QUERY_TYPE_MONTH)){
forecastList= this.generateForecast(forecastDate, startLimit.substring(0,7), endLimit.substring(0,7), resultMap);
forecastKey = forecastDate.substring(0,7);
}
if(queryType.equals(StaticInfo.QUERY_TYPE_DAY)){
forecastList= this.generateForecast(forecastDate, startLimit, endLimit, resultMap);
}
if(forecastList==null){
forecastList = new ArrayList<String>();
for(int i=0;i<areaList.size();i++){
forecastList.add("0");
}
}
resultMap.put(forecastKey,forecastList);
Iterator iter = resultMap.keySet().iterator();
while(iter.hasNext()){
List lt = resultMap.get(iter.next());
if(lt.size()<areaList.size()){
Integer size =areaList.size()-lt.size();
for(int i=0;i<size;i++){
lt.add(" ");
}
}
}
request.setAttribute("titleList", areaList);
request.setAttribute("resultMap", resultMap);
command.doWithConnection();
return false;
}
private List generateForecast(String forecastDate,String startLimit,String endLimit,Map<String,List<String>> resultMap){
if(!resultMap.containsKey(startLimit)&&!resultMap.containsKey(endLimit)) return null;
List<String> startLimitList = resultMap.get(startLimit);
List<String> endLimitList = resultMap.get(endLimit);
List<String> resultList = new ArrayList<String>();
Integer areaCount = startLimitList!=null?startLimitList.size():endLimitList.size();
for(int i=0;i<areaCount;i++){
Integer startCount = Integer.valueOf(startLimitList!=null?startLimitList.get(i):"0");
Integer endCount = Integer.valueOf(endLimitList!=null?endLimitList.get(i):"0");
Integer forecastCount =(startCount==0||endCount==0)?(startCount!=0?startCount:endCount):endCount+(endCount-startCount);
resultList.add(forecastCount.toString());
}
return resultList;
}
private boolean checkForecastDate(String forecastDate,String queryType) throws Exception{
String dateCondition = null;
if(queryType.equals(StaticInfo.QUERY_TYPE_YEAR)){
dateCondition = "substr(LOC_TIME,0,4)= '"+ forecastDate.substring(0,4)+"'";
}
if(queryType.equals(StaticInfo.QUERY_TYPE_MONTH)){
dateCondition = "substr(LOC_TIME,0,7) = '"+forecastDate.substring(0,7)+"'";
}
if(queryType.equals(StaticInfo.QUERY_TYPE_DAY)){
dateCondition = "LOC_TIME ='"+forecastDate+"'";
}
String sql = "select count(distinct TMNL_ID) as recordCount from "+StaticInfo.TATISTICAL_TABLE_NAME+" where "+dateCondition;
List<Map> lt = command.executeSql(sql);
if(lt.size()>0){
Map map = lt.get(0);
String recordCount = (String)map.get("recordCount".toUpperCase());
if(Integer.valueOf(recordCount)>0){
return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -