package com.hrs.dao.impl;

import com.hrs.dao.CollectionDao;
import com.hrs.dao.HouseDao;
import com.hrs.model.entity.Collection;
import com.hrs.model.entity.House;
import com.hrs.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: CollectionDaoImpl.java
* @Description: 收藏数据访问实现类
*
* @version: v1.0.0
* @author: 赵恒
* @date: 2026年4月13日 上午9:33:33 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     赵恒           v1.0.0               新建文件
*/
public class CollectionDaoImpl implements CollectionDao {

    private HouseDao houseDao = new HouseDaoImpl();

    /**
     * 添加收藏
     */
    @Override
    public boolean add(Collection collection) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO hrs_collection (tenant_id, house_id, collect_time, create_time) " +
                         "VALUES (?, ?, NOW(), NOW())";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, collection.getTenantId());
            pstmt.setLong(2, collection.getHouseId());

            int rows = pstmt.executeUpdate();
            success = rows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(pstmt, conn);
        }

        return success;
    }

    /**
     * 检查是否已收藏
     */
    @Override
    public boolean isCollected(Long tenantId, Long houseId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean exists = false;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM hrs_collection WHERE tenant_id = ? AND house_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, tenantId);
            pstmt.setLong(2, houseId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                exists = rs.getInt(1) > 0;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }

        return exists;
    }

    /**
     * 取消收藏
     */
    @Override
    public boolean delete(Long tenantId, Long houseId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM hrs_collection WHERE tenant_id = ? AND house_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, tenantId);
            pstmt.setLong(2, houseId);

            int rows = pstmt.executeUpdate();
            success = rows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(pstmt, conn);
        }

        return success;
    }

    /**
     * 根据收藏ID删除
     */
    @Override
    public boolean deleteById(Long id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM hrs_collection WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, id);

            int rows = pstmt.executeUpdate();
            success = rows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(pstmt, conn);
        }

        return success;
    }

    /**
     * 查询租客的所有收藏
     */
    @Override
    public List<Collection> findByTenantId(Long tenantId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Collection> collectionList = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM hrs_collection WHERE tenant_id = ? ORDER BY collect_time DESC";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, tenantId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Collection collection = resultSetToCollection(rs);
                // 查询关联的房源详情
                House house = houseDao.findById(collection.getHouseId());
                collection.setHouse(house);
                collectionList.add(collection);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }

        return collectionList;
    }

    /**
     * 查询租客的收藏数量
     */
    @Override
    public int countByTenantId(Long tenantId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int count = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM hrs_collection WHERE tenant_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, tenantId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }

        return count;
    }

    /**
     * 将ResultSet转换为Collection对象
     */
    private Collection resultSetToCollection(ResultSet rs) throws SQLException {
        Collection collection = new Collection();
        collection.setId(rs.getLong("id"));
        collection.setTenantId(rs.getLong("tenant_id"));
        collection.setHouseId(rs.getLong("house_id"));
        collection.setCollectTime(rs.getTimestamp("collect_time"));
        collection.setCreateBy(rs.getString("create_by"));
        collection.setCreateTime(rs.getTimestamp("create_time"));
        return collection;
    }
}
