package product.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Supplier;
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 product.DAO.ProductImpl;
import product.DAO.ProductdirImpl;
import product.DAO.Product.IProduct;
import product.DAO.Product.IProductdir;
import product.domain.Productach;
import product.productquery.productquery;
import product.productquery.page.pageresult;
@WebServlet("/product")
public class servlet extends HttpServlet{
private IProduct dao;
private IProductdir dirdao;
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
dao=new ProductImpl();
dirdao=new ProductdirImpl();
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String cmd=req.getParameter("cmd");
switch (cmd) {
case "edit":
edit(req, resp);
break;
case "delete":
delete(req, resp);
break;
case "saveORupdate":
saveORupdate(req, resp);
break;
case "list":
list(req, resp);
break;
default:
list(req, resp);
break;
}
}
public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
productquery go=new productquery();
this.parainit(req,go);
req.setAttribute("go", go);
//List<Productach> list=dao.search(go);
pageresult pageresult=dao.resudesign(go);
req.setAttribute("pageresult", pageresult);
req.setAttribute("dirdao", dirdao.list());
req.setAttribute("go", go);
req.getRequestDispatcher("/WEB-INF/view/product/product.jsp").forward(req, resp);
}
private void parainit(HttpServletRequest req,productquery go) {
try {
req.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name=req.getParameter("name");
String minsale=req.getParameter("minsale");
String maxsale=req.getParameter("maxsale");
String sele=req.getParameter("yaoshi");
String keyword=req.getParameter("keyword");
String currpage=req.getParameter("currentpage");
String psize=req.getParameter("page");
if(currpage!=null&&!"".equals(currpage)) {
go.setCurrentpage(Integer.valueOf(currpage));
}
if(psize!=null&&!"".equals(psize)) {
go.setPagesize(Integer.valueOf(psize));
}
if(haslength(name))
{
go.setProductName(name);
}
if(minsale!=null&&!"".equals(minsale))
{
go.setMinsale(new BigDecimal(minsale));
}
if(maxsale!=null&&!"".equals(maxsale))
{
go.setMaxsale(new BigDecimal(maxsale));
}
if(sele!=null&&!"".equals(sele))
{
go.setDirId(Long.valueOf(sele));
}
if(keyword!=null&&!"".equals(keyword))
{
go.setKeyword(keyword);
}
}
public Boolean haslength(String value) {
return value!=null&&!"".equals(value);
}
public void saveORupdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// TODO Auto-generated method stub
String id=req.getParameter("num");
String productName=req.getParameter("name");
String brand=req.getParameter("brand");
String kind=req.getParameter("kind");
String supplier=req.getParameter("supply");
String costsale=req.getParameter("costsale");
String sale=req.getParameter("sale");
String cost=req.getParameter("cost");
ProductImpl dao=new ProductImpl();
Productach pro=new Productach(0,productName,Long.valueOf(kind),Double.valueOf(sale),
supplier,brand,Double.valueOf(costsale),
Double.valueOf(cost));
if(id!=null&&!"".equals(id))
{
dao.update(Long.valueOf(id), pro);
}else {
dao.insert(pro);
}
resp.sendRedirect("/product?cmd=list");
}
public void delete(HttpServletRequest req, HttpServletResponse resp) {
// TODO Auto-generated method stub
String id=req.getParameter("id");
dao.delete(Long.valueOf(id));
try {
resp.sendRedirect("/product?cmd=list");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String id=req.getParameter("id");
if(id!=null&&!"".equals(id))
{
Productach pro=dao.getone(Long.valueOf(id));
req.setAttribute("pro",pro);
}
req.getRequestDispatcher("/WEB-INF/view/product/saveORupdate.jsp").forward(req, resp);
}
}