package com.hrs.controller.landlord;

import com.hrs.dao.HouseDao;
import com.hrs.dao.impl.HouseDaoImpl;
import com.hrs.dao.OrderDao;
import com.hrs.dao.impl.OrderDaoImpl;
import com.hrs.dao.ReservationDao;
import com.hrs.dao.impl.ReservationDaoImpl;
import com.hrs.model.entity.House;
import com.hrs.model.entity.User;
import com.hrs.util.ImageUploadUtil;

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.net.URLEncoder;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: HouseDeleteController.java
* @Description: 删除房源控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午8:56:09 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/landlord/house/delete")
public class HouseDeleteController extends HttpServlet {
    
    private HouseDao houseDao = new HouseDaoImpl();
    private OrderDao orderDao = new OrderDaoImpl();
    private ReservationDao reservationDao = new ReservationDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 获取登录用户
        HttpSession session = request.getSession(false);
        if (session == null) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null || !"1".equals(loginUser.getRoleType())) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        // 获取房源ID
        String idStr = request.getParameter("id");
        if (idStr == null || idStr.trim().isEmpty()) {
            response.sendRedirect(request.getContextPath() + "/landlord/house/list");
            return;
        }
        
        Long houseId = Long.parseLong(idStr);
        
        // 先获取房源信息（为了删除图片）
        House house = houseDao.findById(houseId);
        
        if (house == null) {
            String encodedError = URLEncoder.encode("房源不存在", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?error=" + encodedError);
            return;
        }
        
        // 检查是否有关联的订单
        boolean hasOrders = orderDao.existsByHouseId(houseId);
        if (hasOrders) {
            String encodedError = URLEncoder.encode("该房源存在关联订单，无法删除", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?error=" + encodedError);
            return;
        }
        
        // 检查是否有关联的预约
        boolean hasReservations = reservationDao.existsByHouseId(houseId);
        if (hasReservations) {
            String encodedError = URLEncoder.encode("该房源存在关联预约，无法删除", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?error=" + encodedError);
            return;
        }
        
        // 删除数据库记录
        boolean success = houseDao.delete(houseId);
        
        if (success && house != null && house.getImgUrl() != null) {
            // 删除图片文件
            ImageUploadUtil.deleteImages(house.getImgUrl(), request);
            String encodedMsg = URLEncoder.encode("删除成功", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?msg=" + encodedMsg);
        } else if (success) {
            String encodedMsg = URLEncoder.encode("删除成功", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?msg=" + encodedMsg);
        } else {
            String encodedError = URLEncoder.encode("删除失败", "UTF-8");
            response.sendRedirect(request.getContextPath() + "/landlord/house/list?error=" + encodedError);
        }
    }
}
