package com.home.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.servlet.jsp.PageContext;
import com.home.entity.District;
import com.home.entity.House;
import com.home.entity.Street;
import com.home.entity.Types;
import com.home.entity.Users;
import com.home.impl.HouseImpl;
import com.home.service.DirstrictService;
import com.home.service.HouseService;
import com.home.service.StreetService;
import com.home.service.TypesService;
import com.home.service.UsersService;
@SuppressWarnings("all")
public class HouseServlet extends HttpServlet {
HouseService houseservice=new HouseService();
StreetService streetservice=new StreetService();
DirstrictService dirstrictservice=new DirstrictService();
TypesService typeservice=new TypesService();
UsersService usersservice=new UsersService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String opr=request.getParameter("op");
HttpSession session=request.getSession();
if(opr.equals("fenye")||opr==null){
Map<String, Object> params=new HashMap<String, Object>();
if(request.getParameter("sou")!=null&&request.getParameter("sou")!=""){
if(session.getAttribute("params")!=null&&session.getAttribute("params")!=""){
params=(Map<String, Object>) session.getAttribute("params");
request.setAttribute("title_if", session.getAttribute("h_title"));
request.setAttribute("price_if", session.getAttribute("h_price"));
request.setAttribute("floorage_if", session.getAttribute("h_floorage"));
request.setAttribute("types_if", session.getAttribute("h_types"));
request.setAttribute("district_if", session.getAttribute("h_district"));
request.setAttribute("street_if", session.getAttribute("h_street"));
}
request.setAttribute("sou", "sou");
}else{
session.setAttribute("title_if", "");
session.setAttribute("price_if", "");
session.setAttribute("floorage_if", "");
session.setAttribute("types_if", "");
session.setAttribute("district_if", "");
session.setAttribute("street_if", "");
request.setAttribute("sou", "");
}
int count=houseservice.SelectCount(params);
System.out.println(count);
int pageindex;
int pagesize=3;
int tolpage;
if(count%pagesize==0){
tolpage=count/pagesize;
}else{
tolpage=count/pagesize+1;
}
if(request.getParameter("pageindex")==null){
pageindex=1;
}else{
pageindex=Integer.parseInt(request.getParameter("pageindex"));
if(pageindex<=0){
pageindex=1;
}else if(pageindex>=tolpage){
pageindex=tolpage;
}
}
System.out.println("过来没有?");
params.put("pageindex", pageindex);
params.put("pagesize",pagesize);
List<House> houselist=houseservice.SelectFenye(params);
List<District> districtlist=dirstrictservice.SelectAllDistrice();
//int ss=0;
//List<Street> streetlist=streetservice.SelectDistrictID(districtlist.get(0));
List<Types> typelist=typeservice.SelectType();
request.setAttribute("typelist", typelist);
request.setAttribute("houselist", houselist);
request.setAttribute("districtlist", districtlist);
/*
for(Street ssss:streetlist){
System.out.println(ssss.getSname()+":"+ssss.getSid());
}*/
request.setAttribute("streetlist", districtlist.get(0).getStreet());
request.setAttribute("tolpage", tolpage);
request.setAttribute("pageindex", pageindex);
request.getRequestDispatcher("ShowHome.jsp").forward(request, response);
}else if(opr.equals("update")){
int id=Integer.parseInt(request.getParameter("hid"));
House houselist=houseservice.SelectById(id);
List<Types> utype=typeservice.SelectType();
List<District> districtlist=dirstrictservice.SelectAllDistrice();
List<Street> jiedaos=streetservice.SelectDistrictID(districtlist.get(0).getDid());
request.setAttribute("houselist", houselist);
request.setAttribute("disarray", districtlist);
request.setAttribute("typearray",utype);
request.setAttribute("jiedaos", jiedaos);
request.setAttribute("hid", id);
request.getRequestDispatcher("Add.jsp").forward(request, response);
}else if(opr.equals("delete")){
String name=(String) session.getAttribute("loginname");
int uid=houseservice.SelectUserID(name);
System.out.println(uid+"sssssssssss");
String is=usersservice.IsAdmin(uid);
System.out.println(is+"isisisisisis");
if(is.equals("Y")){
int id=Integer.parseInt(request.getParameter("hid"));
int result=houseservice.DeleteHouse(id);
if(result>0){
out.write("<script>alert('删除成功!');window.location.href='/BirdHomeDemoMyBatis/HouseServlet?op=fenye'</script>");
}else{
out.write("<script>alert('删除失败!')</script>");
}
}else{
out.write("<script>alert('对不起,您不是管理员,没有权限!');window.location.href='/BirdHomeDemoMyBatis/HouseServlet?op=fenye'</script>");
}
}
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
HouseImpl houseimpl=new HouseImpl();
String opr=request.getParameter("op");
if(opr.equals("addorupdate")){
Users users=new Users();
String loginname=(String) session.getAttribute("loginname");
System.out.println(loginname+"用户名");
int user_id=houseimpl.SelectUserID(loginname);//登陆者ID
System.out.println(user_id);
users.setUuid(user_id);
Types types=new Types();
int selecttpye=Integer.parseInt(request.getParameter("selecttpye"));
System.out.println(selecttpye);
types.setTid(selecttpye);
Street street=new Street();
int jiejie=Integer.parseInt(request.getParameter("jiejie"));
System.out.println(jiejie);
street.setSid(jiejie);
String title=request.getParameter("txttitle");//房子标题
int txtfloorage=Integer.parseInt(request.getParameter("txtfloorage"));//房子面积
int txtprice=Integer.parseInt(request.getParameter("txtprice"));//房子价格
String dates=request.getParameter("dates");//房产证日期
String tell=request.getParameter("tell");//联系方式
String descirpt=request.getParameter("descirpt");//描述
int hid=Integer.parseInt(request.getParameter("hid"));
if(hid==0){
House ho=new House(title, descirpt, txtprice, dates, txtfloorage, tell, types, users, street);
int result=houseimpl.InsertHouse(ho);
if(result>0){
out.write("<script>alert('发布成功');window.location.href='/BirdHomeDemoMyBatis/HouseServlet?op=fenye'</script>");
return;
}else{
out.write("<script>alert('发布失败');</script>");
}
}else{
House ho=new House(title, descirpt, txtprice, dates, txtfloorage, tell, types, users, street);
ho.setHid(hid);
int result1=houseimpl.UpdateHouse(ho);
if(result1>0){
out.write("<script>alert('修改成功');window.location.href='/BirdHomeDemoMyBatis/HouseServlet?op=fenye'</script>");
return;
}
else{
out.write("<script>alert('修改失败');</script>");
}
}
}else if(opr.equals("selectlike")){
Map<String, Object> params=new HashMap<String, Object>();
String title=request.getParameter("titlelike");
String price=request.getParameter("jiagelike");
St