package com.ruoyi.ss.feeRule.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.feeRule.domain.FeeRuleVO;
import com.ruoyi.ss.feeRule.service.IFeeRuleService;
import com.ruoyi.ss.feeRule.service.IFeeRuleValidator;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 收费模板校验器业务层处理
 *
 * @author qzz
 * @date 2024-11-20
 */
@Service
public class FeeRuleValidatorImpl implements IFeeRuleValidator {

    @Autowired
    private IFeeRuleService feeRuleService;

    @Autowired
    private IRoomService roomService;

    /**
     * 校验是否存在基础收费模板
     *
     * @param ids 收费模板ids
     * @return 收费模板
     */
    @Override
    public boolean isExistBasicsRule(List<Long> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        // 是否存在套餐模式为1的收费模版
        FeeRuleVO feeRuleVO = feeRuleService.isExistBasicsRule(ids);
        return !ObjectUtil.isNull(feeRuleVO);
    }

    @Override
    public boolean isExistRule(List<Long> ids) {
        if (ids.isEmpty()) {
            return false;
        }
        for(Long id: ids){
            FeeRuleVO feeRuleVO = feeRuleService.selectEFeeRuleByRuleId(id);
            ServiceUtil.assertion(feeRuleVO == null, "收费模板["+id+"]不存在");
        }
        return true;
    }

    /**
     * 删除套餐前的校验
     *
     * @param ruleIds 收费模板ids
     */
    @Override
    public void preDelRule(Long[] ruleIds) {
        for (Long ruleId : ruleIds) {
            // 检查是否有应用的房间或设施
            List<RoomVO> rooms = roomService.selectIsExistRule(ruleId);

            if (!rooms.isEmpty()) {
                // 将RoomVO列表映射为房间名称列表
                List<String> roomNames = rooms.stream()
                        .map(RoomVO::getRoomName)  // 假设RoomVO有getName方法
                        .collect(Collectors.toList());
                // 使用中文顿号连接房间名称
                String roomNamesStr = String.join("、", roomNames);

                // 抛出异常并列出所有绑定的房间名
                ServiceUtil.assertion(false, "该套餐已经被房间【%s】绑定，请先解绑！", roomNamesStr);
            }
        }
    }
}
