SSH系列:(32)年度投诉数统计
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH系列:(32)年度投诉数统计相关的知识,希望对你有一定的参考价值。
1、JSP页面
annualStatisticChartUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% Calendar cal = Calendar.getInstance(); int curYear = cal.get(Calendar.YEAR);//当前年份 request.setAttribute("curYear", curYear); List yearList = new ArrayList(); //设置页面上的年份下接框,近5年的年份 for(int i=0;i<5;i++){ yearList.add(i, curYear--); } request.setAttribute("yearList", yearList); %> <!DOCTYPE html> <html> <head> <%@include file="/common/header.jsp"%> <title>年度投诉统计图</title> <script type="text/javascript" src="${basePath}/js/fusioncharts/fusioncharts.js"></script> <script type="text/javascript" src="${basePath}/js/fusioncharts/fusioncharts.charts.js"></script> <script type="text/javascript" src="${basePath}/js/fusioncharts/themes/fusioncharts.theme.fint.js"></script> <script type="text/javascript"> //一进入页面后执行,dom加载完后执行 $(document).ready(doAnnualStatistic()); //根据年份统计投诉数 function doAnnualStatistic(){ //1、获取年份 var year = $(‘#year option:selected‘).val(); if(year == "" || year == undefined){ year = "${curYear}"; } //2、统计年度投诉数据并展示图表 $.ajax({ url:"${basePath}/tax/complain_getAnnualStatisticData.action", type:"post", data:{"year":year}, dataType:"json", success:function(result){ if(result != null && result.status == "ok"){ //根据统计结果展示图表 var fc= new FusionCharts({ "type": "line", "renderAt": "chartContainer", "width": "600", "height": "300", "dataFormat": "json", "dataSource": { "chart": { "caption": year + " 年度投诉数统计图", "subCaption": "", "xAxisName": "月 份", "yAxisName": "投 诉 数", // Enable export "exportEnabled": "1", // Hide export menu item "exportShowMenuItem": "0", "theme": "fint" }, "data": result.data } }); fc.render(); }else { alert("统计投诉数失败!"); } }, error:function(){ alert("统计投诉数失败!"); } }); } </script> </head> <body> <br> <s:select id="year" list="#request.yearList" onchange="doAnnualStatistic()" cssStyle="margin-left:200px;"></s:select> <br> <div id="chartContainer" style="text-align:center;width:100%;"></div> </body> </html>
知识点(1):在JSP页面中取前4年的数据
使用了Calendar,并保存在了request中
Calendar cal = Calendar.getInstance(); int curYear = cal.get(Calendar.YEAR);//当前年份 request.setAttribute("curYear", curYear); List yearList = new ArrayList(); //设置页面上的年份下接框,近5年的年份 for(int i=0;i<5;i++){ yearList.add(i, curYear--); } request.setAttribute("yearList", yearList);
2、后台
2.1、Action层
ComplainAction.java
package com.rk.tax.action; import java.net.URLDecoder; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.rk.core.action.BaseAction; import com.rk.core.entity.JsonResult; import com.rk.core.utils.QueryHelper; import com.rk.tax.entity.Complain; import com.rk.tax.entity.Complainreply; import com.rk.tax.service.ComplainService; @Controller("complainAction") @Scope("prototype") public class ComplainAction extends BaseAction { /*****1、业务数据*****/ private Complain complain; private String startTime; private String endTime; private String state; private Complainreply reply; private JsonResult result; /*****2、业务实现类*****/ @Resource private ComplainService complainService; /*****3、响应页面操作*****/ //列表 public String listUI(){ //加载状态集合 ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP); try { QueryHelper queryHelper = new QueryHelper(Complain.class, "c"); if(StringUtils.isNotBlank(startTime)){//查询开始时间之后的投诉数据 startTime = URLDecoder.decode(startTime, "utf-8"); queryHelper.addCondition("c.compTime >= ?", DateUtils.parseDate(startTime+":00", "yyyy-MM-dd HH:mm:ss")); } if(StringUtils.isNotBlank(endTime)){//查询结束时间之前的投诉数据 endTime = URLDecoder.decode(endTime, "utf-8"); queryHelper.addCondition("c.compTime <= ?", DateUtils.parseDate(endTime+":00", "yyyy-MM-dd HH:mm:ss")); } if(StringUtils.isNotBlank(searchContent)){ searchContent = URLDecoder.decode(searchContent, "utf-8"); queryHelper.addCondition("c.compTitle like ?", "%" + searchContent + "%"); } if(StringUtils.isNotBlank(state)){ queryHelper.addCondition("c.state=?", state); } //按照状态升序排序 queryHelper.addOrderByProperty("c.state", QueryHelper.ORDER_BY_ASC); //按照投诉时间升序排序 queryHelper.addOrderByProperty("c.compTime", QueryHelper.ORDER_BY_ASC); pageResult = complainService.getPageResult(queryHelper, pageNo, pageSize); } catch (Exception e) { e.printStackTrace(); } return "listUI"; } //跳转到受理页面 public String dealUI(){ //加载状态集合 ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP); if(complain != null && StringUtils.isNotBlank(complain.getCompId())){ complain = complainService.findById(complain.getCompId()); } return "dealUI"; } public String deal(){ if(complain != null && StringUtils.isNotBlank(complain.getCompId())){ Complain temp = complainService.findById(complain.getCompId()); //1、更新投诉的状态为 已受理 if(!Complain.COMPLAIN_STATE_DONE.equals(temp.getState())){ temp.setState(Complain.COMPLAIN_STATE_DONE); } //2、保存回复信息 if(reply != null){ reply.setComplain(temp); /** * 在后台设置时间,有两点好处: * (1)在前台JSP页面,不需要再提供相应的表单域 * (2)有两个时间:JSP前台页面生成的时间、服务器保存数据的时间,后者更准确一些 */ reply.setReplyTime(new Timestamp(new Date().getTime())); temp.getComplainreplies().add(reply); } complainService.update(temp); } return "list"; } //跳转到统计页面 public String annualStatisticChartUI(){ return "annualStatisticChartUI"; } //根据年度获取该年度下的统计数 public String getAnnualStatisticData(){ //1、获取年份 HttpServletRequest request = ServletActionContext.getRequest(); int year = 0; if(request.getParameter("year") != null){ year = Integer.valueOf(request.getParameter("year")); } else{ //默认 当前年份 year = Calendar.getInstance().get(Calendar.YEAR); } //2、获取统计年度的每个月的投诉数 result = new JsonResult(); result.setStatus(JsonResult.OK); @SuppressWarnings("rawtypes") List<Map> list = complainService.getAnnualStatisticDataByYear(year); result.setData(list); return "annualStatisticData"; } // Properties public Complain getComplain() { return complain; } public void setComplain(Complain complain) { this.complain = complain; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Complainreply getReply() { return reply; } public void setReply(Complainreply reply) { this.reply = reply; } public JsonResult getResult() { return result; } public void setResult(JsonResult result) { this.result = result; } }
其中关注的重点是getAnnualStatisticData方法,它返回需要的JSON数据。
在struts.xml中映射如下:
<!-- ComplainAction --> <action name="complain_*" class="complainAction" method="{1}"> <result name="{1}">/WEB-INF/jsp/tax/complain/{1}.jsp</result> <result name="list" type="redirectAction"> <param name="actionName">complain_listUI</param> <param name="searchContent">${searchContent}</param> <param name="pageNo">${pageNo}</param> <param name="state">${state}</param> <param name="encode">true</param> </result> <result name="annualStatisticData" type="json"> <param name="root">result</param> </result> </action>
2.2、Service层
ComplainService.java
package com.rk.tax.service; import java.util.List; import java.util.Map; import com.rk.core.service.BaseService; import com.rk.tax.entity.Complain; public interface ComplainService extends BaseService<Complain> { //自动受理投诉 void autoDeal(); /** * 根据年份获取统计年度的每个月的投诉数 * @param year 要统计的年份 * @return 统计数据 */ List<Map> getAnnualStatisticDataByYear(int year); }
ComplainServiceImpl.java
package com.rk.tax.service.impl; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.rk.core.service.Impl.BaseServiceImpl; import com.rk.core.utils.QueryHelper; import com.rk.tax.dao.ComplainDao; import com.rk.tax.entity.Complain; import com.rk.tax.service.ComplainService; @Service("complainService") public class ComplainServiceImpl extends BaseServiceImpl<Complain> implements ComplainService { private ComplainDao complainDao; @Resource public void setComplainDao(ComplainDao complainDao) { setBaseDao(complainDao); this.complainDao = complainDao; } @Override public void autoDeal() { //查找本月开始之前未受理的投诉信息 Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1);//本月1号 cal.set(Calendar.HOUR_OF_DAY, 0);//24小时制0时 cal.set(Calendar.MINUTE, 0);//0分 cal.set(Calendar.SECOND, 0);//0秒 //1、查询上个月未受理投诉 QueryHelper queryHelper = new QueryHelper(Complain.class, "c"); queryHelper.addCondition("c.state=?", Complain.COMPLAIN_STATE_UNDONE); queryHelper.addCondition("c.compTime < ?", cal.getTime()); List<Complain> list = findList(queryHelper); //2、更改投诉的状态为已失效 if(list != null &&list.size()>0){ for(Complain comp : list){ //设置投诉状态为无效 comp.setState(Complain.COMPLAIN_STATE_INVALID); update(comp); } } } @Override public List<Map> getAnnualStatisticDataByYear(int year) { List<Map> resList = new ArrayList<Map>(); //1、获取统计数据 List<Object[]> list = complainDao.getAnnualStatisticDataByYear(year); if(list != null && list.size()>0){ Calendar cal = Calendar.getInstance(); //是否当前年份 boolean isCurrentYear = (cal.get(Calendar.YEAR) == year); int curMonth = cal.get(Calendar.MONTH)+1;//当前月份 //2、格式化统计结果 int tempMonth = 0; Map<String,Object> map = null; for(Object[] obj : list){ tempMonth = Integer.valueOf(obj[0]+""); map = new HashMap<String, Object>(); map.put("label", tempMonth + "月"); if(isCurrentYear){//当前年 if(tempMonth > curMonth){ map.put("value", ""); } else{ map.put("value", obj[1]==null?"0":obj[1].toString()); } } else{ map.put("value", obj[1]==null?"0":obj[1].toString()); } resList.add(map); } } return resList; } }
2.3、DAO层
ComplainDao.java
package com.rk.tax.dao; import java.util.List; import com.rk.core.dao.BaseDao; import com.rk.tax.entity.Complain; public interface ComplainDao extends BaseDao<Complain> { /** * 根据年份获取统计年度的每个月的投诉数 * @param year 要统计的年份 * @return 统计数据 */ List<Object[]> getAnnualStatisticDataByYear(int year); }
ComplainDaoImpl.java
package com.rk.tax.dao.impl; import java.util.List; import org.hibernate.SQLQuery; import com.rk.core.dao.impl.BaseDaoImpl; import com.rk.tax.dao.ComplainDao; import com.rk.tax.entity.Complain; public class ComplainDaoImpl extends BaseDaoImpl<Complain> implements ComplainDao { @Override public List<Object[]> getAnnualStatisticDataByYear(int year) { StringBuilder sb = new StringBuilder(); sb.append("SELECT imonth, c2"); sb.append(" FROM T_Month LEFT JOIN (SELECT MONTH(comp_time) as c1,count(comp_id) as c2 FROM T_Complain WHERE YEAR(comp_time)=? GROUP BY MONTH(comp_time)) as t"); sb.append(" ON imonth = c1"); sb.append(" ORDER BY imonth"); SQLQuery sqlQuery = getSession().createSQLQuery(sb.toString()); sqlQuery.setParameter(0, year); return sqlQuery.list(); } }
以上是关于SSH系列:(32)年度投诉数统计的主要内容,如果未能解决你的问题,请参考以下文章