package com.lgx8.gateway.admin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.lgx8.gateway.entities.Supplier;
import com.lgx8.gateway.service.IGatewayService;
/**
* Servlet implementation class AddSupplierServler
*/
/**
* 供货商管理servlet
*/
public class ManageSupplierServler extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ManageSupplierServler() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//操作类型,1为添加,2为编辑,3为删除
String operation = request.getParameter("operation");
PrintWriter out = response.getWriter();
if("1".equals(operation)) {
String name = request.getParameter("name");
String description = request.getParameter("description");
String businessNum = request.getParameter("businessNum");
String legalPerson = request.getParameter("legalPerson");
String mobile = request.getParameter("mobile");
String email = request.getParameter("email");
String qq = request.getParameter("qq");
String wangwang = request.getParameter("wangwang");
String address = request.getParameter("address");
String isproduct = request.getParameter("isproduct");
int isProduct = 1;
if("0".equals(isproduct)) {
isProduct = 0;
}
if(name==null||"".equals(name)) {
out.print("({'result':'0','msg':'供应商名称不能为空!'})");
out.flush();
return;
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IGatewayService gatewayService = (IGatewayService) ac.getBean("gatewayService");
boolean supplierNameExsits = gatewayService.checkSupplierNameExists(name);
if(supplierNameExsits) {
out.print("({'result':'0','msg':'供应商名称已经存在!'})");
out.flush();
return;
}
Supplier supplier = new Supplier();
supplier.setName(name);
supplier.setDescription(description);
supplier.setBusinessNum(businessNum);
supplier.setLegalPerson(legalPerson);
supplier.setMobile(mobile);
supplier.setEmail(email);
supplier.setQq(qq);
supplier.setWangwang(wangwang);
supplier.setAddress(address);
supplier.setIsproduct(isProduct);
gatewayService.createSupplier(supplier);
out.print("({'result':'1','msg':'成功添加供应商!'})");
out.flush();
return;
} else if("2".equals(operation)) {
String id = request.getParameter("id");
long lid = -1;
try {
lid = Long.parseLong(id);
} catch (Exception e) {
lid = -1;
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IGatewayService gatewayService = (IGatewayService) ac.getBean("gatewayService");
Supplier supplier = gatewayService.findSupplier(lid);
if(supplier==null) {
out.print("({'result':'0','msg':'修改的供应商信息不存在!'})");
out.flush();
return;
}
String name = request.getParameter("name");
/*if(gatewayService.checkSupplierNameExists(name)) {
out.print("({'result':'0','msg':'供应商名称已存在!'})");
out.flush();
return;
}*/
String description = request.getParameter("description");
String businessNum = request.getParameter("businessNum");
String legalPerson = request.getParameter("legalPerson");
String mobile = request.getParameter("mobile");
String email = request.getParameter("email");
String qq = request.getParameter("qq");
String wangwang = request.getParameter("wangwang");
String address = request.getParameter("address");
String isproduct = request.getParameter("isproduct");
int isProduct = 1;
if("0".equals(isproduct)) {
isProduct = 0;
}
if(name==null||"".equals(name)) {
out.print("({'result':'0','msg':'供应商名称不能为空!'})");
out.flush();
return;
}
supplier.setName(name);
supplier.setDescription(description);
supplier.setBusinessNum(businessNum);
supplier.setLegalPerson(legalPerson);
supplier.setMobile(mobile);
supplier.setEmail(email);
supplier.setQq(qq);
supplier.setWangwang(wangwang);
supplier.setAddress(address);
supplier.setIsproduct(isProduct);
gatewayService.updateSupplier(supplier);
out.print("({'result':'1','msg':'修改供应商信息成功!'})");
out.flush();
return;
} else if("3".equals(operation)) {
String id = request.getParameter("id");
long lid = -1;
try {
lid = Long.parseLong(id);
} catch (Exception e) {
lid = -1;
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IGatewayService gatewayService = (IGatewayService) ac.getBean("gatewayService");
gatewayService.deleteSupplier(lid);
request.getRequestDispatcher("page/gateway/admin/supplier/SupplierList.jsp").forward(request, response);
}
}
}