昨天写好业务逻辑,在跟需求方接洽的时候发现还没有展现不同时间粒度下的对应男女的人数的图标数据,因此一直赶着写图标展示的数据。这次主要分析时间粒度下的数据计算的思路。
- 首先时间粒度有两个维度,日和月。从前端表单提交过来的日期格式是yyyy-MM-dd的String类型。从数据库得到的时间类型为Date类型。因此如果是日的话我们只要将该粒度下的所有时间的人数累加。关键在于判断是否为一天那我们得把Date类型通过SimpleDateFormat类来转换格式。
- 最开始前端无法获取到数据,后来才发现生成的url当中的参数它是startDate我的是startTime所以查不到数据。后面按照公司的规范统一成了startDate问题得到解决。
Date date = new Date();
System.out.println(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf.format(new Date()));
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf3 = new SimpleDateFormat("y年第D天,y年M月第d天HH:mm");
System.out.println(sdf3.format(date));
运行结果:
Tue Sep 01 14:25:17 CST 2015
2015/09/01
2015年第244天,2015年9月第1天14:25
SimpleDateTime中的函数:
G 年代标志符
y 年
M 月
d 日
h 时 在上午或下午 (1~12)
H 时 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第几天
F 一月中第几个星期几
w 一年中第几个星期
W 一月中第几个星期
a 上午 / 下午 标记符
k 时 在一天中 (1~24)
K 时 在上午或下午 (0~11)
z 时区
- 格式转换的问题得到了解决,接下来注意在计算百分比的时候注意用浮点型的数据类型,否则得不到数据,还有在总人数为0的时候,就不用计算百分比,直接初始化为0。
- 关于返回的数据结构问题,最开始我使用的是在map里面再在value里放map,这样会造成JSONObject无法解析,后来直接在JSONObject里put描述和对应的值,包括一个kpiCycleGraphMap的map结构值。这样得到正确的解析。
- 关于数据库连接的问题,最开始查到数据没返回值,后来发现在Spring数据源的配置文件里把数据库修改了,该回来就解决问题了。
代码如下:
/**
*
*
* @Project:
* @File: TouristGenderAction.java
* @Date: Mar 2, 2015
* @Author: micro_hz
*/
@SuppressWarnings("serial")
@Results({ @Result(name = WebActionSupport.RELOAD, location = "tourist-gender!display.action", type = "redirect"), @Result(name = WebActionSupport.ERROR, location = "../../common/error.jsp") })
public class TouristGenderAction extends WebActionSupport{
private static final Logger logger = LoggerFactory.getLogger(TouristGenderAction.class);
@Override
public String execute() {
return SUCCESS;
}
public void display()
{
String areaId = ParamUtil.getFilteredParameter(request, "areaId", 0 ,"10000");// 景区id
String type = ParamUtil.getFilteredParameter(request, "kpiCycle", 0 ,"3");
Integer kpiCycle = Integer.parseInt(type);
String startTime = ParamUtil.getFilteredParameter(request, "startTime", 0 ,"2013-02-01");
String endTime = ParamUtil.getFilteredParameter(request, "endTime", 0 ,"2015-05-01");
//定义所有游客列表和男女游客列表
List<TouristGender> allTouristGenderList = null;
// List<TouristGender> femaleList = null;
// List<TouristGender> maleList = null;
float femalePopulation = 0;
float malePopulation = 0;
// float otherPopulation = 0;//性别为其他的时候 保留
float sumPopulation = 0;
Map<String,Object> queryMap = new HashMap<String,Object>();
queryMap.put("areaId", areaId);
//按相应粒度返回的<日期,男女人数的数组>
Map<String,List<Integer>> kpiCycleGraphMap = new HashMap<String,List<Integer>>();
//查询map的时间参数插入
queryMap.put("startTime", startTime);
queryMap.put("endTime", endTime);
//获得所有记录
allTouristGenderList = touristGenderManager.getGenderByDay(queryMap);
//判断操作粒度
if(kpiCycle == Constants.VISITOR_COUNT_UNIT_DAY)
{
kpiCycleGraphMap = getkpiCycleGraphByDays(allTouristGenderList);
}
//按月统计
if(kpiCycle == Constants.VISITOR_COUNT_UNIT_MONTH)
{
kpiCycleGraphMap = getkpiCycleGraphByMonth(allTouristGenderList);
}
//获取所有游客男女和总人数
for(TouristGender touristGender : allTouristGenderList)
{
if(touristGender.getGender().equals("female"))
{
femalePopulation += touristGender.getPopulation();
}
else if(touristGender.getGender().equals("male"))
{
malePopulation += touristGender.getPopulation();
}
// else
// {
// otherPopulation += touristGender.getPopulation();
// }
sumPopulation += touristGender.getPopulation();
}
//男女占比
float femalePercent = 0;
float malePercent = 0;
if(sumPopulation != 0)
{
femalePercent = femalePopulation / sumPopulation;
malePercent = malePopulation / sumPopulation;
}
//结果
//返回JSON
JSONObject resultJson = new JSONObject();
resultJson.put("femalePopulation", femalePopulation);
resultJson.put("malePopulation", malePopulation);
// resultJson.put("otherPopulation", otherPopulation);
resultJson.put("sumPopulation", sumPopulation);
resultJson.put("femalePercent", femalePercent);
resultJson.put("malePercent", malePercent);
// resultJson.put("otherPercent", otherPercnet);
resultJson.put("kpiCycleGraphMap", kpiCycleGraphMap);
// resultJson.put("GenderAnalysis", resultMap);
// System.out.println(resultJson.toString());
returnJSON(resultJson.toString());
}
//按月得到对应每月的男女和其他的人数
private Map<String, List<Integer>> getkpiCycleGraphByMonth(
List<TouristGender> allTouristGenderList) {
// TODO Auto-generated method stub
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");//截取年月
//key为月份,value为该月份的男女与其他的人数列表
Map<String,List<Integer>> monthMap = new HashMap<String,List<Integer>>();
//取出所有月份
List<String> allMonthList = new ArrayList<String>();
for(TouristGender touristGender : allTouristGenderList)
{
if(!allMonthList.contains(sdf2.format(touristGender.getDateTime())))
{
allMonthList.add(sdf2.format(touristGender.getDateTime()));
}
}
//遍历所有月份(不含相同)
for(String str : allMonthList)
{
Integer malePop = 0;
Integer femalePop = 0;
// Integer otherPop = 0;
List<Integer> list = new ArrayList<Integer>();
for(TouristGender touristGender : allTouristGenderList)
{
//判断是否为该月份
if(str.equals(sdf2.format(touristGender.getDateTime())))
{
if(touristGender.getGender().equals("male"))
{
malePop += touristGender.getPopulation();
}
else if(touristGender.getGender().equals("female"))
{
femalePop += touristGender.getPopulation();
}
// else
// {
// otherPop += touristGender.getPopulation();
// }
}
}
list.add(malePop);
list.add(femalePop);
// list.add(otherPop);
monthMap.put(str, list);
}
return monthMap;
}
//按月得到对应每天的男女和其他的人数
private Map<String, List<Integer>> getkpiCycleGraphByDays(
List<TouristGender> allTouristGenderList) {
// TODO Auto-generated method stub
//所有日期提取出来
List<String> allDateList = new ArrayList<String>();
//转换日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//截取年月日
for(TouristGender touristGender : allTouristGenderList)
{
// System.out.println(touristGender.getDateTime());
if(!allDateList.contains(sdf.format(touristGender.getDateTime())))
{
allDateList.add(sdf.format(touristGender.getDateTime()));
}
}
//获取到相同日期的男女人数
Map<String,List<Integer>> dayMap = new HashMap<String,List<Integer>>();
for(String str : allDateList)
{
Integer femalePop = 0;
Integer malePop = 0;
// Integer otherPop = 0;
List<Integer> list = new ArrayList<Integer>();
for(TouristGender touristGender : allTouristGenderList)
{
//如果为该日期的
if(sdf.format(touristGender.getDateTime()).equals(str))
{
if(touristGender.getGender().equals("male"))
{
malePop += touristGender.getPopulation();
}
else if(touristGender.getGender().equals("female"))
{
femalePop += touristGender.getPopulation();
}
// else
// {
// otherPop += touristGender.getPopulation();
// }
}
}
list.add(malePop);
list.add(femalePop);
// list.add(otherPop);
dayMap.put(str, list);
}
return dayMap;
}
}