package com.hrs.controller.user;

import com.hrs.dao.HouseDao;
import com.hrs.dao.impl.HouseDaoImpl;
import com.hrs.model.entity.House;
import com.hrs.model.entity.User;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: HouseListController.java
* @Description: 房源列表控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午9:04:41 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/user/house/list")
public class HouseListController extends HttpServlet {
    
    private HouseDao houseDao = new HouseDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 获取请求参数
        String area = request.getParameter("area");
        String minPriceStr = request.getParameter("minPrice");
        String maxPriceStr = request.getParameter("maxPrice");
        
        List<House> houseList;
        
        // 根据条件查询
        if (area != null && !area.trim().isEmpty()) {
            houseList = houseDao.findHousesByArea(area);
            request.setAttribute("searchArea", area);
        } else if (minPriceStr != null && maxPriceStr != null && !minPriceStr.isEmpty() && !maxPriceStr.isEmpty()) {
            int minPrice = Integer.parseInt(minPriceStr);
            int maxPrice = Integer.parseInt(maxPriceStr);
            houseList = houseDao.findHousesByPrice(minPrice, maxPrice);
            request.setAttribute("minPrice", minPrice);
            request.setAttribute("maxPrice", maxPrice);
        } else {
            houseList = houseDao.findPublishedHouses();
        }
        
        // 获取当前登录用户（用于判断是否已收藏）
        HttpSession session = request.getSession(false);
        User loginUser = (User) session.getAttribute("loginUser");
        
        // 将数据存入request
        request.setAttribute("houseList", houseList);
        request.setAttribute("totalCount", houseList.size());
        request.setAttribute("loginUser", loginUser);
        
        // 转发到JSP页面
        request.getRequestDispatcher("/jsp/user/house_list.jsp").forward(request, response);
    }
}