package com.zzrd.commoditySystem.commodity.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zzrd.commoditySystem.commodity.service.CommodityService;
import com.zzrd.commoditySystem.util.ConstVar;
/**
* Servlet implementation class CommodityCotroller
*/
@WebServlet(name = "Commodity", urlPatterns = { "/Commodity" })
public class CommodityCotroller extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String flag = request.getParameter("type");
if(flag.equals("addpage")) { //添加
addCommodity(request,response);
}else if(flag.equals("save")) { //保存
saveCommodity(request,response);
}else if(flag.equals("findone")) { // 查询修改
findone(request,response);
}else if(flag.equals("update")) { //更新操作
update(request,response);
}else if(flag.equals("delete")) { //删除
delete(request,response);
}else if(flag.equals("selectPage")) { // 分页
selectPage(request,response);
}
}
private void selectPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String pageIndex = request.getParameter("pageIndex") == null ? ConstVar.DEFAULT_PAGEINDEX : request.getParameter("pageIndex");
String pageNo = request.getParameter("pageNo") == null ? ConstVar.DEFAULT_PAGENO : request.getParameter("pageNo");
String search = request.getParameter("searchText");
if(search != null && "get".equals(request.getMethod().toLowerCase())) {
search = new String(search.getBytes("iso-8859-1"),"utf-8");
}
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
CommodityService ss = new CommodityService();
list = ss.showListPage(pageIndex,pageNo,search);
int totalNo = ss.showListPageCount(search);
request.setAttribute("pageIndex", pageIndex);//当前页
request.setAttribute("pageNo", pageNo);//每页几条
request.setAttribute("totalNo", totalNo);//总条数
request.setAttribute("totalPage", Math.ceil(totalNo / Double.valueOf(pageNo)));//总页数
request.setAttribute("commodityinfo", list);
request.setAttribute("search", search);
request.getRequestDispatcher("view/commodity/showList.jsp").forward(request, response);
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
String id =request.getParameter("id");
CommodityService ss = new CommodityService();
ss.delete(id);
response.sendRedirect("Commodity?type=selectPage");
}
private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String id =request.getParameter("id");
request.setCharacterEncoding("utf-8");
String name =request.getParameter("name");
String address =request.getParameter("address");
String fid =request.getParameter("fid");
String unit =request.getParameter("unit");
String price =request.getParameter("price");
String type =request.getParameter("atype");
String scdate =request.getParameter("scdate");
String ccdate =request.getParameter("ccdate");
CommodityService ss = new CommodityService();
int a = ss.updateCommodity(id,name,address,fid,unit,price,type,scdate,ccdate);
if(a==0) {
request.setAttribute("mess", "操作失败");
request.getRequestDispatcher("Commodity?type=findone&id="+id).forward(request, response);
}else {
response.sendRedirect("Commodity?type=selectPage");
}
}
private void findone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id =request.getParameter("id");
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> stulist = new ArrayList<Map<String,Object>>();
CommodityService ss = new CommodityService();
list = ss.showFarmList();
stulist=ss.findOne(id);
request.setAttribute("farminfo", list);
request.setAttribute("stulist", stulist);
request.getRequestDispatcher("view/commodity/findone.jsp").forward(request, response);
}
private void saveCommodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id =request.getParameter("id");
request.setCharacterEncoding("utf-8");
String name =request.getParameter("name");
String address =request.getParameter("address");
String fid =request.getParameter("fid");
String unit =request.getParameter("unit");
String price =request.getParameter("price");
String type =request.getParameter("atype");
String scdate =request.getParameter("scdate");
String ccdate =request.getParameter("ccdate");
CommodityService ss = new CommodityService();
int a = ss.addCommodity(name,address,fid,unit,price,type,scdate,ccdate);
if(a==0) {
request.setAttribute("mess", "操作失败");
request.getRequestDispatcher("Commodity?type=findone&id="+id).forward(request, response);
}else {
response.sendRedirect("Commodity?type=selectPage");
}
}
private void addCommodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
CommodityService ss = new CommodityService();
list = ss.showFarmList();
request.setAttribute("farminfo", list);
request.getRequestDispatcher("view/commodity/add.jsp").forward(request, response);
}
}