package com.hrs.service.impl;

import com.hrs.dao.HouseDao;
import com.hrs.dao.impl.HouseDaoImpl;
import com.hrs.model.entity.House;
import com.hrs.service.HouseService;

import java.math.BigDecimal;
import java.util.List;

/**   
* Copyright: Copyright (c) 2026 chinasofti
* 
* @ClassName: HouseServiceImpl.java
* @Description: 房源业务逻辑实现类
*
* @version: v1.0.0
* @author: 黄红
* @date: 2026年4月13日 上午10:04:33 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2026年4月13日     黄红           v1.0.0               新建文件
*/
public class HouseServiceImpl implements HouseService {

    private HouseDao houseDao = new HouseDaoImpl();

    /**
     * 获取已上架房源（租客端）
     */
    @Override
    public List<House> getPublishedHouses() {
        return houseDao.findPublishedHouses();
    }

    /**
     * 按区域筛选房源
     */
    @Override
    public List<House> searchByArea(String area) {
        if (area == null || area.trim().isEmpty()) {
            return getPublishedHouses();
        }
        return houseDao.findHousesByArea(area);
    }

    /**
     * 按价格区间筛选房源
     */
    @Override
    public List<House> searchByPrice(int minPrice, int maxPrice) {
        if (minPrice <= 0 && maxPrice <= 0) {
            return getPublishedHouses();
        }
        if (minPrice <= 0) {
            minPrice = 0;
        }
        if (maxPrice <= 0) {
            maxPrice = Integer.MAX_VALUE;
        }
        return houseDao.findHousesByPrice(minPrice, maxPrice);
    }

    /**
     * 获取房源详情
     */
    @Override
    public House getHouseDetail(Long houseId) {
        if (houseId == null || houseId <= 0) {
            return null;
        }
        return houseDao.findById(houseId);
    }

    /**
     * 获取房东的房源列表
     */
    @Override
    public List<House> getLandlordHouses(Long landlordId) {
        if (landlordId == null || landlordId <= 0) {
            return null;
        }
        return houseDao.findByLandlordId(landlordId);
    }

    /**
     * 添加房源
     * @return 添加结果
     */
    @Override
    public AddHouseResult addHouse(House house) {
        AddHouseResult result = new AddHouseResult();

        // 1. 参数校验
        if (house.getTitle() == null || house.getTitle().trim().isEmpty()) {
            result.setSuccess(false);
            result.setMessage("房源标题不能为空");
            return result;
        }

        if (house.getRentPrice() == null || house.getRentPrice().compareTo(BigDecimal.ZERO) <= 0) {
            result.setSuccess(false);
            result.setMessage("租金不能为空或小于0");
            return result;
        }

        if (house.getLandlordId() == null || house.getLandlordId() <= 0) {
            result.setSuccess(false);
            result.setMessage("房东信息错误");
            return result;
        }

        // 2. 设置默认值
        if (house.getStatus() == null) {
            house.setStatus("0"); // 默认待审核
        }

        // 3. 保存
        boolean success = houseDao.add(house);
        result.setSuccess(success);
        result.setMessage(success ? "房源添加成功，等待管理员审核" : "添加失败，请稍后重试");

        return result;
    }

    /**
     * 更新房源
     */
    @Override
    public UpdateHouseResult updateHouse(House house, Long landlordId) {
        UpdateHouseResult result = new UpdateHouseResult();

        // 1. 参数校验
        if (house.getId() == null || house.getId() <= 0) {
            result.setSuccess(false);
            result.setMessage("房源信息错误");
            return result;
        }

        // 2. 验证权限（该房源是否属于当前房东）
        House existingHouse = houseDao.findById(house.getId());
        if (existingHouse == null || !existingHouse.getLandlordId().equals(landlordId)) {
            result.setSuccess(false);
            result.setMessage("无权修改此房源");
            return result;
        }

        // 3. 已上架的房源修改后需要重新审核
        if ("1".equals(existingHouse.getStatus())) {
            house.setStatus("0"); // 重新进入待审核状态
        } else {
            house.setStatus(existingHouse.getStatus());
        }

        // 4. 更新
        boolean success = houseDao.update(house);
        result.setSuccess(success);
        result.setMessage(success ? "修改成功" : "修改失败");

        return result;
    }

    /**
     * 删除房源
     */
    @Override
    public boolean deleteHouse(Long houseId, Long landlordId) {
        if (houseId == null || landlordId == null) {
            return false;
        }

        House house = houseDao.findById(houseId);
        if (house == null || !house.getLandlordId().equals(landlordId)) {
            return false;
        }

        return houseDao.delete(houseId);
    }

    /**
     * 审核房源（管理员）
     */
    @Override
    public boolean auditHouse(Long houseId, boolean approved) {
        if (houseId == null || houseId <= 0) {
            return false;
        }

        String status = approved ? "1" : "3"; // 1-已上架，3-违规/拒绝
        return houseDao.updateStatus(houseId, status);
    }
}
