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;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: HouseDetailController.java
* @Description: 房源详情控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午9:04:08 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/user/house/detail")
public class HouseDetailController extends HttpServlet {
    
    private HouseDao houseDao = new HouseDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 1. 获取房源ID
        String idStr = request.getParameter("id");
        if (idStr == null || idStr.trim().isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/user/house/list");
            return;
        }
        
        Long houseId = null;
        try {
            houseId = Long.parseLong(idStr);
        } catch (NumberFormatException e) {
            response.sendRedirect(request.getContextPath() + "/user/house/list");
            return;
        }
        
        // 2. 查询房源详情
        House house = houseDao.findById(houseId);
        if (house == null) {
            response.sendRedirect(request.getContextPath() + "/user/house/list");
            return;
        }
        
        // 3. 获取当前登录用户
        HttpSession session = request.getSession(false);
        User loginUser = (User) session.getAttribute("loginUser");
        
        // 4. 将数据存入request
        request.setAttribute("house", house);
        request.setAttribute("loginUser", loginUser);
        
        // 5. 转发到详情页面
        request.getRequestDispatcher("/jsp/user/house_detail.jsp").forward(request, response);
    }
}