package com.sse.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.sse.javabean.Excel;
import com.sse.javabean.PrintExcel;
import com.sse.po.Administer;
import com.sse.po.Products;
import com.sse.po.Traderecord;
import com.sse.service.AdminService;
import com.sse.service.AdminServiceImp;
import com.sse.service.ProductsImpl;
import com.sse.service.ProductsInter;
import com.sse.utils.Page;
import com.sse.utils.PageUtil;
public class AdminServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private AdminService adminService = new AdminServiceImp();
/**
* Constructor of the object.
*/
public AdminServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
switch (method) {
case "login":
login(request, response);
break;
case "checkid":
checkid(request, response);
break;
case "addGoods":
addGoods(request, response);
break;
case "queryAllGoods":
queryAllGoods(request, response);
break;
case "update":
update(request, response);
break;
case "update1":
update1(request, response);
break;
case "delete":
delete1(request, response);
break;
case "totalquery":
totalQuery(request, response);
break;
case "printexcel":
printExcel(request, response);
break;
case "queryGoods":
queryGoods(request, response);
break;
case "purchase":
purchase(request,response);
break;
case "queryadmin":
queryAdmin(request, response);
break;
case "updateadmin":
updateadmin(request, response);
break;
default:
break;
}
}
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
String info = "";
if (adminService.adjust(username)) {
Administer admin = adminService.login(username, passwd);
if (admin != null) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
int type=admin.getType();
if(type==0)
response.sendRedirect("admin/admin.jsp");
else
response.sendRedirect("super_admin/s_admin.jsp");
} else {
info = "密码不正确";
System.out.println("失败");
request.setAttribute("info1", info);
request.getRequestDispatcher("admin/loginAdmin.jsp").forward(
request, response);
}
} else {
System.out.println("失败1");
info = "账号不存在";
request.setAttribute("info1", info);
request.getRequestDispatcher("admin/loginAdmin.jsp").forward(
request, response);
}
}
public void checkid(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// System.out.println(111);
String pid = request.getParameter("fieldValue");
//String id = request.getParameter("fieldId");
Products product = adminService.getProduct(pid);
// JSONObject json = new JSONObject();
System.out.println(pid);
PrintWriter out = response.getWriter();
if (product == null) {
out.print("[\"pid\",true]");
} else {
out.print("[\"pid\",false]");
}
out.flush();
out.close();
}
public void addGoods(HttpServletRequest request,
HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if (username == null) {
PrintWriter out = response.getWriter();
out.print("<html><meta charset='UTF-8'><title></title><script type='text/javascript'>alert('请登录后进行添加商品!');</script></html>");
out.flush();
out.close();
} else {
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
int stock = Integer.parseInt(request.getParameter("stock"));
double price = Double.parseDouble(request.getParameter("price"));
String img = request.getParameter("img");
String introduct = request.getParameter("introduct");
Products product = new Products();
product.setId(pid);
product.setImg(img);
product.setIntroduction(introduct);
product.setPname(pname);
product.setPrice(price);
product.setSellcount(0);
product.setStock(stock);
boolean b = adminService.addGoods(product);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (b) {
out.println("添加商品成功");
} else {
out.println("添加商品失败");
}
out.flush();
out.close();
}
}
public void queryAllGoods(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if (username == null) {
PrintWriter out = response.getWriter();
out.print("<html><meta charset='UTF-8'><title></title><script type='text/javascript'>alert('请登录后进行操作!');</script></html>");
out.flush();
out.close();
} else {
String currentN=request.getParameter("currentPage");
String condition =request.getParameter("condition");
int currentPage=1;
if(currentN!=null){
currentPage=Integer.parseInt(currentN);
}
List<Products> products=new ArrayList<Products>();
ProductsInter productService=new ProductsImpl();
products=productService.queryAll(condition);
Page page=PageUtil.createPage(10, products.size(), currentPage);
products=productService.queryAllBypage(page, condition);
request.setAttribute("page", page);
request.setAttribute("products", products);
request.getRequestDispatcher("admin/queryGoods.jsp").forward(request, response);
}
}
public void queryGoods(HttpServletRequest request,
HttpServletResponse response) throws ServletException,