package com.hrs.controller.user;

import com.hrs.dao.CollectionDao;
import com.hrs.dao.impl.CollectionDaoImpl;
import com.hrs.model.entity.Collection;
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: CollectionListController.java
* @Description: 我的收藏列表控制器
*
* @version: v1.0.0
* @author: 李鑫
* @date: 2026年4月13日 上午9:03:32 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     李鑫           v1.0.0               新建文件
*/
@WebServlet("/user/collection/list")
public class CollectionListController extends HttpServlet {
    
    private CollectionDao collectionDao = new CollectionDaoImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        // 1. 获取登录用户
        HttpSession session = request.getSession(false);
        if (session == null) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null || !"0".equals(loginUser.getRoleType())) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }
        
        // 2. 查询收藏列表
        List<Collection> collectionList = collectionDao.findByTenantId(loginUser.getId());
        
        // 3. 存入request
        request.setAttribute("collectionList", collectionList);
        request.setAttribute("totalCount", collectionList.size());
        request.setAttribute("loginUser", loginUser);
        
        // 4. 转发到JSP
        request.getRequestDispatcher("/jsp/user/collection_list.jsp").forward(request, response);
    }
}