package com.chengxusheji.action;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.chengxusheji.dao.AreaInfoDAO;
import com.chengxusheji.dao.CarPositionDAO;
import com.chengxusheji.dao.CarStopDAO;
import com.chengxusheji.dao.CarTypeDAO;
import com.chengxusheji.domain.AreaInfo;
import com.chengxusheji.domain.CarPosition;
import com.chengxusheji.domain.CarStop;
import com.chengxusheji.domain.CarType;
import com.chengxusheji.utils.DateUtils;
import com.chengxusheji.utils.ExportExcelUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller @Scope("prototype")
public class CarStopAction extends ActionSupport {
/*界面层需要查询的属性: 车牌号*/
private String carNumber;
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
public String getCarNumber() {
return this.carNumber;
}
/*界面层需要查询的属性: 车辆类型*/
private CarType carType;
public void setCarType(CarType carType) {
this.carType = carType;
}
public CarType getCarType() {
return this.carType;
}
/*界面层需要查询的属性: 车主名称*/
private String owner;
public void setOwner(String owner) {
this.owner = owner;
}
public String getOwner() {
return this.owner;
}
/*界面层需要查询的属性: 车主身份证号*/
private String cardNumber;
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
public String getCardNumber() {
return this.cardNumber;
}
/*界面层需要查询的属性: 开始时间*/
private String startTime;
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getStartTime() {
return this.startTime;
}
/*界面层需要查询的属性: 离开时间*/
private String endTime;
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getEndTime() {
return this.endTime;
}
/*界面层需要查询的属性: 停车车位*/
private CarPosition carPositionObj;
public void setCarPositionObj(CarPosition carPositionObj) {
this.carPositionObj = carPositionObj;
}
public CarPosition getCarPositionObj() {
return this.carPositionObj;
}
/*当前第几页*/
private int page;
public void setPage(int page) {
this.page = page;
}
public int getPage() {
return page;
}
/*每页显示多少条数据*/
private int rows;
public void setRows(int rows) {
this.rows = rows;
}
public int getRows() {
return this.rows;
}
/*一共多少页*/
private int totalPage;
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalPage() {
return totalPage;
}
private int stopId;
public void setStopId(int stopId) {
this.stopId = stopId;
}
public int getStopId() {
return stopId;
}
/*要删除的记录主键集合*/
private String stopIds;
public String getStopIds() {
return stopIds;
}
public void setStopIds(String stopIds) {
this.stopIds = stopIds;
}
/*当前查询的总记录数目*/
private int recordNumber;
public void setRecordNumber(int recordNumber) {
this.recordNumber = recordNumber;
}
public int getRecordNumber() {
return recordNumber;
}
/*业务层对象*/
@Resource CarStopDAO carStopDAO;
@Resource CarTypeDAO carTypeDAO;
@Resource CarPositionDAO carPositionDAO;
@Resource AreaInfoDAO areaInfoDAO;
/*待操作的CarStop对象*/
private CarStop carStop;
public void setCarStop(CarStop carStop) {
this.carStop = carStop;
}
public CarStop getCarStop() {
return this.carStop;
}
//泊位数量统计
public void ajaxGetCarStopStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
ArrayList<AreaInfo> allAreaList = areaInfoDAO.QueryAllAreaInfoInfo();
for(AreaInfo areaInfo: allAreaList) {
xData.put(areaInfo.getAreaName());
ArrayList<CarPosition> carPositionList = carPositionDAO.QueryCarPositionInfo(areaInfo, "");
yData.put(carPositionList.size());
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//分区域停车费统计
public void ajaxGetCarStopAreaCostStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
ArrayList<AreaInfo> allAreaList = areaInfoDAO.QueryAllAreaInfoInfo();
for(AreaInfo areaInfo: allAreaList) {
xData.put(areaInfo.getAreaName());
ArrayList<CarStop> carStopList = carStopDAO.QueryCarStopInfo(areaInfo.getAreaId());
float cost = 0.0f;
for(CarStop carStop: carStopList) {
cost += carStop.getStopCost();
}
yData.put(cost);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//泊位使用率统计
public void ajaxGetCarStopUsePercent() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
HttpServletRequest request = ServletActionContext.getRequest();
String queryDate = request.getParameter("queryDate");
int totalCount = carStopDAO.QueryAllCarStopInfo().size(); //总的车位数
//统计十二个时段
for(int i=0;i<12;i++) {
xData.put("时段" + (i+1));
int useCount = carStopDAO.QueryCarStopInfo(queryDate,i).size(); //该时段占用的车位
double userPercent = Math.floor(useCount * 100.0f / totalCount);
yData.put(userPercent);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//每日停车数统计图
public void ajaxGetDayCountStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
DateUtils du = new DateUtils();
int days = du.getDays(du.getYear(), du.getMonth()); //获取本月多少天
for(int i=1;i<=days;i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String queryDate = sdf.format(new java.util.Date());
if(i<10)
queryDate = queryDate + "-0" + i;
else
queryDate += i;
int stopCount = carStopDAO.QueryCarStopInfo(queryDate).size();
xData.put(i);
yData.put(stopCount);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//每日停时长统计图
public void ajaxGetDayDurationCountStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
DateUtils du = new DateUtils();
int days = du.getDays(du.getYear(), du.getMonth()); //获取本月多少天
for(int i=1;i<=days;i++) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String queryDate = sdf.format(new java.util.Date());
if(i<10)
queryDate = queryDate + "-0" + i;
else
queryDate += i;
ArrayList<CarStop> carStopList = carStopDAO.QueryCarStopInfo(queryDate);
float duration = 0.0f;
for(CarStop carStop: carStopList) {
duration += carStop.getStopDuration();
}
xData.put(i);
yData.put(duration);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//每月停车费统计图 数据
public void ajaxGetMonthCostStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
DateUtils du = new DateUtils();
int year = du.getYear();
for(int i=1;i<=12;i++) {
String monthString = "" + i;
if(i<10)monthString = "0" + monthString;
String queryDate = year + "-" + monthString;
ArrayList<CarStop> stopList = carStopDAO.QueryCarStopInfo(queryDate);
float cost = 0.0f;
for(CarStop carStop: stopList) {
cost += carStop.getStopCost();
}
xData.put(i);
yData.put(cost);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//每月停车数统计图
public void ajaxGetMonthCountStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
DateUtils du = new DateUtils();
int year = du.getYear();
for(int i=1;i<=12;i++) {
String monthString = "" + i;
if(i<10)monthString = "0" + monthString;
String queryDate = year + "-" + monthString;
int stopCount = carStopDAO.QueryCarStopInfo(queryDate).size();
xData.put(i);
yData.put(stopCount);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
//每月停车时长统计图
public void ajaxGetMonthDurationStatistic() throws IOException, JSONException {
JSONObject jsonObj = new JSONObject();
JSONArray xData = new JSONArray();
JSONArray yData = new JSONArray();
DateUtils du = new DateUtils();
int year = du.getYear();
for(int i=1;i<=12;i++) {
String monthString = "" + i;
if(i<10)monthString = "0" + monthString;
String queryDate = year + "-" + monthString;
ArrayList<CarStop> carStopList = carStopDAO.QueryCarStopInfo(queryDate);
float duration = 0.0f;
for(CarStop carStop:carStopList) {
duration += carStop.getStopDuration();
}
xData.put(i);
yData.put(duration);
}
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("xData", xData);
json.accumulate("yData", yData);
out.println(json.toString());
out.flush();
out.close();
}
/*ajax添加CarStop信息*/
@SuppressWarnings("deprecation")
public void ajaxAddCarStop() throws IOException, JSONException {
String message = "";
boolean success = false;
try {
CarType carType = carTypeDAO.GetCarTypeByCarTypeId(carStop.getCarType().getCarTypeId());
carStop.setCarType(carType);
CarPosition carPositionObj = carPositionDAO.GetCarPositionByPositionId(carStop.getCarPositionObj().getPositionId());
carStop.setCarPositionObj(carPositionObj);
carStopDAO.AddCarStop(carStop);
success = true;
writeJsonResponse(success, message);
} catch (Exception e) {
e.printStackTrace();
message = "CarStop添加失败!";
writeJsonResponse(success, message);
}
}
/*向客户端输出操作成功或失败信息*/
private void writeJsonResponse(boolean success,String message)
throws IOException, JSONException {
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject json=new JSONObject();
json.accumulate("success", success);
json.accumulate("message", message);
out.println(json.toString());
out.flush();
out.close();
}
/*查询CarStop信息*/
public void ajaxQueryCarStop() throws IOException, JSONException {
if(page == 0) page = 1;
if(carNumber == null) carNumber = "";
if(owner == null) owner = "";
if(cardNumber == null) cardNumber = "";
if(startTime == null) startTime = "";
if(endTime == null) endTime = "";
if(rows != 0) carStopDAO.setRows(rows);
List<CarStop> carStopList = carStopDAO.QueryCarStopInfo(carNumber, carType, owner, cardNumber, startTime, endTime, carPositionObj, page);
/*计算总的页数和总的记录数*/
carStopDAO.CalculateTotalPageAndRecordNumber(carNumber, carType, owner, cardNumber, startTime, endTime, carPositionObj);
/*获取到总的页码数目*/
totalPage = carStopDAO.getTotalPage();
/*当前查询条件下总记录数*/
recordNumber = carStopDAO.getRecordNumber();
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject jsonObj=new JSONObject();
jsonObj.accumulate("total", recordNumber);
JSONArray jsonArray = new JSONArray();
for(CarStop carStop:carStopList) {
JSONObject jsonCarStop = carStop.getJsonObject();
jsonArray.put(jsonCarStop);
}
jsonObj.accumulate("rows", jsonArray);
out.println(jsonObj.toString());
out.flush();
out.close();
}
/*查询CarStop信息*/
public void ajaxQueryAllCarStop() throws IOException, JSONException {
List<CarStop> carStopList = carStopDAO.QueryAllCarStopInfo(); HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONArray jsonArray = new JSONArray();
for(CarStop carStop:carStopList) {
JSONObject jsonCarStop = new JSONObject();
jsonCarStop.accumulate("stopId", carStop.getStopId());
jsonCarStop.accumulate("stopId", carStop.getStopId());
jsonArray.put(jsonCarStop);
}
out.println(jsonArray.toString());
out.flush();
out.close();
}
/*查询要修改的CarStop信息*/
public void ajaxModifyCarStopQuery() throws IOException, JSONException {
/*根据主键stopId获取CarStop对象*/
CarStop carStop = carStopDAO.GetCarStopByStopId(stopId);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
PrintWriter out = response.getWriter();
//将要被返回到客户端的对象
JSONObject jsonCarStop = carStop.getJsonObject();
out.println(jsonCarStop.toString());
out.flush();
out.close();
};
/*更新修改CarStop信息*/
public void ajaxModifyCarStop() throws IOException, JSONException{
String message = "";
boolean success = false;
try {
CarType carType = carTypeDAO.GetCarTypeByCarTypeId(carStop.getCarType().getCarTypeId());
carStop.setCarType(carType);
CarPosition carPositionObj = carPositionDAO.GetCarPositionByPositionId(carStop.getCarPositionObj().getPositionId());
carStop.setCarPositionObj(carPositionObj);
carStopDAO.UpdateCarStop(carStop);
success = true;
writeJsonResponse(success, message);
} catch (Exception e) {
message = "CarStop修改失败!";
writeJsonResponse(success, message);
}
}
/*删除CarStop信息*/
public void ajaxDeleteCarStop() throws IOException, JSONException {
String message = "";
boolean success = false;
try {
String _stopIds[] = stopIds.split(",");
for(String _stopId: _stopIds) {
carStopDAO.DeleteCarStop(Integer.parseInt(_stopId));
}
success = true;
message = _stopIds.length + "条记录删除成功";
writeJsonResponse(success, message);
} catch (Exception e) {
//e.printStackTrace();
message = "有记录存在外键约束,删除失败";
writeJsonResponse(success, message);
}
}
/*前台查询CarStop信息*/
public String FrontQueryCarStop() {
if(page == 0) page = 1;
if(carNumber == null) carNumber = "";
if(owner == null) owner = "";
if(cardNumber == null) cardNumber = "";
if(startTime == null) startTime = "";
if(endTime == null) endTime = "";
List<CarStop> carStopList = carStopDAO.QueryCarStopInfo(carNumber, carType, owner, cardNumber, startTime, endTime, carPositionObj, page);
/*计算总的页数和总的记录数*/
carStopDAO.CalculateTotalPageAndRecordNumber(carNumber, carType, owner, cardNumber, startTime, endTime, carPositionObj);
/*获取到总的页码数目*/
totalPage = carStopDAO.getTotalPage();
/*当前查询条件下总记录数*/
recordNumber = carStopDAO.getRecordNumber();
ActionContext ctx = ActionContext.getContext();
ctx.put("carStopList", carStopList);
ctx.put("totalPage", totalPage);
ctx.put("recordNumber", recordNumber);
ctx.put("page", page);
ctx.put("carNumber", carNumber);
ctx.put("carType", carType);
List<CarType> carTypeList = carTypeDAO.QueryAllCarTypeInfo();
ctx.put("carTypeList", carTypeList);
ctx.put("owner", owner);
ctx.put("cardNumber", cardNumber);
ctx.put("startTime", startTime);
ctx.put("endTime", endTime);
ctx.put("carPositionObj", carPositionObj);
List<CarPosition> carPositionList = carPositionDAO.QueryAllCarPositionInfo();
ctx.put("carPositionList", carPositionList);
return "front_query_view";
}
/*查询要修改的CarStop信息*/
public String FrontShowCarStopQuery() {
ActionContext ctx = ActionContext.getContext();
/*根据主键stopId获取CarStop对象*/
CarStop carStop = carStopDAO.GetCarStopByStopId(stopId);
List<CarType> carTypeList = carTypeDAO.QueryAllCarTypeInfo();
ctx.put("carTypeList", carTypeList);
List<CarPosition> carPositionList = carPositionDAO.QueryAllCarPositionInfo();
ctx.put("carPositionList", carPositionList);
ctx.put("carStop", carStop);
return "front_show_view";
}
/*删除CarStop信息*/
public String DeleteCarStop() {
ActionContext ctx = ActionContext.getContext();
try {
carStopDAO.DeleteCarStop(stopId);
ctx.put("message", "CarStop删除成功!");
return "delete_success";
} catch (Exception e) {
e.printStackTrace();
ctx.put("error", "CarStop删除失败!");
return "error";
}
}
/*后台导出到excel*/
public String queryCarStopOutputToExcel() {
if(carNumber == null) carNumber = "";
if(owner == null) owner = "";
if(cardNumber == null) cardNumber = "";
if(startTime == null) startTime = "";
if(endTime == null) endTime = "";
List<CarStop> carStopList = carStopDAO.QueryCarStopInfo(carNumber,carType,owner,cardNumber,startTime,endTime,carPositionObj);
ExportExcelUtil ex = new ExportExcelUtil();
String title = "CarStop信息记录";
String[] headers = { "标识ID","车牌号","车辆类型","车主名称","车主身份证号","开始时间","离开时间","停车时长","停车车位","停车费用"};
List<String[]> dataset = new ArrayList<String[]>();
for(int i=0;i<carStopList.size();i++) {
CarStop carStop = carStopList.get(i);
dataset.add(new String[]{carStop.getStopId() + "",carStop.getCarNumber(),carStop.getCarType().getCarTypeName(),
carStop.getOwner(),carStop.getCardNumber(),carStop.getStartTime(),carStop.getEndTime(),carStop.getStopDuration() + "",carStop.getCarPositionObj().getPositionNo(),
carStop.getStopCost() + ""});
}
/*
OutputStream out = null;
try {
out = new FileOutputStream("C://output.xls");
ex.exportExcel(title,headers, dataset, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
*/
HttpServletResponse response = null;//创建一个HttpServletResponse对象
OutputStream out = null;//创建一个输出流对象
try {
response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
out = response.getOutputStream();//
response.setHeader("Content-disposition","attachment; filename="+"CarStop.xls");//filename是下载的xls的名,建议最好用英文
response.setContentType("application/msexcel;charset=UTF-8");//设置类型
response.setHeader("Pragma","No-cache");//设置头
response.setHeader("Cache-Control","no-cache");//设置头
response.setDateHeader("Expires", 0);//设置日期头
String rootPath = ServletActionContext.getServletContext().getRealPath("/");
ex.exportExcel(rootPath,title,headers, dataset, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return null;
}
}
最近下载更多
wuying8208 LV15
2024年10月23日
朱朱啊哈 LV16
2023年2月1日
2017143155 LV12
2022年12月2日
bluesky2016 LV15
2022年7月4日
随便取个名字_哈哈 LV27
2022年6月10日
testuser1234567 LV24
2022年5月23日
and123456 LV11
2022年4月28日
ksksksks LV11
2022年2月27日
mafangnu LV8
2021年10月7日
雷阳雷 LV9
2021年6月26日

最近浏览