package com.hrs.controller.admin;

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.UserDao;
import com.hrs.dao.impl.UserDaoImpl;
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.io.PrintWriter;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: AdminStatisticsController.java
* @Description: 管理员统计数据AP
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午8:53:55 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/admin/statistics")
public class AdminStatisticsController extends HttpServlet {
    
    private UserDao userDao = new UserDaoImpl();
    private HouseDao houseDao = new HouseDaoImpl();
    private OrderDao orderDao = new OrderDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        HttpSession session = request.getSession(false);
        if (session == null) {
            response.setStatus(401);
            return;
        }
        
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null || !"2".equals(loginUser.getRoleType())) {
            response.setStatus(403);
            return;
        }
        
        // 获取统计数据
        int userCount = userDao.count();
        int houseCount = houseDao.countAll();
        int orderCount = orderDao.countAll();
        int pendingCount = houseDao.findPendingHouses().size();
        
        // 返回JSON
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("{");
        out.print("\"userCount\":" + userCount + ",");
        out.print("\"houseCount\":" + houseCount + ",");
        out.print("\"orderCount\":" + orderCount + ",");
        out.print("\"pendingCount\":" + pendingCount);
        out.print("}");
    }
}
